D-Bus  1.6.18
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 
53 #ifdef HAVE_SYSLOG_H
54 #include <syslog.h>
55 #endif
56 
57 #ifdef HAVE_SYS_SYSLIMITS_H
58 #include <sys/syslimits.h>
59 #endif
60 
61 #ifndef O_BINARY
62 #define O_BINARY 0
63 #endif
64 
82  DBusPipe *print_pid_pipe,
83  DBusError *error,
84  dbus_bool_t keep_umask)
85 {
86  const char *s;
87  pid_t child_pid;
88  int dev_null_fd;
89 
90  _dbus_verbose ("Becoming a daemon...\n");
91 
92  _dbus_verbose ("chdir to /\n");
93  if (chdir ("/") < 0)
94  {
96  "Could not chdir() to root directory");
97  return FALSE;
98  }
99 
100  _dbus_verbose ("forking...\n");
101  switch ((child_pid = fork ()))
102  {
103  case -1:
104  _dbus_verbose ("fork failed\n");
105  dbus_set_error (error, _dbus_error_from_errno (errno),
106  "Failed to fork daemon: %s", _dbus_strerror (errno));
107  return FALSE;
108  break;
109 
110  case 0:
111  _dbus_verbose ("in child, closing std file descriptors\n");
112 
113  /* silently ignore failures here, if someone
114  * doesn't have /dev/null we may as well try
115  * to continue anyhow
116  */
117 
118  dev_null_fd = open ("/dev/null", O_RDWR);
119  if (dev_null_fd >= 0)
120  {
121  dup2 (dev_null_fd, 0);
122  dup2 (dev_null_fd, 1);
123 
124  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
125  if (s == NULL || *s == '\0')
126  dup2 (dev_null_fd, 2);
127  else
128  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
129  close (dev_null_fd);
130  }
131 
132  if (!keep_umask)
133  {
134  /* Get a predictable umask */
135  _dbus_verbose ("setting umask\n");
136  umask (022);
137  }
138 
139  _dbus_verbose ("calling setsid()\n");
140  if (setsid () == -1)
141  _dbus_assert_not_reached ("setsid() failed");
142 
143  break;
144 
145  default:
146  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
147  child_pid, error))
148  {
149  _dbus_verbose ("pid file or pipe write failed: %s\n",
150  error->message);
151  kill (child_pid, SIGTERM);
152  return FALSE;
153  }
154 
155  _dbus_verbose ("parent exiting\n");
156  _exit (0);
157  break;
158  }
159 
160  return TRUE;
161 }
162 
163 
172 static dbus_bool_t
173 _dbus_write_pid_file (const DBusString *filename,
174  unsigned long pid,
175  DBusError *error)
176 {
177  const char *cfilename;
178  int fd;
179  FILE *f;
180 
181  cfilename = _dbus_string_get_const_data (filename);
182 
183  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
184 
185  if (fd < 0)
186  {
187  dbus_set_error (error, _dbus_error_from_errno (errno),
188  "Failed to open \"%s\": %s", cfilename,
189  _dbus_strerror (errno));
190  return FALSE;
191  }
192 
193  if ((f = fdopen (fd, "w")) == NULL)
194  {
195  dbus_set_error (error, _dbus_error_from_errno (errno),
196  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
197  _dbus_close (fd, NULL);
198  return FALSE;
199  }
200 
201  if (fprintf (f, "%lu\n", pid) < 0)
202  {
203  dbus_set_error (error, _dbus_error_from_errno (errno),
204  "Failed to write to \"%s\": %s", cfilename,
205  _dbus_strerror (errno));
206 
207  fclose (f);
208  return FALSE;
209  }
210 
211  if (fclose (f) == EOF)
212  {
213  dbus_set_error (error, _dbus_error_from_errno (errno),
214  "Failed to close \"%s\": %s", cfilename,
215  _dbus_strerror (errno));
216  return FALSE;
217  }
218 
219  return TRUE;
220 }
221 
235  DBusPipe *print_pid_pipe,
236  dbus_pid_t pid_to_write,
237  DBusError *error)
238 {
239  if (pidfile)
240  {
241  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
242  if (!_dbus_write_pid_file (pidfile,
243  pid_to_write,
244  error))
245  {
246  _dbus_verbose ("pid file write failed\n");
247  _DBUS_ASSERT_ERROR_IS_SET(error);
248  return FALSE;
249  }
250  }
251  else
252  {
253  _dbus_verbose ("No pid file requested\n");
254  }
255 
256  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
257  {
258  DBusString pid;
259  int bytes;
260 
261  _dbus_verbose ("writing our pid to pipe %d\n",
262  print_pid_pipe->fd);
263 
264  if (!_dbus_string_init (&pid))
265  {
266  _DBUS_SET_OOM (error);
267  return FALSE;
268  }
269 
270  if (!_dbus_string_append_int (&pid, pid_to_write) ||
271  !_dbus_string_append (&pid, "\n"))
272  {
273  _dbus_string_free (&pid);
274  _DBUS_SET_OOM (error);
275  return FALSE;
276  }
277 
278  bytes = _dbus_string_get_length (&pid);
279  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
280  {
281  /* _dbus_pipe_write sets error only on failure, not short write */
282  if (error != NULL && !dbus_error_is_set(error))
283  {
285  "Printing message bus PID: did not write enough bytes\n");
286  }
287  _dbus_string_free (&pid);
288  return FALSE;
289  }
290 
291  _dbus_string_free (&pid);
292  }
293  else
294  {
295  _dbus_verbose ("No pid pipe to write to\n");
296  }
297 
298  return TRUE;
299 }
300 
308 _dbus_verify_daemon_user (const char *user)
309 {
310  DBusString u;
311 
312  _dbus_string_init_const (&u, user);
313 
315 }
316 
317 
318 /* The HAVE_LIBAUDIT case lives in selinux.c */
319 #ifndef HAVE_LIBAUDIT
320 
328 _dbus_change_to_daemon_user (const char *user,
329  DBusError *error)
330 {
331  dbus_uid_t uid;
332  dbus_gid_t gid;
333  DBusString u;
334 
335  _dbus_string_init_const (&u, user);
336 
337  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
338  {
340  "User '%s' does not appear to exist?",
341  user);
342  return FALSE;
343  }
344 
345  /* setgroups() only works if we are a privileged process,
346  * so we don't return error on failure; the only possible
347  * failure is that we don't have perms to do it.
348  *
349  * not sure this is right, maybe if setuid()
350  * is going to work then setgroups() should also work.
351  */
352  if (setgroups (0, NULL) < 0)
353  _dbus_warn ("Failed to drop supplementary groups: %s\n",
354  _dbus_strerror (errno));
355 
356  /* Set GID first, or the setuid may remove our permission
357  * to change the GID
358  */
359  if (setgid (gid) < 0)
360  {
361  dbus_set_error (error, _dbus_error_from_errno (errno),
362  "Failed to set GID to %lu: %s", gid,
363  _dbus_strerror (errno));
364  return FALSE;
365  }
366 
367  if (setuid (uid) < 0)
368  {
369  dbus_set_error (error, _dbus_error_from_errno (errno),
370  "Failed to set UID to %lu: %s", uid,
371  _dbus_strerror (errno));
372  return FALSE;
373  }
374 
375  return TRUE;
376 }
377 #endif /* !HAVE_LIBAUDIT */
378 
379 
390 void
392 {
393 #ifdef HAVE_SETRLIMIT
394  struct rlimit lim;
395  struct rlimit target_lim;
396 
397  /* No point to doing this practically speaking
398  * if we're not uid 0. We expect the system
399  * bus to use this before we change UID, and
400  * the session bus takes the Linux default
401  * of 1024 for both cur and max.
402  */
403  if (getuid () != 0)
404  return;
405 
406  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
407  return;
408 
409  if (lim.rlim_cur >= limit)
410  return;
411 
412  /* Ignore "maximum limit", assume we have the "superuser"
413  * privileges. On Linux this is CAP_SYS_RESOURCE.
414  */
415  target_lim.rlim_cur = target_lim.rlim_max = limit;
416  /* Also ignore errors; if we fail, we will at least work
417  * up to whatever limit we had, which seems better than
418  * just outright aborting.
419  *
420  * However, in the future we should probably log this so OS builders
421  * have a chance to notice any misconfiguration like dbus-daemon
422  * being started without CAP_SYS_RESOURCE.
423  */
424  setrlimit (RLIMIT_NOFILE, &target_lim);
425 #endif
426 }
427 
428 void
429 _dbus_init_system_log (dbus_bool_t is_daemon)
430 {
431 #ifdef HAVE_SYSLOG_H
432  int logopts = LOG_PID;
433 
434 #ifdef HAVE_DECL_LOG_PERROR
435 #ifdef HAVE_SYSTEMD
436  if (!is_daemon || sd_booted () <= 0)
437 #endif
438  logopts |= LOG_PERROR;
439 #endif
440 
441  openlog ("dbus", logopts, LOG_DAEMON);
442 #endif
443 }
444 
453 void
454 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
455 {
456  va_list args;
457 
458  va_start (args, msg);
459 
460  _dbus_system_logv (severity, msg, args);
461 
462  va_end (args);
463 }
464 
475 void
476 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
477 {
478 #ifdef HAVE_SYSLOG_H
479  int flags;
480  switch (severity)
481  {
482  case DBUS_SYSTEM_LOG_INFO:
483  flags = LOG_DAEMON | LOG_NOTICE;
484  break;
485  case DBUS_SYSTEM_LOG_SECURITY:
486  flags = LOG_AUTH | LOG_NOTICE;
487  break;
488  case DBUS_SYSTEM_LOG_FATAL:
489  flags = LOG_DAEMON|LOG_CRIT;
490  break;
491  default:
492  return;
493  }
494 
495  vsyslog (flags, msg, args);
496 #endif
497 
498 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
499  {
500  /* vsyslog() won't write to stderr, so we'd better do it */
501  va_list tmp;
502 
503  DBUS_VA_COPY (tmp, args);
504  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
505  vfprintf (stderr, msg, tmp);
506  fputc ('\n', stderr);
507  va_end (tmp);
508  }
509 #endif
510 
511  if (severity == DBUS_SYSTEM_LOG_FATAL)
512  exit (1);
513 }
514 
520 void
522  DBusSignalHandler handler)
523 {
524  struct sigaction act;
525  sigset_t empty_mask;
526 
527  sigemptyset (&empty_mask);
528  act.sa_handler = handler;
529  act.sa_mask = empty_mask;
530  act.sa_flags = 0;
531  sigaction (sig, &act, NULL);
532 }
533 
540 _dbus_file_exists (const char *file)
541 {
542  return (access (file, F_OK) == 0);
543 }
544 
552 _dbus_user_at_console (const char *username,
553  DBusError *error)
554 {
555 
556  DBusString u, f;
557  dbus_bool_t result;
558 
559  result = FALSE;
560  if (!_dbus_string_init (&f))
561  {
562  _DBUS_SET_OOM (error);
563  return FALSE;
564  }
565 
566  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
567  {
568  _DBUS_SET_OOM (error);
569  goto out;
570  }
571 
572  _dbus_string_init_const (&u, username);
573 
574  if (!_dbus_concat_dir_and_file (&f, &u))
575  {
576  _DBUS_SET_OOM (error);
577  goto out;
578  }
579 
581 
582  out:
583  _dbus_string_free (&f);
584 
585  return result;
586 }
587 
588 
597 {
598  if (_dbus_string_get_length (filename) > 0)
599  return _dbus_string_get_byte (filename, 0) == '/';
600  else
601  return FALSE;
602 }
603 
613 _dbus_stat (const DBusString *filename,
614  DBusStat *statbuf,
615  DBusError *error)
616 {
617  const char *filename_c;
618  struct stat sb;
619 
620  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
621 
622  filename_c = _dbus_string_get_const_data (filename);
623 
624  if (stat (filename_c, &sb) < 0)
625  {
626  dbus_set_error (error, _dbus_error_from_errno (errno),
627  "%s", _dbus_strerror (errno));
628  return FALSE;
629  }
630 
631  statbuf->mode = sb.st_mode;
632  statbuf->nlink = sb.st_nlink;
633  statbuf->uid = sb.st_uid;
634  statbuf->gid = sb.st_gid;
635  statbuf->size = sb.st_size;
636  statbuf->atime = sb.st_atime;
637  statbuf->mtime = sb.st_mtime;
638  statbuf->ctime = sb.st_ctime;
639 
640  return TRUE;
641 }
642 
643 
648 {
649  DIR *d;
651 };
652 
662  DBusError *error)
663 {
664  DIR *d;
665  DBusDirIter *iter;
666  const char *filename_c;
667 
668  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
669 
670  filename_c = _dbus_string_get_const_data (filename);
671 
672  d = opendir (filename_c);
673  if (d == NULL)
674  {
675  dbus_set_error (error, _dbus_error_from_errno (errno),
676  "Failed to read directory \"%s\": %s",
677  filename_c,
678  _dbus_strerror (errno));
679  return NULL;
680  }
681  iter = dbus_new0 (DBusDirIter, 1);
682  if (iter == NULL)
683  {
684  closedir (d);
686  "Could not allocate memory for directory iterator");
687  return NULL;
688  }
689 
690  iter->d = d;
691 
692  return iter;
693 }
694 
710  DBusString *filename,
711  DBusError *error)
712 {
713  struct dirent *ent;
714  int err;
715 
716  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717 
718  again:
719  errno = 0;
720  ent = readdir (iter->d);
721 
722  if (!ent)
723  {
724  err = errno;
725 
726  if (err != 0)
727  dbus_set_error (error,
729  "%s", _dbus_strerror (err));
730 
731  return FALSE;
732  }
733  else if (ent->d_name[0] == '.' &&
734  (ent->d_name[1] == '\0' ||
735  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
736  goto again;
737  else
738  {
739  _dbus_string_set_length (filename, 0);
740  if (!_dbus_string_append (filename, ent->d_name))
741  {
743  "No memory to read directory entry");
744  return FALSE;
745  }
746  else
747  {
748  return TRUE;
749  }
750  }
751 }
752 
756 void
758 {
759  closedir (iter->d);
760  dbus_free (iter);
761 }
762 
763 static dbus_bool_t
764 fill_user_info_from_group (struct group *g,
765  DBusGroupInfo *info,
766  DBusError *error)
767 {
768  _dbus_assert (g->gr_name != NULL);
769 
770  info->gid = g->gr_gid;
771  info->groupname = _dbus_strdup (g->gr_name);
772 
773  /* info->members = dbus_strdupv (g->gr_mem) */
774 
775  if (info->groupname == NULL)
776  {
778  return FALSE;
779  }
780 
781  return TRUE;
782 }
783 
784 static dbus_bool_t
785 fill_group_info (DBusGroupInfo *info,
786  dbus_gid_t gid,
787  const DBusString *groupname,
788  DBusError *error)
789 {
790  const char *group_c_str;
791 
792  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
793  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
794 
795  if (groupname)
796  group_c_str = _dbus_string_get_const_data (groupname);
797  else
798  group_c_str = NULL;
799 
800  /* For now assuming that the getgrnam() and getgrgid() flavors
801  * always correspond to the pwnam flavors, if not we have
802  * to add more configure checks.
803  */
804 
805 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
806  {
807  struct group *g;
808  int result;
809  size_t buflen;
810  char *buf;
811  struct group g_str;
812  dbus_bool_t b;
813 
814  /* retrieve maximum needed size for buf */
815  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
816 
817  /* sysconf actually returns a long, but everything else expects size_t,
818  * so just recast here.
819  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
820  */
821  if ((long) buflen <= 0)
822  buflen = 1024;
823 
824  result = -1;
825  while (1)
826  {
827  buf = dbus_malloc (buflen);
828  if (buf == NULL)
829  {
831  return FALSE;
832  }
833 
834  g = NULL;
835 #ifdef HAVE_POSIX_GETPWNAM_R
836  if (group_c_str)
837  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
838  &g);
839  else
840  result = getgrgid_r (gid, &g_str, buf, buflen,
841  &g);
842 #else
843  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
844  result = 0;
845 #endif /* !HAVE_POSIX_GETPWNAM_R */
846  /* Try a bigger buffer if ERANGE was returned:
847  https://bugs.freedesktop.org/show_bug.cgi?id=16727
848  */
849  if (result == ERANGE && buflen < 512 * 1024)
850  {
851  dbus_free (buf);
852  buflen *= 2;
853  }
854  else
855  {
856  break;
857  }
858  }
859 
860  if (result == 0 && g == &g_str)
861  {
862  b = fill_user_info_from_group (g, info, error);
863  dbus_free (buf);
864  return b;
865  }
866  else
867  {
868  dbus_set_error (error, _dbus_error_from_errno (errno),
869  "Group %s unknown or failed to look it up\n",
870  group_c_str ? group_c_str : "???");
871  dbus_free (buf);
872  return FALSE;
873  }
874  }
875 #else /* ! HAVE_GETPWNAM_R */
876  {
877  /* I guess we're screwed on thread safety here */
878  struct group *g;
879 
880  g = getgrnam (group_c_str);
881 
882  if (g != NULL)
883  {
884  return fill_user_info_from_group (g, info, error);
885  }
886  else
887  {
888  dbus_set_error (error, _dbus_error_from_errno (errno),
889  "Group %s unknown or failed to look it up\n",
890  group_c_str ? group_c_str : "???");
891  return FALSE;
892  }
893  }
894 #endif /* ! HAVE_GETPWNAM_R */
895 }
896 
908  const DBusString *groupname,
909  DBusError *error)
910 {
911  return fill_group_info (info, DBUS_GID_UNSET,
912  groupname, error);
913 
914 }
915 
927  dbus_gid_t gid,
928  DBusError *error)
929 {
930  return fill_group_info (info, gid, NULL, error);
931 }
932 
943  dbus_uid_t *uid_p)
944 {
945  return _dbus_get_user_id (username, uid_p);
946 
947 }
948 
959  dbus_gid_t *gid_p)
960 {
961  return _dbus_get_group_id (groupname, gid_p);
962 }
963 
976  dbus_gid_t **group_ids,
977  int *n_group_ids)
978 {
979  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
980 }
981 
993  DBusError *error)
994 {
995  return _dbus_is_console_user (uid, error);
996 
997 }
998 
1008 {
1009  return uid == _dbus_geteuid ();
1010 }
1011 
1020 _dbus_windows_user_is_process_owner (const char *windows_sid)
1021 {
1022  return FALSE;
1023 }
1024  /* End of DBusInternalsUtils functions */
1026 
1040  DBusString *dirname)
1041 {
1042  int sep;
1043 
1044  _dbus_assert (filename != dirname);
1045  _dbus_assert (filename != NULL);
1046  _dbus_assert (dirname != NULL);
1047 
1048  /* Ignore any separators on the end */
1049  sep = _dbus_string_get_length (filename);
1050  if (sep == 0)
1051  return _dbus_string_append (dirname, "."); /* empty string passed in */
1052 
1053  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1054  --sep;
1055 
1056  _dbus_assert (sep >= 0);
1057 
1058  if (sep == 0)
1059  return _dbus_string_append (dirname, "/");
1060 
1061  /* Now find the previous separator */
1062  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1063  if (sep < 0)
1064  return _dbus_string_append (dirname, ".");
1065 
1066  /* skip multiple separators */
1067  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1068  --sep;
1069 
1070  _dbus_assert (sep >= 0);
1071 
1072  if (sep == 0 &&
1073  _dbus_string_get_byte (filename, 0) == '/')
1074  return _dbus_string_append (dirname, "/");
1075  else
1076  return _dbus_string_copy_len (filename, 0, sep - 0,
1077  dirname, _dbus_string_get_length (dirname));
1078 } /* DBusString stuff */
1080 
1081 static void
1082 string_squash_nonprintable (DBusString *str)
1083 {
1084  unsigned char *buf;
1085  int i, len;
1086 
1087  buf = _dbus_string_get_data (str);
1088  len = _dbus_string_get_length (str);
1089 
1090  for (i = 0; i < len; i++)
1091  {
1092  unsigned char c = (unsigned char) buf[i];
1093  if (c == '\0')
1094  buf[i] = ' ';
1095  else if (c < 0x20 || c > 127)
1096  buf[i] = '?';
1097  }
1098 }
1099 
1114 dbus_bool_t
1115 _dbus_command_for_pid (unsigned long pid,
1116  DBusString *str,
1117  int max_len,
1118  DBusError *error)
1119 {
1120  /* This is all Linux-specific for now */
1121  DBusString path;
1122  DBusString cmdline;
1123  int fd;
1124 
1125  if (!_dbus_string_init (&path))
1126  {
1127  _DBUS_SET_OOM (error);
1128  return FALSE;
1129  }
1130 
1131  if (!_dbus_string_init (&cmdline))
1132  {
1133  _DBUS_SET_OOM (error);
1134  _dbus_string_free (&path);
1135  return FALSE;
1136  }
1137 
1138  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1139  goto oom;
1140 
1141  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1142  if (fd < 0)
1143  {
1144  dbus_set_error (error,
1145  _dbus_error_from_errno (errno),
1146  "Failed to open \"%s\": %s",
1148  _dbus_strerror (errno));
1149  goto fail;
1150  }
1151 
1152  if (!_dbus_read (fd, &cmdline, max_len))
1153  {
1154  dbus_set_error (error,
1155  _dbus_error_from_errno (errno),
1156  "Failed to read from \"%s\": %s",
1158  _dbus_strerror (errno));
1159  _dbus_close (fd, NULL);
1160  goto fail;
1161  }
1162 
1163  if (!_dbus_close (fd, error))
1164  goto fail;
1165 
1166  string_squash_nonprintable (&cmdline);
1167 
1168  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1169  goto oom;
1170 
1171  _dbus_string_free (&cmdline);
1172  _dbus_string_free (&path);
1173  return TRUE;
1174 oom:
1175  _DBUS_SET_OOM (error);
1176 fail:
1177  _dbus_string_free (&cmdline);
1178  _dbus_string_free (&path);
1179  return FALSE;
1180 }
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_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:390
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:397
unsigned char _dbus_string_get_byte(const DBusString *str, int start)
Gets the byte at the given position.
Definition: dbus-string.c:548
dbus_bool_t _dbus_file_exists(const char *file)
File interface.
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&#39;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:392
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:395
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_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_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1119
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:432
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:399
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
#define TRUE
Expands to &quot;1&quot;.
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:393
#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_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...
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:394
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; &quot;something went wrong&quot; - 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 &quot;0&quot;.
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:398
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:396
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
void _dbus_request_file_descriptor_limit(unsigned int limit)
Attempt to ensure that the current process can open at least file descriptors.
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