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 }
00127
00128 if (!keep_umask)
00129 {
00130
00131 _dbus_verbose ("setting umask\n");
00132 umask (022);
00133 }
00134
00135 _dbus_verbose ("calling setsid()\n");
00136 if (setsid () == -1)
00137 _dbus_assert_not_reached ("setsid() failed");
00138
00139 break;
00140
00141 default:
00142 if (!_dbus_write_pid_to_file_and_pipe (pidfile, print_pid_pipe,
00143 child_pid, error))
00144 {
00145 _dbus_verbose ("pid file or pipe write failed: %s\n",
00146 error->message);
00147 kill (child_pid, SIGTERM);
00148 return FALSE;
00149 }
00150
00151 _dbus_verbose ("parent exiting\n");
00152 _exit (0);
00153 break;
00154 }
00155
00156 return TRUE;
00157 }
00158
00159
00168 static dbus_bool_t
00169 _dbus_write_pid_file (const DBusString *filename,
00170 unsigned long pid,
00171 DBusError *error)
00172 {
00173 const char *cfilename;
00174 int fd;
00175 FILE *f;
00176
00177 cfilename = _dbus_string_get_const_data (filename);
00178
00179 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
00180
00181 if (fd < 0)
00182 {
00183 dbus_set_error (error, _dbus_error_from_errno (errno),
00184 "Failed to open \"%s\": %s", cfilename,
00185 _dbus_strerror (errno));
00186 return FALSE;
00187 }
00188
00189 if ((f = fdopen (fd, "w")) == NULL)
00190 {
00191 dbus_set_error (error, _dbus_error_from_errno (errno),
00192 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
00193 _dbus_close (fd, NULL);
00194 return FALSE;
00195 }
00196
00197 if (fprintf (f, "%lu\n", pid) < 0)
00198 {
00199 dbus_set_error (error, _dbus_error_from_errno (errno),
00200 "Failed to write to \"%s\": %s", cfilename,
00201 _dbus_strerror (errno));
00202
00203 fclose (f);
00204 return FALSE;
00205 }
00206
00207 if (fclose (f) == EOF)
00208 {
00209 dbus_set_error (error, _dbus_error_from_errno (errno),
00210 "Failed to close \"%s\": %s", cfilename,
00211 _dbus_strerror (errno));
00212 return FALSE;
00213 }
00214
00215 return TRUE;
00216 }
00217
00229 dbus_bool_t
00230 _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile,
00231 DBusPipe *print_pid_pipe,
00232 dbus_pid_t pid_to_write,
00233 DBusError *error)
00234 {
00235 if (pidfile)
00236 {
00237 _dbus_verbose ("writing pid file %s\n", _dbus_string_get_const_data (pidfile));
00238 if (!_dbus_write_pid_file (pidfile,
00239 pid_to_write,
00240 error))
00241 {
00242 _dbus_verbose ("pid file write failed\n");
00243 _DBUS_ASSERT_ERROR_IS_SET(error);
00244 return FALSE;
00245 }
00246 }
00247 else
00248 {
00249 _dbus_verbose ("No pid file requested\n");
00250 }
00251
00252 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
00253 {
00254 DBusString pid;
00255 int bytes;
00256
00257 _dbus_verbose ("writing our pid to pipe %"PRIuPTR"\n",
00258 print_pid_pipe->fd_or_handle);
00259
00260 if (!_dbus_string_init (&pid))
00261 {
00262 _DBUS_SET_OOM (error);
00263 return FALSE;
00264 }
00265
00266 if (!_dbus_string_append_int (&pid, pid_to_write) ||
00267 !_dbus_string_append (&pid, "\n"))
00268 {
00269 _dbus_string_free (&pid);
00270 _DBUS_SET_OOM (error);
00271 return FALSE;
00272 }
00273
00274 bytes = _dbus_string_get_length (&pid);
00275 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
00276 {
00277
00278 if (error != NULL && !dbus_error_is_set(error))
00279 {
00280 dbus_set_error (error, DBUS_ERROR_FAILED,
00281 "Printing message bus PID: did not write enough bytes\n");
00282 }
00283 _dbus_string_free (&pid);
00284 return FALSE;
00285 }
00286
00287 _dbus_string_free (&pid);
00288 }
00289 else
00290 {
00291 _dbus_verbose ("No pid pipe to write to\n");
00292 }
00293
00294 return TRUE;
00295 }
00296
00303 dbus_bool_t
00304 _dbus_verify_daemon_user (const char *user)
00305 {
00306 DBusString u;
00307
00308 _dbus_string_init_const (&u, user);
00309
00310 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
00311 }
00312
00313
00314
00315 #ifndef HAVE_LIBAUDIT
00316
00323 dbus_bool_t
00324 _dbus_change_to_daemon_user (const char *user,
00325 DBusError *error)
00326 {
00327 dbus_uid_t uid;
00328 dbus_gid_t gid;
00329 DBusString u;
00330
00331 _dbus_string_init_const (&u, user);
00332
00333 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
00334 {
00335 dbus_set_error (error, DBUS_ERROR_FAILED,
00336 "User '%s' does not appear to exist?",
00337 user);
00338 return FALSE;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348 if (setgroups (0, NULL) < 0)
00349 _dbus_warn ("Failed to drop supplementary groups: %s\n",
00350 _dbus_strerror (errno));
00351
00352
00353
00354
00355 if (setgid (gid) < 0)
00356 {
00357 dbus_set_error (error, _dbus_error_from_errno (errno),
00358 "Failed to set GID to %lu: %s", gid,
00359 _dbus_strerror (errno));
00360 return FALSE;
00361 }
00362
00363 if (setuid (uid) < 0)
00364 {
00365 dbus_set_error (error, _dbus_error_from_errno (errno),
00366 "Failed to set UID to %lu: %s", uid,
00367 _dbus_strerror (errno));
00368 return FALSE;
00369 }
00370
00371 return TRUE;
00372 }
00373 #endif
00374
00375
00386 void
00387 _dbus_request_file_descriptor_limit (unsigned int limit)
00388 {
00389 #ifdef HAVE_SETRLIMIT
00390 struct rlimit lim;
00391 struct rlimit target_lim;
00392 unsigned int current_limit;
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 #ifdef 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 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
00561 if (!_dbus_string_append (&f, username))
00562 {
00563 _DBUS_SET_OOM (error);
00564 goto out;
00565 }
00566
00567 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00568
00569 out:
00570 _dbus_string_free (&f);
00571
00572 return result;
00573 }
00574
00575
00582 dbus_bool_t
00583 _dbus_path_is_absolute (const DBusString *filename)
00584 {
00585 if (_dbus_string_get_length (filename) > 0)
00586 return _dbus_string_get_byte (filename, 0) == '/';
00587 else
00588 return FALSE;
00589 }
00590
00599 dbus_bool_t
00600 _dbus_stat (const DBusString *filename,
00601 DBusStat *statbuf,
00602 DBusError *error)
00603 {
00604 const char *filename_c;
00605 struct stat sb;
00606
00607 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00608
00609 filename_c = _dbus_string_get_const_data (filename);
00610
00611 if (stat (filename_c, &sb) < 0)
00612 {
00613 dbus_set_error (error, _dbus_error_from_errno (errno),
00614 "%s", _dbus_strerror (errno));
00615 return FALSE;
00616 }
00617
00618 statbuf->mode = sb.st_mode;
00619 statbuf->nlink = sb.st_nlink;
00620 statbuf->uid = sb.st_uid;
00621 statbuf->gid = sb.st_gid;
00622 statbuf->size = sb.st_size;
00623 statbuf->atime = sb.st_atime;
00624 statbuf->mtime = sb.st_mtime;
00625 statbuf->ctime = sb.st_ctime;
00626
00627 return TRUE;
00628 }
00629
00630
00634 struct DBusDirIter
00635 {
00636 DIR *d;
00638 };
00639
00647 DBusDirIter*
00648 _dbus_directory_open (const DBusString *filename,
00649 DBusError *error)
00650 {
00651 DIR *d;
00652 DBusDirIter *iter;
00653 const char *filename_c;
00654
00655 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00656
00657 filename_c = _dbus_string_get_const_data (filename);
00658
00659 d = opendir (filename_c);
00660 if (d == NULL)
00661 {
00662 dbus_set_error (error, _dbus_error_from_errno (errno),
00663 "Failed to read directory \"%s\": %s",
00664 filename_c,
00665 _dbus_strerror (errno));
00666 return NULL;
00667 }
00668 iter = dbus_new0 (DBusDirIter, 1);
00669 if (iter == NULL)
00670 {
00671 closedir (d);
00672 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00673 "Could not allocate memory for directory iterator");
00674 return NULL;
00675 }
00676
00677 iter->d = d;
00678
00679 return iter;
00680 }
00681
00695 dbus_bool_t
00696 _dbus_directory_get_next_file (DBusDirIter *iter,
00697 DBusString *filename,
00698 DBusError *error)
00699 {
00700 struct dirent *ent;
00701 int err;
00702
00703 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00704
00705 again:
00706 errno = 0;
00707 ent = readdir (iter->d);
00708
00709 if (!ent)
00710 {
00711 err = errno;
00712
00713 if (err != 0)
00714 dbus_set_error (error,
00715 _dbus_error_from_errno (err),
00716 "%s", _dbus_strerror (err));
00717
00718 return FALSE;
00719 }
00720 else if (ent->d_name[0] == '.' &&
00721 (ent->d_name[1] == '\0' ||
00722 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00723 goto again;
00724 else
00725 {
00726 _dbus_string_set_length (filename, 0);
00727 if (!_dbus_string_append (filename, ent->d_name))
00728 {
00729 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00730 "No memory to read directory entry");
00731 return FALSE;
00732 }
00733 else
00734 {
00735 return TRUE;
00736 }
00737 }
00738 }
00739
00743 void
00744 _dbus_directory_close (DBusDirIter *iter)
00745 {
00746 closedir (iter->d);
00747 dbus_free (iter);
00748 }
00749
00750 static dbus_bool_t
00751 fill_user_info_from_group (struct group *g,
00752 DBusGroupInfo *info,
00753 DBusError *error)
00754 {
00755 _dbus_assert (g->gr_name != NULL);
00756
00757 info->gid = g->gr_gid;
00758 info->groupname = _dbus_strdup (g->gr_name);
00759
00760
00761
00762 if (info->groupname == NULL)
00763 {
00764 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00765 return FALSE;
00766 }
00767
00768 return TRUE;
00769 }
00770
00771 static dbus_bool_t
00772 fill_group_info (DBusGroupInfo *info,
00773 dbus_gid_t gid,
00774 const DBusString *groupname,
00775 DBusError *error)
00776 {
00777 const char *group_c_str;
00778
00779 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00780 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00781
00782 if (groupname)
00783 group_c_str = _dbus_string_get_const_data (groupname);
00784 else
00785 group_c_str = NULL;
00786
00787
00788
00789
00790
00791
00792 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00793 {
00794 struct group *g;
00795 int result;
00796 size_t buflen;
00797 char *buf;
00798 struct group g_str;
00799 dbus_bool_t b;
00800
00801
00802 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00803
00804
00805
00806
00807
00808 if ((long) buflen <= 0)
00809 buflen = 1024;
00810
00811 result = -1;
00812 while (1)
00813 {
00814 buf = dbus_malloc (buflen);
00815 if (buf == NULL)
00816 {
00817 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00818 return FALSE;
00819 }
00820
00821 g = NULL;
00822 #ifdef HAVE_POSIX_GETPWNAM_R
00823 if (group_c_str)
00824 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00825 &g);
00826 else
00827 result = getgrgid_r (gid, &g_str, buf, buflen,
00828 &g);
00829 #else
00830 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00831 result = 0;
00832 #endif
00833
00834
00835
00836 if (result == ERANGE && buflen < 512 * 1024)
00837 {
00838 dbus_free (buf);
00839 buflen *= 2;
00840 }
00841 else
00842 {
00843 break;
00844 }
00845 }
00846
00847 if (result == 0 && g == &g_str)
00848 {
00849 b = fill_user_info_from_group (g, info, error);
00850 dbus_free (buf);
00851 return b;
00852 }
00853 else
00854 {
00855 dbus_set_error (error, _dbus_error_from_errno (errno),
00856 "Group %s unknown or failed to look it up\n",
00857 group_c_str ? group_c_str : "???");
00858 dbus_free (buf);
00859 return FALSE;
00860 }
00861 }
00862 #else
00863 {
00864
00865 struct group *g;
00866
00867 g = getgrnam (group_c_str);
00868
00869 if (g != NULL)
00870 {
00871 return fill_user_info_from_group (g, info, error);
00872 }
00873 else
00874 {
00875 dbus_set_error (error, _dbus_error_from_errno (errno),
00876 "Group %s unknown or failed to look it up\n",
00877 group_c_str ? group_c_str : "???");
00878 return FALSE;
00879 }
00880 }
00881 #endif
00882 }
00883
00893 dbus_bool_t
00894 _dbus_group_info_fill (DBusGroupInfo *info,
00895 const DBusString *groupname,
00896 DBusError *error)
00897 {
00898 return fill_group_info (info, DBUS_GID_UNSET,
00899 groupname, error);
00900
00901 }
00902
00912 dbus_bool_t
00913 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00914 dbus_gid_t gid,
00915 DBusError *error)
00916 {
00917 return fill_group_info (info, gid, NULL, error);
00918 }
00919
00928 dbus_bool_t
00929 _dbus_parse_unix_user_from_config (const DBusString *username,
00930 dbus_uid_t *uid_p)
00931 {
00932 return _dbus_get_user_id (username, uid_p);
00933
00934 }
00935
00944 dbus_bool_t
00945 _dbus_parse_unix_group_from_config (const DBusString *groupname,
00946 dbus_gid_t *gid_p)
00947 {
00948 return _dbus_get_group_id (groupname, gid_p);
00949 }
00950
00961 dbus_bool_t
00962 _dbus_unix_groups_from_uid (dbus_uid_t uid,
00963 dbus_gid_t **group_ids,
00964 int *n_group_ids)
00965 {
00966 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00967 }
00968
00978 dbus_bool_t
00979 _dbus_unix_user_is_at_console (dbus_uid_t uid,
00980 DBusError *error)
00981 {
00982 return _dbus_is_console_user (uid, error);
00983
00984 }
00985
00993 dbus_bool_t
00994 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00995 {
00996 return uid == _dbus_geteuid ();
00997 }
00998
01006 dbus_bool_t
01007 _dbus_windows_user_is_process_owner (const char *windows_sid)
01008 {
01009 return FALSE;
01010 }
01011
01013
01025 dbus_bool_t
01026 _dbus_string_get_dirname (const DBusString *filename,
01027 DBusString *dirname)
01028 {
01029 int sep;
01030
01031 _dbus_assert (filename != dirname);
01032 _dbus_assert (filename != NULL);
01033 _dbus_assert (dirname != NULL);
01034
01035
01036 sep = _dbus_string_get_length (filename);
01037 if (sep == 0)
01038 return _dbus_string_append (dirname, ".");
01039
01040 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01041 --sep;
01042
01043 _dbus_assert (sep >= 0);
01044
01045 if (sep == 0)
01046 return _dbus_string_append (dirname, "/");
01047
01048
01049 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01050 if (sep < 0)
01051 return _dbus_string_append (dirname, ".");
01052
01053
01054 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01055 --sep;
01056
01057 _dbus_assert (sep >= 0);
01058
01059 if (sep == 0 &&
01060 _dbus_string_get_byte (filename, 0) == '/')
01061 return _dbus_string_append (dirname, "/");
01062 else
01063 return _dbus_string_copy_len (filename, 0, sep - 0,
01064 dirname, _dbus_string_get_length (dirname));
01065 }
01067
01068 static void
01069 string_squash_nonprintable (DBusString *str)
01070 {
01071 unsigned char *buf;
01072 int i, len;
01073
01074 buf = _dbus_string_get_data (str);
01075 len = _dbus_string_get_length (str);
01076
01077 for (i = 0; i < len; i++)
01078 {
01079 unsigned char c = (unsigned char) buf[i];
01080 if (c == '\0')
01081 buf[i] = ' ';
01082 else if (c < 0x20 || c > 127)
01083 buf[i] = '?';
01084 }
01085 }
01086
01101 dbus_bool_t
01102 _dbus_command_for_pid (unsigned long pid,
01103 DBusString *str,
01104 int max_len,
01105 DBusError *error)
01106 {
01107
01108 DBusString path;
01109 DBusString cmdline;
01110 int fd;
01111
01112 if (!_dbus_string_init (&path))
01113 {
01114 _DBUS_SET_OOM (error);
01115 return FALSE;
01116 }
01117
01118 if (!_dbus_string_init (&cmdline))
01119 {
01120 _DBUS_SET_OOM (error);
01121 _dbus_string_free (&path);
01122 return FALSE;
01123 }
01124
01125 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01126 goto oom;
01127
01128 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01129 if (fd < 0)
01130 {
01131 dbus_set_error (error,
01132 _dbus_error_from_errno (errno),
01133 "Failed to open \"%s\": %s",
01134 _dbus_string_get_const_data (&path),
01135 _dbus_strerror (errno));
01136 goto fail;
01137 }
01138
01139 if (!_dbus_read (fd, &cmdline, max_len))
01140 {
01141 dbus_set_error (error,
01142 _dbus_error_from_errno (errno),
01143 "Failed to read from \"%s\": %s",
01144 _dbus_string_get_const_data (&path),
01145 _dbus_strerror (errno));
01146 goto fail;
01147 }
01148
01149 if (!_dbus_close (fd, error))
01150 goto fail;
01151
01152 string_squash_nonprintable (&cmdline);
01153
01154 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01155 goto oom;
01156
01157 _dbus_string_free (&cmdline);
01158 _dbus_string_free (&path);
01159 return TRUE;
01160 oom:
01161 _DBUS_SET_OOM (error);
01162 fail:
01163 _dbus_string_free (&cmdline);
01164 _dbus_string_free (&path);
01165 return FALSE;
01166 }