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>
49 #include <sys/prctl.h>
50 #include <sys/capability.h>
52 #endif /* HAVE_LIBAUDIT */
54 #ifdef HAVE_SYS_SYSLIMITS_H
55 #include <sys/syslimits.h>
63 * @addtogroup DBusInternalsUtils
69 * Does the chdir, fork, setsid, etc. to become a daemon process.
71 * @param pidfile #NULL, or pidfile to create
72 * @param print_pid_pipe pipe to print daemon's pid to, or -1 for none
73 * @param error return location for errors
74 * @param keep_umask #TRUE to keep the original umask
75 * @returns #FALSE on failure
78 _dbus_become_daemon (const DBusString *pidfile,
79 DBusPipe *print_pid_pipe,
81 dbus_bool_t keep_umask)
87 _dbus_verbose ("Becoming a daemon...\n");
89 _dbus_verbose ("chdir to /\n");
92 dbus_set_error (error, DBUS_ERROR_FAILED,
93 "Could not chdir() to root directory");
97 _dbus_verbose ("forking...\n");
98 switch ((child_pid = fork ()))
101 _dbus_verbose ("fork failed\n");
102 dbus_set_error (error, _dbus_error_from_errno (errno),
103 "Failed to fork daemon: %s", _dbus_strerror (errno));
108 _dbus_verbose ("in child, closing std file descriptors\n");
110 /* silently ignore failures here, if someone
111 * doesn't have /dev/null we may as well try
115 dev_null_fd = open ("/dev/null", O_RDWR);
116 if (dev_null_fd >= 0)
118 dup2 (dev_null_fd, 0);
119 dup2 (dev_null_fd, 1);
121 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
122 if (s == NULL || *s == '\0')
123 dup2 (dev_null_fd, 2);
125 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
130 /* Get a predictable umask */
131 _dbus_verbose ("setting umask\n");
135 _dbus_verbose ("calling setsid()\n");
137 _dbus_assert_not_reached ("setsid() failed");
142 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
145 _dbus_verbose ("pid file or pipe write failed: %s\n",
147 kill (child_pid, SIGTERM);
151 _dbus_verbose ("parent exiting\n");
161 * Creates a file containing the process ID.
163 * @param filename the filename to write to
164 * @param pid our process ID
165 * @param error return location for errors
166 * @returns #FALSE on failure
169 _dbus_write_pid_file (const DBusString *filename,
173 const char *cfilename;
177 cfilename = _dbus_string_get_const_data (filename);
179 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
183 dbus_set_error (error, _dbus_error_from_errno (errno),
184 "Failed to open \"%s\": %s", cfilename,
185 _dbus_strerror (errno));
189 if ((f = fdopen (fd, "w")) == NULL)
191 dbus_set_error (error, _dbus_error_from_errno (errno),
192 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
193 _dbus_close (fd, NULL);
197 if (fprintf (f, "%lu\n", pid) < 0)
199 dbus_set_error (error, _dbus_error_from_errno (errno),
200 "Failed to write to \"%s\": %s", cfilename,
201 _dbus_strerror (errno));
207 if (fclose (f) == EOF)
209 dbus_set_error (error, _dbus_error_from_errno (errno),
210 "Failed to close \"%s\": %s", cfilename,
211 _dbus_strerror (errno));
219 * Writes the given pid_to_write to a pidfile (if non-NULL) and/or to a
220 * pipe (if non-NULL). Does nothing if pidfile and print_pid_pipe are both
223 * @param pidfile the file to write to or #NULL
224 * @param print_pid_pipe the pipe to write to or #NULL
225 * @param pid_to_write the pid to write out
226 * @param error error on failure
227 * @returns FALSE if error is set
230 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
231 DBusPipe *print_pid_pipe,
232 dbus_pid_t pid_to_write,
237 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
238 if (!_dbus_write_pid_file (pidfile,
242 _dbus_verbose ("pid file write failed\n");
243 _DBUS_ASSERT_ERROR_IS_SET(error);
249 _dbus_verbose ("No pid file requested\n");
252 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
257 _dbus_verbose ("writing our pid to pipe %d\n", print_pid_pipe->fd_or_handle);
259 if (!_dbus_string_init (&pid))
261 _DBUS_SET_OOM (error);
265 if (!_dbus_string_append_int (&pid, pid_to_write) ||
266 !_dbus_string_append (&pid, "\n"))
268 _dbus_string_free (&pid);
269 _DBUS_SET_OOM (error);
273 bytes = _dbus_string_get_length (&pid);
274 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
276 /* _dbus_pipe_write sets error only on failure, not short write */
277 if (error != NULL && !dbus_error_is_set(error))
279 dbus_set_error (error, DBUS_ERROR_FAILED,
280 "Printing message bus PID: did not write enough bytes\n");
282 _dbus_string_free (&pid);
286 _dbus_string_free (&pid);
290 _dbus_verbose ("No pid pipe to write to\n");
297 * Verify that after the fork we can successfully change to this user.
299 * @param user the username given in the daemon configuration
300 * @returns #TRUE if username is valid
303 _dbus_verify_daemon_user (const char *user)
307 _dbus_string_init_const (&u, user);
309 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
313 * Changes the user and group the bus is running as.
315 * @param user the user to become
316 * @param error return location for errors
317 * @returns #FALSE on failure
320 _dbus_change_to_daemon_user (const char *user,
327 dbus_bool_t we_were_root;
331 _dbus_string_init_const (&u, user);
333 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
335 dbus_set_error (error, DBUS_ERROR_FAILED,
336 "User '%s' does not appear to exist?",
342 we_were_root = _dbus_geteuid () == 0;
344 /* have a tmp set of caps that we use to transition to the usr/grp dbus should
345 * run as ... doesn't really help. But keeps people happy.
350 cap_value_t new_cap_list[] = { CAP_AUDIT_WRITE };
351 cap_value_t tmp_cap_list[] = { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
352 cap_t tmp_caps = cap_init();
354 if (!tmp_caps || !(new_caps = cap_init ()))
356 dbus_set_error (error, DBUS_ERROR_FAILED,
357 "Failed to initialize drop of capabilities: %s\n",
358 _dbus_strerror (errno));
366 /* assume these work... */
367 cap_set_flag (new_caps, CAP_PERMITTED, 1, new_cap_list, CAP_SET);
368 cap_set_flag (new_caps, CAP_EFFECTIVE, 1, new_cap_list, CAP_SET);
369 cap_set_flag (tmp_caps, CAP_PERMITTED, 3, tmp_cap_list, CAP_SET);
370 cap_set_flag (tmp_caps, CAP_EFFECTIVE, 3, tmp_cap_list, CAP_SET);
372 if (prctl (PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
374 dbus_set_error (error, _dbus_error_from_errno (errno),
375 "Failed to set keep-capabilities: %s\n",
376 _dbus_strerror (errno));
381 if (cap_set_proc (tmp_caps) == -1)
383 dbus_set_error (error, DBUS_ERROR_FAILED,
384 "Failed to drop capabilities: %s\n",
385 _dbus_strerror (errno));
391 #endif /* HAVE_LIBAUDIT */
393 /* setgroups() only works if we are a privileged process,
394 * so we don't return error on failure; the only possible
395 * failure is that we don't have perms to do it.
397 * not sure this is right, maybe if setuid()
398 * is going to work then setgroups() should also work.
400 if (setgroups (0, NULL) < 0)
401 _dbus_warn ("Failed to drop supplementary groups: %s\n",
402 _dbus_strerror (errno));
404 /* Set GID first, or the setuid may remove our permission
407 if (setgid (gid) < 0)
409 dbus_set_error (error, _dbus_error_from_errno (errno),
410 "Failed to set GID to %lu: %s", gid,
411 _dbus_strerror (errno));
415 if (setuid (uid) < 0)
417 dbus_set_error (error, _dbus_error_from_errno (errno),
418 "Failed to set UID to %lu: %s", uid,
419 _dbus_strerror (errno));
426 if (cap_set_proc (new_caps))
428 dbus_set_error (error, DBUS_ERROR_FAILED,
429 "Failed to drop capabilities: %s\n",
430 _dbus_strerror (errno));
435 /* should always work, if it did above */
436 if (prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0) == -1)
438 dbus_set_error (error, _dbus_error_from_errno (errno),
439 "Failed to unset keep-capabilities: %s\n",
440 _dbus_strerror (errno));
452 /* should always work, if it did above */
453 prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0);
462 _dbus_init_system_log (void)
464 openlog ("dbus", LOG_PID, LOG_DAEMON);
468 * Log an informative message. Intended for use primarily by
471 * @param msg a printf-style format string
472 * @param args arguments for the format string
475 _dbus_log_info (const char *msg, va_list args)
477 vsyslog (LOG_DAEMON|LOG_NOTICE, msg, args);
481 * Log a security-related message. Intended for use primarily by
484 * @param msg a printf-style format string
485 * @param args arguments for the format string
488 _dbus_log_security (const char *msg, va_list args)
490 vsyslog (LOG_AUTH|LOG_NOTICE, msg, args);
493 /** Installs a UNIX signal handler
495 * @param sig the signal to handle
496 * @param handler the handler
499 _dbus_set_signal_handler (int sig,
500 DBusSignalHandler handler)
502 struct sigaction act;
505 sigemptyset (&empty_mask);
506 act.sa_handler = handler;
507 act.sa_mask = empty_mask;
509 sigaction (sig, &act, NULL);
514 * Removes a directory; Directory must be empty
516 * @param filename directory filename
517 * @param error initialized error object
518 * @returns #TRUE on success
521 _dbus_delete_directory (const DBusString *filename,
524 const char *filename_c;
526 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
528 filename_c = _dbus_string_get_const_data (filename);
530 if (rmdir (filename_c) != 0)
532 dbus_set_error (error, DBUS_ERROR_FAILED,
533 "Failed to remove directory %s: %s\n",
534 filename_c, _dbus_strerror (errno));
541 /** Checks if a file exists
543 * @param file full path to the file
544 * @returns #TRUE if file exists
547 _dbus_file_exists (const char *file)
549 return (access (file, F_OK) == 0);
552 /** Checks if user is at the console
554 * @param username user to check
555 * @param error return location for errors
556 * @returns #TRUE is the user is at the consolei and there are no errors
559 _dbus_user_at_console (const char *username,
567 if (!_dbus_string_init (&f))
569 _DBUS_SET_OOM (error);
573 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
575 _DBUS_SET_OOM (error);
580 if (!_dbus_string_append (&f, username))
582 _DBUS_SET_OOM (error);
586 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
589 _dbus_string_free (&f);
596 * Checks whether the filename is an absolute path
598 * @param filename the filename
599 * @returns #TRUE if an absolute path
602 _dbus_path_is_absolute (const DBusString *filename)
604 if (_dbus_string_get_length (filename) > 0)
605 return _dbus_string_get_byte (filename, 0) == '/';
613 * @param filename the filename to stat
614 * @param statbuf the stat info to fill in
615 * @param error return location for error
616 * @returns #FALSE if error was set
619 _dbus_stat (const DBusString *filename,
623 const char *filename_c;
626 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
628 filename_c = _dbus_string_get_const_data (filename);
630 if (stat (filename_c, &sb) < 0)
632 dbus_set_error (error, _dbus_error_from_errno (errno),
633 "%s", _dbus_strerror (errno));
637 statbuf->mode = sb.st_mode;
638 statbuf->nlink = sb.st_nlink;
639 statbuf->uid = sb.st_uid;
640 statbuf->gid = sb.st_gid;
641 statbuf->size = sb.st_size;
642 statbuf->atime = sb.st_atime;
643 statbuf->mtime = sb.st_mtime;
644 statbuf->ctime = sb.st_ctime;
651 * Internals of directory iterator
655 DIR *d; /**< The DIR* from opendir() */
660 * Open a directory to iterate over.
662 * @param filename the directory name
663 * @param error exception return object or #NULL
664 * @returns new iterator, or #NULL on error
667 _dbus_directory_open (const DBusString *filename,
672 const char *filename_c;
674 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
676 filename_c = _dbus_string_get_const_data (filename);
678 d = opendir (filename_c);
681 dbus_set_error (error, _dbus_error_from_errno (errno),
682 "Failed to read directory \"%s\": %s",
684 _dbus_strerror (errno));
687 iter = dbus_new0 (DBusDirIter, 1);
691 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
692 "Could not allocate memory for directory iterator");
701 /* Calculate the required buffer size (in bytes) for directory
702 * entries read from the given directory handle. Return -1 if this
703 * this cannot be done.
705 * If you use autoconf, include fpathconf and dirfd in your
706 * AC_CHECK_FUNCS list. Otherwise use some other method to detect
707 * and use them where available.
710 dirent_buf_size(DIR * dirp, size_t *size)
713 # if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX)
714 # if defined(HAVE_DIRFD)
715 name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
716 # elif defined(HAVE_DDFD)
717 name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX);
719 name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX);
720 # endif /* HAVE_DIRFD */
722 # if defined(NAME_MAX)
727 # elif defined(MAXNAMELEN)
728 name_max = MAXNAMELEN;
730 # if defined(NAME_MAX)
733 # error "buffer size for readdir_r cannot be determined"
737 *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1;
745 * Get next file in the directory. Will not return "." or ".." on
746 * UNIX. If an error occurs, the contents of "filename" are
747 * undefined. The error is never set if the function succeeds.
749 * @param iter the iterator
750 * @param filename string to be set to the next file in the dir
751 * @param error return location for error
752 * @returns #TRUE if filename was filled in with a new filename
755 _dbus_directory_get_next_file (DBusDirIter *iter,
756 DBusString *filename,
759 struct dirent *d, *ent;
763 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
765 if (!dirent_buf_size (iter->d, &buf_size))
767 dbus_set_error (error, DBUS_ERROR_FAILED,
768 "Can't calculate buffer size when reading directory");
772 d = (struct dirent *)dbus_malloc (buf_size);
775 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
776 "No memory to read directory entry");
781 err = readdir_r (iter->d, d, &ent);
785 dbus_set_error (error,
786 _dbus_error_from_errno (err),
787 "%s", _dbus_strerror (err));
792 else if (ent->d_name[0] == '.' &&
793 (ent->d_name[1] == '\0' ||
794 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
798 _dbus_string_set_length (filename, 0);
799 if (!_dbus_string_append (filename, ent->d_name))
801 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
802 "No memory to read directory entry");
815 * Closes a directory iteration.
818 _dbus_directory_close (DBusDirIter *iter)
825 fill_user_info_from_group (struct group *g,
829 _dbus_assert (g->gr_name != NULL);
831 info->gid = g->gr_gid;
832 info->groupname = _dbus_strdup (g->gr_name);
834 /* info->members = dbus_strdupv (g->gr_mem) */
836 if (info->groupname == NULL)
838 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
846 fill_group_info (DBusGroupInfo *info,
848 const DBusString *groupname,
851 const char *group_c_str;
853 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
854 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
857 group_c_str = _dbus_string_get_const_data (groupname);
861 /* For now assuming that the getgrnam() and getgrgid() flavors
862 * always correspond to the pwnam flavors, if not we have
863 * to add more configure checks.
866 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
875 /* retrieve maximum needed size for buf */
876 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
878 /* sysconf actually returns a long, but everything else expects size_t,
879 * so just recast here.
880 * https://bugs.freedesktop.org/show_bug.cgi?id=17061
882 if ((long) buflen <= 0)
888 buf = dbus_malloc (buflen);
891 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
896 #ifdef HAVE_POSIX_GETPWNAM_R
898 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
901 result = getgrgid_r (gid, &g_str, buf, buflen,
904 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
906 #endif /* !HAVE_POSIX_GETPWNAM_R */
907 /* Try a bigger buffer if ERANGE was returned:
908 https://bugs.freedesktop.org/show_bug.cgi?id=16727
910 if (result == ERANGE && buflen < 512 * 1024)
921 if (result == 0 && g == &g_str)
923 b = fill_user_info_from_group (g, info, error);
929 dbus_set_error (error, _dbus_error_from_errno (errno),
930 "Group %s unknown or failed to look it up\n",
931 group_c_str ? group_c_str : "???");
936 #else /* ! HAVE_GETPWNAM_R */
938 /* I guess we're screwed on thread safety here */
941 g = getgrnam (group_c_str);
945 return fill_user_info_from_group (g, info, error);
949 dbus_set_error (error, _dbus_error_from_errno (errno),
950 "Group %s unknown or failed to look it up\n",
951 group_c_str ? group_c_str : "???");
955 #endif /* ! HAVE_GETPWNAM_R */
959 * Initializes the given DBusGroupInfo struct
960 * with information about the given group name.
962 * @param info the group info struct
963 * @param groupname name of group
964 * @param error the error return
965 * @returns #FALSE if error is set
968 _dbus_group_info_fill (DBusGroupInfo *info,
969 const DBusString *groupname,
972 return fill_group_info (info, DBUS_GID_UNSET,
978 * Initializes the given DBusGroupInfo struct
979 * with information about the given group ID.
981 * @param info the group info struct
982 * @param gid group ID
983 * @param error the error return
984 * @returns #FALSE if error is set
987 _dbus_group_info_fill_gid (DBusGroupInfo *info,
991 return fill_group_info (info, gid, NULL, error);
995 * Parse a UNIX user from the bus config file. On Windows, this should
996 * simply always fail (just return #FALSE).
998 * @param username the username text
999 * @param uid_p place to return the uid
1000 * @returns #TRUE on success
1003 _dbus_parse_unix_user_from_config (const DBusString *username,
1006 return _dbus_get_user_id (username, uid_p);
1011 * Parse a UNIX group from the bus config file. On Windows, this should
1012 * simply always fail (just return #FALSE).
1014 * @param groupname the groupname text
1015 * @param gid_p place to return the gid
1016 * @returns #TRUE on success
1019 _dbus_parse_unix_group_from_config (const DBusString *groupname,
1022 return _dbus_get_group_id (groupname, gid_p);
1026 * Gets all groups corresponding to the given UNIX user ID. On UNIX,
1027 * just calls _dbus_groups_from_uid(). On Windows, should always
1028 * fail since we don't know any UNIX groups.
1030 * @param uid the UID
1031 * @param group_ids return location for array of group IDs
1032 * @param n_group_ids return location for length of returned array
1033 * @returns #TRUE if the UID existed and we got some credentials
1036 _dbus_unix_groups_from_uid (dbus_uid_t uid,
1037 dbus_gid_t **group_ids,
1040 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
1044 * Checks to see if the UNIX user ID is at the console.
1045 * Should always fail on Windows (set the error to
1046 * #DBUS_ERROR_NOT_SUPPORTED).
1048 * @param uid UID of person to check
1049 * @param error return location for errors
1050 * @returns #TRUE if the UID is the same as the console user and there are no errors
1053 _dbus_unix_user_is_at_console (dbus_uid_t uid,
1056 return _dbus_is_console_user (uid, error);
1061 * Checks to see if the UNIX user ID matches the UID of
1062 * the process. Should always return #FALSE on Windows.
1064 * @param uid the UNIX user ID
1065 * @returns #TRUE if this uid owns the process.
1068 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
1070 return uid == _dbus_geteuid ();
1074 * Checks to see if the Windows user SID matches the owner of
1075 * the process. Should always return #FALSE on UNIX.
1077 * @param windows_sid the Windows user SID
1078 * @returns #TRUE if this user owns the process.
1081 _dbus_windows_user_is_process_owner (const char *windows_sid)
1086 /** @} */ /* End of DBusInternalsUtils functions */
1089 * @addtogroup DBusString
1094 * Get the directory name from a complete filename
1095 * @param filename the filename
1096 * @param dirname string to append directory name to
1097 * @returns #FALSE if no memory
1100 _dbus_string_get_dirname (const DBusString *filename,
1101 DBusString *dirname)
1105 _dbus_assert (filename != dirname);
1106 _dbus_assert (filename != NULL);
1107 _dbus_assert (dirname != NULL);
1109 /* Ignore any separators on the end */
1110 sep = _dbus_string_get_length (filename);
1112 return _dbus_string_append (dirname, "."); /* empty string passed in */
1114 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1117 _dbus_assert (sep >= 0);
1120 return _dbus_string_append (dirname, "/");
1122 /* Now find the previous separator */
1123 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
1125 return _dbus_string_append (dirname, ".");
1127 /* skip multiple separators */
1128 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
1131 _dbus_assert (sep >= 0);
1134 _dbus_string_get_byte (filename, 0) == '/')
1135 return _dbus_string_append (dirname, "/");
1137 return _dbus_string_copy_len (filename, 0, sep - 0,
1138 dirname, _dbus_string_get_length (dirname));
1140 /** @} */ /* DBusString stuff */
1143 string_squash_nonprintable (DBusString *str)
1148 buf = _dbus_string_get_data (str);
1149 len = _dbus_string_get_length (str);
1151 for (i = 0; i < len; i++)
1153 unsigned char c = (unsigned char) buf[i];
1156 else if (c < 0x20 || c > 127)
1162 * Get a printable string describing the command used to execute
1163 * the process with pid. This string should only be used for
1164 * informative purposes such as logging; it may not be trusted.
1166 * The command is guaranteed to be printable ASCII and no longer
1169 * @param pid Process id
1170 * @param str Append command to this string
1171 * @param max_len Maximum length of returned command
1172 * @param error return location for errors
1173 * @returns #FALSE on error
1176 _dbus_command_for_pid (unsigned long pid,
1181 /* This is all Linux-specific for now */
1186 if (!_dbus_string_init (&path))
1188 _DBUS_SET_OOM (error);
1192 if (!_dbus_string_init (&cmdline))
1194 _DBUS_SET_OOM (error);
1195 _dbus_string_free (&path);
1199 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
1202 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
1205 dbus_set_error (error,
1206 _dbus_error_from_errno (errno),
1207 "Failed to open \"%s\": %s",
1208 _dbus_string_get_const_data (&path),
1209 _dbus_strerror (errno));
1213 if (!_dbus_read (fd, &cmdline, max_len))
1215 dbus_set_error (error,
1216 _dbus_error_from_errno (errno),
1217 "Failed to read from \"%s\": %s",
1218 _dbus_string_get_const_data (&path),
1219 _dbus_strerror (errno));
1223 if (!_dbus_close (fd, error))
1226 string_squash_nonprintable (&cmdline);
1228 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
1231 _dbus_string_free (&cmdline);
1232 _dbus_string_free (&path);
1235 _DBUS_SET_OOM (error);
1237 _dbus_string_free (&cmdline);
1238 _dbus_string_free (&path);