D-Bus  1.6.2
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 
386 void
388 {
389 #ifdef HAVE_SETRLIMIT
390  struct rlimit lim;
391  struct rlimit target_lim;
392 
393  /* No point to doing this practically speaking
394  * if we're not uid 0. We expect the system
395  * bus to use this before we change UID, and
396  * the session bus takes the Linux default
397  * of 1024 for both cur and max.
398  */
399  if (getuid () != 0)
400  return;
401 
402  if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
403  return;
404 
405  if (lim.rlim_cur >= limit)
406  return;
407 
408  /* Ignore "maximum limit", assume we have the "superuser"
409  * privileges. On Linux this is CAP_SYS_RESOURCE.
410  */
411  target_lim.rlim_cur = target_lim.rlim_max = limit;
412  /* Also ignore errors; if we fail, we will at least work
413  * up to whatever limit we had, which seems better than
414  * just outright aborting.
415  *
416  * However, in the future we should probably log this so OS builders
417  * have a chance to notice any misconfiguration like dbus-daemon
418  * being started without CAP_SYS_RESOURCE.
419  */
420  setrlimit (RLIMIT_NOFILE, &target_lim);
421 #endif
422 }
423 
424 void
425 _dbus_init_system_log (void)
426 {
427 #ifdef HAVE_DECL_LOG_PERROR
428  openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
429 #else
430  openlog ("dbus", LOG_PID, LOG_DAEMON);
431 #endif
432 }
433 
442 void
443 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
444 {
445  va_list args;
446 
447  va_start (args, msg);
448 
449  _dbus_system_logv (severity, msg, args);
450 
451  va_end (args);
452 }
453 
464 void
465 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
466 {
467  int flags;
468  switch (severity)
469  {
470  case DBUS_SYSTEM_LOG_INFO:
471  flags = LOG_DAEMON | LOG_NOTICE;
472  break;
473  case DBUS_SYSTEM_LOG_SECURITY:
474  flags = LOG_AUTH | LOG_NOTICE;
475  break;
476  case DBUS_SYSTEM_LOG_FATAL:
477  flags = LOG_DAEMON|LOG_CRIT;
478  break;
479  default:
480  return;
481  }
482 
483 #ifndef HAVE_DECL_LOG_PERROR
484  {
485  /* vsyslog() won't write to stderr, so we'd better do it */
486  va_list tmp;
487 
488  DBUS_VA_COPY (tmp, args);
489  fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
490  vfprintf (stderr, msg, tmp);
491  fputc ('\n', stderr);
492  va_end (tmp);
493  }
494 #endif
495 
496  vsyslog (flags, msg, args);
497 
498  if (severity == DBUS_SYSTEM_LOG_FATAL)
499  exit (1);
500 }
501 
507 void
509  DBusSignalHandler handler)
510 {
511  struct sigaction act;
512  sigset_t empty_mask;
513 
514  sigemptyset (&empty_mask);
515  act.sa_handler = handler;
516  act.sa_mask = empty_mask;
517  act.sa_flags = 0;
518  sigaction (sig, &act, NULL);
519 }
520 
527 _dbus_file_exists (const char *file)
528 {
529  return (access (file, F_OK) == 0);
530 }
531 
539 _dbus_user_at_console (const char *username,
540  DBusError *error)
541 {
542 
543  DBusString f;
544  dbus_bool_t result;
545 
546  result = FALSE;
547  if (!_dbus_string_init (&f))
548  {
549  _DBUS_SET_OOM (error);
550  return FALSE;
551  }
552 
553  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
554  {
555  _DBUS_SET_OOM (error);
556  goto out;
557  }
558 
559 
560  if (!_dbus_string_append (&f, username))
561  {
562  _DBUS_SET_OOM (error);
563  goto out;
564  }
565 
566  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
567 
568  out:
569  _dbus_string_free (&f);
570 
571  return result;
572 }
573 
574 
583 {
584  if (_dbus_string_get_length (filename) > 0)
585  return _dbus_string_get_byte (filename, 0) == '/';
586  else
587  return FALSE;
588 }
589 
599 _dbus_stat (const DBusString *filename,
600  DBusStat *statbuf,
601  DBusError *error)
602 {
603  const char *filename_c;
604  struct stat sb;
605 
606  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
607 
608  filename_c = _dbus_string_get_const_data (filename);
609 
610  if (stat (filename_c, &sb) < 0)
611  {
612  dbus_set_error (error, _dbus_error_from_errno (errno),
613  "%s", _dbus_strerror (errno));
614  return FALSE;
615  }
616 
617  statbuf->mode = sb.st_mode;
618  statbuf->nlink = sb.st_nlink;
619  statbuf->uid = sb.st_uid;
620  statbuf->gid = sb.st_gid;
621  statbuf->size = sb.st_size;
622  statbuf->atime = sb.st_atime;
623  statbuf->mtime = sb.st_mtime;
624  statbuf->ctime = sb.st_ctime;
625 
626  return TRUE;
627 }
628 
629 
634 {
635  DIR *d;
637 };
638 
648  DBusError *error)
649 {
650  DIR *d;
651  DBusDirIter *iter;
652  const char *filename_c;
653 
654  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
655 
656  filename_c = _dbus_string_get_const_data (filename);
657 
658  d = opendir (filename_c);
659  if (d == NULL)
660  {
661  dbus_set_error (error, _dbus_error_from_errno (errno),
662  "Failed to read directory \"%s\": %s",
663  filename_c,
664  _dbus_strerror (errno));
665  return NULL;
666  }
667  iter = dbus_new0 (DBusDirIter, 1);
668  if (iter == NULL)
669  {
670  closedir (d);
672  "Could not allocate memory for directory iterator");
673  return NULL;
674  }
675 
676  iter->d = d;
677 
678  return iter;
679 }
680 
696  DBusString *filename,
697  DBusError *error)
698 {
699  struct dirent *ent;
700  int err;
701 
702  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
703 
704  again:
705  errno = 0;
706  ent = readdir (iter->d);
707 
708  if (!ent)
709  {
710  err = errno;
711 
712  if (err != 0)
713  dbus_set_error (error,
715  "%s", _dbus_strerror (err));
716 
717  return FALSE;
718  }
719  else if (ent->d_name[0] == '.' &&
720  (ent->d_name[1] == '\0' ||
721  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
722  goto again;
723  else
724  {
725  _dbus_string_set_length (filename, 0);
726  if (!_dbus_string_append (filename, ent->d_name))
727  {
729  "No memory to read directory entry");
730  return FALSE;
731  }
732  else
733  {
734  return TRUE;
735  }
736  }
737 }
738 
742 void
744 {
745  closedir (iter->d);
746  dbus_free (iter);
747 }
748 
749 static dbus_bool_t
750 fill_user_info_from_group (struct group *g,
751  DBusGroupInfo *info,
752  DBusError *error)
753 {
754  _dbus_assert (g->gr_name != NULL);
755 
756  info->gid = g->gr_gid;
757  info->groupname = _dbus_strdup (g->gr_name);
758 
759  /* info->members = dbus_strdupv (g->gr_mem) */
760 
761  if (info->groupname == NULL)
762  {
764  return FALSE;
765  }
766 
767  return TRUE;
768 }
769 
770 static dbus_bool_t
771 fill_group_info (DBusGroupInfo *info,
772  dbus_gid_t gid,
773  const DBusString *groupname,
774  DBusError *error)
775 {
776  const char *group_c_str;
777 
778  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
779  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
780 
781  if (groupname)
782  group_c_str = _dbus_string_get_const_data (groupname);
783  else
784  group_c_str = NULL;
785 
786  /* For now assuming that the getgrnam() and getgrgid() flavors
787  * always correspond to the pwnam flavors, if not we have
788  * to add more configure checks.
789  */
790 
791 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
792  {
793  struct group *g;
794  int result;
795  size_t buflen;
796  char *buf;
797  struct group g_str;
798  dbus_bool_t b;
799 
800  /* retrieve maximum needed size for buf */
801  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
802 
803  /* sysconf actually returns a long, but everything else expects size_t,
804  * so just recast here.
805  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
806  */
807  if ((long) buflen <= 0)
808  buflen = 1024;
809 
810  result = -1;
811  while (1)
812  {
813  buf = dbus_malloc (buflen);
814  if (buf == NULL)
815  {
817  return FALSE;
818  }
819 
820  g = NULL;
821 #ifdef HAVE_POSIX_GETPWNAM_R
822  if (group_c_str)
823  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
824  &g);
825  else
826  result = getgrgid_r (gid, &g_str, buf, buflen,
827  &g);
828 #else
829  g = getgrnam_r (group_c_str, &g_str, buf, buflen);
830  result = 0;
831 #endif /* !HAVE_POSIX_GETPWNAM_R */
832  /* Try a bigger buffer if ERANGE was returned:
833  https://bugs.freedesktop.org/show_bug.cgi?id=16727
834  */
835  if (result == ERANGE && buflen < 512 * 1024)
836  {
837  dbus_free (buf);
838  buflen *= 2;
839  }
840  else
841  {
842  break;
843  }
844  }
845 
846  if (result == 0 && g == &g_str)
847  {
848  b = fill_user_info_from_group (g, info, error);
849  dbus_free (buf);
850  return b;
851  }
852  else
853  {
854  dbus_set_error (error, _dbus_error_from_errno (errno),
855  "Group %s unknown or failed to look it up\n",
856  group_c_str ? group_c_str : "???");
857  dbus_free (buf);
858  return FALSE;
859  }
860  }
861 #else /* ! HAVE_GETPWNAM_R */
862  {
863  /* I guess we're screwed on thread safety here */
864  struct group *g;
865 
866  g = getgrnam (group_c_str);
867 
868  if (g != NULL)
869  {
870  return fill_user_info_from_group (g, info, error);
871  }
872  else
873  {
874  dbus_set_error (error, _dbus_error_from_errno (errno),
875  "Group %s unknown or failed to look it up\n",
876  group_c_str ? group_c_str : "???");
877  return FALSE;
878  }
879  }
880 #endif /* ! HAVE_GETPWNAM_R */
881 }
882 
894  const DBusString *groupname,
895  DBusError *error)
896 {
897  return fill_group_info (info, DBUS_GID_UNSET,
898  groupname, error);
899 
900 }
901 
913  dbus_gid_t gid,
914  DBusError *error)
915 {
916  return fill_group_info (info, gid, NULL, error);
917 }
918 
929  dbus_uid_t *uid_p)
930 {
931  return _dbus_get_user_id (username, uid_p);
932 
933 }
934 
945  dbus_gid_t *gid_p)
946 {
947  return _dbus_get_group_id (groupname, gid_p);
948 }
949 
962  dbus_gid_t **group_ids,
963  int *n_group_ids)
964 {
965  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
966 }
967 
979  DBusError *error)
980 {
981  return _dbus_is_console_user (uid, error);
982 
983 }
984 
994 {
995  return uid == _dbus_geteuid ();
996 }
997 
1006 _dbus_windows_user_is_process_owner (const char *windows_sid)
1007 {
1008  return FALSE;
1009 }
1010  /* End of DBusInternalsUtils functions */
1012 
1026  DBusString *dirname)
1027 {
1028  int sep;
1029 
1030  _dbus_assert (filename != dirname);
1031  _dbus_assert (filename != NULL);
1032  _dbus_assert (dirname != NULL);
1033 
1034  /* Ignore any separators on the end */
1035  sep = _dbus_string_get_length (filename);
1036  if (sep == 0)
1037  return _dbus_string_append (dirname, "."); /* empty string passed in */
1038 
1039  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1040  --sep;
1041 
1042  _dbus_assert (sep >= 0);
1043 
1044  if (sep == 0)
1045  return _dbus_string_append (dirname, "/");
1046 
1047  /* Now find the previous separator */
1048  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1049  if (sep < 0)
1050  return _dbus_string_append (dirname, ".");
1051 
1052  /* skip multiple separators */
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  _dbus_string_get_byte (filename, 0) == '/')
1060  return _dbus_string_append (dirname, "/");
1061  else
1062  return _dbus_string_copy_len (filename, 0, sep - 0,
1063  dirname, _dbus_string_get_length (dirname));
1064 } /* DBusString stuff */
1066 
1067 static void
1068 string_squash_nonprintable (DBusString *str)
1069 {
1070  unsigned char *buf;
1071  int i, len;
1072 
1073  buf = _dbus_string_get_data (str);
1074  len = _dbus_string_get_length (str);
1075 
1076  for (i = 0; i < len; i++)
1077  {
1078  unsigned char c = (unsigned char) buf[i];
1079  if (c == '\0')
1080  buf[i] = ' ';
1081  else if (c < 0x20 || c > 127)
1082  buf[i] = '?';
1083  }
1084 }
1085 
1100 dbus_bool_t
1101 _dbus_command_for_pid (unsigned long pid,
1102  DBusString *str,
1103  int max_len,
1104  DBusError *error)
1105 {
1106  /* This is all Linux-specific for now */
1107  DBusString path;
1108  DBusString cmdline;
1109  int fd;
1110 
1111  if (!_dbus_string_init (&path))
1112  {
1113  _DBUS_SET_OOM (error);
1114  return FALSE;
1115  }
1116 
1117  if (!_dbus_string_init (&cmdline))
1118  {
1119  _DBUS_SET_OOM (error);
1120  _dbus_string_free (&path);
1121  return FALSE;
1122  }
1123 
1124  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1125  goto oom;
1126 
1127  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1128  if (fd < 0)
1129  {
1130  dbus_set_error (error,
1131  _dbus_error_from_errno (errno),
1132  "Failed to open \"%s\": %s",
1133  _dbus_string_get_const_data (&path),
1134  _dbus_strerror (errno));
1135  goto fail;
1136  }
1137 
1138  if (!_dbus_read (fd, &cmdline, max_len))
1139  {
1140  dbus_set_error (error,
1141  _dbus_error_from_errno (errno),
1142  "Failed to read from \"%s\": %s",
1143  _dbus_string_get_const_data (&path),
1144  _dbus_strerror (errno));
1145  goto fail;
1146  }
1147 
1148  if (!_dbus_close (fd, error))
1149  goto fail;
1150 
1151  string_squash_nonprintable (&cmdline);
1152 
1153  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1154  goto oom;
1155 
1156  _dbus_string_free (&cmdline);
1157  _dbus_string_free (&path);
1158  return TRUE;
1159 oom:
1160  _DBUS_SET_OOM (error);
1161 fail:
1162  _dbus_string_free (&cmdline);
1163  _dbus_string_free (&path);
1164  return FALSE;
1165 }