00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <config.h>
00026 #include "dbus-sysdeps.h"
00027 #include "dbus-sysdeps-unix.h"
00028 #include "dbus-internals.h"
00029 #include "dbus-pipe.h"
00030 #include "dbus-protocol.h"
00031 #include "dbus-string.h"
00032 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00033 #include "dbus-userdb.h"
00034 #include "dbus-test.h"
00035
00036 #include <sys/types.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <signal.h>
00040 #include <unistd.h>
00041 #include <stdio.h>
00042 #include <errno.h>
00043 #include <fcntl.h>
00044 #include <sys/stat.h>
00045 #ifdef HAVE_SYS_RESOURCE_H
00046 #include <sys/resource.h>
00047 #endif
00048 #include <grp.h>
00049 #include <sys/socket.h>
00050 #include <dirent.h>
00051 #include <sys/un.h>
00052 #include <syslog.h>
00053
00054 #ifdef HAVE_SYS_SYSLIMITS_H
00055 #include <sys/syslimits.h>
00056 #endif
00057
00058 #ifndef O_BINARY
00059 #define O_BINARY 0
00060 #endif
00061
00077 dbus_bool_t
00078 _dbus_become_daemon (const DBusString *pidfile,
00079 DBusPipe *print_pid_pipe,
00080 DBusError *error,
00081 dbus_bool_t keep_umask)
00082 {
00083 const char *s;
00084 pid_t child_pid;
00085 int dev_null_fd;
00086
00087 _dbus_verbose ("Becoming a daemon...\n");
00088
00089 _dbus_verbose ("chdir to /\n");
00090 if (chdir ("/") < 0)
00091 {
00092 dbus_set_error (error, DBUS_ERROR_FAILED,
00093 "Could not chdir() to root directory");
00094 return FALSE;
00095 }
00096
00097 _dbus_verbose ("forking...\n");
00098 switch ((child_pid = fork ()))
00099 {
00100 case -1:
00101 _dbus_verbose ("fork failed\n");
00102 dbus_set_error (error, _dbus_error_from_errno (errno),
00103 "Failed to fork daemon: %s", _dbus_strerror (errno));
00104 return FALSE;
00105 break;
00106
00107 case 0:
00108 _dbus_verbose ("in child, closing std file descriptors\n");
00109
00110
00111
00112
00113
00114
00115 dev_null_fd = open ("/dev/null", O_RDWR);
00116 if (dev_null_fd >= 0)
00117 {
00118 dup2 (dev_null_fd, 0);
00119 dup2 (dev_null_fd, 1);
00120
00121 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
00122 if (s == NULL || *s == '\0')
00123 dup2 (dev_null_fd, 2);
00124 else
00125 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
00126 close (dev_null_fd);
00127 }
00128
00129 if (!keep_umask)
00130 {
00131
00132 _dbus_verbose ("setting umask\n");
00133 umask (022);
00134 }
00135
00136 _dbus_verbose ("calling setsid()\n");
00137 if (setsid () == -1)
00138 _dbus_assert_not_reached ("setsid() failed");
00139
00140 break;
00141
00142 default:
00143 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
00144 child_pid, error))
00145 {
00146 _dbus_verbose ("pid file or pipe write failed: %s\n",
00147 error->message);
00148 kill (child_pid, SIGTERM);
00149 return FALSE;
00150 }
00151
00152 _dbus_verbose ("parent exiting\n");
00153 _exit (0);
00154 break;
00155 }
00156
00157 return TRUE;
00158 }
00159
00160
00169 static dbus_bool_t
00170 _dbus_write_pid_file (const DBusString *filename,
00171 unsigned long pid,
00172 DBusError *error)
00173 {
00174 const char *cfilename;
00175 int fd;
00176 FILE *f;
00177
00178 cfilename = _dbus_string_get_const_data (filename);
00179
00180 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
00181
00182 if (fd < 0)
00183 {
00184 dbus_set_error (error, _dbus_error_from_errno (errno),
00185 "Failed to open \"%s\": %s", cfilename,
00186 _dbus_strerror (errno));
00187 return FALSE;
00188 }
00189
00190 if ((f = fdopen (fd, "w")) == NULL)
00191 {
00192 dbus_set_error (error, _dbus_error_from_errno (errno),
00193 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
00194 _dbus_close (fd, NULL);
00195 return FALSE;
00196 }
00197
00198 if (fprintf (f, "%lu\n", pid) < 0)
00199 {
00200 dbus_set_error (error, _dbus_error_from_errno (errno),
00201 "Failed to write to \"%s\": %s", cfilename,
00202 _dbus_strerror (errno));
00203
00204 fclose (f);
00205 return FALSE;
00206 }
00207
00208 if (fclose (f) == EOF)
00209 {
00210 dbus_set_error (error, _dbus_error_from_errno (errno),
00211 "Failed to close \"%s\": %s", cfilename,
00212 _dbus_strerror (errno));
00213 return FALSE;
00214 }
00215
00216 return TRUE;
00217 }
00218
00230 dbus_bool_t
00231 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00232 DBusPipe *print_pid_pipe,
00233 dbus_pid_t pid_to_write,
00234 DBusError *error)
00235 {
00236 if (pidfile)
00237 {
00238 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
00239 if (!_dbus_write_pid_file (pidfile,
00240 pid_to_write,
00241 error))
00242 {
00243 _dbus_verbose ("pid file write failed\n");
00244 _DBUS_ASSERT_ERROR_IS_SET(error);
00245 return FALSE;
00246 }
00247 }
00248 else
00249 {
00250 _dbus_verbose ("No pid file requested\n");
00251 }
00252
00253 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
00254 {
00255 DBusString pid;
00256 int bytes;
00257
00258 _dbus_verbose ("writing our pid to pipe %d\n",
00259 print_pid_pipe->fd);
00260
00261 if (!_dbus_string_init (&pid))
00262 {
00263 _DBUS_SET_OOM (error);
00264 return FALSE;
00265 }
00266
00267 if (!_dbus_string_append_int (&pid, pid_to_write) ||
00268 !_dbus_string_append (&pid, "\n"))
00269 {
00270 _dbus_string_free (&pid);
00271 _DBUS_SET_OOM (error);
00272 return FALSE;
00273 }
00274
00275 bytes = _dbus_string_get_length (&pid);
00276 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
00277 {
00278
00279 if (error != NULL && !dbus_error_is_set(error))
00280 {
00281 dbus_set_error (error, DBUS_ERROR_FAILED,
00282 "Printing message bus PID: did not write enough bytes\n");
00283 }
00284 _dbus_string_free (&pid);
00285 return FALSE;
00286 }
00287
00288 _dbus_string_free (&pid);
00289 }
00290 else
00291 {
00292 _dbus_verbose ("No pid pipe to write to\n");
00293 }
00294
00295 return TRUE;
00296 }
00297
00304 dbus_bool_t
00305 _dbus_verify_daemon_user (const char *user)
00306 {
00307 DBusString u;
00308
00309 _dbus_string_init_const (&u, user);
00310
00311 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
00312 }
00313
00314
00315
00316 #ifndef HAVE_LIBAUDIT
00317
00324 dbus_bool_t
00325 _dbus_change_to_daemon_user (const char *user,
00326 DBusError *error)
00327 {
00328 dbus_uid_t uid;
00329 dbus_gid_t gid;
00330 DBusString u;
00331
00332 _dbus_string_init_const (&u, user);
00333
00334 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
00335 {
00336 dbus_set_error (error, DBUS_ERROR_FAILED,
00337 "User '%s' does not appear to exist?",
00338 user);
00339 return FALSE;
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349 if (setgroups (0, NULL) < 0)
00350 _dbus_warn ("Failed to drop supplementary groups: %s\n",
00351 _dbus_strerror (errno));
00352
00353
00354
00355
00356 if (setgid (gid) < 0)
00357 {
00358 dbus_set_error (error, _dbus_error_from_errno (errno),
00359 "Failed to set GID to %lu: %s", gid,
00360 _dbus_strerror (errno));
00361 return FALSE;
00362 }
00363
00364 if (setuid (uid) < 0)
00365 {
00366 dbus_set_error (error, _dbus_error_from_errno (errno),
00367 "Failed to set UID to %lu: %s", uid,
00368 _dbus_strerror (errno));
00369 return FALSE;
00370 }
00371
00372 return TRUE;
00373 }
00374 #endif
00375
00376
00387 void
00388 _dbus_request_file_descriptor_limit (unsigned int limit)
00389 {
00390 #ifdef HAVE_SETRLIMIT
00391 struct rlimit lim;
00392 struct rlimit target_lim;
00393
00394
00395
00396
00397
00398
00399
00400 if (getuid () != 0)
00401 return;
00402
00403 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
00404 return;
00405
00406 if (lim.rlim_cur >= limit)
00407 return;
00408
00409
00410
00411
00412 target_lim.rlim_cur = target_lim.rlim_max = limit;
00413
00414
00415
00416
00417
00418
00419
00420
00421 setrlimit (RLIMIT_NOFILE, &target_lim);
00422 #endif
00423 }
00424
00425 void
00426 _dbus_init_system_log (void)
00427 {
00428 #if HAVE_DECL_LOG_PERROR
00429 openlog ("dbus", LOG_PID | LOG_PERROR, LOG_DAEMON);
00430 #else
00431 openlog ("dbus", LOG_PID, LOG_DAEMON);
00432 #endif
00433 }
00434
00443 void
00444 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
00445 {
00446 va_list args;
00447
00448 va_start (args, msg);
00449
00450 _dbus_system_logv (severity, msg, args);
00451
00452 va_end (args);
00453 }
00454
00465 void
00466 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
00467 {
00468 int flags;
00469 switch (severity)
00470 {
00471 case DBUS_SYSTEM_LOG_INFO:
00472 flags = LOG_DAEMON | LOG_NOTICE;
00473 break;
00474 case DBUS_SYSTEM_LOG_SECURITY:
00475 flags = LOG_AUTH | LOG_NOTICE;
00476 break;
00477 case DBUS_SYSTEM_LOG_FATAL:
00478 flags = LOG_DAEMON|LOG_CRIT;
00479 break;
00480 default:
00481 return;
00482 }
00483
00484 #ifndef HAVE_DECL_LOG_PERROR
00485 {
00486
00487 va_list tmp;
00488
00489 DBUS_VA_COPY (tmp, args);
00490 fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ());
00491 vfprintf (stderr, msg, tmp);
00492 fputc ('\n', stderr);
00493 va_end (tmp);
00494 }
00495 #endif
00496
00497 vsyslog (flags, msg, args);
00498
00499 if (severity == DBUS_SYSTEM_LOG_FATAL)
00500 exit (1);
00501 }
00502
00508 void
00509 _dbus_set_signal_handler (int sig,
00510 DBusSignalHandler handler)
00511 {
00512 struct sigaction act;
00513 sigset_t empty_mask;
00514
00515 sigemptyset (&empty_mask);
00516 act.sa_handler = handler;
00517 act.sa_mask = empty_mask;
00518 act.sa_flags = 0;
00519 sigaction (sig, &act, NULL);
00520 }
00521
00527 dbus_bool_t
00528 _dbus_file_exists (const char *file)
00529 {
00530 return (access (file, F_OK) == 0);
00531 }
00532
00539 dbus_bool_t
00540 _dbus_user_at_console (const char *username,
00541 DBusError *error)
00542 {
00543
00544 DBusString u, f;
00545 dbus_bool_t result;
00546
00547 result = FALSE;
00548 if (!_dbus_string_init (&f))
00549 {
00550 _DBUS_SET_OOM (error);
00551 return FALSE;
00552 }
00553
00554 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00555 {
00556 _DBUS_SET_OOM (error);
00557 goto out;
00558 }
00559
00560 _dbus_string_init_const (&u, username);
00561
00562 if (!_dbus_concat_dir_and_file (&f, &u))
00563 {
00564 _DBUS_SET_OOM (error);
00565 goto out;
00566 }
00567
00568 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00569
00570 out:
00571 _dbus_string_free (&f);
00572
00573 return result;
00574 }
00575
00576
00583 dbus_bool_t
00584 _dbus_path_is_absolute (const DBusString *filename)
00585 {
00586 if (_dbus_string_get_length (filename) > 0)
00587 return _dbus_string_get_byte (filename, 0) == '/';
00588 else
00589 return FALSE;
00590 }
00591
00600 dbus_bool_t
00601 _dbus_stat (const DBusString *filename,
00602 DBusStat *statbuf,
00603 DBusError *error)
00604 {
00605 const char *filename_c;
00606 struct stat sb;
00607
00608 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00609
00610 filename_c = _dbus_string_get_const_data (filename);
00611
00612 if (stat (filename_c, &sb) < 0)
00613 {
00614 dbus_set_error (error, _dbus_error_from_errno (errno),
00615 "%s", _dbus_strerror (errno));
00616 return FALSE;
00617 }
00618
00619 statbuf->mode = sb.st_mode;
00620 statbuf->nlink = sb.st_nlink;
00621 statbuf->uid = sb.st_uid;
00622 statbuf->gid = sb.st_gid;
00623 statbuf->size = sb.st_size;
00624 statbuf->atime = sb.st_atime;
00625 statbuf->mtime = sb.st_mtime;
00626 statbuf->ctime = sb.st_ctime;
00627
00628 return TRUE;
00629 }
00630
00631
00635 struct DBusDirIter
00636 {
00637 DIR *d;
00639 };
00640
00648 DBusDirIter*
00649 _dbus_directory_open (const DBusString *filename,
00650 DBusError *error)
00651 {
00652 DIR *d;
00653 DBusDirIter *iter;
00654 const char *filename_c;
00655
00656 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00657
00658 filename_c = _dbus_string_get_const_data (filename);
00659
00660 d = opendir (filename_c);
00661 if (d == NULL)
00662 {
00663 dbus_set_error (error, _dbus_error_from_errno (errno),
00664 "Failed to read directory \"%s\": %s",
00665 filename_c,
00666 _dbus_strerror (errno));
00667 return NULL;
00668 }
00669 iter = dbus_new0 (DBusDirIter, 1);
00670 if (iter == NULL)
00671 {
00672 closedir (d);
00673 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00674 "Could not allocate memory for directory iterator");
00675 return NULL;
00676 }
00677
00678 iter->d = d;
00679
00680 return iter;
00681 }
00682
00696 dbus_bool_t
00697 _dbus_directory_get_next_file (DBusDirIter *iter,
00698 DBusString *filename,
00699 DBusError *error)
00700 {
00701 struct dirent *ent;
00702 int err;
00703
00704 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00705
00706 again:
00707 errno = 0;
00708 ent = readdir (iter->d);
00709
00710 if (!ent)
00711 {
00712 err = errno;
00713
00714 if (err != 0)
00715 dbus_set_error (error,
00716 _dbus_error_from_errno (err),
00717 "%s", _dbus_strerror (err));
00718
00719 return FALSE;
00720 }
00721 else if (ent->d_name[0] == '.' &&
00722 (ent->d_name[1] == '\0' ||
00723 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00724 goto again;
00725 else
00726 {
00727 _dbus_string_set_length (filename, 0);
00728 if (!_dbus_string_append (filename, ent->d_name))
00729 {
00730 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00731 "No memory to read directory entry");
00732 return FALSE;
00733 }
00734 else
00735 {
00736 return TRUE;
00737 }
00738 }
00739 }
00740
00744 void
00745 _dbus_directory_close (DBusDirIter *iter)
00746 {
00747 closedir (iter->d);
00748 dbus_free (iter);
00749 }
00750
00751 static dbus_bool_t
00752 fill_user_info_from_group (struct group *g,
00753 DBusGroupInfo *info,
00754 DBusError *error)
00755 {
00756 _dbus_assert (g->gr_name != NULL);
00757
00758 info->gid = g->gr_gid;
00759 info->groupname = _dbus_strdup (g->gr_name);
00760
00761
00762
00763 if (info->groupname == NULL)
00764 {
00765 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00766 return FALSE;
00767 }
00768
00769 return TRUE;
00770 }
00771
00772 static dbus_bool_t
00773 fill_group_info (DBusGroupInfo *info,
00774 dbus_gid_t gid,
00775 const DBusString *groupname,
00776 DBusError *error)
00777 {
00778 const char *group_c_str;
00779
00780 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00781 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00782
00783 if (groupname)
00784 group_c_str = _dbus_string_get_const_data (groupname);
00785 else
00786 group_c_str = NULL;
00787
00788
00789
00790
00791
00792
00793 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00794 {
00795 struct group *g;
00796 int result;
00797 size_t buflen;
00798 char *buf;
00799 struct group g_str;
00800 dbus_bool_t b;
00801
00802
00803 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00804
00805
00806
00807
00808
00809 if ((long) buflen <= 0)
00810 buflen = 1024;
00811
00812 result = -1;
00813 while (1)
00814 {
00815 buf = dbus_malloc (buflen);
00816 if (buf == NULL)
00817 {
00818 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00819 return FALSE;
00820 }
00821
00822 g = NULL;
00823 #ifdef HAVE_POSIX_GETPWNAM_R
00824 if (group_c_str)
00825 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00826 &g);
00827 else
00828 result = getgrgid_r (gid, &g_str, buf, buflen,
00829 &g);
00830 #else
00831 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00832 result = 0;
00833 #endif
00834
00835
00836
00837 if (result == ERANGE && buflen < 512 * 1024)
00838 {
00839 dbus_free (buf);
00840 buflen *= 2;
00841 }
00842 else
00843 {
00844 break;
00845 }
00846 }
00847
00848 if (result == 0 && g == &g_str)
00849 {
00850 b = fill_user_info_from_group (g, info, error);
00851 dbus_free (buf);
00852 return b;
00853 }
00854 else
00855 {
00856 dbus_set_error (error, _dbus_error_from_errno (errno),
00857 "Group %s unknown or failed to look it up\n",
00858 group_c_str ? group_c_str : "???");
00859 dbus_free (buf);
00860 return FALSE;
00861 }
00862 }
00863 #else
00864 {
00865
00866 struct group *g;
00867
00868 g = getgrnam (group_c_str);
00869
00870 if (g != NULL)
00871 {
00872 return fill_user_info_from_group (g, info, error);
00873 }
00874 else
00875 {
00876 dbus_set_error (error, _dbus_error_from_errno (errno),
00877 "Group %s unknown or failed to look it up\n",
00878 group_c_str ? group_c_str : "???");
00879 return FALSE;
00880 }
00881 }
00882 #endif
00883 }
00884
00894 dbus_bool_t
00895 _dbus_group_info_fill (DBusGroupInfo *info,
00896 const DBusString *groupname,
00897 DBusError *error)
00898 {
00899 return fill_group_info (info, DBUS_GID_UNSET,
00900 groupname, error);
00901
00902 }
00903
00913 dbus_bool_t
00914 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00915 dbus_gid_t gid,
00916 DBusError *error)
00917 {
00918 return fill_group_info (info, gid, NULL, error);
00919 }
00920
00929 dbus_bool_t
00930 _dbus_parse_unix_user_from_config (const DBusString *username,
00931 dbus_uid_t *uid_p)
00932 {
00933 return _dbus_get_user_id (username, uid_p);
00934
00935 }
00936
00945 dbus_bool_t
00946 _dbus_parse_unix_group_from_config (const DBusString *groupname,
00947 dbus_gid_t *gid_p)
00948 {
00949 return _dbus_get_group_id (groupname, gid_p);
00950 }
00951
00962 dbus_bool_t
00963 _dbus_unix_groups_from_uid (dbus_uid_t uid,
00964 dbus_gid_t **group_ids,
00965 int *n_group_ids)
00966 {
00967 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00968 }
00969
00979 dbus_bool_t
00980 _dbus_unix_user_is_at_console (dbus_uid_t uid,
00981 DBusError *error)
00982 {
00983 return _dbus_is_console_user (uid, error);
00984
00985 }
00986
00994 dbus_bool_t
00995 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00996 {
00997 return uid == _dbus_geteuid ();
00998 }
00999
01007 dbus_bool_t
01008 _dbus_windows_user_is_process_owner (const char *windows_sid)
01009 {
01010 return FALSE;
01011 }
01012
01014
01026 dbus_bool_t
01027 _dbus_string_get_dirname (const DBusString *filename,
01028 DBusString *dirname)
01029 {
01030 int sep;
01031
01032 _dbus_assert (filename != dirname);
01033 _dbus_assert (filename != NULL);
01034 _dbus_assert (dirname != NULL);
01035
01036
01037 sep = _dbus_string_get_length (filename);
01038 if (sep == 0)
01039 return _dbus_string_append (dirname, ".");
01040
01041 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01042 --sep;
01043
01044 _dbus_assert (sep >= 0);
01045
01046 if (sep == 0)
01047 return _dbus_string_append (dirname, "/");
01048
01049
01050 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01051 if (sep < 0)
01052 return _dbus_string_append (dirname, ".");
01053
01054
01055 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01056 --sep;
01057
01058 _dbus_assert (sep >= 0);
01059
01060 if (sep == 0 &&
01061 _dbus_string_get_byte (filename, 0) == '/')
01062 return _dbus_string_append (dirname, "/");
01063 else
01064 return _dbus_string_copy_len (filename, 0, sep - 0,
01065 dirname, _dbus_string_get_length (dirname));
01066 }
01068
01069 static void
01070 string_squash_nonprintable (DBusString *str)
01071 {
01072 unsigned char *buf;
01073 int i, len;
01074
01075 buf = _dbus_string_get_data (str);
01076 len = _dbus_string_get_length (str);
01077
01078 for (i = 0; i < len; i++)
01079 {
01080 unsigned char c = (unsigned char) buf[i];
01081 if (c == '\0')
01082 buf[i] = ' ';
01083 else if (c < 0x20 || c > 127)
01084 buf[i] = '?';
01085 }
01086 }
01087
01102 dbus_bool_t
01103 _dbus_command_for_pid (unsigned long pid,
01104 DBusString *str,
01105 int max_len,
01106 DBusError *error)
01107 {
01108
01109 DBusString path;
01110 DBusString cmdline;
01111 int fd;
01112
01113 if (!_dbus_string_init (&path))
01114 {
01115 _DBUS_SET_OOM (error);
01116 return FALSE;
01117 }
01118
01119 if (!_dbus_string_init (&cmdline))
01120 {
01121 _DBUS_SET_OOM (error);
01122 _dbus_string_free (&path);
01123 return FALSE;
01124 }
01125
01126 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01127 goto oom;
01128
01129 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01130 if (fd < 0)
01131 {
01132 dbus_set_error (error,
01133 _dbus_error_from_errno (errno),
01134 "Failed to open \"%s\": %s",
01135 _dbus_string_get_const_data (&path),
01136 _dbus_strerror (errno));
01137 goto fail;
01138 }
01139
01140 if (!_dbus_read (fd, &cmdline, max_len))
01141 {
01142 dbus_set_error (error,
01143 _dbus_error_from_errno (errno),
01144 "Failed to read from \"%s\": %s",
01145 _dbus_string_get_const_data (&path),
01146 _dbus_strerror (errno));
01147 goto fail;
01148 }
01149
01150 if (!_dbus_close (fd, error))
01151 goto fail;
01152
01153 string_squash_nonprintable (&cmdline);
01154
01155 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01156 goto oom;
01157
01158 _dbus_string_free (&cmdline);
01159 _dbus_string_free (&path);
01160 return TRUE;
01161 oom:
01162 _DBUS_SET_OOM (error);
01163 fail:
01164 _dbus_string_free (&cmdline);
01165 _dbus_string_free (&path);
01166 return FALSE;
01167 }