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"
36 #include <sys/types.h>
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
49 #include <sys/socket.h>
57 #ifdef HAVE_SYS_SYSLIMITS_H
58 #include <sys/syslimits.h>
61 #include "sd-daemon.h"
92 _dbus_verbose (
"Becoming a daemon...\n");
94 _dbus_verbose (
"chdir to /\n");
98 "Could not chdir() to root directory");
102 _dbus_verbose (
"forking...\n");
103 switch ((child_pid = fork ()))
106 _dbus_verbose (
"fork failed\n");
108 "Failed to fork daemon: %s", _dbus_strerror (errno));
113 _dbus_verbose (
"in child, closing std file descriptors\n");
120 dev_null_fd = open (
"/dev/null", O_RDWR);
121 if (dev_null_fd >= 0)
123 dup2 (dev_null_fd, 0);
124 dup2 (dev_null_fd, 1);
127 if (s ==
NULL || *s ==
'\0')
128 dup2 (dev_null_fd, 2);
130 _dbus_verbose (
"keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
137 _dbus_verbose (
"setting umask\n");
141 _dbus_verbose (
"calling setsid()\n");
151 _dbus_verbose (
"pid file or pipe write failed: %s\n",
153 kill (child_pid, SIGTERM);
157 _dbus_verbose (
"parent exiting\n");
175 _dbus_write_pid_file (
const DBusString *filename,
179 const char *cfilename;
183 cfilename = _dbus_string_get_const_data (filename);
185 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
190 "Failed to open \"%s\": %s", cfilename,
191 _dbus_strerror (errno));
195 if ((f = fdopen (fd,
"w")) ==
NULL)
198 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
203 if (fprintf (f,
"%lu\n", pid) < 0)
206 "Failed to write to \"%s\": %s", cfilename,
207 _dbus_strerror (errno));
213 if (fclose (f) == EOF)
216 "Failed to close \"%s\": %s", cfilename,
217 _dbus_strerror (errno));
243 _dbus_verbose (
"writing pid file %s\n", _dbus_string_get_const_data (pidfile));
244 if (!_dbus_write_pid_file (pidfile,
248 _dbus_verbose (
"pid file write failed\n");
249 _DBUS_ASSERT_ERROR_IS_SET(error);
255 _dbus_verbose (
"No pid file requested\n");
258 if (print_pid_pipe !=
NULL && _dbus_pipe_is_valid (print_pid_pipe))
263 _dbus_verbose (
"writing our pid to pipe %d\n",
268 _DBUS_SET_OOM (error);
276 _DBUS_SET_OOM (error);
280 bytes = _dbus_string_get_length (&pid);
281 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
287 "Printing message bus PID: did not write enough bytes\n");
297 _dbus_verbose (
"No pid pipe to write to\n");
321 #ifndef HAVE_LIBAUDIT
342 "User '%s' does not appear to exist?",
354 if (setgroups (0,
NULL) < 0)
355 _dbus_warn (
"Failed to drop supplementary groups: %s\n",
356 _dbus_strerror (errno));
361 if (setgid (gid) < 0)
364 "Failed to set GID to %lu: %s", gid,
365 _dbus_strerror (errno));
369 if (setuid (uid) < 0)
372 "Failed to set UID to %lu: %s", uid,
373 _dbus_strerror (errno));
395 #ifdef HAVE_SETRLIMIT
397 struct rlimit target_lim;
408 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
411 if (lim.rlim_cur >= limit)
417 target_lim.rlim_cur = target_lim.rlim_max = limit;
426 setrlimit (RLIMIT_NOFILE, &target_lim);
434 int logopts = LOG_PID;
436 #if HAVE_DECL_LOG_PERROR
438 if (!is_daemon || sd_booted () <= 0)
440 logopts |= LOG_PERROR;
443 openlog (
"dbus", logopts, LOG_DAEMON);
458 va_start (args, msg);
483 case DBUS_SYSTEM_LOG_INFO:
484 flags = LOG_DAEMON | LOG_NOTICE;
486 case DBUS_SYSTEM_LOG_SECURITY:
487 flags = LOG_AUTH | LOG_NOTICE;
489 case DBUS_SYSTEM_LOG_FATAL:
490 flags = LOG_DAEMON|LOG_CRIT;
496 DBUS_VA_COPY (tmp, args);
497 vsyslog (flags, msg, tmp);
501 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
504 DBUS_VA_COPY (tmp, args);
506 vfprintf (stderr, msg, tmp);
507 fputc (
'\n', stderr);
512 if (severity == DBUS_SYSTEM_LOG_FATAL)
525 struct sigaction act;
528 sigemptyset (&empty_mask);
529 act.sa_handler = handler;
530 act.sa_mask = empty_mask;
532 sigaction (sig, &act,
NULL);
543 return (access (file, F_OK) == 0);
563 _DBUS_SET_OOM (error);
569 _DBUS_SET_OOM (error);
577 _DBUS_SET_OOM (error);
599 if (_dbus_string_get_length (filename) > 0)
600 return _dbus_string_get_byte (filename, 0) ==
'/';
618 const char *filename_c;
621 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
623 filename_c = _dbus_string_get_const_data (filename);
625 if (stat (filename_c, &sb) < 0)
628 "%s", _dbus_strerror (errno));
632 statbuf->
mode = sb.st_mode;
633 statbuf->
nlink = sb.st_nlink;
634 statbuf->
uid = sb.st_uid;
635 statbuf->
gid = sb.st_gid;
636 statbuf->
size = sb.st_size;
637 statbuf->
atime = sb.st_atime;
638 statbuf->
mtime = sb.st_mtime;
639 statbuf->
ctime = sb.st_ctime;
667 const char *filename_c;
669 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
671 filename_c = _dbus_string_get_const_data (filename);
673 d = opendir (filename_c);
677 "Failed to read directory \"%s\": %s",
679 _dbus_strerror (errno));
687 "Could not allocate memory for directory iterator");
717 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
721 ent = readdir (iter->
d);
730 "%s", _dbus_strerror (err));
734 else if (ent->d_name[0] ==
'.' &&
735 (ent->d_name[1] ==
'\0' ||
736 (ent->d_name[1] ==
'.' && ent->d_name[2] ==
'\0')))
744 "No memory to read directory entry");
765 fill_user_info_from_group (
struct group *g,
771 info->
gid = g->gr_gid;
791 const char *group_c_str;
797 group_c_str = _dbus_string_get_const_data (groupname);
806 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
816 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
822 if ((
long) buflen <= 0)
836 #ifdef HAVE_POSIX_GETPWNAM_R
838 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
841 result = getgrgid_r (gid, &g_str, buf, buflen,
844 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
850 if (result == ERANGE && buflen < 512 * 1024)
861 if (result == 0 && g == &g_str)
863 b = fill_user_info_from_group (g, info, error);
870 "Group %s unknown or failed to look it up\n",
871 group_c_str ? group_c_str :
"???");
881 g = getgrnam (group_c_str);
885 return fill_user_info_from_group (g, info, error);
890 "Group %s unknown or failed to look it up\n",
891 group_c_str ? group_c_str :
"???");
931 return fill_group_info (info, gid,
NULL, error);
1050 sep = _dbus_string_get_length (filename);
1054 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) ==
'/')
1068 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) ==
'/')
1074 _dbus_string_get_byte (filename, 0) ==
'/')
1078 dirname, _dbus_string_get_length (dirname));
1088 buf = _dbus_string_get_data (str);
1089 len = _dbus_string_get_length (str);
1091 for (i = 0; i < len; i++)
1093 unsigned char c = (
unsigned char) buf[i];
1096 else if (c < 0x20 || c > 127)
1128 _DBUS_SET_OOM (error);
1134 _DBUS_SET_OOM (error);
1142 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1147 "Failed to open \"%s\": %s",
1148 _dbus_string_get_const_data (&path),
1149 _dbus_strerror (errno));
1157 "Failed to read from \"%s\": %s",
1158 _dbus_string_get_const_data (&path),
1159 _dbus_strerror (errno));
1167 string_squash_nonprintable (&cmdline);
1176 _DBUS_SET_OOM (error);
1191 _dbus_replace_install_prefix (
const char *configure_time_path)
1193 return configure_time_path;
1196 #define DBUS_UNIX_STANDARD_SESSION_SERVICEDIR "/dbus-1/services"
1197 #define DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR "/dbus-1/system-services"
1219 const char *xdg_data_home;
1220 const char *xdg_data_dirs;
1229 if (xdg_data_home !=
NULL)
1253 if (xdg_data_dirs !=
NULL)
1277 DBUS_UNIX_STANDARD_SESSION_SERVICEDIR,
1320 static const char standard_search_path[] =
1330 DBUS_UNIX_STANDARD_SYSTEM_SERVICEDIR,