D-Bus  1.6.8
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  }
127 
128  if (!keep_umask)
129  {
130  /* Get a predictable umask */
131  _dbus_verbose ("setting umask\n");
132  umask (022);
133  }
134 
135  _dbus_verbose ("calling setsid()\n");
136  if (setsid () == -1)
137  _dbus_assert_not_reached ("setsid() failed");
138 
139  break;
140 
141  default:
142  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
143  child_pid, error))
144  {
145  _dbus_verbose ("pid file or pipe write failed: %s\n",
146  error->message);
147  kill (child_pid, SIGTERM);
148  return FALSE;
149  }
150 
151  _dbus_verbose ("parent exiting\n");
152  _exit (0);
153  break;
154  }
155 
156  return TRUE;
157 }
158 
159 
168 static dbus_bool_t
169 _dbus_write_pid_file (const DBusString *filename,
170  unsigned long pid,
171  DBusError *error)
172 {
173  const char *cfilename;
174  int fd;
175  FILE *f;
176 
177  cfilename = _dbus_string_get_const_data (filename);
178 
179  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
180 
181  if (fd < 0)
182  {
183  dbus_set_error (error, _dbus_error_from_errno (errno),
184  "Failed to open \"%s\": %s", cfilename,
185  _dbus_strerror (errno));
186  return FALSE;
187  }
188 
189  if ((f = fdopen (fd, "w")) == NULL)
190  {
191  dbus_set_error (error, _dbus_error_from_errno (errno),
192  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
193  _dbus_close (fd, NULL);
194  return FALSE;
195  }
196 
197  if (fprintf (f, "%lu\n", pid) < 0)
198  {
199  dbus_set_error (error, _dbus_error_from_errno (errno),
200  "Failed to write to \"%s\": %s", cfilename,
201  _dbus_strerror (errno));
202 
203  fclose (f);
204  return FALSE;
205  }
206 
207  if (fclose (f) == EOF)
208  {
209  dbus_set_error (error, _dbus_error_from_errno (errno),
210  "Failed to close \"%s\": %s", cfilename,
211  _dbus_strerror (errno));
212  return FALSE;
213  }
214 
215  return TRUE;
216 }
217 
231  DBusPipe *print_pid_pipe,
232  dbus_pid_t pid_to_write,
233  DBusError *error)
234 {
235  if (pidfile)
236  {
237  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
238  if (!_dbus_write_pid_file (pidfile,
239  pid_to_write,
240  error))
241  {
242  _dbus_verbose ("pid file write failed\n");
243  _DBUS_ASSERT_ERROR_IS_SET(error);
244  return FALSE;
245  }
246  }
247  else
248  {
249  _dbus_verbose ("No pid file requested\n");
250  }
251 
252  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
253  {
254  DBusString pid;
255  int bytes;
256 
257  _dbus_verbose ("writing our pid to pipe %d\n",
258  print_pid_pipe->fd);
259 
260  if (!_dbus_string_init (&pid))
261  {
262  _DBUS_SET_OOM (error);
263  return FALSE;
264  }
265 
266  if (!_dbus_string_append_int (&pid, pid_to_write) ||
267  !_dbus_string_append (&pid, "\n"))
268  {
269  _dbus_string_free (&pid);
270  _DBUS_SET_OOM (error);
271  return FALSE;
272  }
273 
274  bytes = _dbus_string_get_length (&pid);
275  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
276  {
277  /* _dbus_pipe_write sets error only on failure, not short write */
278  if (error != NULL && !dbus_error_is_set(error))
279  {
281  "Printing message bus PID: did not write enough bytes\n");
282  }
283  _dbus_string_free (&pid);
284  return FALSE;
285  }
286 
287  _dbus_string_free (&pid);
288  }
289  else
290  {
291  _dbus_verbose ("No pid pipe to write to\n");
292  }
293 
294  return TRUE;
295 }
296 
304 _dbus_verify_daemon_user (const char *user)
305 {
306  DBusString u;
307 
308  _dbus_string_init_const (&u, user);
309 
311 }
312 
313 
314 /* The HAVE_LIBAUDIT case lives in selinux.c */
315 #ifndef HAVE_LIBAUDIT
316 
324 _dbus_change_to_daemon_user (const char *user,
325  DBusError *error)
326 {
327  dbus_uid_t uid;
328  dbus_gid_t gid;
329  DBusString u;
330 
331  _dbus_string_init_const (&u, user);
332 
333  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
334  {
336  "User '%s' does not appear to exist?",
337  user);
338  return FALSE;
339  }
340 
341  /* setgroups() only works if we are a privileged process,
342  * so we don't return error on failure; the only possible
343  * failure is that we don't have perms to do it.
344  *
345  * not sure this is right, maybe if setuid()
346  * is going to work then setgroups() should also work.
347  */
348  if (setgroups (0, NULL) < 0)
349  _dbus_warn ("Failed to drop supplementary groups: %s\n",
350  _dbus_strerror (errno));
351 
352  /* Set GID first, or the setuid may remove our permission
353  * to change the GID
354  */
355  if (setgid (gid) < 0)
356  {
357  dbus_set_error (error, _dbus_error_from_errno (errno),
358  "Failed to set GID to %lu: %s", gid,
359  _dbus_strerror (errno));
360  return FALSE;
361  }
362 
363  if (setuid (uid) < 0)
364  {
365  dbus_set_error (error, _dbus_error_from_errno (errno),
366  "Failed to set UID to %lu: %s", uid,
367  _dbus_strerror (errno));
368  return FALSE;
369  }
370 
371  return TRUE;
372 }
373 #endif /* !HAVE_LIBAUDIT */
374 
375 #ifdef HAVE_SETRLIMIT
376 
377 /* We assume that if we have setrlimit, we also have getrlimit and
378  * struct rlimit.
379  */
380 
381 struct DBusRLimit {
382  struct rlimit lim;
383 };
384 
385 DBusRLimit *
386 _dbus_rlimit_save_fd_limit (DBusError *error)
387 {
388  DBusRLimit *self;
389 
390  self = dbus_new0 (DBusRLimit, 1);
391 
392  if (self == NULL)
393  {
394  _DBUS_SET_OOM (error);
395  return NULL;
396  }
397 
398  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
399  {
400  dbus_set_error (error, _dbus_error_from_errno (errno),
401  "Failed to get fd limit: %s", _dbus_strerror (errno));
402  dbus_free (self);
403  return NULL;
404  }
405 
406  return self;
407 }
408 
410 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
411  DBusError *error)
412 {
413  struct rlimit lim;
414 
415  /* No point to doing this practically speaking
416  * if we're not uid 0. We expect the system
417  * bus to use this before we change UID, and
418  * the session bus takes the Linux default,
419  * currently 1024 for cur and 4096 for max.
420  */
421  if (getuid () != 0)
422  {
423  /* not an error, we're probably the session bus */
424  return TRUE;
425  }
426 
427  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
428  {
429  dbus_set_error (error, _dbus_error_from_errno (errno),
430  "Failed to get fd limit: %s", _dbus_strerror (errno));
431  return FALSE;
432  }
433 
434  if (lim.rlim_cur == RLIM_INFINITY || lim.rlim_cur >= desired)
435  {
436  /* not an error, everything is fine */
437  return TRUE;
438  }
439 
440  /* Ignore "maximum limit", assume we have the "superuser"
441  * privileges. On Linux this is CAP_SYS_RESOURCE.
442  */
443  lim.rlim_cur = lim.rlim_max = desired;
444 
445  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
446  {
447  dbus_set_error (error, _dbus_error_from_errno (errno),
448  "Failed to set fd limit to %u: %s",
449  desired, _dbus_strerror (errno));
450  return FALSE;
451  }
452 
453  return TRUE;
454 }
455 
457 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
458  DBusError *error)
459 {
460  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
461  {
462  dbus_set_error (error, _dbus_error_from_errno (errno),
463  "Failed to restore old fd limit: %s",
464  _dbus_strerror (errno));
465  return FALSE;
466  }
467 
468  return TRUE;
469 }
470 
471 #else /* !HAVE_SETRLIMIT */
472 
473 static void
474 fd_limit_not_supported (DBusError *error)
475 {
477  "cannot change fd limit on this platform");
478 }
479 
480 DBusRLimit *
481 _dbus_rlimit_save_fd_limit (DBusError *error)
482 {
483  fd_limit_not_supported (error);
484  return NULL;
485 }
486 
488 _dbus_rlimit_raise_fd_limit_if_privileged (unsigned int desired,
489  DBusError *error)
490 {
491  fd_limit_not_supported (error);
492  return FALSE;
493 }
494 
496 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
497  DBusError *error)
498 {
499  fd_limit_not_supported (error);
500  return FALSE;
501 }
502 
503 #endif
504 
505 void
506 _dbus_rlimit_free (DBusRLimit *lim)
507 {
508  dbus_free (lim);
509 }
510 
511 void
512 _dbus_init_system_log (void)
513 {
514 #if HAVE_DECL_LOG_PERROR
515  openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
516 #else
517  openlog ("dbus", LOG_PID, LOG_DAEMON);
518 #endif
519 }
520 
529 void
530 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
531 {
532  va_list args;
533 
534  va_start (args, msg);
535 
536  _dbus_system_logv (severity, msg, args);
537 
538  va_end (args);
539 }
540 
551 void
552 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
553 {
554  int flags;
555  switch (severity)
556  {
557  case DBUS_SYSTEM_LOG_INFO:
558  flags = LOG_DAEMON | LOG_NOTICE;
559  break;
560  case DBUS_SYSTEM_LOG_SECURITY:
561  flags = LOG_AUTH | LOG_NOTICE;
562  break;
563  case DBUS_SYSTEM_LOG_FATAL:
564  flags = LOG_DAEMON|LOG_CRIT;
565  break;
566  default:
567  return;
568  }
569 
570 #ifndef HAVE_DECL_LOG_PERROR
571  {
572  /* vsyslog() won't write to stderr, so we'd better do it */
573  va_list tmp;
574 
575  DBUS_VA_COPY (tmp, args);
576  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
577  vfprintf (stderr, msg, tmp);
578  fputc ('\n', stderr);
579  va_end (tmp);
580  }
581 #endif
582 
583  vsyslog (flags, msg, args);
584 
585  if (severity == DBUS_SYSTEM_LOG_FATAL)
586  exit (1);
587 }
588 
594 void
596  DBusSignalHandler handler)
597 {
598  struct sigaction act;
599  sigset_t empty_mask;
600 
601  sigemptyset (&empty_mask);
602  act.sa_handler = handler;
603  act.sa_mask = empty_mask;
604  act.sa_flags = 0;
605  sigaction (sig, &act, NULL);
606 }
607 
614 _dbus_file_exists (const char *file)
615 {
616  return (access (file, F_OK) == 0);
617 }
618 
626 _dbus_user_at_console (const char *username,
627  DBusError *error)
628 {
629 
630  DBusString u, f;
631  dbus_bool_t result;
632 
633  result = FALSE;
634  if (!_dbus_string_init (&f))
635  {
636  _DBUS_SET_OOM (error);
637  return FALSE;
638  }
639 
640  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
641  {
642  _DBUS_SET_OOM (error);
643  goto out;
644  }
645 
646  _dbus_string_init_const (&u, username);
647 
648  if (!_dbus_concat_dir_and_file (&f, &u))
649  {
650  _DBUS_SET_OOM (error);
651  goto out;
652  }
653 
654  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
655 
656  out:
657  _dbus_string_free (&f);
658 
659  return result;
660 }
661 
662 
671 {
672  if (_dbus_string_get_length (filename) > 0)
673  return _dbus_string_get_byte (filename, 0) == '/';
674  else
675  return FALSE;
676 }
677 
687 _dbus_stat (const DBusString *filename,
688  DBusStat *statbuf,
689  DBusError *error)
690 {
691  const char *filename_c;
692  struct stat sb;
693 
694  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
695 
696  filename_c = _dbus_string_get_const_data (filename);
697 
698  if (stat (filename_c, &sb) < 0)
699  {
700  dbus_set_error (error, _dbus_error_from_errno (errno),
701  "%s", _dbus_strerror (errno));
702  return FALSE;
703  }
704 
705  statbuf->mode = sb.st_mode;
706  statbuf->nlink = sb.st_nlink;
707  statbuf->uid = sb.st_uid;
708  statbuf->gid = sb.st_gid;
709  statbuf->size = sb.st_size;
710  statbuf->atime = sb.st_atime;
711  statbuf->mtime = sb.st_mtime;
712  statbuf->ctime = sb.st_ctime;
713 
714  return TRUE;
715 }
716 
717 
722 {
723  DIR *d;
725 };
726 
736  DBusError *error)
737 {
738  DIR *d;
739  DBusDirIter *iter;
740  const char *filename_c;
741 
742  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
743 
744  filename_c = _dbus_string_get_const_data (filename);
745 
746  d = opendir (filename_c);
747  if (d == NULL)
748  {
749  dbus_set_error (error, _dbus_error_from_errno (errno),
750  "Failed to read directory \"%s\": %s",
751  filename_c,
752  _dbus_strerror (errno));
753  return NULL;
754  }
755  iter = dbus_new0 (DBusDirIter, 1);
756  if (iter == NULL)
757  {
758  closedir (d);
760  "Could not allocate memory for directory iterator");
761  return NULL;
762  }
763 
764  iter->d = d;
765 
766  return iter;
767 }
768 
784  DBusString *filename,
785  DBusError *error)
786 {
787  struct dirent *ent;
788  int err;
789 
790  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
791 
792  again:
793  errno = 0;
794  ent = readdir (iter->d);
795 
796  if (!ent)
797  {
798  err = errno;
799 
800  if (err != 0)
801  dbus_set_error (error,
803  "%s", _dbus_strerror (err));
804 
805  return FALSE;
806  }
807  else if (ent->d_name[0] == '.' &&
808  (ent->d_name[1] == '\0' ||
809  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
810  goto again;
811  else
812  {
813  _dbus_string_set_length (filename, 0);
814  if (!_dbus_string_append (filename, ent->d_name))
815  {
817  "No memory to read directory entry");
818  return FALSE;
819  }
820  else
821  {
822  return TRUE;
823  }
824  }
825 }
826 
830 void
832 {
833  closedir (iter->d);
834  dbus_free (iter);
835 }
836 
837 static dbus_bool_t
838 fill_user_info_from_group (struct group *g,
839  DBusGroupInfo *info,
840  DBusError *error)
841 {
842  _dbus_assert (g->gr_name != NULL);
843 
844  info->gid = g->gr_gid;
845  info->groupname = _dbus_strdup (g->gr_name);
846 
847  /* info->members = dbus_strdupv (g->gr_mem) */
848 
849  if (info->groupname == NULL)
850  {
852  return FALSE;
853  }
854 
855  return TRUE;
856 }
857 
858 static dbus_bool_t
859 fill_group_info (DBusGroupInfo *info,
860  dbus_gid_t gid,
861  const DBusString *groupname,
862  DBusError *error)
863 {
864  const char *group_c_str;
865 
866  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
867  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
868 
869  if (groupname)
870  group_c_str = _dbus_string_get_const_data (groupname);
871  else
872  group_c_str = NULL;
873 
874  /* For now assuming that the getgrnam() and getgrgid() flavors
875  * always correspond to the pwnam flavors, if not we have
876  * to add more configure checks.
877  */
878 
879 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
880  {
881  struct group *g;
882  int result;
883  size_t buflen;
884  char *buf;
885  struct group g_str;
886  dbus_bool_t b;
887 
888  /* retrieve maximum needed size for buf */
889  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
890 
891  /* sysconf actually returns a long, but everything else expects size_t,
892  * so just recast here.
893  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
894  */
895  if ((long) buflen <= 0)
896  buflen = 1024;
897 
898  result = -1;
899  while (1)
900  {
901  buf = dbus_malloc (buflen);
902  if (buf == NULL)
903  {
905  return FALSE;
906  }
907 
908  g = NULL;
909 #ifdef HAVE_POSIX_GETPWNAM_R
910  if (group_c_str)
911  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
912  &g);
913  else
914  result = getgrgid_r (gid, &g_str, buf, buflen,
915  &g);
916 #else
917  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
918  result = 0;
919 #endif /* !HAVE_POSIX_GETPWNAM_R */
920  /* Try a bigger buffer if ERANGE was returned:
921  https://bugs.freedesktop.org/show_bug.cgi?id=16727
922  */
923  if (result == ERANGE && buflen < 512 * 1024)
924  {
925  dbus_free (buf);
926  buflen *= 2;
927  }
928  else
929  {
930  break;
931  }
932  }
933 
934  if (result == 0 && g == &g_str)
935  {
936  b = fill_user_info_from_group (g, info, error);
937  dbus_free (buf);
938  return b;
939  }
940  else
941  {
942  dbus_set_error (error, _dbus_error_from_errno (errno),
943  "Group %s unknown or failed to look it up\n",
944  group_c_str ? group_c_str : "???");
945  dbus_free (buf);
946  return FALSE;
947  }
948  }
949 #else /* ! HAVE_GETPWNAM_R */
950  {
951  /* I guess we're screwed on thread safety here */
952  struct group *g;
953 
954  g = getgrnam (group_c_str);
955 
956  if (g != NULL)
957  {
958  return fill_user_info_from_group (g, info, error);
959  }
960  else
961  {
962  dbus_set_error (error, _dbus_error_from_errno (errno),
963  "Group %s unknown or failed to look it up\n",
964  group_c_str ? group_c_str : "???");
965  return FALSE;
966  }
967  }
968 #endif /* ! HAVE_GETPWNAM_R */
969 }
970 
982  const DBusString *groupname,
983  DBusError *error)
984 {
985  return fill_group_info (info, DBUS_GID_UNSET,
986  groupname, error);
987 
988 }
989 
1001  dbus_gid_t gid,
1002  DBusError *error)
1003 {
1004  return fill_group_info (info, gid, NULL, error);
1005 }
1006 
1017  dbus_uid_t *uid_p)
1018 {
1019  return _dbus_get_user_id (username, uid_p);
1020 
1021 }
1022 
1033  dbus_gid_t *gid_p)
1034 {
1035  return _dbus_get_group_id (groupname, gid_p);
1036 }
1037 
1050  dbus_gid_t **group_ids,
1051  int *n_group_ids)
1052 {
1053  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1054 }
1055 
1067  DBusError *error)
1068 {
1069  return _dbus_is_console_user (uid, error);
1070 
1071 }
1072 
1082 {
1083  return uid == _dbus_geteuid ();
1084 }
1085 
1094 _dbus_windows_user_is_process_owner (const char *windows_sid)
1095 {
1096  return FALSE;
1097 }
1098  /* End of DBusInternalsUtils functions */
1100 
1114  DBusString *dirname)
1115 {
1116  int sep;
1117 
1118  _dbus_assert (filename != dirname);
1119  _dbus_assert (filename != NULL);
1120  _dbus_assert (dirname != NULL);
1121 
1122  /* Ignore any separators on the end */
1123  sep = _dbus_string_get_length (filename);
1124  if (sep == 0)
1125  return _dbus_string_append (dirname, "."); /* empty string passed in */
1126 
1127  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1128  --sep;
1129 
1130  _dbus_assert (sep >= 0);
1131 
1132  if (sep == 0)
1133  return _dbus_string_append (dirname, "/");
1134 
1135  /* Now find the previous separator */
1136  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1137  if (sep < 0)
1138  return _dbus_string_append (dirname, ".");
1139 
1140  /* skip multiple separators */
1141  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1142  --sep;
1143 
1144  _dbus_assert (sep >= 0);
1145 
1146  if (sep == 0 &&
1147  _dbus_string_get_byte (filename, 0) == '/')
1148  return _dbus_string_append (dirname, "/");
1149  else
1150  return _dbus_string_copy_len (filename, 0, sep - 0,
1151  dirname, _dbus_string_get_length (dirname));
1152 } /* DBusString stuff */
1154 
1155 static void
1156 string_squash_nonprintable (DBusString *str)
1157 {
1158  unsigned char *buf;
1159  int i, len;
1160 
1161  buf = _dbus_string_get_data (str);
1162  len = _dbus_string_get_length (str);
1163 
1164  for (i = 0; i < len; i++)
1165  {
1166  unsigned char c = (unsigned char) buf[i];
1167  if (c == '\0')
1168  buf[i] = ' ';
1169  else if (c < 0x20 || c > 127)
1170  buf[i] = '?';
1171  }
1172 }
1173 
1188 dbus_bool_t
1189 _dbus_command_for_pid (unsigned long pid,
1190  DBusString *str,
1191  int max_len,
1192  DBusError *error)
1193 {
1194  /* This is all Linux-specific for now */
1195  DBusString path;
1196  DBusString cmdline;
1197  int fd;
1198 
1199  if (!_dbus_string_init (&path))
1200  {
1201  _DBUS_SET_OOM (error);
1202  return FALSE;
1203  }
1204 
1205  if (!_dbus_string_init (&cmdline))
1206  {
1207  _DBUS_SET_OOM (error);
1208  _dbus_string_free (&path);
1209  return FALSE;
1210  }
1211 
1212  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1213  goto oom;
1214 
1215  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1216  if (fd < 0)
1217  {
1218  dbus_set_error (error,
1219  _dbus_error_from_errno (errno),
1220  "Failed to open \"%s\": %s",
1221  _dbus_string_get_const_data (&path),
1222  _dbus_strerror (errno));
1223  goto fail;
1224  }
1225 
1226  if (!_dbus_read (fd, &cmdline, max_len))
1227  {
1228  dbus_set_error (error,
1229  _dbus_error_from_errno (errno),
1230  "Failed to read from \"%s\": %s",
1231  _dbus_string_get_const_data (&path),
1232  _dbus_strerror (errno));
1233  goto fail;
1234  }
1235 
1236  if (!_dbus_close (fd, error))
1237  goto fail;
1238 
1239  string_squash_nonprintable (&cmdline);
1240 
1241  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1242  goto oom;
1243 
1244  _dbus_string_free (&cmdline);
1245  _dbus_string_free (&path);
1246  return TRUE;
1247 oom:
1248  _DBUS_SET_OOM (error);
1249 fail:
1250  _dbus_string_free (&cmdline);
1251  _dbus_string_free (&path);
1252  return FALSE;
1253 }
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:913
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
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:1280
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
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:1111
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...
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:780
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:1372
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
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