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
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
68 * @addtogroup DBusInternalsUtils
74 * Does the chdir, fork, setsid, etc. to become a daemon process.
76 * @param pidfile #NULL, or pidfile to create
77 * @param print_pid_pipe pipe to print daemon's pid to, or -1 for none
78 * @param error return location for errors
79 * @param keep_umask #TRUE to keep the original umask
80 * @returns #FALSE on failure
83 _dbus_become_daemon (const DBusString *pidfile,
84 DBusPipe *print_pid_pipe,
86 dbus_bool_t keep_umask)
92 _dbus_verbose ("Becoming a daemon...\n");
94 _dbus_verbose ("chdir to /\n");
97 dbus_set_error (error, DBUS_ERROR_FAILED,
98 "Could not chdir() to root directory");
102 _dbus_verbose ("forking...\n");
103 switch ((child_pid = fork ()))
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));
113 _dbus_verbose ("in child, closing std file descriptors\n");
115 /* silently ignore failures here, if someone
116 * doesn't have /dev/null we may as well try
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);
126 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
127 if (s == NULL || *s == '\0')
128 dup2 (dev_null_fd, 2);
130 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
136 /* Get a predictable umask */
137 _dbus_verbose ("setting umask\n");
141 _dbus_verbose ("calling setsid()\n");
143 _dbus_assert_not_reached ("setsid() failed");
148 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
151 _dbus_verbose ("pid file or pipe write failed: %s\n",
153 kill (child_pid, SIGTERM);
157 _dbus_verbose ("parent exiting\n");
167 * Creates a file containing the process ID.
169 * @param filename the filename to write to
170 * @param pid our process ID
171 * @param error return location for errors
172 * @returns #FALSE on failure
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);
189 dbus_set_error (error, _dbus_error_from_errno (errno),
190 "Failed to open \"%s\": %s", cfilename,
191 _dbus_strerror (errno));
195 if ((f = fdopen (fd, "w")) == NULL)
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);
203 if (fprintf (f, "%lu\n", pid) < 0)
205 dbus_set_error (error, _dbus_error_from_errno (errno),
206 "Failed to write to \"%s\": %s", cfilename,
207 _dbus_strerror (errno));
213 if (fclose (f) == EOF)
215 dbus_set_error (error, _dbus_error_from_errno (errno),
216 "Failed to close \"%s\": %s", cfilename,
217 _dbus_strerror (errno));
225 * Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a
226 * pipe (if non-NULL). Does nothing if pidfile and print_pid_pipe are both
229 * @param pidfile the file to write to or #NULL
230 * @param print_pid_pipe the pipe to write to or #NULL
231 * @param pid_to_write the pid to write out
232 * @param error error on failure
233 * @returns FALSE if error is set
236 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
237 DBusPipe *print_pid_pipe,
238 dbus_pid_t pid_to_write,
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",
266 if (!_dbus_string_init (&pid))
268 _DBUS_SET_OOM (error);
272 if (!_dbus_string_append_int (&pid, pid_to_write) ||
273 !_dbus_string_append (&pid, "\n"))
275 _dbus_string_free (&pid);
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)
283 /* _dbus_pipe_write sets error only on failure, not short write */
284 if (error != NULL && !dbus_error_is_set(error))
286 dbus_set_error (error, DBUS_ERROR_FAILED,
287 "Printing message bus PID: did not write enough bytes\n");
289 _dbus_string_free (&pid);
293 _dbus_string_free (&pid);
297 _dbus_verbose ("No pid pipe to write to\n");
304 * Verify that after the fork we can successfully change to this user.
306 * @param user the username given in the daemon configuration
307 * @returns #TRUE if username is valid
310 _dbus_verify_daemon_user (const char *user)
314 _dbus_string_init_const (&u, user);
316 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
320 /* The HAVE_LIBAUDIT case lives in selinux.c */
321 #ifndef HAVE_LIBAUDIT
323 * Changes the user and group the bus is running as.
325 * @param user the user to become
326 * @param error return location for errors
327 * @returns #FALSE on failure
330 _dbus_change_to_daemon_user (const char *user,
337 _dbus_string_init_const (&u, user);
339 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
341 dbus_set_error (error, DBUS_ERROR_FAILED,
342 "User '%s' does not appear to exist?",
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.
351 * not sure this is right, maybe if setuid()
352 * is going to work then setgroups() should also work.
354 if (setgroups (0, NULL) < 0)
355 _dbus_warn ("Failed to drop supplementary groups: %s\n",
356 _dbus_strerror (errno));
358 /* Set GID first, or the setuid may remove our permission
361 if (setgid (gid) < 0)
363 dbus_set_error (error, _dbus_error_from_errno (errno),
364 "Failed to set GID to %lu: %s", gid,
365 _dbus_strerror (errno));
369 if (setuid (uid) < 0)
371 dbus_set_error (error, _dbus_error_from_errno (errno),
372 "Failed to set UID to %lu: %s", uid,
373 _dbus_strerror (errno));
379 #endif /* !HAVE_LIBAUDIT */
383 * Attempt to ensure that the current process can open
384 * at least @p limit file descriptors.
386 * If @p limit is lower than the current, it will not be
387 * lowered. No error is returned if the request can
390 * @param limit number of file descriptors
393 _dbus_request_file_descriptor_limit (unsigned int limit)
395 #ifdef HAVE_SETRLIMIT
397 struct rlimit target_lim;
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.
408 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
411 if (lim.rlim_cur >= limit)
414 /* Ignore "maximum limit", assume we have the "superuser"
415 * privileges. On Linux this is CAP_SYS_RESOURCE.
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.
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.
426 setrlimit (RLIMIT_NOFILE, &target_lim);
431 _dbus_init_system_log (dbus_bool_t is_daemon)
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);
448 * Log a message to the system log file (e.g. syslog on Unix).
450 * @param severity a severity value
451 * @param msg a printf-style format string
452 * @param args arguments for the format string
456 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
460 va_start (args, msg);
462 _dbus_system_logv (severity, msg, args);
468 * Log a message to the system log file (e.g. syslog on Unix).
470 * @param severity a severity value
471 * @param msg a printf-style format string
472 * @param args arguments for the format string
474 * If the FATAL severity is given, this function will terminate the program
475 * with an error code.
478 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
485 case DBUS_SYSTEM_LOG_INFO:
486 flags = LOG_DAEMON | LOG_NOTICE;
488 case DBUS_SYSTEM_LOG_SECURITY:
489 flags = LOG_AUTH | LOG_NOTICE;
491 case DBUS_SYSTEM_LOG_FATAL:
492 flags = LOG_DAEMON|LOG_CRIT;
498 DBUS_VA_COPY (tmp, args);
499 vsyslog (flags, msg, tmp);
503 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
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);
514 if (severity == DBUS_SYSTEM_LOG_FATAL)
518 /** Installs a UNIX signal handler
520 * @param sig the signal to handle
521 * @param handler the handler
524 _dbus_set_signal_handler (int sig,
525 DBusSignalHandler handler)
527 struct sigaction act;
530 sigemptyset (&empty_mask);
531 act.sa_handler = handler;
532 act.sa_mask = empty_mask;
534 sigaction (sig, &act, NULL);
537 /** Checks if a file exists
539 * @param file full path to the file
540 * @returns #TRUE if file exists
543 _dbus_file_exists (const char *file)
545 return (access (file, F_OK) == 0);
548 /** Checks if user is at the console
550 * @param username user to check
551 * @param error return location for errors
552 * @returns #TRUE is the user is at the consolei and there are no errors
555 _dbus_user_at_console (const char *username,
563 if (!_dbus_string_init (&f))
565 _DBUS_SET_OOM (error);
569 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
571 _DBUS_SET_OOM (error);
575 _dbus_string_init_const (&u, username);
577 if (!_dbus_concat_dir_and_file (&f, &u))
579 _DBUS_SET_OOM (error);
583 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
586 _dbus_string_free (&f);
593 * Checks whether the filename is an absolute path
595 * @param filename the filename
596 * @returns #TRUE if an absolute path
599 _dbus_path_is_absolute (const DBusString *filename)
601 if (_dbus_string_get_length (filename) > 0)
602 return _dbus_string_get_byte (filename, 0) == '/';
610 * @param filename the filename to stat
611 * @param statbuf the stat info to fill in
612 * @param error return location for error
613 * @returns #FALSE if error was set
616 _dbus_stat (const DBusString *filename,
620 const char *filename_c;
623 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
625 filename_c = _dbus_string_get_const_data (filename);
627 if (stat (filename_c, &sb) < 0)
629 dbus_set_error (error, _dbus_error_from_errno (errno),
630 "%s", _dbus_strerror (errno));
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;
648 * Internals of directory iterator
652 DIR *d; /**< The DIR* from opendir() */
657 * Open a directory to iterate over.
659 * @param filename the directory name
660 * @param error exception return object or #NULL
661 * @returns new iterator, or #NULL on error
664 _dbus_directory_open (const DBusString *filename,
669 const char *filename_c;
671 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
673 filename_c = _dbus_string_get_const_data (filename);
675 d = opendir (filename_c);
678 dbus_set_error (error, _dbus_error_from_errno (errno),
679 "Failed to read directory \"%s\": %s",
681 _dbus_strerror (errno));
684 iter = dbus_new0 (DBusDirIter, 1);
688 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
689 "Could not allocate memory for directory iterator");
699 * Get next file in the directory. Will not return "." or ".." on
700 * UNIX. If an error occurs, the contents of "filename" are
701 * undefined. The error is never set if the function succeeds.
703 * This function is not re-entrant, and not necessarily thread-safe.
704 * Only use it for test code or single-threaded utilities.
706 * @param iter the iterator
707 * @param filename string to be set to the next file in the dir
708 * @param error return location for error
709 * @returns #TRUE if filename was filled in with a new filename
712 _dbus_directory_get_next_file (DBusDirIter *iter,
713 DBusString *filename,
719 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
723 ent = readdir (iter->d);
730 dbus_set_error (error,
731 _dbus_error_from_errno (err),
732 "%s", _dbus_strerror (err));
736 else if (ent->d_name[0] == '.' &&
737 (ent->d_name[1] == '\0' ||
738 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
742 _dbus_string_set_length (filename, 0);
743 if (!_dbus_string_append (filename, ent->d_name))
745 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
746 "No memory to read directory entry");
757 * Closes a directory iteration.
760 _dbus_directory_close (DBusDirIter *iter)
767 fill_user_info_from_group (struct group *g,
771 _dbus_assert (g->gr_name != NULL);
773 info->gid = g->gr_gid;
774 info->groupname = _dbus_strdup (g->gr_name);
776 /* info->members = dbus_strdupv (g->gr_mem) */
778 if (info->groupname == NULL)
780 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
788 fill_group_info (DBusGroupInfo *info,
790 const DBusString *groupname,
793 const char *group_c_str;
795 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
796 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
799 group_c_str = _dbus_string_get_const_data (groupname);
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.
808 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
817 /* retrieve maximum needed size for buf */
818 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
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
824 if ((long) buflen <= 0)
830 buf = dbus_malloc (buflen);
833 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
838 #ifdef HAVE_POSIX_GETPWNAM_R
840 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
843 result = getgrgid_r (gid, &g_str, buf, buflen,
846 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
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
852 if (result == ERANGE && buflen < 512 * 1024)
863 if (result == 0 && g == &g_str)
865 b = fill_user_info_from_group (g, info, error);
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 : "???");
878 #else /* ! HAVE_GETPWNAM_R */
880 /* I guess we're screwed on thread safety here */
883 g = getgrnam (group_c_str);
887 return fill_user_info_from_group (g, info, error);
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 : "???");
897 #endif /* ! HAVE_GETPWNAM_R */
901 * Initializes the given DBusGroupInfo struct
902 * with information about the given group name.
904 * @param info the group info struct
905 * @param groupname name of group
906 * @param error the error return
907 * @returns #FALSE if error is set
910 _dbus_group_info_fill (DBusGroupInfo *info,
911 const DBusString *groupname,
914 return fill_group_info (info, DBUS_GID_UNSET,
920 * Initializes the given DBusGroupInfo struct
921 * with information about the given group ID.
923 * @param info the group info struct
924 * @param gid group ID
925 * @param error the error return
926 * @returns #FALSE if error is set
929 _dbus_group_info_fill_gid (DBusGroupInfo *info,
933 return fill_group_info (info, gid, NULL, error);
937 * Parse a UNIX user from the bus config file. On Windows, this should
938 * simply always fail (just return #FALSE).
940 * @param username the username text
941 * @param uid_p place to return the uid
942 * @returns #TRUE on success
945 _dbus_parse_unix_user_from_config (const DBusString *username,
948 return _dbus_get_user_id (username, uid_p);
953 * Parse a UNIX group from the bus config file. On Windows, this should
954 * simply always fail (just return #FALSE).
956 * @param groupname the groupname text
957 * @param gid_p place to return the gid
958 * @returns #TRUE on success
961 _dbus_parse_unix_group_from_config (const DBusString *groupname,
964 return _dbus_get_group_id (groupname, gid_p);
968 * Gets all groups corresponding to the given UNIX user ID. On UNIX,
969 * just calls _dbus_groups_from_uid(). On Windows, should always
970 * fail since we don't know any UNIX groups.
973 * @param group_ids return location for array of group IDs
974 * @param n_group_ids return location for length of returned array
975 * @returns #TRUE if the UID existed and we got some credentials
978 _dbus_unix_groups_from_uid (dbus_uid_t uid,
979 dbus_gid_t **group_ids,
982 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
986 * Checks to see if the UNIX user ID is at the console.
987 * Should always fail on Windows (set the error to
988 * #DBUS_ERROR_NOT_SUPPORTED).
990 * @param uid UID of person to check
991 * @param error return location for errors
992 * @returns #TRUE if the UID is the same as the console user and there are no errors
995 _dbus_unix_user_is_at_console (dbus_uid_t uid,
998 return _dbus_is_console_user (uid, error);
1003 * Checks to see if the UNIX user ID matches the UID of
1004 * the process. Should always return #FALSE on Windows.
1006 * @param uid the UNIX user ID
1007 * @returns #TRUE if this uid owns the process.
1010 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
1012 return uid == _dbus_geteuid ();
1016 * Checks to see if the Windows user SID matches the owner of
1017 * the process. Should always return #FALSE on UNIX.
1019 * @param windows_sid the Windows user SID
1020 * @returns #TRUE if this user owns the process.
1023 _dbus_windows_user_is_process_owner (const char *windows_sid)
1028 /** @} */ /* End of DBusInternalsUtils functions */
1031 * @addtogroup DBusString
1036 * Get the directory name from a complete filename
1037 * @param filename the filename
1038 * @param dirname string to append directory name to
1039 * @returns #FALSE if no memory
1042 _dbus_string_get_dirname (const DBusString *filename,
1043 DBusString *dirname)
1047 _dbus_assert (filename != dirname);
1048 _dbus_assert (filename != NULL);
1049 _dbus_assert (dirname != NULL);
1051 /* Ignore any separators on the end */
1052 sep = _dbus_string_get_length (filename);
1054 return _dbus_string_append (dirname, "."); /* empty string passed in */
1056 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1059 _dbus_assert (sep >= 0);
1062 return _dbus_string_append (dirname, "/");
1064 /* Now find the previous separator */
1065 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1067 return _dbus_string_append (dirname, ".");
1069 /* skip multiple separators */
1070 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1073 _dbus_assert (sep >= 0);
1076 _dbus_string_get_byte (filename, 0) == '/')
1077 return _dbus_string_append (dirname, "/");
1079 return _dbus_string_copy_len (filename, 0, sep - 0,
1080 dirname, _dbus_string_get_length (dirname));
1082 /** @} */ /* DBusString stuff */
1085 string_squash_nonprintable (DBusString *str)
1090 buf = _dbus_string_get_data (str);
1091 len = _dbus_string_get_length (str);
1093 for (i = 0; i < len; i++)
1095 unsigned char c = (unsigned char) buf[i];
1098 else if (c < 0x20 || c > 127)
1104 * Get a printable string describing the command used to execute
1105 * the process with pid. This string should only be used for
1106 * informative purposes such as logging; it may not be trusted.
1108 * The command is guaranteed to be printable ASCII and no longer
1111 * @param pid Process id
1112 * @param str Append command to this string
1113 * @param max_len Maximum length of returned command
1114 * @param error return location for errors
1115 * @returns #FALSE on error
1118 _dbus_command_for_pid (unsigned long pid,
1123 /* This is all Linux-specific for now */
1128 if (!_dbus_string_init (&path))
1130 _DBUS_SET_OOM (error);
1134 if (!_dbus_string_init (&cmdline))
1136 _DBUS_SET_OOM (error);
1137 _dbus_string_free (&path);
1141 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1144 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1147 dbus_set_error (error,
1148 _dbus_error_from_errno (errno),
1149 "Failed to open \"%s\": %s",
1150 _dbus_string_get_const_data (&path),
1151 _dbus_strerror (errno));
1155 if (!_dbus_read (fd, &cmdline, max_len))
1157 dbus_set_error (error,
1158 _dbus_error_from_errno (errno),
1159 "Failed to read from \"%s\": %s",
1160 _dbus_string_get_const_data (&path),
1161 _dbus_strerror (errno));
1165 if (!_dbus_close (fd, error))
1168 string_squash_nonprintable (&cmdline);
1170 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1173 _dbus_string_free (&cmdline);
1174 _dbus_string_free (&path);
1177 _DBUS_SET_OOM (error);
1179 _dbus_string_free (&cmdline);
1180 _dbus_string_free (&path);