D-Bus  1.14.6
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-list.h"
30 #include "dbus-pipe.h"
31 #include "dbus-protocol.h"
32 #include "dbus-string.h"
33 #define DBUS_USERDB_INCLUDES_PRIVATE 1
34 #include "dbus-userdb.h"
35 #include "dbus-test.h"
36 
37 #include <sys/types.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <signal.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <limits.h>
47 #include <sys/stat.h>
48 #ifdef HAVE_SYS_RESOURCE_H
49 #include <sys/resource.h>
50 #endif
51 #include <grp.h>
52 #include <sys/socket.h>
53 #include <dirent.h>
54 #include <sys/un.h>
55 
56 #ifdef HAVE_SYS_PRCTL_H
57 #include <sys/prctl.h>
58 #endif
59 
60 #ifdef HAVE_SYSTEMD
61 #include <systemd/sd-daemon.h>
62 #endif
63 
64 #ifndef O_BINARY
65 #define O_BINARY 0
66 #endif
67 
85  DBusPipe *print_pid_pipe,
86  DBusError *error,
87  dbus_bool_t keep_umask)
88 {
89  const char *s;
90  pid_t child_pid;
91  DBusEnsureStandardFdsFlags flags;
92 
93  _dbus_verbose ("Becoming a daemon...\n");
94 
95  _dbus_verbose ("chdir to /\n");
96  if (chdir ("/") < 0)
97  {
99  "Could not chdir() to root directory");
100  return FALSE;
101  }
102 
103  _dbus_verbose ("forking...\n");
104 
105  /* Make sure our output buffers aren't redundantly printed by both the
106  * parent and the child */
107  fflush (stdout);
108  fflush (stderr);
109 
110  switch ((child_pid = fork ()))
111  {
112  case -1:
113  _dbus_verbose ("fork failed\n");
114  dbus_set_error (error, _dbus_error_from_errno (errno),
115  "Failed to fork daemon: %s", _dbus_strerror (errno));
116  return FALSE;
117  break;
118 
119  case 0:
120  _dbus_verbose ("in child, closing std file descriptors\n");
121 
122  flags = DBUS_FORCE_STDIN_NULL | DBUS_FORCE_STDOUT_NULL;
123  s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
124 
125  if (s == NULL || *s == '\0')
126  flags |= DBUS_FORCE_STDERR_NULL;
127  else
128  _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
129 
130  if (!_dbus_ensure_standard_fds (flags, &s))
131  {
132  _dbus_warn ("%s: %s", s, _dbus_strerror (errno));
133  _exit (1);
134  }
135 
136  if (!keep_umask)
137  {
138  /* Get a predictable umask */
139  _dbus_verbose ("setting umask\n");
140  umask (022);
141  }
142 
143  _dbus_verbose ("calling setsid()\n");
144  if (setsid () == -1)
145  _dbus_assert_not_reached ("setsid() failed");
146 
147  break;
148 
149  default:
150  if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
151  child_pid, error))
152  {
153  _dbus_verbose ("pid file or pipe write failed: %s\n",
154  error->message);
155  kill (child_pid, SIGTERM);
156  return FALSE;
157  }
158 
159  _dbus_verbose ("parent exiting\n");
160  _exit (0);
161  break;
162  }
163 
164  return TRUE;
165 }
166 
167 
176 static dbus_bool_t
177 _dbus_write_pid_file (const DBusString *filename,
178  unsigned long pid,
179  DBusError *error)
180 {
181  const char *cfilename;
182  int fd;
183  FILE *f;
184 
185  cfilename = _dbus_string_get_const_data (filename);
186 
187  fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
188 
189  if (fd < 0)
190  {
191  dbus_set_error (error, _dbus_error_from_errno (errno),
192  "Failed to open \"%s\": %s", cfilename,
193  _dbus_strerror (errno));
194  return FALSE;
195  }
196 
197  if ((f = fdopen (fd, "w")) == NULL)
198  {
199  dbus_set_error (error, _dbus_error_from_errno (errno),
200  "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
201  _dbus_close (fd, NULL);
202  return FALSE;
203  }
204 
205  if (fprintf (f, "%lu\n", pid) < 0)
206  {
207  dbus_set_error (error, _dbus_error_from_errno (errno),
208  "Failed to write to \"%s\": %s", cfilename,
209  _dbus_strerror (errno));
210 
211  fclose (f);
212  return FALSE;
213  }
214 
215  if (fclose (f) == EOF)
216  {
217  dbus_set_error (error, _dbus_error_from_errno (errno),
218  "Failed to close \"%s\": %s", cfilename,
219  _dbus_strerror (errno));
220  return FALSE;
221  }
222 
223  return TRUE;
224 }
225 
239  DBusPipe *print_pid_pipe,
240  dbus_pid_t pid_to_write,
241  DBusError *error)
242 {
243  if (pidfile)
244  {
245  _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
246  if (!_dbus_write_pid_file (pidfile,
247  pid_to_write,
248  error))
249  {
250  _dbus_verbose ("pid file write failed\n");
251  _DBUS_ASSERT_ERROR_IS_SET(error);
252  return FALSE;
253  }
254  }
255  else
256  {
257  _dbus_verbose ("No pid file requested\n");
258  }
259 
260  if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
261  {
262  DBusString pid;
263  int bytes;
264 
265  _dbus_verbose ("writing our pid to pipe %d\n",
266  print_pid_pipe->fd);
267 
268  if (!_dbus_string_init (&pid))
269  {
270  _DBUS_SET_OOM (error);
271  return FALSE;
272  }
273 
274  if (!_dbus_string_append_int (&pid, pid_to_write) ||
275  !_dbus_string_append (&pid, "\n"))
276  {
277  _dbus_string_free (&pid);
278  _DBUS_SET_OOM (error);
279  return FALSE;
280  }
281 
282  bytes = _dbus_string_get_length (&pid);
283  if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
284  {
285  /* _dbus_pipe_write sets error only on failure, not short write */
286  if (error != NULL && !dbus_error_is_set(error))
287  {
289  "Printing message bus PID: did not write enough bytes\n");
290  }
291  _dbus_string_free (&pid);
292  return FALSE;
293  }
294 
295  _dbus_string_free (&pid);
296  }
297  else
298  {
299  _dbus_verbose ("No pid pipe to write to\n");
300  }
301 
302  return TRUE;
303 }
304 
312 _dbus_verify_daemon_user (const char *user)
313 {
314  DBusString u;
315 
316  _dbus_string_init_const (&u, user);
317 
319 }
320 
321 
322 /* The HAVE_LIBAUDIT case lives in selinux.c */
323 #ifndef HAVE_LIBAUDIT
324 
332 _dbus_change_to_daemon_user (const char *user,
333  DBusError *error)
334 {
335  dbus_uid_t uid;
336  dbus_gid_t gid;
337  DBusString u;
338 
339  _dbus_string_init_const (&u, user);
340 
341  if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
342  {
344  "User '%s' does not appear to exist?",
345  user);
346  return FALSE;
347  }
348 
349  /* setgroups() only works if we are a privileged process,
350  * so we don't return error on failure; the only possible
351  * failure is that we don't have perms to do it.
352  *
353  * not sure this is right, maybe if setuid()
354  * is going to work then setgroups() should also work.
355  */
356  if (setgroups (0, NULL) < 0)
357  _dbus_warn ("Failed to drop supplementary groups: %s",
358  _dbus_strerror (errno));
359 
360  /* Set GID first, or the setuid may remove our permission
361  * to change the GID
362  */
363  if (setgid (gid) < 0)
364  {
365  dbus_set_error (error, _dbus_error_from_errno (errno),
366  "Failed to set GID to %lu: %s", gid,
367  _dbus_strerror (errno));
368  return FALSE;
369  }
370 
371  if (setuid (uid) < 0)
372  {
373  dbus_set_error (error, _dbus_error_from_errno (errno),
374  "Failed to set UID to %lu: %s", uid,
375  _dbus_strerror (errno));
376  return FALSE;
377  }
378 
379  return TRUE;
380 }
381 #endif /* !HAVE_LIBAUDIT */
382 
383 #ifdef HAVE_SETRLIMIT
384 
385 /* We assume that if we have setrlimit, we also have getrlimit and
386  * struct rlimit.
387  */
388 
389 struct DBusRLimit {
390  struct rlimit lim;
391 };
392 
393 DBusRLimit *
394 _dbus_rlimit_save_fd_limit (DBusError *error)
395 {
396  DBusRLimit *self;
397 
398  self = dbus_new0 (DBusRLimit, 1);
399 
400  if (self == NULL)
401  {
402  _DBUS_SET_OOM (error);
403  return NULL;
404  }
405 
406  if (getrlimit (RLIMIT_NOFILE, &self->lim) < 0)
407  {
408  dbus_set_error (error, _dbus_error_from_errno (errno),
409  "Failed to get fd limit: %s", _dbus_strerror (errno));
410  dbus_free (self);
411  return NULL;
412  }
413 
414  return self;
415 }
416 
417 /* Enough fds that we shouldn't run out, even if several uids work
418  * together to carry out a denial-of-service attack. This happens to be
419  * the same number that systemd < 234 would normally use. */
420 #define ENOUGH_FDS 65536
421 
423 _dbus_rlimit_raise_fd_limit (DBusError *error)
424 {
425  struct rlimit old, lim;
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  old = lim;
435 
436  if (getuid () == 0)
437  {
438  /* We are privileged, so raise the soft limit to at least
439  * ENOUGH_FDS, and the hard limit to at least the desired soft
440  * limit. This assumes we can exercise CAP_SYS_RESOURCE on Linux,
441  * or other OSs' equivalents. */
442  if (lim.rlim_cur != RLIM_INFINITY &&
443  lim.rlim_cur < ENOUGH_FDS)
444  lim.rlim_cur = ENOUGH_FDS;
445 
446  if (lim.rlim_max != RLIM_INFINITY &&
447  lim.rlim_max < lim.rlim_cur)
448  lim.rlim_max = lim.rlim_cur;
449  }
450 
451  /* Raise the soft limit to match the hard limit, which we can do even
452  * if we are unprivileged. In particular, systemd >= 240 will normally
453  * set rlim_cur to 1024 and rlim_max to 512*1024, recent Debian
454  * versions end up setting rlim_cur to 1024 and rlim_max to 1024*1024,
455  * and older and non-systemd Linux systems would typically set rlim_cur
456  * to 1024 and rlim_max to 4096. */
457  if (lim.rlim_max == RLIM_INFINITY || lim.rlim_cur < lim.rlim_max)
458  {
459 #if defined(__APPLE__) && defined(__MACH__)
460  /* macOS 10.5 and above no longer allows RLIM_INFINITY for rlim_cur */
461  lim.rlim_cur = MIN (OPEN_MAX, lim.rlim_max);
462 #else
463  lim.rlim_cur = lim.rlim_max;
464 #endif
465  }
466 
467  /* Early-return if there is nothing to do. */
468  if (lim.rlim_max == old.rlim_max &&
469  lim.rlim_cur == old.rlim_cur)
470  return TRUE;
471 
472  if (setrlimit (RLIMIT_NOFILE, &lim) < 0)
473  {
474  dbus_set_error (error, _dbus_error_from_errno (errno),
475  "Failed to set fd limit to %lu: %s",
476  (unsigned long) lim.rlim_cur,
477  _dbus_strerror (errno));
478  return FALSE;
479  }
480 
481  return TRUE;
482 }
483 
485 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
486  DBusError *error)
487 {
488  if (setrlimit (RLIMIT_NOFILE, &saved->lim) < 0)
489  {
490  dbus_set_error (error, _dbus_error_from_errno (errno),
491  "Failed to restore old fd limit: %s",
492  _dbus_strerror (errno));
493  return FALSE;
494  }
495 
496  return TRUE;
497 }
498 
499 #else /* !HAVE_SETRLIMIT */
500 
501 static void
502 fd_limit_not_supported (DBusError *error)
503 {
505  "cannot change fd limit on this platform");
506 }
507 
508 DBusRLimit *
509 _dbus_rlimit_save_fd_limit (DBusError *error)
510 {
511  fd_limit_not_supported (error);
512  return NULL;
513 }
514 
516 _dbus_rlimit_raise_fd_limit (DBusError *error)
517 {
518  fd_limit_not_supported (error);
519  return FALSE;
520 }
521 
523 _dbus_rlimit_restore_fd_limit (DBusRLimit *saved,
524  DBusError *error)
525 {
526  fd_limit_not_supported (error);
527  return FALSE;
528 }
529 
530 #endif
531 
532 void
533 _dbus_rlimit_free (DBusRLimit *lim)
534 {
535  dbus_free (lim);
536 }
537 
543 void
545  DBusSignalHandler handler)
546 {
547  struct sigaction act;
548  sigset_t empty_mask;
549 
550  sigemptyset (&empty_mask);
551  act.sa_handler = handler;
552  act.sa_mask = empty_mask;
553  act.sa_flags = 0;
554  sigaction (sig, &act, NULL);
555 }
556 
563 _dbus_file_exists (const char *file)
564 {
565  return (access (file, F_OK) == 0);
566 }
567 
575 _dbus_user_at_console (const char *username,
576  DBusError *error)
577 {
578 #ifdef DBUS_CONSOLE_AUTH_DIR
579  DBusString u, f;
580  dbus_bool_t result;
581 
582  result = FALSE;
583  if (!_dbus_string_init (&f))
584  {
585  _DBUS_SET_OOM (error);
586  return FALSE;
587  }
588 
589  if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
590  {
591  _DBUS_SET_OOM (error);
592  goto out;
593  }
594 
595  _dbus_string_init_const (&u, username);
596 
597  if (!_dbus_concat_dir_and_file (&f, &u))
598  {
599  _DBUS_SET_OOM (error);
600  goto out;
601  }
602 
603  result = _dbus_file_exists (_dbus_string_get_const_data (&f));
604 
605  out:
606  _dbus_string_free (&f);
607 
608  return result;
609 #else
610  return FALSE;
611 #endif
612 }
613 
614 
623 {
624  if (_dbus_string_get_length (filename) > 0)
625  return _dbus_string_get_byte (filename, 0) == '/';
626  else
627  return FALSE;
628 }
629 
639 _dbus_stat (const DBusString *filename,
640  DBusStat *statbuf,
641  DBusError *error)
642 {
643  const char *filename_c;
644  struct stat sb;
645 
646  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
647 
648  filename_c = _dbus_string_get_const_data (filename);
649 
650  if (stat (filename_c, &sb) < 0)
651  {
652  dbus_set_error (error, _dbus_error_from_errno (errno),
653  "%s", _dbus_strerror (errno));
654  return FALSE;
655  }
656 
657  statbuf->mode = sb.st_mode;
658  statbuf->nlink = sb.st_nlink;
659  statbuf->uid = sb.st_uid;
660  statbuf->gid = sb.st_gid;
661  statbuf->size = sb.st_size;
662  statbuf->atime = sb.st_atime;
663  statbuf->mtime = sb.st_mtime;
664  statbuf->ctime = sb.st_ctime;
665 
666  return TRUE;
667 }
668 
669 
674 {
675  DIR *d;
677 };
678 
688  DBusError *error)
689 {
690  DIR *d;
691  DBusDirIter *iter;
692  const char *filename_c;
693 
694  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
695 
696  filename_c = _dbus_string_get_const_data (filename);
697 
698  d = opendir (filename_c);
699  if (d == NULL)
700  {
701  dbus_set_error (error, _dbus_error_from_errno (errno),
702  "Failed to read directory \"%s\": %s",
703  filename_c,
704  _dbus_strerror (errno));
705  return NULL;
706  }
707  iter = dbus_new0 (DBusDirIter, 1);
708  if (iter == NULL)
709  {
710  closedir (d);
712  "Could not allocate memory for directory iterator");
713  return NULL;
714  }
715 
716  iter->d = d;
717 
718  return iter;
719 }
720 
736  DBusString *filename,
737  DBusError *error)
738 {
739  struct dirent *ent;
740  int err;
741 
742  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
743 
744  again:
745  errno = 0;
746  ent = readdir (iter->d);
747 
748  if (!ent)
749  {
750  err = errno;
751 
752  if (err != 0)
753  dbus_set_error (error,
755  "%s", _dbus_strerror (err));
756 
757  return FALSE;
758  }
759  else if (ent->d_name[0] == '.' &&
760  (ent->d_name[1] == '\0' ||
761  (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
762  goto again;
763  else
764  {
765  _dbus_string_set_length (filename, 0);
766  if (!_dbus_string_append (filename, ent->d_name))
767  {
769  "No memory to read directory entry");
770  return FALSE;
771  }
772  else
773  {
774  return TRUE;
775  }
776  }
777 }
778 
782 void
784 {
785  closedir (iter->d);
786  dbus_free (iter);
787 }
788 
789 static dbus_bool_t
790 fill_user_info_from_group (struct group *g,
791  DBusGroupInfo *info,
792  DBusError *error)
793 {
794  _dbus_assert (g->gr_name != NULL);
795 
796  info->gid = g->gr_gid;
797  info->groupname = _dbus_strdup (g->gr_name);
798 
799  /* info->members = dbus_strdupv (g->gr_mem) */
800 
801  if (info->groupname == NULL)
802  {
804  return FALSE;
805  }
806 
807  return TRUE;
808 }
809 
810 static dbus_bool_t
811 fill_group_info (DBusGroupInfo *info,
812  dbus_gid_t gid,
813  const DBusString *groupname,
814  DBusError *error)
815 {
816  const char *group_c_str;
817 
818  _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
819  _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
820 
821  if (groupname)
822  group_c_str = _dbus_string_get_const_data (groupname);
823  else
824  group_c_str = NULL;
825 
826  /* For now assuming that the getgrnam() and getgrgid() flavors
827  * always correspond to the pwnam flavors, if not we have
828  * to add more configure checks.
829  */
830 
831 #ifdef HAVE_GETPWNAM_R
832  {
833  struct group *g;
834  int result;
835  size_t buflen;
836  char *buf;
837  struct group g_str;
838  dbus_bool_t b;
839 
840  /* retrieve maximum needed size for buf */
841  buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
842 
843  /* sysconf actually returns a long, but everything else expects size_t,
844  * so just recast here.
845  * https://bugs.freedesktop.org/show_bug.cgi?id=17061
846  */
847  if ((long) buflen <= 0)
848  buflen = 1024;
849 
850  result = -1;
851  while (1)
852  {
853  buf = dbus_malloc (buflen);
854  if (buf == NULL)
855  {
857  return FALSE;
858  }
859 
860  g = NULL;
861  if (group_c_str)
862  result = getgrnam_r (group_c_str, &g_str, buf, buflen,
863  &g);
864  else
865  result = getgrgid_r (gid, &g_str, buf, buflen,
866  &g);
867  /* Try a bigger buffer if ERANGE was returned:
868  https://bugs.freedesktop.org/show_bug.cgi?id=16727
869  */
870  if (result == ERANGE && buflen < 512 * 1024)
871  {
872  dbus_free (buf);
873  buflen *= 2;
874  }
875  else
876  {
877  break;
878  }
879  }
880 
881  if (result == 0 && g == &g_str)
882  {
883  b = fill_user_info_from_group (g, info, error);
884  dbus_free (buf);
885  return b;
886  }
887  else
888  {
889  dbus_set_error (error, _dbus_error_from_errno (errno),
890  "Group %s unknown or failed to look it up\n",
891  group_c_str ? group_c_str : "???");
892  dbus_free (buf);
893  return FALSE;
894  }
895  }
896 #else /* ! HAVE_GETPWNAM_R */
897  {
898  /* I guess we're screwed on thread safety here */
899  struct group *g;
900 
901 #warning getpwnam_r() not available, please report this to the dbus maintainers with details of your OS
902 
903  g = getgrnam (group_c_str);
904 
905  if (g != NULL)
906  {
907  return fill_user_info_from_group (g, info, error);
908  }
909  else
910  {
911  dbus_set_error (error, _dbus_error_from_errno (errno),
912  "Group %s unknown or failed to look it up\n",
913  group_c_str ? group_c_str : "???");
914  return FALSE;
915  }
916  }
917 #endif /* ! HAVE_GETPWNAM_R */
918 }
919 
931  const DBusString *groupname,
932  DBusError *error)
933 {
934  return fill_group_info (info, DBUS_GID_UNSET,
935  groupname, error);
936 
937 }
938 
950  dbus_gid_t gid,
951  DBusError *error)
952 {
953  return fill_group_info (info, gid, NULL, error);
954 }
955 
966  dbus_uid_t *uid_p)
967 {
968  return _dbus_get_user_id (username, uid_p);
969 
970 }
971 
982  dbus_gid_t *gid_p)
983 {
984  return _dbus_get_group_id (groupname, gid_p);
985 }
986 
999  dbus_gid_t **group_ids,
1000  int *n_group_ids)
1001 {
1002  return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1003 }
1004 
1016  DBusError *error)
1017 {
1018  return _dbus_is_console_user (uid, error);
1019 
1020 }
1021 
1031 {
1032  return uid == _dbus_geteuid ();
1033 }
1034 
1043 _dbus_windows_user_is_process_owner (const char *windows_sid)
1044 {
1045  return FALSE;
1046 }
1047  /* End of DBusInternalsUtils functions */
1049 
1063  DBusString *dirname)
1064 {
1065  int sep;
1066 
1067  _dbus_assert (filename != dirname);
1068  _dbus_assert (filename != NULL);
1069  _dbus_assert (dirname != NULL);
1070 
1071  /* Ignore any separators on the end */
1072  sep = _dbus_string_get_length (filename);
1073  if (sep == 0)
1074  return _dbus_string_append (dirname, "."); /* empty string passed in */
1075 
1076  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1077  --sep;
1078 
1079  _dbus_assert (sep >= 0);
1080 
1081  if (sep == 0)
1082  return _dbus_string_append (dirname, "/");
1083 
1084  /* Now find the previous separator */
1085  _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1086  if (sep < 0)
1087  return _dbus_string_append (dirname, ".");
1088 
1089  /* skip multiple separators */
1090  while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1091  --sep;
1092 
1093  _dbus_assert (sep >= 0);
1094 
1095  if (sep == 0 &&
1096  _dbus_string_get_byte (filename, 0) == '/')
1097  return _dbus_string_append (dirname, "/");
1098  else
1099  return _dbus_string_copy_len (filename, 0, sep - 0,
1100  dirname, _dbus_string_get_length (dirname));
1101 } /* DBusString stuff */
1103 
1104 static void
1105 string_squash_nonprintable (DBusString *str)
1106 {
1107  unsigned char *buf;
1108  int i, len;
1109 
1110  buf = _dbus_string_get_udata (str);
1111  len = _dbus_string_get_length (str);
1112 
1113  /* /proc/$pid/cmdline is a sequence of \0-terminated words, but we
1114  * want a sequence of space-separated words, with no extra trailing
1115  * space:
1116  * "/bin/sleep" "\0" "60" "\0"
1117  * -> "/bin/sleep" "\0" "60"
1118  * -> "/bin/sleep" " " "60"
1119  *
1120  * so chop off the trailing NUL before cleaning up unprintable
1121  * characters. */
1122  if (len > 0 && buf[len - 1] == '\0')
1123  {
1124  _dbus_string_shorten (str, 1);
1125  len--;
1126  }
1127 
1128  for (i = 0; i < len; i++)
1129  {
1130  unsigned char c = (unsigned char) buf[i];
1131  if (c == '\0')
1132  buf[i] = ' ';
1133  else if (c < 0x20 || c > 127)
1134  buf[i] = '?';
1135  }
1136 }
1137 
1152 dbus_bool_t
1153 _dbus_command_for_pid (unsigned long pid,
1154  DBusString *str,
1155  int max_len,
1156  DBusError *error)
1157 {
1158  /* This is all Linux-specific for now */
1159  DBusString path;
1160  DBusString cmdline;
1161  int fd;
1162 
1163  if (!_dbus_string_init (&path))
1164  {
1165  _DBUS_SET_OOM (error);
1166  return FALSE;
1167  }
1168 
1169  if (!_dbus_string_init (&cmdline))
1170  {
1171  _DBUS_SET_OOM (error);
1172  _dbus_string_free (&path);
1173  return FALSE;
1174  }
1175 
1176  if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1177  goto oom;
1178 
1179  fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1180  if (fd < 0)
1181  {
1182  dbus_set_error (error,
1183  _dbus_error_from_errno (errno),
1184  "Failed to open \"%s\": %s",
1185  _dbus_string_get_const_data (&path),
1186  _dbus_strerror (errno));
1187  goto fail;
1188  }
1189 
1190  if (!_dbus_read (fd, &cmdline, max_len))
1191  {
1192  dbus_set_error (error,
1193  _dbus_error_from_errno (errno),
1194  "Failed to read from \"%s\": %s",
1195  _dbus_string_get_const_data (&path),
1196  _dbus_strerror (errno));
1197  _dbus_close (fd, NULL);
1198  goto fail;
1199  }
1200 
1201  if (!_dbus_close (fd, error))
1202  goto fail;
1203 
1204  string_squash_nonprintable (&cmdline);
1205 
1206  if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1207  goto oom;
1208 
1209  _dbus_string_free (&cmdline);
1210  _dbus_string_free (&path);
1211  return TRUE;
1212 oom:
1213  _DBUS_SET_OOM (error);
1214 fail:
1215  _dbus_string_free (&cmdline);
1216  _dbus_string_free (&path);
1217  return FALSE;
1218 }
1219 
1230 {
1231  return TRUE;
1232 }
1233 
1234 static dbus_bool_t
1235 ensure_owned_directory (const char *label,
1236  const DBusString *string,
1237  dbus_bool_t create,
1238  DBusError *error)
1239 {
1240  const char *dir = _dbus_string_get_const_data (string);
1241  struct stat buf;
1242 
1243  if (create && !_dbus_ensure_directory (string, error))
1244  return FALSE;
1245 
1246  /*
1247  * The stat()-based checks in this function are to protect against
1248  * mistakes, not malice. We are working in a directory that is meant
1249  * to be trusted; but if a user has used `su` or similar to escalate
1250  * their privileges without correctly clearing the environment, the
1251  * XDG_RUNTIME_DIR in the environment might still be the user's
1252  * and not root's. We don't want to write root-owned files into that
1253  * directory, so just warn and don't provide support for transient
1254  * services in that case.
1255  *
1256  * In particular, we use stat() and not lstat() so that if we later
1257  * decide to use a different directory name for transient services,
1258  * we can drop in a compatibility symlink without breaking older
1259  * libdbus.
1260  */
1261 
1262  if (stat (dir, &buf) != 0)
1263  {
1264  int saved_errno = errno;
1265 
1266  dbus_set_error (error, _dbus_error_from_errno (saved_errno),
1267  "%s \"%s\" not available: %s", label, dir,
1268  _dbus_strerror (saved_errno));
1269  return FALSE;
1270  }
1271 
1272  if (!S_ISDIR (buf.st_mode))
1273  {
1274  dbus_set_error (error, DBUS_ERROR_FAILED, "%s \"%s\" is not a directory",
1275  label, dir);
1276  return FALSE;
1277  }
1278 
1279  if (buf.st_uid != geteuid ())
1280  {
1282  "%s \"%s\" is owned by uid %ld, not our uid %ld",
1283  label, dir, (long) buf.st_uid, (long) geteuid ());
1284  return FALSE;
1285  }
1286 
1287  /* This is just because we have the stat() results already, so we might
1288  * as well check opportunistically. */
1289  if ((S_IWOTH | S_IWGRP) & buf.st_mode)
1290  {
1292  "%s \"%s\" can be written by others (mode 0%o)",
1293  label, dir, buf.st_mode);
1294  return FALSE;
1295  }
1296 
1297  return TRUE;
1298 }
1299 
1300 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1301 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1302 
1312  DBusError *error)
1313 {
1314  const char *xdg_runtime_dir;
1315  DBusString services;
1316  DBusString dbus1;
1317  DBusString xrd;
1318  dbus_bool_t ret = FALSE;
1319  char *data = NULL;
1320 
1321  if (!_dbus_string_init (&dbus1))
1322  {
1323  _DBUS_SET_OOM (error);
1324  return FALSE;
1325  }
1326 
1327  if (!_dbus_string_init (&services))
1328  {
1329  _dbus_string_free (&dbus1);
1330  _DBUS_SET_OOM (error);
1331  return FALSE;
1332  }
1333 
1334  if (!_dbus_string_init (&xrd))
1335  {
1336  _dbus_string_free (&dbus1);
1337  _dbus_string_free (&services);
1338  _DBUS_SET_OOM (error);
1339  return FALSE;
1340  }
1341 
1342  xdg_runtime_dir = _dbus_getenv ("XDG_RUNTIME_DIR");
1343 
1344  /* Not an error, we just can't have transient session services */
1345  if (xdg_runtime_dir == NULL)
1346  {
1347  _dbus_verbose ("XDG_RUNTIME_DIR is unset: transient session services "
1348  "not available here\n");
1349  ret = TRUE;
1350  goto out;
1351  }
1352 
1353  if (!_dbus_string_append (&xrd, xdg_runtime_dir) ||
1354  !_dbus_string_append_printf (&dbus1, "%s/dbus-1",
1355  xdg_runtime_dir) ||
1356  !_dbus_string_append_printf (&services, "%s/dbus-1/services",
1357  xdg_runtime_dir))
1358  {
1359  _DBUS_SET_OOM (error);
1360  goto out;
1361  }
1362 
1363  if (!ensure_owned_directory ("XDG_RUNTIME_DIR", &xrd, FALSE, error) ||
1364  !ensure_owned_directory ("XDG_RUNTIME_DIR subdirectory", &dbus1, TRUE,
1365  error) ||
1366  !ensure_owned_directory ("XDG_RUNTIME_DIR subdirectory", &services,
1367  TRUE, error))
1368  goto out;
1369 
1370  if (!_dbus_string_steal_data (&services, &data) ||
1371  !_dbus_list_append (dirs, data))
1372  {
1373  _DBUS_SET_OOM (error);
1374  goto out;
1375  }
1376 
1377  _dbus_verbose ("Transient service directory is %s\n", data);
1378  /* Ownership was transferred to @dirs */
1379  data = NULL;
1380  ret = TRUE;
1381 
1382 out:
1383  _dbus_string_free (&dbus1);
1384  _dbus_string_free (&services);
1385  _dbus_string_free (&xrd);
1386  dbus_free (data);
1387  return ret;
1388 }
1389 
1409 {
1410  const char *xdg_data_home;
1411  const char *xdg_data_dirs;
1412  DBusString servicedir_path;
1413 
1414  if (!_dbus_string_init (&servicedir_path))
1415  return FALSE;
1416 
1417  xdg_data_home = _dbus_getenv ("XDG_DATA_HOME");
1418  xdg_data_dirs = _dbus_getenv ("XDG_DATA_DIRS");
1419 
1420  if (xdg_data_home != NULL)
1421  {
1422  if (!_dbus_string_append (&servicedir_path, xdg_data_home))
1423  goto oom;
1424  }
1425  else
1426  {
1427  const DBusString *homedir;
1428  DBusString local_share;
1429 
1430  if (!_dbus_homedir_from_current_process (&homedir))
1431  goto oom;
1432 
1433  if (!_dbus_string_append (&servicedir_path, _dbus_string_get_const_data (homedir)))
1434  goto oom;
1435 
1436  _dbus_string_init_const (&local_share, "/.local/share");
1437  if (!_dbus_concat_dir_and_file (&servicedir_path, &local_share))
1438  goto oom;
1439  }
1440 
1441  if (!_dbus_string_append (&servicedir_path, ":"))
1442  goto oom;
1443 
1444  if (xdg_data_dirs != NULL)
1445  {
1446  if (!_dbus_string_append (&servicedir_path, xdg_data_dirs))
1447  goto oom;
1448 
1449  if (!_dbus_string_append (&servicedir_path, ":"))
1450  goto oom;
1451  }
1452  else
1453  {
1454  if (!_dbus_string_append (&servicedir_path, "/usr/local/share:/usr/share:"))
1455  goto oom;
1456  }
1457 
1458  /*
1459  * add configured datadir to defaults
1460  * this may be the same as an xdg dir
1461  * however the config parser should take
1462  * care of duplicates
1463  */
1464  if (!_dbus_string_append (&servicedir_path, DBUS_DATADIR))
1465  goto oom;
1466 
1467  if (!_dbus_split_paths_and_append (&servicedir_path,
1468  DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1469  dirs))
1470  goto oom;
1471 
1472  _dbus_string_free (&servicedir_path);
1473  return TRUE;
1474 
1475  oom:
1476  _dbus_string_free (&servicedir_path);
1477  return FALSE;
1478 }
1479 
1480 
1501 {
1502  /*
1503  * DBUS_DATADIR may be the same as one of the standard directories. However,
1504  * the config parser should take care of the duplicates.
1505  *
1506  * Also, append /lib as counterpart of /usr/share on the root
1507  * directory (the root directory does not know /share), in order to
1508  * facilitate early boot system bus activation where /usr might not
1509  * be available.
1510  */
1511  static const char standard_search_path[] =
1512  "/usr/local/share:"
1513  "/usr/share:"
1514  DBUS_DATADIR ":"
1515  "/lib";
1516  DBusString servicedir_path;
1517 
1518  _dbus_string_init_const (&servicedir_path, standard_search_path);
1519 
1520  return _dbus_split_paths_and_append (&servicedir_path,
1521  DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,
1522  dirs);
1523 }
1524 
1535 {
1536  _dbus_assert (_dbus_string_get_length (str) == 0);
1537 
1538  return _dbus_string_append (str, DBUS_SYSTEM_CONFIG_FILE);
1539 }
1540 
1549 {
1550  _dbus_assert (_dbus_string_get_length (str) == 0);
1551 
1552  return _dbus_string_append (str, DBUS_SESSION_CONFIG_FILE);
1553 }
1554 
1559 void
1561 {
1562 #ifdef HAVE_SYSTEMD
1563  sd_notify (0, "READY=1");
1564 #endif
1565 }
1566 
1571 void
1573 {
1574 #ifdef HAVE_SYSTEMD
1575  sd_notify (0, "RELOADING=1");
1576 #endif
1577 }
1578 
1583 void
1585 {
1586 #ifdef HAVE_SYSTEMD
1587  /* For systemd, this is the same code */
1589 #endif
1590 }
1591 
1596 void
1598 {
1599 #ifdef HAVE_SYSTEMD
1600  sd_notify (0, "STOPPING=1");
1601 #endif
1602 }
1603 
1618 _dbus_reset_oom_score_adj (const char **error_str_p)
1619 {
1620 #ifdef __linux__
1621  int fd = -1;
1622  dbus_bool_t ret = FALSE;
1623  int saved_errno = 0;
1624  const char *error_str = NULL;
1625 
1626 #ifdef O_CLOEXEC
1627  fd = open ("/proc/self/oom_score_adj", O_RDONLY | O_CLOEXEC);
1628 #endif
1629 
1630  if (fd < 0)
1631  {
1632  fd = open ("/proc/self/oom_score_adj", O_RDONLY);
1633  if (fd >= 0)
1635  }
1636 
1637  if (fd >= 0)
1638  {
1639  ssize_t read_result = -1;
1640  /* It doesn't actually matter whether we read the whole file,
1641  * as long as we get the presence or absence of the minus sign */
1642  char first_char = '\0';
1643 
1644  read_result = read (fd, &first_char, 1);
1645 
1646  if (read_result < 0)
1647  {
1648  /* This probably can't actually happen in practice: if we can
1649  * open it, then we can hopefully read from it */
1650  ret = FALSE;
1651  error_str = "failed to read from /proc/self/oom_score_adj";
1652  saved_errno = errno;
1653  goto out;
1654  }
1655 
1656  /* If we are running with protection from the OOM killer
1657  * (typical for the system dbus-daemon under systemd), then
1658  * oom_score_adj will be negative. Drop that protection,
1659  * returning to oom_score_adj = 0.
1660  *
1661  * Conversely, if we are running with increased susceptibility
1662  * to the OOM killer (as user sessions typically do in
1663  * systemd >= 250), oom_score_adj will be strictly positive,
1664  * and we are not allowed to decrease it to 0 without privileges.
1665  *
1666  * If it's exactly 0 (typical for non-systemd systems, and
1667  * user processes on older systemd) then there's no need to
1668  * alter it.
1669  *
1670  * We shouldn't get an empty result, but if we do, assume it
1671  * means zero and don't try to change it. */
1672  if (read_result == 0 || first_char != '-')
1673  {
1674  /* Nothing needs to be done: the OOM score adjustment is
1675  * non-negative */
1676  ret = TRUE;
1677  goto out;
1678  }
1679 
1680  close (fd);
1681 #ifdef O_CLOEXEC
1682  fd = open ("/proc/self/oom_score_adj", O_WRONLY | O_CLOEXEC);
1683 
1684  if (fd < 0)
1685 #endif
1686  {
1687  fd = open ("/proc/self/oom_score_adj", O_WRONLY);
1688  if (fd >= 0)
1690  }
1691 
1692  if (fd < 0)
1693  {
1694  ret = FALSE;
1695  error_str = "open(/proc/self/oom_score_adj) for writing";
1696  saved_errno = errno;
1697  goto out;
1698  }
1699 
1700  if (pwrite (fd, "0", sizeof (char), 0) < 0)
1701  {
1702  ret = FALSE;
1703  error_str = "writing oom_score_adj error";
1704  saved_errno = errno;
1705  goto out;
1706  }
1707 
1708  /* Success */
1709  ret = TRUE;
1710  }
1711  else if (errno == ENOENT)
1712  {
1713  /* If /proc/self/oom_score_adj doesn't exist, assume the kernel
1714  * doesn't support this feature and ignore it. */
1715  ret = TRUE;
1716  }
1717  else
1718  {
1719  ret = FALSE;
1720  error_str = "open(/proc/self/oom_score_adj) for reading";
1721  saved_errno = errno;
1722  goto out;
1723  }
1724 
1725 out:
1726  if (fd >= 0)
1727  _dbus_close (fd, NULL);
1728 
1729  if (error_str_p != NULL)
1730  *error_str_p = error_str;
1731 
1732  errno = saved_errno;
1733  return ret;
1734 #else
1735  /* nothing to do on this platform */
1736  return TRUE;
1737 #endif
1738 }
_dbus_daemon_report_ready
void _dbus_daemon_report_ready(void)
Report to a service manager that the daemon calling this function is ready for use.
Definition: dbus-sysdeps-util-unix.c:1560
_dbus_get_session_config_file
dbus_bool_t _dbus_get_session_config_file(DBusString *str)
Get the absolute path of the session.conf file.
Definition: dbus-sysdeps-util-unix.c:1548
_dbus_ensure_standard_fds
dbus_bool_t _dbus_ensure_standard_fds(DBusEnsureStandardFdsFlags flags, const char **error_str_p)
Ensure that the standard file descriptors stdin, stdout and stderr are open, by opening /dev/null if ...
Definition: dbus-sysdeps-unix.c:155
_dbus_concat_dir_and_file
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
Definition: dbus-sysdeps-unix.c:3336
_dbus_set_up_transient_session_servicedirs
dbus_bool_t _dbus_set_up_transient_session_servicedirs(DBusList **dirs, DBusError *error)
Returns the standard directories for a session bus to look for transient service activation files.
Definition: dbus-sysdeps-util-unix.c:1311
_dbus_get_system_config_file
dbus_bool_t _dbus_get_system_config_file(DBusString *str)
Get the absolute path of the system.conf file (there is no system bus on Windows so this can just ret...
Definition: dbus-sysdeps-util-unix.c:1534
_dbus_group_info_fill_gid
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.
Definition: dbus-sysdeps-util-unix.c:949
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
DBusStat::uid
dbus_uid_t uid
User owning file.
Definition: dbus-sysdeps.h:553
_dbus_get_standard_system_servicedirs
dbus_bool_t _dbus_get_standard_system_servicedirs(DBusList **dirs)
Returns the standard directories for a system bus to look for service activation files.
Definition: dbus-sysdeps-util-unix.c:1500
_dbus_stat
dbus_bool_t _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error)
stat() wrapper.
Definition: dbus-sysdeps-util-unix.c:639
_dbus_string_append_int
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:363
_dbus_error_from_errno
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:599
_dbus_replace_install_prefix
dbus_bool_t _dbus_replace_install_prefix(DBusString *path)
Replace the DBUS_PREFIX in the given path, in-place, by the current D-Bus installation directory.
Definition: dbus-sysdeps-util-unix.c:1229
_dbus_directory_close
void _dbus_directory_close(DBusDirIter *iter)
Closes a directory iteration.
Definition: dbus-sysdeps-util-unix.c:783
_dbus_string_find_byte_backward
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.
Definition: dbus-string-util.c:96
DBusStat
Portable struct with stat() results.
Definition: dbus-sysdeps.h:549
_dbus_daemon_report_reloading
void _dbus_daemon_report_reloading(void)
Report to a service manager that the daemon calling this function is reloading configuration.
Definition: dbus-sysdeps-util-unix.c:1572
_dbus_get_user_id_and_primary_group
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.
Definition: dbus-userdb-util.c:217
DBusGroupInfo::gid
dbus_gid_t gid
GID.
Definition: dbus-sysdeps-unix.h:123
_dbus_reset_oom_score_adj
dbus_bool_t _dbus_reset_oom_score_adj(const char **error_str_p)
If the current process has been protected from the Linux OOM killer (the oom_score_adj process parame...
Definition: dbus-sysdeps-util-unix.c:1618
_dbus_string_copy
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:1343
DBusPipe
Definition: dbus-pipe.h:41
_dbus_group_info_fill
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.
Definition: dbus-sysdeps-util-unix.c:930
DBusStat::ctime
unsigned long ctime
Creation time.
Definition: dbus-sysdeps.h:558
DBusGroupInfo
Information about a UNIX group.
Definition: dbus-sysdeps-unix.h:120
_dbus_path_is_absolute
dbus_bool_t _dbus_path_is_absolute(const DBusString *filename)
Checks whether the filename is an absolute path.
Definition: dbus-sysdeps-util-unix.c:622
dbus_gid_t
unsigned long dbus_gid_t
A group ID.
Definition: dbus-sysdeps.h:139
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
_dbus_groups_from_uid
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.
Definition: dbus-userdb-util.c:379
_dbus_unix_user_is_process_owner
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.
Definition: dbus-sysdeps-util-unix.c:1030
_dbus_string_get_dirname
dbus_bool_t _dbus_string_get_dirname(const DBusString *filename, DBusString *dirname)
Get the directory name from a complete filename.
Definition: dbus-sysdeps-util-unix.c:1062
TRUE
#define TRUE
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:271
_dbus_user_at_console
dbus_bool_t _dbus_user_at_console(const char *username, DBusError *error)
Checks if user is at the console.
Definition: dbus-sysdeps-util-unix.c:575
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition: dbus-protocol.h:359
_dbus_change_to_daemon_user
dbus_bool_t _dbus_change_to_daemon_user(const char *user, DBusError *error)
Changes the user and group the bus is running as.
Definition: dbus-sysdeps-util-unix.c:332
DBusStat::atime
unsigned long atime
Access time.
Definition: dbus-sysdeps.h:556
_dbus_read
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.
Definition: dbus-sysdeps-unix.c:731
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
_dbus_verify_daemon_user
dbus_bool_t _dbus_verify_daemon_user(const char *user)
Verify that after the fork we can successfully change to this user.
Definition: dbus-sysdeps-util-unix.c:312
dbus_malloc
void * dbus_malloc(size_t bytes)
Allocates the given number of bytes, as with standard malloc().
Definition: dbus-memory.c:452
DBusDirIter::d
DIR * d
The DIR* from opendir()
Definition: dbus-sysdeps-util-unix.c:675
DBusString
Definition: dbus-string.h:42
DBusStat::mode
unsigned long mode
File mode.
Definition: dbus-sysdeps.h:551
_dbus_string_append_printf
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:1145
_dbus_get_group_id
dbus_bool_t _dbus_get_group_id(const DBusString *groupname, dbus_gid_t *gid)
Gets group ID given groupname.
Definition: dbus-userdb-util.c:176
_dbus_unix_user_is_at_console
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.
Definition: dbus-sysdeps-util-unix.c:1015
dbus_pid_t
unsigned long dbus_pid_t
A process ID.
Definition: dbus-sysdeps.h:135
_dbus_split_paths_and_append
dbus_bool_t _dbus_split_paths_and_append(DBusString *dirs, const char *suffix, DBusList **dir_list)
Split paths into a list of char strings.
Definition: dbus-sysdeps.c:236
dbus_uid_t
unsigned long dbus_uid_t
A user ID.
Definition: dbus-sysdeps.h:137
_dbus_daemon_report_reloaded
void _dbus_daemon_report_reloaded(void)
Report to a service manager that the daemon calling this function is reloading configuration.
Definition: dbus-sysdeps-util-unix.c:1584
FALSE
#define FALSE
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:361
DBusStat::nlink
unsigned long nlink
Number of hard links.
Definition: dbus-sysdeps.h:552
_dbus_parse_unix_group_from_config
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.
Definition: dbus-sysdeps-util-unix.c:981
_dbus_is_console_user
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.
Definition: dbus-userdb-util.c:65
_dbus_directory_get_next_file
dbus_bool_t _dbus_directory_get_next_file(DBusDirIter *iter, DBusString *filename, DBusError *error)
Get next file in the directory.
Definition: dbus-sysdeps-util-unix.c:735
_dbus_fd_set_close_on_exec
void _dbus_fd_set_close_on_exec(int fd)
Sets the file descriptor to be close on exec.
Definition: dbus-sysdeps-unix.c:3522
_dbus_file_exists
dbus_bool_t _dbus_file_exists(const char *file)
Checks if a file exists.
Definition: dbus-sysdeps-util-unix.c:563
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Definition: dbus-internals.h:164
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:833
DBusStat::mtime
unsigned long mtime
Modify time.
Definition: dbus-sysdeps.h:557
DBusStat::gid
dbus_gid_t gid
Group owning file.
Definition: dbus-sysdeps.h:554
_dbus_command_for_pid
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.
Definition: dbus-sysdeps-util-unix.c:1153
_dbus_assert
#define _dbus_assert(condition)
Definition: dbus-internals.h:153
_dbus_close
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
Definition: dbus-sysdeps-unix.c:3566
_dbus_get_standard_session_servicedirs
dbus_bool_t _dbus_get_standard_session_servicedirs(DBusList **dirs)
Returns the standard directories for a session bus to look for service activation files.
Definition: dbus-sysdeps-util-unix.c:1408
_dbus_warn
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
Definition: dbus-internals.c:238
_dbus_geteuid
dbus_uid_t _dbus_geteuid(void)
Gets our effective UID.
Definition: dbus-sysdeps-unix.c:3001
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition: dbus-internals.c:589
_dbus_set_signal_handler
void _dbus_set_signal_handler(int sig, DBusSignalHandler handler)
Installs a UNIX signal handler.
Definition: dbus-sysdeps-util-unix.c:544
_dbus_unix_groups_from_uid
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.
Definition: dbus-sysdeps-util-unix.c:998
_dbus_getenv
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:195
DBusError
Object representing an exception.
Definition: dbus-errors.h:48
DBusList
Definition: dbus-list.h:34
_dbus_directory_open
DBusDirIter * _dbus_directory_open(const DBusString *filename, DBusError *error)
Open a directory to iterate over.
Definition: dbus-sysdeps-util-unix.c:687
_dbus_ensure_directory
dbus_bool_t _dbus_ensure_directory(const DBusString *filename, DBusError *error)
Creates a directory; succeeds if the directory is created or already existed.
Definition: dbus-sysdeps-unix.c:3273
_dbus_become_daemon
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.
Definition: dbus-sysdeps-util-unix.c:84
dbus_set_error
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_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:672
_dbus_parse_unix_user_from_config
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.
Definition: dbus-sysdeps-util-unix.c:965
DBusError::message
const char * message
public error message field
Definition: dbus-errors.h:51
DBusDirIter
Internals of directory iterator.
Definition: dbus-sysdeps-util-unix.c:673
DBUS_GID_UNSET
#define DBUS_GID_UNSET
an invalid GID used to represent an uninitialized dbus_gid_t field
Definition: dbus-sysdeps.h:146
_dbus_get_user_id
dbus_bool_t _dbus_get_user_id(const DBusString *username, dbus_uid_t *uid)
Gets user ID given username.
Definition: dbus-userdb-util.c:162
_dbus_write_pid_to_file_and_pipe
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).
Definition: dbus-sysdeps-util-unix.c:238
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition: dbus-string.c:197
_dbus_string_copy_len
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:1435
dbus_new0
#define dbus_new0(type, count)
Definition: dbus-memory.h:58
_dbus_daemon_report_stopping
void _dbus_daemon_report_stopping(void)
Report to a service manager that the daemon calling this function is shutting down.
Definition: dbus-sysdeps-util-unix.c:1597
DBUS_ERROR_NOT_SUPPORTED
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
Definition: dbus-protocol.h:373
DBusSignalHandler
void(* DBusSignalHandler)(int sig)
A UNIX signal handler.
Definition: dbus-sysdeps-unix.h:172
_dbus_homedir_from_current_process
dbus_bool_t _dbus_homedir_from_current_process(const DBusString **homedir)
Gets homedir of user owning current process.
Definition: dbus-userdb.c:440
_dbus_string_shorten
void _dbus_string_shorten(DBusString *str, int length_to_remove)
Makes a string shorter by the given number of bytes.
Definition: dbus-string.c:811
_dbus_string_append
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:966
DBusGroupInfo::groupname
char * groupname
Group name.
Definition: dbus-sysdeps-unix.h:124
_dbus_windows_user_is_process_owner
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.
Definition: dbus-sysdeps-util-unix.c:1043
DBusStat::size
unsigned long size
Size of file.
Definition: dbus-sysdeps.h:555
dbus_bool_t
dbus_uint32_t dbus_bool_t
Definition: dbus-types.h:35
NULL
#define NULL