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