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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-sysdeps.h"
25 #include "dbus-sysdeps-unix.h"
26 #include "dbus-internals.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
29 #define DBUS_USERDB_INCLUDES_PRIVATE 1
30 #include "dbus-userdb.h"
31 #include "dbus-test.h"
33 #include <sys/types.h>
43 #include <sys/socket.h>
47 #include <sys/prctl.h>
48 #include <sys/capability.h>
50 #endif /* HAVE_LIBAUDIT */
52 #ifdef HAVE_SYS_SYSLIMITS_H
53 #include <sys/syslimits.h>
61 * @addtogroup DBusInternalsUtils
67 * Does the chdir, fork, setsid, etc. to become a daemon process.
69 * @param pidfile #NULL, or pidfile to create
70 * @param print_pid_pipe pipe to print daemon's pid to, or -1 for none
71 * @param error return location for errors
72 * @param keep_umask #TRUE to keep the original umask
73 * @returns #FALSE on failure
76 _dbus_become_daemon (const DBusString *pidfile,
77 DBusPipe *print_pid_pipe,
79 dbus_bool_t keep_umask)
85 _dbus_verbose ("Becoming a daemon...\n");
87 _dbus_verbose ("chdir to /\n");
90 dbus_set_error (error, DBUS_ERROR_FAILED,
91 "Could not chdir() to root directory");
95 _dbus_verbose ("forking...\n");
96 switch ((child_pid = fork ()))
99 _dbus_verbose ("fork failed\n");
100 dbus_set_error (error, _dbus_error_from_errno (errno),
101 "Failed to fork daemon: %s", _dbus_strerror (errno));
106 _dbus_verbose ("in child, closing std file descriptors\n");
108 /* silently ignore failures here, if someone
109 * doesn't have /dev/null we may as well try
113 dev_null_fd = open ("/dev/null", O_RDWR);
114 if (dev_null_fd >= 0)
116 dup2 (dev_null_fd, 0);
117 dup2 (dev_null_fd, 1);
119 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
120 if (s == NULL || *s == '\0')
121 dup2 (dev_null_fd, 2);
123 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
128 /* Get a predictable umask */
129 _dbus_verbose ("setting umask\n");
133 _dbus_verbose ("calling setsid()\n");
135 _dbus_assert_not_reached ("setsid() failed");
140 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
143 _dbus_verbose ("pid file or pipe write failed: %s\n",
145 kill (child_pid, SIGTERM);
149 _dbus_verbose ("parent exiting\n");
159 * Creates a file containing the process ID.
161 * @param filename the filename to write to
162 * @param pid our process ID
163 * @param error return location for errors
164 * @returns #FALSE on failure
167 _dbus_write_pid_file (const DBusString *filename,
171 const char *cfilename;
175 cfilename = _dbus_string_get_const_data (filename);
177 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
181 dbus_set_error (error, _dbus_error_from_errno (errno),
182 "Failed to open \"%s\": %s", cfilename,
183 _dbus_strerror (errno));
187 if ((f = fdopen (fd, "w")) == NULL)
189 dbus_set_error (error, _dbus_error_from_errno (errno),
190 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
191 _dbus_close (fd, NULL);
195 if (fprintf (f, "%lu\n", pid) < 0)
197 dbus_set_error (error, _dbus_error_from_errno (errno),
198 "Failed to write to \"%s\": %s", cfilename,
199 _dbus_strerror (errno));
205 if (fclose (f) == EOF)
207 dbus_set_error (error, _dbus_error_from_errno (errno),
208 "Failed to close \"%s\": %s", cfilename,
209 _dbus_strerror (errno));
217 * Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a
218 * pipe (if non-NULL). Does nothing if pidfile and print_pid_pipe are both
221 * @param pidfile the file to write to or #NULL
222 * @param print_pid_pipe the pipe to write to or #NULL
223 * @param pid_to_write the pid to write out
224 * @param error error on failure
225 * @returns FALSE if error is set
228 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
229 DBusPipe *print_pid_pipe,
230 dbus_pid_t pid_to_write,
235 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
236 if (!_dbus_write_pid_file (pidfile,
240 _dbus_verbose ("pid file write failed\n");
241 _DBUS_ASSERT_ERROR_IS_SET(error);
247 _dbus_verbose ("No pid file requested\n");
250 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
255 _dbus_verbose ("writing our pid to pipe %d\n", print_pid_pipe->fd_or_handle);
257 if (!_dbus_string_init (&pid))
259 _DBUS_SET_OOM (error);
263 if (!_dbus_string_append_int (&pid, pid_to_write) ||
264 !_dbus_string_append (&pid, "\n"))
266 _dbus_string_free (&pid);
267 _DBUS_SET_OOM (error);
271 bytes = _dbus_string_get_length (&pid);
272 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
274 /* _dbus_pipe_write sets error only on failure, not short write */
275 if (error != NULL && !dbus_error_is_set(error))
277 dbus_set_error (error, DBUS_ERROR_FAILED,
278 "Printing message bus PID: did not write enough bytes\n");
280 _dbus_string_free (&pid);
284 _dbus_string_free (&pid);
288 _dbus_verbose ("No pid pipe to write to\n");
295 * Verify that after the fork we can successfully change to this user.
297 * @param user the username given in the daemon configuration
298 * @returns #TRUE if username is valid
301 _dbus_verify_daemon_user (const char *user)
305 _dbus_string_init_const (&u, user);
307 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
311 * Changes the user and group the bus is running as.
313 * @param user the user to become
314 * @param error return location for errors
315 * @returns #FALSE on failure
318 _dbus_change_to_daemon_user (const char *user,
325 dbus_bool_t we_were_root;
329 _dbus_string_init_const (&u, user);
331 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
333 dbus_set_error (error, DBUS_ERROR_FAILED,
334 "User '%s' does not appear to exist?",
340 we_were_root = _dbus_geteuid () == 0;
342 /* have a tmp set of caps that we use to transition to the usr/grp dbus should
343 * run as ... doesn't really help. But keeps people happy.
348 cap_value_t new_cap_list[] = { CAP_AUDIT_WRITE };
349 cap_value_t tmp_cap_list[] = { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
350 cap_t tmp_caps = cap_init();
352 if (!tmp_caps || !(new_caps = cap_init ()))
354 dbus_set_error (error, DBUS_ERROR_FAILED,
355 "Failed to initialize drop of capabilities: %s\n",
356 _dbus_strerror (errno));
364 /* assume these work... */
365 cap_set_flag (new_caps, CAP_PERMITTED, 1, new_cap_list, CAP_SET);
366 cap_set_flag (new_caps, CAP_EFFECTIVE, 1, new_cap_list, CAP_SET);
367 cap_set_flag (tmp_caps, CAP_PERMITTED, 3, tmp_cap_list, CAP_SET);
368 cap_set_flag (tmp_caps, CAP_EFFECTIVE, 3, tmp_cap_list, CAP_SET);
370 if (prctl (PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
372 dbus_set_error (error, _dbus_error_from_errno (errno),
373 "Failed to set keep-capabilities: %s\n",
374 _dbus_strerror (errno));
379 if (cap_set_proc (tmp_caps) == -1)
381 dbus_set_error (error, DBUS_ERROR_FAILED,
382 "Failed to drop capabilities: %s\n",
383 _dbus_strerror (errno));
389 #endif /* HAVE_LIBAUDIT */
391 /* setgroups() only works if we are a privileged process,
392 * so we don't return error on failure; the only possible
393 * failure is that we don't have perms to do it.
395 * not sure this is right, maybe if setuid()
396 * is going to work then setgroups() should also work.
398 if (setgroups (0, NULL) < 0)
399 _dbus_warn ("Failed to drop supplementary groups: %s\n",
400 _dbus_strerror (errno));
402 /* Set GID first, or the setuid may remove our permission
405 if (setgid (gid) < 0)
407 dbus_set_error (error, _dbus_error_from_errno (errno),
408 "Failed to set GID to %lu: %s", gid,
409 _dbus_strerror (errno));
413 if (setuid (uid) < 0)
415 dbus_set_error (error, _dbus_error_from_errno (errno),
416 "Failed to set UID to %lu: %s", uid,
417 _dbus_strerror (errno));
424 if (cap_set_proc (new_caps))
426 dbus_set_error (error, DBUS_ERROR_FAILED,
427 "Failed to drop capabilities: %s\n",
428 _dbus_strerror (errno));
433 /* should always work, if it did above */
434 if (prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0) == -1)
436 dbus_set_error (error, _dbus_error_from_errno (errno),
437 "Failed to unset keep-capabilities: %s\n",
438 _dbus_strerror (errno));
450 /* should always work, if it did above */
451 prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0);
460 _dbus_init_system_log (void)
462 openlog ("dbus", LOG_PID, LOG_DAEMON);
466 * Log an informative message. Intended for use primarily by
469 * @param msg a printf-style format string
470 * @param args arguments for the format string
473 _dbus_log_info (const char *msg, va_list args)
475 vsyslog (LOG_DAEMON|LOG_NOTICE, msg, args);
479 * Log a security-related message. Intended for use primarily by
482 * @param msg a printf-style format string
483 * @param args arguments for the format string
486 _dbus_log_security (const char *msg, va_list args)
488 vsyslog (LOG_AUTH|LOG_NOTICE, msg, args);
491 /** Installs a UNIX signal handler
493 * @param sig the signal to handle
494 * @param handler the handler
497 _dbus_set_signal_handler (int sig,
498 DBusSignalHandler handler)
500 struct sigaction act;
503 sigemptyset (&empty_mask);
504 act.sa_handler = handler;
505 act.sa_mask = empty_mask;
507 sigaction (sig, &act, NULL);
512 * Removes a directory; Directory must be empty
514 * @param filename directory filename
515 * @param error initialized error object
516 * @returns #TRUE on success
519 _dbus_delete_directory (const DBusString *filename,
522 const char *filename_c;
524 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
526 filename_c = _dbus_string_get_const_data (filename);
528 if (rmdir (filename_c) != 0)
530 dbus_set_error (error, DBUS_ERROR_FAILED,
531 "Failed to remove directory %s: %s\n",
532 filename_c, _dbus_strerror (errno));
539 /** Checks if a file exists
541 * @param file full path to the file
542 * @returns #TRUE if file exists
545 _dbus_file_exists (const char *file)
547 return (access (file, F_OK) == 0);
550 /** Checks if user is at the console
552 * @param username user to check
553 * @param error return location for errors
554 * @returns #TRUE is the user is at the consolei and there are no errors
557 _dbus_user_at_console (const char *username,
565 if (!_dbus_string_init (&f))
567 _DBUS_SET_OOM (error);
571 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
573 _DBUS_SET_OOM (error);
578 if (!_dbus_string_append (&f, username))
580 _DBUS_SET_OOM (error);
584 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
587 _dbus_string_free (&f);
594 * Checks whether the filename is an absolute path
596 * @param filename the filename
597 * @returns #TRUE if an absolute path
600 _dbus_path_is_absolute (const DBusString *filename)
602 if (_dbus_string_get_length (filename) > 0)
603 return _dbus_string_get_byte (filename, 0) == '/';
611 * @param filename the filename to stat
612 * @param statbuf the stat info to fill in
613 * @param error return location for error
614 * @returns #FALSE if error was set
617 _dbus_stat (const DBusString *filename,
621 const char *filename_c;
624 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
626 filename_c = _dbus_string_get_const_data (filename);
628 if (stat (filename_c, &sb) < 0)
630 dbus_set_error (error, _dbus_error_from_errno (errno),
631 "%s", _dbus_strerror (errno));
635 statbuf->mode = sb.st_mode;
636 statbuf->nlink = sb.st_nlink;
637 statbuf->uid = sb.st_uid;
638 statbuf->gid = sb.st_gid;
639 statbuf->size = sb.st_size;
640 statbuf->atime = sb.st_atime;
641 statbuf->mtime = sb.st_mtime;
642 statbuf->ctime = sb.st_ctime;
649 * Internals of directory iterator
653 DIR *d; /**< The DIR* from opendir() */
658 * Open a directory to iterate over.
660 * @param filename the directory name
661 * @param error exception return object or #NULL
662 * @returns new iterator, or #NULL on error
665 _dbus_directory_open (const DBusString *filename,
670 const char *filename_c;
672 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
674 filename_c = _dbus_string_get_const_data (filename);
676 d = opendir (filename_c);
679 dbus_set_error (error, _dbus_error_from_errno (errno),
680 "Failed to read directory \"%s\": %s",
682 _dbus_strerror (errno));
685 iter = dbus_new0 (DBusDirIter, 1);
689 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
690 "Could not allocate memory for directory iterator");
699 /* Calculate the required buffer size (in bytes) for directory
700 * entries read from the given directory handle. Return -1 if this
701 * this cannot be done.
703 * If you use autoconf, include fpathconf and dirfd in your
704 * AC_CHECK_FUNCS list. Otherwise use some other method to detect
705 * and use them where available.
708 dirent_buf_size(DIR * dirp, size_t *size)
711 # if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX)
712 # if defined(HAVE_DIRFD)
713 name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
714 # elif defined(HAVE_DDFD)
715 name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX);
717 name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX);
718 # endif /* HAVE_DIRFD */
720 # if defined(NAME_MAX)
725 # elif defined(MAXNAMELEN)
726 name_max = MAXNAMELEN;
728 # if defined(NAME_MAX)
731 # error "buffer size for readdir_r cannot be determined"
735 *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1;
743 * Get next file in the directory. Will not return "." or ".." on
744 * UNIX. If an error occurs, the contents of "filename" are
745 * undefined. The error is never set if the function succeeds.
747 * @param iter the iterator
748 * @param filename string to be set to the next file in the dir
749 * @param error return location for error
750 * @returns #TRUE if filename was filled in with a new filename
753 _dbus_directory_get_next_file (DBusDirIter *iter,
754 DBusString *filename,
757 struct dirent *d, *ent;
761 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
763 if (!dirent_buf_size (iter->d, &buf_size))
765 dbus_set_error (error, DBUS_ERROR_FAILED,
766 "Can't calculate buffer size when reading directory");
770 d = (struct dirent *)dbus_malloc (buf_size);
773 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
774 "No memory to read directory entry");
779 err = readdir_r (iter->d, d, &ent);
783 dbus_set_error (error,
784 _dbus_error_from_errno (err),
785 "%s", _dbus_strerror (err));
790 else if (ent->d_name[0] == '.' &&
791 (ent->d_name[1] == '\0' ||
792 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
796 _dbus_string_set_length (filename, 0);
797 if (!_dbus_string_append (filename, ent->d_name))
799 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
800 "No memory to read directory entry");
813 * Closes a directory iteration.
816 _dbus_directory_close (DBusDirIter *iter)
823 fill_user_info_from_group (struct group *g,
827 _dbus_assert (g->gr_name != NULL);
829 info->gid = g->gr_gid;
830 info->groupname = _dbus_strdup (g->gr_name);
832 /* info->members = dbus_strdupv (g->gr_mem) */
834 if (info->groupname == NULL)
836 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
844 fill_group_info (DBusGroupInfo *info,
846 const DBusString *groupname,
849 const char *group_c_str;
851 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
852 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
855 group_c_str = _dbus_string_get_const_data (groupname);
859 /* For now assuming that the getgrnam() and getgrgid() flavors
860 * always correspond to the pwnam flavors, if not we have
861 * to add more configure checks.
864 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
873 /* retrieve maximum needed size for buf */
874 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
876 /* sysconf actually returns a long, but everything else expects size_t,
877 * so just recast here.
878 * https://bugs.freedesktop.org/show_bug.cgi?id=17061
880 if ((long) buflen <= 0)
886 buf = dbus_malloc (buflen);
889 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
894 #ifdef HAVE_POSIX_GETPWNAM_R
896 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
899 result = getgrgid_r (gid, &g_str, buf, buflen,
902 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
904 #endif /* !HAVE_POSIX_GETPWNAM_R */
905 /* Try a bigger buffer if ERANGE was returned:
906 https://bugs.freedesktop.org/show_bug.cgi?id=16727
908 if (result == ERANGE && buflen < 512 * 1024)
919 if (result == 0 && g == &g_str)
921 b = fill_user_info_from_group (g, info, error);
927 dbus_set_error (error, _dbus_error_from_errno (errno),
928 "Group %s unknown or failed to look it up\n",
929 group_c_str ? group_c_str : "???");
934 #else /* ! HAVE_GETPWNAM_R */
936 /* I guess we're screwed on thread safety here */
939 g = getgrnam (group_c_str);
943 return fill_user_info_from_group (g, info, error);
947 dbus_set_error (error, _dbus_error_from_errno (errno),
948 "Group %s unknown or failed to look it up\n",
949 group_c_str ? group_c_str : "???");
953 #endif /* ! HAVE_GETPWNAM_R */
957 * Initializes the given DBusGroupInfo struct
958 * with information about the given group name.
960 * @param info the group info struct
961 * @param groupname name of group
962 * @param error the error return
963 * @returns #FALSE if error is set
966 _dbus_group_info_fill (DBusGroupInfo *info,
967 const DBusString *groupname,
970 return fill_group_info (info, DBUS_GID_UNSET,
976 * Initializes the given DBusGroupInfo struct
977 * with information about the given group ID.
979 * @param info the group info struct
980 * @param gid group ID
981 * @param error the error return
982 * @returns #FALSE if error is set
985 _dbus_group_info_fill_gid (DBusGroupInfo *info,
989 return fill_group_info (info, gid, NULL, error);
993 * Parse a UNIX user from the bus config file. On Windows, this should
994 * simply always fail (just return #FALSE).
996 * @param username the username text
997 * @param uid_p place to return the uid
998 * @returns #TRUE on success
1001 _dbus_parse_unix_user_from_config (const DBusString *username,
1004 return _dbus_get_user_id (username, uid_p);
1009 * Parse a UNIX group from the bus config file. On Windows, this should
1010 * simply always fail (just return #FALSE).
1012 * @param groupname the groupname text
1013 * @param gid_p place to return the gid
1014 * @returns #TRUE on success
1017 _dbus_parse_unix_group_from_config (const DBusString *groupname,
1020 return _dbus_get_group_id (groupname, gid_p);
1024 * Gets all groups corresponding to the given UNIX user ID. On UNIX,
1025 * just calls _dbus_groups_from_uid(). On Windows, should always
1026 * fail since we don't know any UNIX groups.
1028 * @param uid the UID
1029 * @param group_ids return location for array of group IDs
1030 * @param n_group_ids return location for length of returned array
1031 * @returns #TRUE if the UID existed and we got some credentials
1034 _dbus_unix_groups_from_uid (dbus_uid_t uid,
1035 dbus_gid_t **group_ids,
1038 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1042 * Checks to see if the UNIX user ID is at the console.
1043 * Should always fail on Windows (set the error to
1044 * #DBUS_ERROR_NOT_SUPPORTED).
1046 * @param uid UID of person to check
1047 * @param error return location for errors
1048 * @returns #TRUE if the UID is the same as the console user and there are no errors
1051 _dbus_unix_user_is_at_console (dbus_uid_t uid,
1054 return _dbus_is_console_user (uid, error);
1059 * Checks to see if the UNIX user ID matches the UID of
1060 * the process. Should always return #FALSE on Windows.
1062 * @param uid the UNIX user ID
1063 * @returns #TRUE if this uid owns the process.
1066 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
1068 return uid == _dbus_geteuid ();
1072 * Checks to see if the Windows user SID matches the owner of
1073 * the process. Should always return #FALSE on UNIX.
1075 * @param windows_sid the Windows user SID
1076 * @returns #TRUE if this user owns the process.
1079 _dbus_windows_user_is_process_owner (const char *windows_sid)
1084 /** @} */ /* End of DBusInternalsUtils functions */
1087 * @addtogroup DBusString
1092 * Get the directory name from a complete filename
1093 * @param filename the filename
1094 * @param dirname string to append directory name to
1095 * @returns #FALSE if no memory
1098 _dbus_string_get_dirname (const DBusString *filename,
1099 DBusString *dirname)
1103 _dbus_assert (filename != dirname);
1104 _dbus_assert (filename != NULL);
1105 _dbus_assert (dirname != NULL);
1107 /* Ignore any separators on the end */
1108 sep = _dbus_string_get_length (filename);
1110 return _dbus_string_append (dirname, "."); /* empty string passed in */
1112 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1115 _dbus_assert (sep >= 0);
1118 return _dbus_string_append (dirname, "/");
1120 /* Now find the previous separator */
1121 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1123 return _dbus_string_append (dirname, ".");
1125 /* skip multiple separators */
1126 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1129 _dbus_assert (sep >= 0);
1132 _dbus_string_get_byte (filename, 0) == '/')
1133 return _dbus_string_append (dirname, "/");
1135 return _dbus_string_copy_len (filename, 0, sep - 0,
1136 dirname, _dbus_string_get_length (dirname));
1138 /** @} */ /* DBusString stuff */
1141 string_squash_nonprintable (DBusString *str)
1146 buf = _dbus_string_get_data (str);
1147 len = _dbus_string_get_length (str);
1149 for (i = 0; i < len; i++)
1152 else if (buf[i] < 0x20 || buf[i] > 127)
1157 * Get a printable string describing the command used to execute
1158 * the process with pid. This string should only be used for
1159 * informative purposes such as logging; it may not be trusted.
1161 * The command is guaranteed to be printable ASCII and no longer
1164 * @param pid Process id
1165 * @param str Append command to this string
1166 * @param max_len Maximum length of returned command
1167 * @param error return location for errors
1168 * @returns #FALSE on error
1171 _dbus_command_for_pid (unsigned long pid,
1176 /* This is all Linux-specific for now */
1181 if (!_dbus_string_init (&path))
1183 _DBUS_SET_OOM (error);
1187 if (!_dbus_string_init (&cmdline))
1189 _DBUS_SET_OOM (error);
1190 _dbus_string_free (&path);
1194 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1197 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1200 dbus_set_error (error,
1201 _dbus_error_from_errno (errno),
1202 "Failed to open \"%s\": %s",
1203 _dbus_string_get_const_data (&path),
1204 _dbus_strerror (errno));
1208 if (!_dbus_read (fd, &cmdline, max_len))
1210 dbus_set_error (error,
1211 _dbus_error_from_errno (errno),
1212 "Failed to read from \"%s\": %s",
1213 _dbus_string_get_const_data (&path),
1214 _dbus_strerror (errno));
1218 if (!_dbus_close (fd, error))
1221 string_squash_nonprintable (&cmdline);
1223 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1226 _dbus_string_free (&cmdline);
1227 _dbus_string_free (&path);
1230 _DBUS_SET_OOM (error);
1232 _dbus_string_free (&cmdline);
1233 _dbus_string_free (&path);