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;
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));
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);
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);
460 va_start (args, msg);
484 case DBUS_SYSTEM_LOG_INFO:
485 flags = LOG_DAEMON | LOG_NOTICE;
487 case DBUS_SYSTEM_LOG_SECURITY:
488 flags = LOG_AUTH | LOG_NOTICE;
490 case DBUS_SYSTEM_LOG_FATAL:
491 flags = LOG_DAEMON|LOG_CRIT;
497 vsyslog (flags, msg, args);
500 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
505 DBUS_VA_COPY (tmp, args);
507 vfprintf (stderr, msg, tmp);
508 fputc (
'\n', stderr);
513 if (severity == DBUS_SYSTEM_LOG_FATAL)
526 struct sigaction act;
529 sigemptyset (&empty_mask);
530 act.sa_handler = handler;
531 act.sa_mask = empty_mask;
533 sigaction (sig, &act,
NULL);
544 return (access (file, F_OK) == 0);
564 _DBUS_SET_OOM (error);
570 _DBUS_SET_OOM (error);
578 _DBUS_SET_OOM (error);
619 const char *filename_c;
622 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
626 if (stat (filename_c, &sb) < 0)
629 "%s", _dbus_strerror (errno));
633 statbuf->
mode = sb.st_mode;
634 statbuf->
nlink = sb.st_nlink;
635 statbuf->
uid = sb.st_uid;
636 statbuf->
gid = sb.st_gid;
637 statbuf->
size = sb.st_size;
638 statbuf->
atime = sb.st_atime;
639 statbuf->
mtime = sb.st_mtime;
640 statbuf->
ctime = sb.st_ctime;
668 const char *filename_c;
670 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
674 d = opendir (filename_c);
678 "Failed to read directory \"%s\": %s",
680 _dbus_strerror (errno));
688 "Could not allocate memory for directory iterator");
718 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
722 ent = readdir (iter->
d);
731 "%s", _dbus_strerror (err));
735 else if (ent->d_name[0] ==
'.' &&
736 (ent->d_name[1] ==
'\0' ||
737 (ent->d_name[1] ==
'.' && ent->d_name[2] ==
'\0')))
745 "No memory to read directory entry");
766 fill_user_info_from_group (
struct group *g,
772 info->
gid = g->gr_gid;
792 const char *group_c_str;
807 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
817 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
823 if ((
long) buflen <= 0)
837 #ifdef HAVE_POSIX_GETPWNAM_R
839 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
842 result = getgrgid_r (gid, &g_str, buf, buflen,
845 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
851 if (result == ERANGE && buflen < 512 * 1024)
862 if (result == 0 && g == &g_str)
864 b = fill_user_info_from_group (g, info, error);
871 "Group %s unknown or failed to look it up\n",
872 group_c_str ? group_c_str :
"???");
882 g = getgrnam (group_c_str);
886 return fill_user_info_from_group (g, info, error);
891 "Group %s unknown or failed to look it up\n",
892 group_c_str ? group_c_str :
"???");
932 return fill_group_info (info, gid,
NULL, error);
1092 for (i = 0; i < len; i++)
1094 unsigned char c = (
unsigned char) buf[i];
1097 else if (c < 0x20 || c > 127)
1129 _DBUS_SET_OOM (error);
1135 _DBUS_SET_OOM (error);
1148 "Failed to open \"%s\": %s",
1150 _dbus_strerror (errno));
1158 "Failed to read from \"%s\": %s",
1160 _dbus_strerror (errno));
1167 string_squash_nonprintable (&cmdline);
1176 _DBUS_SET_OOM (error);