D-Bus  1.6.30
dbus-sysdeps-util-unix.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-util-unix.c Would be in dbus-sysdeps-unix.c, but not used in libdbus
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5  * Copyright (C) 2003 CodeFactory AB
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-sysdeps.h"
27 #include "dbus-sysdeps-unix.h"
28 #include "dbus-internals.h"
29 #include "dbus-pipe.h"
30 #include "dbus-protocol.h"
31 #include "dbus-string.h"
32 #define DBUS_USERDB_INCLUDES_PRIVATE 1
33 #include "dbus-userdb.h"
34 #include "dbus-test.h"
35 
36 #include <sys/types.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <sys/stat.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
47 #endif
48 #include <grp.h>
49 #include <sys/socket.h>
50 #include <dirent.h>
51 #include <sys/un.h>
52 #include <syslog.h>
53 
54 #ifdef HAVE_SYS_SYSLIMITS_H
55 #include <sys/syslimits.h>
56 #endif
57 
58 #ifndef O_BINARY
59 #define O_BINARY 0
60 #endif
61 
79  DBusPipe *print_pid_pipe,
80  DBusError *error,
81  dbus_bool_t keep_umask)
82 {
83  const char *s;
84  pid_t child_pid;
85  int dev_null_fd;
86 
87  _dbus_verbose ("Becoming a daemon...\n");
88 
89  _dbus_verbose ("chdir to /\n");
90  if (chdir ("/") < 0)
91  {
93  "Could not chdir() to root directory");
94  return FALSE;
95  }
96 
97  _dbus_verbose ("forking...\n");
98  switch ((child_pid = fork ()))
99  {
100  case -1:
101  _dbus_verbose ("fork failed\n");
102  dbus_set_error (error, _dbus_error_from_errno (errno),
103  "Failed to fork daemon: %s", _dbus_strerror (errno));
104  return FALSE;
105  break;
106 
107  case 0:
108  _dbus_verbose ("in child, closing std file descriptors\n");
109 
110  /* silently ignore failures here, if someone
111  * doesn't have /dev/null we may as well try
112  * to continue anyhow
113  */
114 
115  dev_null_fd = open ("/dev/null", O_RDWR);
116  if (dev_null_fd >= 0)
117  {
118  dup2 (dev_null_fd, 0);
119  dup2 (dev_null_fd, 1);
120 
121  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
122  if (s == NULL || *s == '\0')
123  dup2 (dev_null_fd, 2);
124  else
125  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
126  close (dev_null_fd);
127  }
128 
129  if (!keep_umask)
130  {
131  /* Get a predictable umask */
132  _dbus_verbose ("setting umask\n");
133  umask (022);
134  }
135 
136  _dbus_verbose ("calling setsid()\n");
137  if (setsid () == -1)
138  _dbus_assert_not_reached ("setsid() failed");
139 
140  break;
141 
142  default:
143  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
144  child_pid, error))
145  {
146  _dbus_verbose ("pid file or pipe write failed: %s\n",
147  error->message);
148  kill (child_pid, SIGTERM);
149  return FALSE;
150  }
151 
152  _dbus_verbose ("parent exiting\n");
153  _exit (0);
154  break;
155  }
156 
157  return TRUE;
158 }
159 
160 
169 static dbus_bool_t
170 _dbus_write_pid_file (const DBusString *filename,
171  unsigned long pid,
172  DBusError *error)
173 {
174  const char *cfilename;
175  int fd;
176  FILE *f;
177 
178  cfilename = _dbus_string_get_const_data (filename);
179 
180  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
181 
182  if (fd < 0)
183  {
184  dbus_set_error (error, _dbus_error_from_errno (errno),
185  "Failed to open \"%s\": %s", cfilename,
186  _dbus_strerror (errno));
187  return FALSE;
188  }
189 
190  if ((f = fdopen (fd, "w")) == NULL)
191  {
192  dbus_set_error (error, _dbus_error_from_errno (errno),
193  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
194  _dbus_close (fd, NULL);
195  return FALSE;
196  }
197 
198  if (fprintf (f, "%lu\n", pid) < 0)
199  {
200  dbus_set_error (error, _dbus_error_from_errno (errno),
201  "Failed to write to \"%s\": %s", cfilename,
202  _dbus_strerror (errno));
203 
204  fclose (f);
205  return FALSE;
206  }
207 
208  if (fclose (f) == EOF)
209  {
210  dbus_set_error (error, _dbus_error_from_errno (errno),
211  "Failed to close \"%s\": %s", cfilename,
212  _dbus_strerror (errno));
213  return FALSE;
214  }
215 
216  return TRUE;
217 }
218 
232  DBusPipe *print_pid_pipe,
233  dbus_pid_t pid_to_write,
234  DBusError *error)
235 {
236  if (pidfile)
237  {
238  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
239  if (!_dbus_write_pid_file (pidfile,
240  pid_to_write,
241  error))
242  {
243  _dbus_verbose ("pid file write failed\n");
244  _DBUS_ASSERT_ERROR_IS_SET(error);
245  return FALSE;
246  }
247  }
248  else
249  {
250  _dbus_verbose ("No pid file requested\n");
251  }
252 
253  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
254  {
255  DBusString pid;
256  int bytes;
257 
258  _dbus_verbose ("writing our pid to pipe %d\n",
259  print_pid_pipe->fd);
260 
261  if (!_dbus_string_init (&pid))
262  {
263  _DBUS_SET_OOM (error);
264  return FALSE;
265  }
266 
267  if (!_dbus_string_append_int (&pid, pid_to_write) ||
268  !_dbus_string_append (&pid, "\n"))
269  {
270  _dbus_string_free (&pid);
271  _DBUS_SET_OOM (error);
272  return FALSE;
273  }
274 
275  bytes = _dbus_string_get_length (&pid);
276  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
277  {
278  /* _dbus_pipe_write sets error only on failure, not short write */
279  if (error != NULL && !dbus_error_is_set(error))
280  {
282  "Printing message bus PID: did not write enough bytes\n");
283  }
284  _dbus_string_free (&pid);
285  return FALSE;
286  }
287 
288  _dbus_string_free (&pid);
289  }
290  else
291  {
292  _dbus_verbose ("No pid pipe to write to\n");
293  }
294 
295  return TRUE;
296 }
297 
305 _dbus_verify_daemon_user (const char *user)
306 {
307  DBusString u;
308 
309  _dbus_string_init_const (&u, user);
310 
312 }
313 
314 
315 /* The HAVE_LIBAUDIT case lives in selinux.c */
316 #ifndef HAVE_LIBAUDIT
317 
325 _dbus_change_to_daemon_user (const char *user,
326  DBusError *error)
327 {
328  dbus_uid_t uid;
329  dbus_gid_t gid;
330  DBusString u;
331 
332  _dbus_string_init_const (&u, user);
333 
334  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
335  {
337  "User '%s' does not appear to exist?",
338  user);
339  return FALSE;
340  }
341 
342  /* setgroups() only works if we are a privileged process,
343  * so we don't return error on failure; the only possible
344  * failure is that we don't have perms to do it.
345  *
346  * not sure this is right, maybe if setuid()
347  * is going to work then setgroups() should also work.
348  */
349  if (setgroups (0, NULL) < 0)
350  _dbus_warn ("Failed to drop supplementary groups: %s\n",
351  _dbus_strerror (errno));
352 
353  /* Set GID first, or the setuid may remove our permission
354  * to change the GID
355  */
356  if (setgid (gid) < 0)
357  {
358  dbus_set_error (error, _dbus_error_from_errno (errno),
359  "Failed to set GID to %lu: %s", gid,
360  _dbus_strerror (errno));
361  return FALSE;
362  }
363 
364  if (setuid (uid) < 0)
365  {
366  dbus_set_error (error, _dbus_error_from_errno (errno),
367  "Failed to set UID to %lu: %s", uid,
368  _dbus_strerror (errno));
369  return FALSE;
370  }
371 
372  return TRUE;
373 }
374 #endif /* !HAVE_LIBAUDIT */
375 
376 #ifdef HAVE_SETRLIMIT
377 
378 /* We assume that if we have setrlimit, we also have getrlimit and
379  * struct rlimit.
380  */
381 
382 struct DBusRLimit {
383  struct rlimit lim;
384 };
385 
386 DBusRLimit *
387 _dbus_rlimit_save_fd_limit (DBusError *error)
388 {
389  DBusRLimit *self;
390 
391  self = dbus_new0 (DBusRLimit, 1);
392 
393  if (self == NULL)
394  {
395  _DBUS_SET_OOM (error);
396  return NULL;
397  }
398 
399  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
400  {
401  dbus_set_error (error, _dbus_error_from_errno (errno),
402  "Failed to get fd limit: %s", _dbus_strerror (errno));
403  dbus_free (self);
404  return NULL;
405  }
406 
407  return self;
408 }
409 
411 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
412  DBusError *error)
413 {
414  struct rlimit lim;
415 
416  /* No point to doing this practically speaking
417  * if we're not uid 0. We expect the system
418  * bus to use this before we change UID, and
419  * the session bus takes the Linux default,
420  * currently 1024 for cur and 4096 for max.
421  */
422  if (getuid () != 0)
423  {
424  /* not an error, we're probably the session bus */
425  return TRUE;
426  }
427 
428  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
429  {
430  dbus_set_error (error, _dbus_error_from_errno (errno),
431  "Failed to get fd limit: %s", _dbus_strerror (errno));
432  return FALSE;
433  }
434 
435  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
436  {
437  /* not an error, everything is fine */
438  return TRUE;
439  }
440 
441  /* Ignore "maximum limit", assume we have the "superuser"
442  * privileges. On Linux this is CAP_SYS_RESOURCE.
443  */
444  lim.rlim_cur = lim.rlim_max = desired;
445 
446  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
447  {
448  dbus_set_error (error, _dbus_error_from_errno (errno),
449  "Failed to set fd limit to %u: %s",
450  desired, _dbus_strerror (errno));
451  return FALSE;
452  }
453 
454  return TRUE;
455 }
456 
458 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
459  DBusError *error)
460 {
461  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
462  {
463  dbus_set_error (error, _dbus_error_from_errno (errno),
464  "Failed to restore old fd limit: %s",
465  _dbus_strerror (errno));
466  return FALSE;
467  }
468 
469  return TRUE;
470 }
471 
472 #else /* !HAVE_SETRLIMIT */
473 
474 static void
475 fd_limit_not_supported (DBusError *error)
476 {
478  "cannot change fd limit on this platform");
479 }
480 
481 DBusRLimit *
482 _dbus_rlimit_save_fd_limit (DBusError *error)
483 {
484  fd_limit_not_supported (error);
485  return NULL;
486 }
487 
489 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
490  DBusError *error)
491 {
492  fd_limit_not_supported (error);
493  return FALSE;
494 }
495 
497 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
498  DBusError *error)
499 {
500  fd_limit_not_supported (error);
501  return FALSE;
502 }
503 
504 #endif
505 
506 void
507 _dbus_rlimit_free (DBusRLimit *lim)
508 {
509  dbus_free (lim);
510 }
511 
512 void
513 _dbus_init_system_log (void)
514 {
515 #if HAVE_DECL_LOG_PERROR
516  openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
517 #else
518  openlog ("dbus", LOG_PID, LOG_DAEMON);
519 #endif
520 }
521 
530 void
531 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
532 {
533  va_list args;
534 
535  va_start (args, msg);
536 
537  _dbus_system_logv (severity, msg, args);
538 
539  va_end (args);
540 }
541 
552 void
553 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
554 {
555  int flags;
556  switch (severity)
557  {
558  case DBUS_SYSTEM_LOG_INFO:
559  flags = LOG_DAEMON | LOG_NOTICE;
560  break;
561  case DBUS_SYSTEM_LOG_SECURITY:
562  flags = LOG_AUTH | LOG_NOTICE;
563  break;
564  case DBUS_SYSTEM_LOG_FATAL:
565  flags = LOG_DAEMON|LOG_CRIT;
566  break;
567  default:
568  return;
569  }
570 
571 #ifndef HAVE_DECL_LOG_PERROR
572  {
573  /* vsyslog() won't write to stderr, so we'd better do it */
574  va_list tmp;
575 
576  DBUS_VA_COPY (tmp, args);
577  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
578  vfprintf (stderr, msg, tmp);
579  fputc ('\n', stderr);
580  va_end (tmp);
581  }
582 #endif
583 
584  vsyslog (flags, msg, args);
585 
586  if (severity == DBUS_SYSTEM_LOG_FATAL)
587  exit (1);
588 }
589 
595 void
597  DBusSignalHandler handler)
598 {
599  struct sigaction act;
600  sigset_t empty_mask;
601 
602  sigemptyset (&empty_mask);
603  act.sa_handler = handler;
604  act.sa_mask = empty_mask;
605  act.sa_flags = 0;
606  sigaction (sig, &act, NULL);
607 }
608 
615 _dbus_file_exists (const char *file)
616 {
617  return (access (file, F_OK) == 0);
618 }
619 
627 _dbus_user_at_console (const char *username,
628  DBusError *error)
629 {
630 
631  DBusString u, f;
632  dbus_bool_t result;
633 
634  result = FALSE;
635  if (!_dbus_string_init (&f))
636  {
637  _DBUS_SET_OOM (error);
638  return FALSE;
639  }
640 
641  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
642  {
643  _DBUS_SET_OOM (error);
644  goto out;
645  }
646 
647  _dbus_string_init_const (&u, username);
648 
649  if (!_dbus_concat_dir_and_file (&f, &u))
650  {
651  _DBUS_SET_OOM (error);
652  goto out;
653  }
654 
656 
657  out:
658  _dbus_string_free (&f);
659 
660  return result;
661 }
662 
663 
672 {
673  if (_dbus_string_get_length (filename) > 0)
674  return _dbus_string_get_byte (filename, 0) == '/';
675  else
676  return FALSE;
677 }
678 
688 _dbus_stat (const DBusString *filename,
689  DBusStat *statbuf,
690  DBusError *error)
691 {
692  const char *filename_c;
693  struct stat sb;
694 
695  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
696 
697  filename_c = _dbus_string_get_const_data (filename);
698 
699  if (stat (filename_c, &sb) < 0)
700  {
701  dbus_set_error (error, _dbus_error_from_errno (errno),
702  "%s", _dbus_strerror (errno));
703  return FALSE;
704  }
705 
706  statbuf->mode = sb.st_mode;
707  statbuf->nlink = sb.st_nlink;
708  statbuf->uid = sb.st_uid;
709  statbuf->gid = sb.st_gid;
710  statbuf->size = sb.st_size;
711  statbuf->atime = sb.st_atime;
712  statbuf->mtime = sb.st_mtime;
713  statbuf->ctime = sb.st_ctime;
714 
715  return TRUE;
716 }
717 
718 
723 {
724  DIR *d;
726 };
727 
737  DBusError *error)
738 {
739  DIR *d;
740  DBusDirIter *iter;
741  const char *filename_c;
742 
743  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
744 
745  filename_c = _dbus_string_get_const_data (filename);
746 
747  d = opendir (filename_c);
748  if (d == NULL)
749  {
750  dbus_set_error (error, _dbus_error_from_errno (errno),
751  "Failed to read directory \"%s\": %s",
752  filename_c,
753  _dbus_strerror (errno));
754  return NULL;
755  }
756  iter = dbus_new0 (DBusDirIter, 1);
757  if (iter == NULL)
758  {
759  closedir (d);
761  "Could not allocate memory for directory iterator");
762  return NULL;
763  }
764 
765  iter->d = d;
766 
767  return iter;
768 }
769 
785  DBusString *filename,
786  DBusError *error)
787 {
788  struct dirent *ent;
789  int err;
790 
791  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
792 
793  again:
794  errno = 0;
795  ent = readdir (iter->d);
796 
797  if (!ent)
798  {
799  err = errno;
800 
801  if (err != 0)
802  dbus_set_error (error,
804  "%s", _dbus_strerror (err));
805 
806  return FALSE;
807  }
808  else if (ent->d_name[0] == '.' &&
809  (ent->d_name[1] == '\0' ||
810  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
811  goto again;
812  else
813  {
814  _dbus_string_set_length (filename, 0);
815  if (!_dbus_string_append (filename, ent->d_name))
816  {
818  "No memory to read directory entry");
819  return FALSE;
820  }
821  else
822  {
823  return TRUE;
824  }
825  }
826 }
827 
831 void
833 {
834  closedir (iter->d);
835  dbus_free (iter);
836 }
837 
838 static dbus_bool_t
839 fill_user_info_from_group (struct group *g,
840  DBusGroupInfo *info,
841  DBusError *error)
842 {
843  _dbus_assert (g->gr_name != NULL);
844 
845  info->gid = g->gr_gid;
846  info->groupname = _dbus_strdup (g->gr_name);
847 
848  /* info->members = dbus_strdupv (g->gr_mem) */
849 
850  if (info->groupname == NULL)
851  {
853  return FALSE;
854  }
855 
856  return TRUE;
857 }
858 
859 static dbus_bool_t
860 fill_group_info (DBusGroupInfo *info,
861  dbus_gid_t gid,
862  const DBusString *groupname,
863  DBusError *error)
864 {
865  const char *group_c_str;
866 
867  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
868  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
869 
870  if (groupname)
871  group_c_str = _dbus_string_get_const_data (groupname);
872  else
873  group_c_str = NULL;
874 
875  /* For now assuming that the getgrnam() and getgrgid() flavors
876  * always correspond to the pwnam flavors, if not we have
877  * to add more configure checks.
878  */
879 
880 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
881  {
882  struct group *g;
883  int result;
884  size_t buflen;
885  char *buf;
886  struct group g_str;
887  dbus_bool_t b;
888 
889  /* retrieve maximum needed size for buf */
890  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
891 
892  /* sysconf actually returns a long, but everything else expects size_t,
893  * so just recast here.
894  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
895  */
896  if ((long) buflen <= 0)
897  buflen = 1024;
898 
899  result = -1;
900  while (1)
901  {
902  buf = dbus_malloc (buflen);
903  if (buf == NULL)
904  {
906  return FALSE;
907  }
908 
909  g = NULL;
910 #ifdef HAVE_POSIX_GETPWNAM_R
911  if (group_c_str)
912  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
913  &g);
914  else
915  result = getgrgid_r (gid, &g_str, buf, buflen,
916  &g);
917 #else
918  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
919  result = 0;
920 #endif /* !HAVE_POSIX_GETPWNAM_R */
921  /* Try a bigger buffer if ERANGE was returned:
922  https://bugs.freedesktop.org/show_bug.cgi?id=16727
923  */
924  if (result == ERANGE && buflen < 512 * 1024)
925  {
926  dbus_free (buf);
927  buflen *= 2;
928  }
929  else
930  {
931  break;
932  }
933  }
934 
935  if (result == 0 && g == &g_str)
936  {
937  b = fill_user_info_from_group (g, info, error);
938  dbus_free (buf);
939  return b;
940  }
941  else
942  {
943  dbus_set_error (error, _dbus_error_from_errno (errno),
944  "Group %s unknown or failed to look it up\n",
945  group_c_str ? group_c_str : "???");
946  dbus_free (buf);
947  return FALSE;
948  }
949  }
950 #else /* ! HAVE_GETPWNAM_R */
951  {
952  /* I guess we're screwed on thread safety here */
953  struct group *g;
954 
955  g = getgrnam (group_c_str);
956 
957  if (g != NULL)
958  {
959  return fill_user_info_from_group (g, info, error);
960  }
961  else
962  {
963  dbus_set_error (error, _dbus_error_from_errno (errno),
964  "Group %s unknown or failed to look it up\n",
965  group_c_str ? group_c_str : "???");
966  return FALSE;
967  }
968  }
969 #endif /* ! HAVE_GETPWNAM_R */
970 }
971 
983  const DBusString *groupname,
984  DBusError *error)
985 {
986  return fill_group_info (info, DBUS_GID_UNSET,
987  groupname, error);
988 
989 }
990 
1002  dbus_gid_t gid,
1003  DBusError *error)
1004 {
1005  return fill_group_info (info, gid, NULL, error);
1006 }
1007 
1018  dbus_uid_t *uid_p)
1019 {
1020  return _dbus_get_user_id (username, uid_p);
1021 
1022 }
1023 
1034  dbus_gid_t *gid_p)
1035 {
1036  return _dbus_get_group_id (groupname, gid_p);
1037 }
1038 
1051  dbus_gid_t **group_ids,
1052  int *n_group_ids)
1053 {
1054  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1055 }
1056 
1068  DBusError *error)
1069 {
1070  return _dbus_is_console_user (uid, error);
1071 
1072 }
1073 
1083 {
1084  return uid == _dbus_geteuid ();
1085 }
1086 
1095 _dbus_windows_user_is_process_owner (const char *windows_sid)
1096 {
1097  return FALSE;
1098 }
1099  /* End of DBusInternalsUtils functions */
1101 
1115  DBusString *dirname)
1116 {
1117  int sep;
1118 
1119  _dbus_assert (filename != dirname);
1120  _dbus_assert (filename != NULL);
1121  _dbus_assert (dirname != NULL);
1122 
1123  /* Ignore any separators on the end */
1124  sep = _dbus_string_get_length (filename);
1125  if (sep == 0)
1126  return _dbus_string_append (dirname, "."); /* empty string passed in */
1127 
1128  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1129  --sep;
1130 
1131  _dbus_assert (sep >= 0);
1132 
1133  if (sep == 0)
1134  return _dbus_string_append (dirname, "/");
1135 
1136  /* Now find the previous separator */
1137  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1138  if (sep < 0)
1139  return _dbus_string_append (dirname, ".");
1140 
1141  /* skip multiple separators */
1142  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1143  --sep;
1144 
1145  _dbus_assert (sep >= 0);
1146 
1147  if (sep == 0 &&
1148  _dbus_string_get_byte (filename, 0) == '/')
1149  return _dbus_string_append (dirname, "/");
1150  else
1151  return _dbus_string_copy_len (filename, 0, sep - 0,
1152  dirname, _dbus_string_get_length (dirname));
1153 } /* DBusString stuff */
1155 
1156 static void
1157 string_squash_nonprintable (DBusString *str)
1158 {
1159  unsigned char *buf;
1160  int i, len;
1161 
1162  buf = _dbus_string_get_data (str);
1163  len = _dbus_string_get_length (str);
1164 
1165  for (i = 0; i < len; i++)
1166  {
1167  unsigned char c = (unsigned char) buf[i];
1168  if (c == '\0')
1169  buf[i] = ' ';
1170  else if (c < 0x20 || c > 127)
1171  buf[i] = '?';
1172  }
1173 }
1174 
1189 dbus_bool_t
1190 _dbus_command_for_pid (unsigned long pid,
1191  DBusString *str,
1192  int max_len,
1193  DBusError *error)
1194 {
1195  /* This is all Linux-specific for now */
1196  DBusString path;
1197  DBusString cmdline;
1198  int fd;
1199 
1200  if (!_dbus_string_init (&path))
1201  {
1202  _DBUS_SET_OOM (error);
1203  return FALSE;
1204  }
1205 
1206  if (!_dbus_string_init (&cmdline))
1207  {
1208  _DBUS_SET_OOM (error);
1209  _dbus_string_free (&path);
1210  return FALSE;
1211  }
1212 
1213  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1214  goto oom;
1215 
1216  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1217  if (fd < 0)
1218  {
1219  dbus_set_error (error,
1220  _dbus_error_from_errno (errno),
1221  "Failed to open \"%s\": %s",
1223  _dbus_strerror (errno));
1224  goto fail;
1225  }
1226 
1227  if (!_dbus_read (fd, &cmdline, max_len))
1228  {
1229  dbus_set_error (error,
1230  _dbus_error_from_errno (errno),
1231  "Failed to read from \"%s\": %s",
1233  _dbus_strerror (errno));
1234  _dbus_close (fd, NULL);
1235  goto fail;
1236  }
1237 
1238  if (!_dbus_close (fd, error))
1239  goto fail;
1240 
1241  string_squash_nonprintable (&cmdline);
1242 
1243  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1244  goto oom;
1245 
1246  _dbus_string_free (&cmdline);
1247  _dbus_string_free (&path);
1248  return TRUE;
1249 oom:
1250  _DBUS_SET_OOM (error);
1251 fail:
1252  _dbus_string_free (&cmdline);
1253  _dbus_string_free (&path);
1254  return FALSE;
1255 }
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:921
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_bool_t _dbus_become_daemon(const DBusString *pidfile, DBusPipe *print_pid_pipe, DBusError *error, dbus_bool_t keep_umask)
Does the chdir, fork, setsid, etc.
dbus_bool_t _dbus_unix_user_is_at_console(dbus_uid_t uid, DBusError *error)
Checks to see if the UNIX user ID is at the console.
dbus_bool_t _dbus_group_info_fill_gid(DBusGroupInfo *info, dbus_gid_t gid, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group ID...
dbus_bool_t _dbus_string_get_dirname(const DBusString *filename, DBusString *dirname)
Get the directory name from a complete filename.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
void _dbus_system_log(DBusSystemLogSeverity severity, const char *msg,...)
Log a message to the system log file (e.g.
Portable struct with stat() results.
Definition: dbus-sysdeps.h:391
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:352
dbus_bool_t _dbus_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids)
Gets all groups corresponding to the given UID.
#define DBUS_PID_FORMAT
an appropriate printf format for dbus_pid_t
Definition: dbus-sysdeps.h:112
dbus_bool_t _dbus_parse_unix_group_from_config(const DBusString *groupname, dbus_gid_t *gid_p)
Parse a UNIX group from the bus config file.
void _dbus_directory_close(DBusDirIter *iter)
Closes a directory iteration.
dbus_bool_t _dbus_is_console_user(dbus_uid_t uid, DBusError *error)
Checks to see if the UID sent in is the console user.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_directory_get_next_file(DBusDirIter *iter, DBusString *filename, DBusError *error)
Get next file in the directory.
unsigned long atime
Access time.
Definition: dbus-sysdeps.h:398
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
Definition: dbus-string.c:548
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
dbus_pid_t _dbus_getpid(void)
Gets our process ID.
dbus_bool_t _dbus_command_for_pid(unsigned long pid, DBusString *str, int max_len, DBusError *error)
Get a printable string describing the command used to execute the process with pid.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
Definition: dbus-string.c:1288
char * groupname
Group name.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:183
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
Definition: dbus-sysdeps.c:612
Internals of directory iterator.
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:393
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:98
dbus_bool_t _dbus_get_user_id_and_primary_group(const DBusString *username, dbus_uid_t *uid_p, dbus_gid_t *gid_p)
Gets user ID and primary group given username.
dbus_bool_t _dbus_change_to_daemon_user(const char *user, DBusError *error)
Changes the user and group the bus is running as.
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:460
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:396
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:190
char * _dbus_string_get_data(DBusString *str)
Gets the raw character buffer from the string.
Definition: dbus-string.c:437
DIR * d
The DIR* from opendir()
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1119
dbus_bool_t _dbus_group_info_fill(DBusGroupInfo *info, const DBusString *groupname, DBusError *error)
Initializes the given DBusGroupInfo struct with information about the given group name...
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:725
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps.h:433
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Object representing an exception.
Definition: dbus-errors.h:48
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_bool_t _dbus_unix_groups_from_uid(dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids)
Gets all groups corresponding to the given UNIX user ID.
unsigned long ctime
Creation time.
Definition: dbus-sysdeps.h:400
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:109
dbus_bool_t _dbus_file_exists(const char *file)
Checks if a file exists.
#define TRUE
Expands to "1".
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:394
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
dbus_bool_t _dbus_write_pid_to_file_and_pipe(const DBusString *pidfile, DBusPipe *print_pid_pipe, dbus_pid_t pid_to_write, DBusError *error)
Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a pipe (if non-NULL).
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:395
void _dbus_system_logv(DBusSystemLogSeverity severity, const char *msg, va_list args)
Log a message to the system log file (e.g.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
dbus_bool_t _dbus_verify_daemon_user(const char *user)
Verify that after the fork we can successfully change to this user.
dbus_bool_t _dbus_string_find_byte_backward(const DBusString *str, int start, unsigned char byte, int *found)
Find the given byte scanning backward from the given start.
Information about a UNIX group.
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
dbus_bool_t _dbus_unix_user_is_process_owner(dbus_uid_t uid)
Checks to see if the UNIX user ID matches the UID of the process.
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
dbus_bool_t _dbus_windows_user_is_process_owner(const char *windows_sid)
Checks to see if the Windows user SID matches the owner of the process.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:399
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:788
dbus_bool_t _dbus_string_copy_len(const DBusString *source, int start, int len, DBusString *dest, int insert_at)
Like _dbus_string_copy(), but can copy a segment from the middle of the source string.
Definition: dbus-string.c:1380
dbus_gid_t gid
GID.
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:102
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:397
dbus_bool_t _dbus_parse_unix_user_from_config(const DBusString *username, dbus_uid_t *uid_p)
Parse a UNIX user from the bus config file.
char * _dbus_strdup(const char *str)
Duplicates a string.
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:100
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:454
int _dbus_read(int fd, DBusString *buffer, int count)
Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer...
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329