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>
66 * @addtogroup DBusInternalsUtils
72 * Does the chdir, fork, setsid, etc. to become a daemon process.
74 * @param pidfile #NULL, or pidfile to create
75 * @param print_pid_pipe pipe to print daemon's pid to, or -1 for none
76 * @param error return location for errors
77 * @param keep_umask #TRUE to keep the original umask
78 * @returns #FALSE on failure
81 _dbus_become_daemon (const DBusString *pidfile,
82 DBusPipe *print_pid_pipe,
84 dbus_bool_t keep_umask)
90 _dbus_verbose ("Becoming a daemon...\n");
92 _dbus_verbose ("chdir to /\n");
95 dbus_set_error (error, DBUS_ERROR_FAILED,
96 "Could not chdir() to root directory");
100 _dbus_verbose ("forking...\n");
101 switch ((child_pid = fork ()))
104 _dbus_verbose ("fork failed\n");
105 dbus_set_error (error, _dbus_error_from_errno (errno),
106 "Failed to fork daemon: %s", _dbus_strerror (errno));
111 _dbus_verbose ("in child, closing std file descriptors\n");
113 /* silently ignore failures here, if someone
114 * doesn't have /dev/null we may as well try
118 dev_null_fd = open ("/dev/null", O_RDWR);
119 if (dev_null_fd >= 0)
121 dup2 (dev_null_fd, 0);
122 dup2 (dev_null_fd, 1);
124 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
125 if (s == NULL || *s == '\0')
126 dup2 (dev_null_fd, 2);
128 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
134 /* Get a predictable umask */
135 _dbus_verbose ("setting umask\n");
139 _dbus_verbose ("calling setsid()\n");
141 _dbus_assert_not_reached ("setsid() failed");
146 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
149 _dbus_verbose ("pid file or pipe write failed: %s\n",
151 kill (child_pid, SIGTERM);
155 _dbus_verbose ("parent exiting\n");
165 * Creates a file containing the process ID.
167 * @param filename the filename to write to
168 * @param pid our process ID
169 * @param error return location for errors
170 * @returns #FALSE on failure
173 _dbus_write_pid_file (const DBusString *filename,
177 const char *cfilename;
181 cfilename = _dbus_string_get_const_data (filename);
183 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
187 dbus_set_error (error, _dbus_error_from_errno (errno),
188 "Failed to open \"%s\": %s", cfilename,
189 _dbus_strerror (errno));
193 if ((f = fdopen (fd, "w")) == NULL)
195 dbus_set_error (error, _dbus_error_from_errno (errno),
196 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
197 _dbus_close (fd, NULL);
201 if (fprintf (f, "%lu\n", pid) < 0)
203 dbus_set_error (error, _dbus_error_from_errno (errno),
204 "Failed to write to \"%s\": %s", cfilename,
205 _dbus_strerror (errno));
211 if (fclose (f) == EOF)
213 dbus_set_error (error, _dbus_error_from_errno (errno),
214 "Failed to close \"%s\": %s", cfilename,
215 _dbus_strerror (errno));
223 * Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a
224 * pipe (if non-NULL). Does nothing if pidfile and print_pid_pipe are both
227 * @param pidfile the file to write to or #NULL
228 * @param print_pid_pipe the pipe to write to or #NULL
229 * @param pid_to_write the pid to write out
230 * @param error error on failure
231 * @returns FALSE if error is set
234 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
235 DBusPipe *print_pid_pipe,
236 dbus_pid_t pid_to_write,
241 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
242 if (!_dbus_write_pid_file (pidfile,
246 _dbus_verbose ("pid file write failed\n");
247 _DBUS_ASSERT_ERROR_IS_SET(error);
253 _dbus_verbose ("No pid file requested\n");
256 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
261 _dbus_verbose ("writing our pid to pipe %d\n",
264 if (!_dbus_string_init (&pid))
266 _DBUS_SET_OOM (error);
270 if (!_dbus_string_append_int (&pid, pid_to_write) ||
271 !_dbus_string_append (&pid, "\n"))
273 _dbus_string_free (&pid);
274 _DBUS_SET_OOM (error);
278 bytes = _dbus_string_get_length (&pid);
279 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
281 /* _dbus_pipe_write sets error only on failure, not short write */
282 if (error != NULL && !dbus_error_is_set(error))
284 dbus_set_error (error, DBUS_ERROR_FAILED,
285 "Printing message bus PID: did not write enough bytes\n");
287 _dbus_string_free (&pid);
291 _dbus_string_free (&pid);
295 _dbus_verbose ("No pid pipe to write to\n");
302 * Verify that after the fork we can successfully change to this user.
304 * @param user the username given in the daemon configuration
305 * @returns #TRUE if username is valid
308 _dbus_verify_daemon_user (const char *user)
312 _dbus_string_init_const (&u, user);
314 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
318 /* The HAVE_LIBAUDIT case lives in selinux.c */
319 #ifndef HAVE_LIBAUDIT
321 * Changes the user and group the bus is running as.
323 * @param user the user to become
324 * @param error return location for errors
325 * @returns #FALSE on failure
328 _dbus_change_to_daemon_user (const char *user,
335 _dbus_string_init_const (&u, user);
337 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
339 dbus_set_error (error, DBUS_ERROR_FAILED,
340 "User '%s' does not appear to exist?",
345 /* setgroups() only works if we are a privileged process,
346 * so we don't return error on failure; the only possible
347 * failure is that we don't have perms to do it.
349 * not sure this is right, maybe if setuid()
350 * is going to work then setgroups() should also work.
352 if (setgroups (0, NULL) < 0)
353 _dbus_warn ("Failed to drop supplementary groups: %s\n",
354 _dbus_strerror (errno));
356 /* Set GID first, or the setuid may remove our permission
359 if (setgid (gid) < 0)
361 dbus_set_error (error, _dbus_error_from_errno (errno),
362 "Failed to set GID to %lu: %s", gid,
363 _dbus_strerror (errno));
367 if (setuid (uid) < 0)
369 dbus_set_error (error, _dbus_error_from_errno (errno),
370 "Failed to set UID to %lu: %s", uid,
371 _dbus_strerror (errno));
377 #endif /* !HAVE_LIBAUDIT */
381 * Attempt to ensure that the current process can open
382 * at least @limit file descriptors.
384 * If @limit is lower than the current, it will not be
385 * lowered. No error is returned if the request can
388 * @limit Number of file descriptors
391 _dbus_request_file_descriptor_limit (unsigned int limit)
393 #ifdef HAVE_SETRLIMIT
395 struct rlimit target_lim;
397 /* No point to doing this practically speaking
398 * if we're not uid 0. We expect the system
399 * bus to use this before we change UID, and
400 * the session bus takes the Linux default
401 * of 1024 for both cur and max.
406 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
409 if (lim.rlim_cur >= limit)
412 /* Ignore "maximum limit", assume we have the "superuser"
413 * privileges. On Linux this is CAP_SYS_RESOURCE.
415 target_lim.rlim_cur = target_lim.rlim_max = limit;
416 /* Also ignore errors; if we fail, we will at least work
417 * up to whatever limit we had, which seems better than
418 * just outright aborting.
420 * However, in the future we should probably log this so OS builders
421 * have a chance to notice any misconfiguration like dbus-daemon
422 * being started without CAP_SYS_RESOURCE.
424 setrlimit (RLIMIT_NOFILE, &target_lim);
429 _dbus_init_system_log (void)
433 #if HAVE_DECL_LOG_PERROR
434 openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
436 openlog ("dbus", LOG_PID, LOG_DAEMON);
443 * Log a message to the system log file (e.g. syslog on Unix).
445 * @param severity a severity value
446 * @param msg a printf-style format string
447 * @param args arguments for the format string
451 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
455 va_start (args, msg);
457 _dbus_system_logv (severity, msg, args);
463 * Log a message to the system log file (e.g. syslog on Unix).
465 * @param severity a severity value
466 * @param msg a printf-style format string
467 * @param args arguments for the format string
469 * If the FATAL severity is given, this function will terminate the program
470 * with an error code.
473 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
479 case DBUS_SYSTEM_LOG_INFO:
480 flags = LOG_DAEMON | LOG_NOTICE;
482 case DBUS_SYSTEM_LOG_SECURITY:
483 flags = LOG_AUTH | LOG_NOTICE;
485 case DBUS_SYSTEM_LOG_FATAL:
486 flags = LOG_DAEMON|LOG_CRIT;
492 vsyslog (flags, msg, args);
495 #if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR
497 /* vsyslog() won't write to stderr, so we'd better do it */
500 DBUS_VA_COPY (tmp, args);
501 fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
502 vfprintf (stderr, msg, tmp);
503 fputc ('\n', stderr);
508 if (severity == DBUS_SYSTEM_LOG_FATAL)
512 /** Installs a UNIX signal handler
514 * @param sig the signal to handle
515 * @param handler the handler
518 _dbus_set_signal_handler (int sig,
519 DBusSignalHandler handler)
521 struct sigaction act;
524 sigemptyset (&empty_mask);
525 act.sa_handler = handler;
526 act.sa_mask = empty_mask;
528 sigaction (sig, &act, NULL);
531 /** Checks if a file exists
533 * @param file full path to the file
534 * @returns #TRUE if file exists
537 _dbus_file_exists (const char *file)
539 return (access (file, F_OK) == 0);
542 /** Checks if user is at the console
544 * @param username user to check
545 * @param error return location for errors
546 * @returns #TRUE is the user is at the consolei and there are no errors
549 _dbus_user_at_console (const char *username,
557 if (!_dbus_string_init (&f))
559 _DBUS_SET_OOM (error);
563 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
565 _DBUS_SET_OOM (error);
569 _dbus_string_init_const (&u, username);
571 if (!_dbus_concat_dir_and_file (&f, &u))
573 _DBUS_SET_OOM (error);
577 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
580 _dbus_string_free (&f);
587 * Checks whether the filename is an absolute path
589 * @param filename the filename
590 * @returns #TRUE if an absolute path
593 _dbus_path_is_absolute (const DBusString *filename)
595 if (_dbus_string_get_length (filename) > 0)
596 return _dbus_string_get_byte (filename, 0) == '/';
604 * @param filename the filename to stat
605 * @param statbuf the stat info to fill in
606 * @param error return location for error
607 * @returns #FALSE if error was set
610 _dbus_stat (const DBusString *filename,
614 const char *filename_c;
617 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
619 filename_c = _dbus_string_get_const_data (filename);
621 if (stat (filename_c, &sb) < 0)
623 dbus_set_error (error, _dbus_error_from_errno (errno),
624 "%s", _dbus_strerror (errno));
628 statbuf->mode = sb.st_mode;
629 statbuf->nlink = sb.st_nlink;
630 statbuf->uid = sb.st_uid;
631 statbuf->gid = sb.st_gid;
632 statbuf->size = sb.st_size;
633 statbuf->atime = sb.st_atime;
634 statbuf->mtime = sb.st_mtime;
635 statbuf->ctime = sb.st_ctime;
642 * Internals of directory iterator
646 DIR *d; /**< The DIR* from opendir() */
651 * Open a directory to iterate over.
653 * @param filename the directory name
654 * @param error exception return object or #NULL
655 * @returns new iterator, or #NULL on error
658 _dbus_directory_open (const DBusString *filename,
663 const char *filename_c;
665 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
667 filename_c = _dbus_string_get_const_data (filename);
669 d = opendir (filename_c);
672 dbus_set_error (error, _dbus_error_from_errno (errno),
673 "Failed to read directory \"%s\": %s",
675 _dbus_strerror (errno));
678 iter = dbus_new0 (DBusDirIter, 1);
682 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
683 "Could not allocate memory for directory iterator");
693 * Get next file in the directory. Will not return "." or ".." on
694 * UNIX. If an error occurs, the contents of "filename" are
695 * undefined. The error is never set if the function succeeds.
697 * This function is not re-entrant, and not necessarily thread-safe.
698 * Only use it for test code or single-threaded utilities.
700 * @param iter the iterator
701 * @param filename string to be set to the next file in the dir
702 * @param error return location for error
703 * @returns #TRUE if filename was filled in with a new filename
706 _dbus_directory_get_next_file (DBusDirIter *iter,
707 DBusString *filename,
713 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717 ent = readdir (iter->d);
724 dbus_set_error (error,
725 _dbus_error_from_errno (err),
726 "%s", _dbus_strerror (err));
730 else if (ent->d_name[0] == '.' &&
731 (ent->d_name[1] == '\0' ||
732 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
736 _dbus_string_set_length (filename, 0);
737 if (!_dbus_string_append (filename, ent->d_name))
739 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
740 "No memory to read directory entry");
751 * Closes a directory iteration.
754 _dbus_directory_close (DBusDirIter *iter)
761 fill_user_info_from_group (struct group *g,
765 _dbus_assert (g->gr_name != NULL);
767 info->gid = g->gr_gid;
768 info->groupname = _dbus_strdup (g->gr_name);
770 /* info->members = dbus_strdupv (g->gr_mem) */
772 if (info->groupname == NULL)
774 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
782 fill_group_info (DBusGroupInfo *info,
784 const DBusString *groupname,
787 const char *group_c_str;
789 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
790 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
793 group_c_str = _dbus_string_get_const_data (groupname);
797 /* For now assuming that the getgrnam() and getgrgid() flavors
798 * always correspond to the pwnam flavors, if not we have
799 * to add more configure checks.
802 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
811 /* retrieve maximum needed size for buf */
812 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
814 /* sysconf actually returns a long, but everything else expects size_t,
815 * so just recast here.
816 * https://bugs.freedesktop.org/show_bug.cgi?id=17061
818 if ((long) buflen <= 0)
824 buf = dbus_malloc (buflen);
827 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
832 #ifdef HAVE_POSIX_GETPWNAM_R
834 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
837 result = getgrgid_r (gid, &g_str, buf, buflen,
840 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
842 #endif /* !HAVE_POSIX_GETPWNAM_R */
843 /* Try a bigger buffer if ERANGE was returned:
844 https://bugs.freedesktop.org/show_bug.cgi?id=16727
846 if (result == ERANGE && buflen < 512 * 1024)
857 if (result == 0 && g == &g_str)
859 b = fill_user_info_from_group (g, info, error);
865 dbus_set_error (error, _dbus_error_from_errno (errno),
866 "Group %s unknown or failed to look it up\n",
867 group_c_str ? group_c_str : "???");
872 #else /* ! HAVE_GETPWNAM_R */
874 /* I guess we're screwed on thread safety here */
877 g = getgrnam (group_c_str);
881 return fill_user_info_from_group (g, info, error);
885 dbus_set_error (error, _dbus_error_from_errno (errno),
886 "Group %s unknown or failed to look it up\n",
887 group_c_str ? group_c_str : "???");
891 #endif /* ! HAVE_GETPWNAM_R */
895 * Initializes the given DBusGroupInfo struct
896 * with information about the given group name.
898 * @param info the group info struct
899 * @param groupname name of group
900 * @param error the error return
901 * @returns #FALSE if error is set
904 _dbus_group_info_fill (DBusGroupInfo *info,
905 const DBusString *groupname,
908 return fill_group_info (info, DBUS_GID_UNSET,
914 * Initializes the given DBusGroupInfo struct
915 * with information about the given group ID.
917 * @param info the group info struct
918 * @param gid group ID
919 * @param error the error return
920 * @returns #FALSE if error is set
923 _dbus_group_info_fill_gid (DBusGroupInfo *info,
927 return fill_group_info (info, gid, NULL, error);
931 * Parse a UNIX user from the bus config file. On Windows, this should
932 * simply always fail (just return #FALSE).
934 * @param username the username text
935 * @param uid_p place to return the uid
936 * @returns #TRUE on success
939 _dbus_parse_unix_user_from_config (const DBusString *username,
942 return _dbus_get_user_id (username, uid_p);
947 * Parse a UNIX group from the bus config file. On Windows, this should
948 * simply always fail (just return #FALSE).
950 * @param groupname the groupname text
951 * @param gid_p place to return the gid
952 * @returns #TRUE on success
955 _dbus_parse_unix_group_from_config (const DBusString *groupname,
958 return _dbus_get_group_id (groupname, gid_p);
962 * Gets all groups corresponding to the given UNIX user ID. On UNIX,
963 * just calls _dbus_groups_from_uid(). On Windows, should always
964 * fail since we don't know any UNIX groups.
967 * @param group_ids return location for array of group IDs
968 * @param n_group_ids return location for length of returned array
969 * @returns #TRUE if the UID existed and we got some credentials
972 _dbus_unix_groups_from_uid (dbus_uid_t uid,
973 dbus_gid_t **group_ids,
976 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
980 * Checks to see if the UNIX user ID is at the console.
981 * Should always fail on Windows (set the error to
982 * #DBUS_ERROR_NOT_SUPPORTED).
984 * @param uid UID of person to check
985 * @param error return location for errors
986 * @returns #TRUE if the UID is the same as the console user and there are no errors
989 _dbus_unix_user_is_at_console (dbus_uid_t uid,
992 return _dbus_is_console_user (uid, error);
997 * Checks to see if the UNIX user ID matches the UID of
998 * the process. Should always return #FALSE on Windows.
1000 * @param uid the UNIX user ID
1001 * @returns #TRUE if this uid owns the process.
1004 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
1006 return uid == _dbus_geteuid ();
1010 * Checks to see if the Windows user SID matches the owner of
1011 * the process. Should always return #FALSE on UNIX.
1013 * @param windows_sid the Windows user SID
1014 * @returns #TRUE if this user owns the process.
1017 _dbus_windows_user_is_process_owner (const char *windows_sid)
1022 /** @} */ /* End of DBusInternalsUtils functions */
1025 * @addtogroup DBusString
1030 * Get the directory name from a complete filename
1031 * @param filename the filename
1032 * @param dirname string to append directory name to
1033 * @returns #FALSE if no memory
1036 _dbus_string_get_dirname (const DBusString *filename,
1037 DBusString *dirname)
1041 _dbus_assert (filename != dirname);
1042 _dbus_assert (filename != NULL);
1043 _dbus_assert (dirname != NULL);
1045 /* Ignore any separators on the end */
1046 sep = _dbus_string_get_length (filename);
1048 return _dbus_string_append (dirname, "."); /* empty string passed in */
1050 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1053 _dbus_assert (sep >= 0);
1056 return _dbus_string_append (dirname, "/");
1058 /* Now find the previous separator */
1059 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1061 return _dbus_string_append (dirname, ".");
1063 /* skip multiple separators */
1064 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1067 _dbus_assert (sep >= 0);
1070 _dbus_string_get_byte (filename, 0) == '/')
1071 return _dbus_string_append (dirname, "/");
1073 return _dbus_string_copy_len (filename, 0, sep - 0,
1074 dirname, _dbus_string_get_length (dirname));
1076 /** @} */ /* DBusString stuff */
1079 string_squash_nonprintable (DBusString *str)
1084 buf = _dbus_string_get_data (str);
1085 len = _dbus_string_get_length (str);
1087 for (i = 0; i < len; i++)
1089 unsigned char c = (unsigned char) buf[i];
1092 else if (c < 0x20 || c > 127)
1098 * Get a printable string describing the command used to execute
1099 * the process with pid. This string should only be used for
1100 * informative purposes such as logging; it may not be trusted.
1102 * The command is guaranteed to be printable ASCII and no longer
1105 * @param pid Process id
1106 * @param str Append command to this string
1107 * @param max_len Maximum length of returned command
1108 * @param error return location for errors
1109 * @returns #FALSE on error
1112 _dbus_command_for_pid (unsigned long pid,
1117 /* This is all Linux-specific for now */
1122 if (!_dbus_string_init (&path))
1124 _DBUS_SET_OOM (error);
1128 if (!_dbus_string_init (&cmdline))
1130 _DBUS_SET_OOM (error);
1131 _dbus_string_free (&path);
1135 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1138 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1141 dbus_set_error (error,
1142 _dbus_error_from_errno (errno),
1143 "Failed to open \"%s\": %s",
1144 _dbus_string_get_const_data (&path),
1145 _dbus_strerror (errno));
1149 if (!_dbus_read (fd, &cmdline, max_len))
1151 dbus_set_error (error,
1152 _dbus_error_from_errno (errno),
1153 "Failed to read from \"%s\": %s",
1154 _dbus_string_get_const_data (&path),
1155 _dbus_strerror (errno));
1159 if (!_dbus_close (fd, error))
1162 string_squash_nonprintable (&cmdline);
1164 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1167 _dbus_string_free (&cmdline);
1168 _dbus_string_free (&path);
1171 _DBUS_SET_OOM (error);
1173 _dbus_string_free (&cmdline);
1174 _dbus_string_free (&path);