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
00026 #include <config.h>
00027 #include "dbus-sysdeps.h"
00028 #include "dbus-sysdeps-unix.h"
00029 #include "dbus-internals.h"
00030 #include "dbus-pipe.h"
00031 #include "dbus-protocol.h"
00032 #include "dbus-string.h"
00033 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00034 #include "dbus-userdb.h"
00035 #include "dbus-test.h"
00036
00037 #include <sys/types.h>
00038 #include <stdlib.h>
00039 #include <string.h>
00040 #include <signal.h>
00041 #include <unistd.h>
00042 #include <stdio.h>
00043 #include <errno.h>
00044 #include <fcntl.h>
00045 #include <sys/stat.h>
00046 #ifdef HAVE_SYS_RESOURCE_H
00047 #include <sys/resource.h>
00048 #endif
00049 #include <grp.h>
00050 #include <sys/socket.h>
00051 #include <dirent.h>
00052 #include <sys/un.h>
00053 #include <syslog.h>
00054
00055 #ifdef HAVE_SYS_SYSLIMITS_H
00056 #include <sys/syslimits.h>
00057 #endif
00058
00059 #ifndef O_BINARY
00060 #define O_BINARY 0
00061 #endif
00062
00078 dbus_bool_t
00079 _dbus_become_daemon (const DBusString *pidfile,
00080 DBusPipe *print_pid_pipe,
00081 DBusError *error,
00082 dbus_bool_t keep_umask)
00083 {
00084 const char *s;
00085 pid_t child_pid;
00086 int dev_null_fd;
00087
00088 _dbus_verbose ("Becoming a daemon...\n");
00089
00090 _dbus_verbose ("chdir to /\n");
00091 if (chdir ("/") < 0)
00092 {
00093 dbus_set_error (error, DBUS_ERROR_FAILED,
00094 "Could not chdir() to root directory");
00095 return FALSE;
00096 }
00097
00098 _dbus_verbose ("forking...\n");
00099 switch ((child_pid = fork ()))
00100 {
00101 case -1:
00102 _dbus_verbose ("fork failed\n");
00103 dbus_set_error (error, _dbus_error_from_errno (errno),
00104 "Failed to fork daemon: %s", _dbus_strerror (errno));
00105 return FALSE;
00106 break;
00107
00108 case 0:
00109 _dbus_verbose ("in child, closing std file descriptors\n");
00110
00111
00112
00113
00114
00115
00116 dev_null_fd = open ("/dev/null", O_RDWR);
00117 if (dev_null_fd >= 0)
00118 {
00119 dup2 (dev_null_fd, 0);
00120 dup2 (dev_null_fd, 1);
00121
00122 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
00123 if (s == NULL || *s == '\0')
00124 dup2 (dev_null_fd, 2);
00125 else
00126 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
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 %"PRIuPTR"\n",
00259 print_pid_pipe->fd_or_handle);
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 unsigned int current_limit;
00394
00395
00396
00397
00398
00399
00400
00401 if (getuid () != 0)
00402 return;
00403
00404 if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
00405 return;
00406
00407 if (lim.rlim_cur >= limit)
00408 return;
00409
00410
00411
00412
00413 target_lim.rlim_cur = target_lim.rlim_max = limit;
00414
00415
00416
00417
00418
00419
00420
00421
00422 setrlimit (RLIMIT_NOFILE, &target_lim);
00423 #endif
00424 }
00425
00426 void
00427 _dbus_init_system_log (void)
00428 {
00429 openlog ("dbus", LOG_PID, LOG_DAEMON);
00430 }
00439 void
00440 _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...)
00441 {
00442 va_list args;
00443
00444 va_start (args, msg);
00445
00446 _dbus_system_logv (severity, msg, args);
00447
00448 va_end (args);
00449 }
00450
00461 void
00462 _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args)
00463 {
00464 int flags;
00465 switch (severity)
00466 {
00467 case DBUS_SYSTEM_LOG_INFO:
00468 flags = LOG_DAEMON | LOG_NOTICE;
00469 break;
00470 case DBUS_SYSTEM_LOG_SECURITY:
00471 flags = LOG_AUTH | LOG_NOTICE;
00472 break;
00473 case DBUS_SYSTEM_LOG_FATAL:
00474 flags = LOG_DAEMON|LOG_CRIT;
00475 break;
00476 default:
00477 return;
00478 }
00479
00480 vsyslog (flags, msg, args);
00481
00482 if (severity == DBUS_SYSTEM_LOG_FATAL)
00483 exit (1);
00484 }
00485
00491 void
00492 _dbus_set_signal_handler (int sig,
00493 DBusSignalHandler handler)
00494 {
00495 struct sigaction act;
00496 sigset_t empty_mask;
00497
00498 sigemptyset (&empty_mask);
00499 act.sa_handler = handler;
00500 act.sa_mask = empty_mask;
00501 act.sa_flags = 0;
00502 sigaction (sig, &act, NULL);
00503 }
00504
00510 dbus_bool_t
00511 _dbus_file_exists (const char *file)
00512 {
00513 return (access (file, F_OK) == 0);
00514 }
00515
00522 dbus_bool_t
00523 _dbus_user_at_console (const char *username,
00524 DBusError *error)
00525 {
00526
00527 DBusString f;
00528 dbus_bool_t result;
00529
00530 result = FALSE;
00531 if (!_dbus_string_init (&f))
00532 {
00533 _DBUS_SET_OOM (error);
00534 return FALSE;
00535 }
00536
00537 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00538 {
00539 _DBUS_SET_OOM (error);
00540 goto out;
00541 }
00542
00543
00544 if (!_dbus_string_append (&f, username))
00545 {
00546 _DBUS_SET_OOM (error);
00547 goto out;
00548 }
00549
00550 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00551
00552 out:
00553 _dbus_string_free (&f);
00554
00555 return result;
00556 }
00557
00558
00565 dbus_bool_t
00566 _dbus_path_is_absolute (const DBusString *filename)
00567 {
00568 if (_dbus_string_get_length (filename) > 0)
00569 return _dbus_string_get_byte (filename, 0) == '/';
00570 else
00571 return FALSE;
00572 }
00573
00582 dbus_bool_t
00583 _dbus_stat (const DBusString *filename,
00584 DBusStat *statbuf,
00585 DBusError *error)
00586 {
00587 const char *filename_c;
00588 struct stat sb;
00589
00590 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00591
00592 filename_c = _dbus_string_get_const_data (filename);
00593
00594 if (stat (filename_c, &sb) < 0)
00595 {
00596 dbus_set_error (error, _dbus_error_from_errno (errno),
00597 "%s", _dbus_strerror (errno));
00598 return FALSE;
00599 }
00600
00601 statbuf->mode = sb.st_mode;
00602 statbuf->nlink = sb.st_nlink;
00603 statbuf->uid = sb.st_uid;
00604 statbuf->gid = sb.st_gid;
00605 statbuf->size = sb.st_size;
00606 statbuf->atime = sb.st_atime;
00607 statbuf->mtime = sb.st_mtime;
00608 statbuf->ctime = sb.st_ctime;
00609
00610 return TRUE;
00611 }
00612
00613
00617 struct DBusDirIter
00618 {
00619 DIR *d;
00621 };
00622
00630 DBusDirIter*
00631 _dbus_directory_open (const DBusString *filename,
00632 DBusError *error)
00633 {
00634 DIR *d;
00635 DBusDirIter *iter;
00636 const char *filename_c;
00637
00638 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00639
00640 filename_c = _dbus_string_get_const_data (filename);
00641
00642 d = opendir (filename_c);
00643 if (d == NULL)
00644 {
00645 dbus_set_error (error, _dbus_error_from_errno (errno),
00646 "Failed to read directory \"%s\": %s",
00647 filename_c,
00648 _dbus_strerror (errno));
00649 return NULL;
00650 }
00651 iter = dbus_new0 (DBusDirIter, 1);
00652 if (iter == NULL)
00653 {
00654 closedir (d);
00655 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00656 "Could not allocate memory for directory iterator");
00657 return NULL;
00658 }
00659
00660 iter->d = d;
00661
00662 return iter;
00663 }
00664
00678 dbus_bool_t
00679 _dbus_directory_get_next_file (DBusDirIter *iter,
00680 DBusString *filename,
00681 DBusError *error)
00682 {
00683 struct dirent *ent;
00684 int err;
00685
00686 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00687
00688 again:
00689 errno = 0;
00690 ent = readdir (iter->d);
00691
00692 if (!ent)
00693 {
00694 err = errno;
00695
00696 if (err != 0)
00697 dbus_set_error (error,
00698 _dbus_error_from_errno (err),
00699 "%s", _dbus_strerror (err));
00700
00701 return FALSE;
00702 }
00703 else if (ent->d_name[0] == '.' &&
00704 (ent->d_name[1] == '\0' ||
00705 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00706 goto again;
00707 else
00708 {
00709 _dbus_string_set_length (filename, 0);
00710 if (!_dbus_string_append (filename, ent->d_name))
00711 {
00712 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00713 "No memory to read directory entry");
00714 return FALSE;
00715 }
00716 else
00717 {
00718 return TRUE;
00719 }
00720 }
00721 }
00722
00726 void
00727 _dbus_directory_close (DBusDirIter *iter)
00728 {
00729 closedir (iter->d);
00730 dbus_free (iter);
00731 }
00732
00733 static dbus_bool_t
00734 fill_user_info_from_group (struct group *g,
00735 DBusGroupInfo *info,
00736 DBusError *error)
00737 {
00738 _dbus_assert (g->gr_name != NULL);
00739
00740 info->gid = g->gr_gid;
00741 info->groupname = _dbus_strdup (g->gr_name);
00742
00743
00744
00745 if (info->groupname == NULL)
00746 {
00747 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00748 return FALSE;
00749 }
00750
00751 return TRUE;
00752 }
00753
00754 static dbus_bool_t
00755 fill_group_info (DBusGroupInfo *info,
00756 dbus_gid_t gid,
00757 const DBusString *groupname,
00758 DBusError *error)
00759 {
00760 const char *group_c_str;
00761
00762 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00763 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00764
00765 if (groupname)
00766 group_c_str = _dbus_string_get_const_data (groupname);
00767 else
00768 group_c_str = NULL;
00769
00770
00771
00772
00773
00774
00775 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00776 {
00777 struct group *g;
00778 int result;
00779 size_t buflen;
00780 char *buf;
00781 struct group g_str;
00782 dbus_bool_t b;
00783
00784
00785 buflen = sysconf (_SC_GETGR_R_SIZE_MAX);
00786
00787
00788
00789
00790
00791 if ((long) buflen <= 0)
00792 buflen = 1024;
00793
00794 result = -1;
00795 while (1)
00796 {
00797 buf = dbus_malloc (buflen);
00798 if (buf == NULL)
00799 {
00800 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00801 return FALSE;
00802 }
00803
00804 g = NULL;
00805 #ifdef HAVE_POSIX_GETPWNAM_R
00806 if (group_c_str)
00807 result = getgrnam_r (group_c_str, &g_str, buf, buflen,
00808 &g);
00809 else
00810 result = getgrgid_r (gid, &g_str, buf, buflen,
00811 &g);
00812 #else
00813 g = getgrnam_r (group_c_str, &g_str, buf, buflen);
00814 result = 0;
00815 #endif
00816
00817
00818
00819 if (result == ERANGE && buflen < 512 * 1024)
00820 {
00821 dbus_free (buf);
00822 buflen *= 2;
00823 }
00824 else
00825 {
00826 break;
00827 }
00828 }
00829
00830 if (result == 0 && g == &g_str)
00831 {
00832 b = fill_user_info_from_group (g, info, error);
00833 dbus_free (buf);
00834 return b;
00835 }
00836 else
00837 {
00838 dbus_set_error (error, _dbus_error_from_errno (errno),
00839 "Group %s unknown or failed to look it up\n",
00840 group_c_str ? group_c_str : "???");
00841 dbus_free (buf);
00842 return FALSE;
00843 }
00844 }
00845 #else
00846 {
00847
00848 struct group *g;
00849
00850 g = getgrnam (group_c_str);
00851
00852 if (g != NULL)
00853 {
00854 return fill_user_info_from_group (g, info, error);
00855 }
00856 else
00857 {
00858 dbus_set_error (error, _dbus_error_from_errno (errno),
00859 "Group %s unknown or failed to look it up\n",
00860 group_c_str ? group_c_str : "???");
00861 return FALSE;
00862 }
00863 }
00864 #endif
00865 }
00866
00876 dbus_bool_t
00877 _dbus_group_info_fill (DBusGroupInfo *info,
00878 const DBusString *groupname,
00879 DBusError *error)
00880 {
00881 return fill_group_info (info, DBUS_GID_UNSET,
00882 groupname, error);
00883
00884 }
00885
00895 dbus_bool_t
00896 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00897 dbus_gid_t gid,
00898 DBusError *error)
00899 {
00900 return fill_group_info (info, gid, NULL, error);
00901 }
00902
00911 dbus_bool_t
00912 _dbus_parse_unix_user_from_config (const DBusString *username,
00913 dbus_uid_t *uid_p)
00914 {
00915 return _dbus_get_user_id (username, uid_p);
00916
00917 }
00918
00927 dbus_bool_t
00928 _dbus_parse_unix_group_from_config (const DBusString *groupname,
00929 dbus_gid_t *gid_p)
00930 {
00931 return _dbus_get_group_id (groupname, gid_p);
00932 }
00933
00944 dbus_bool_t
00945 _dbus_unix_groups_from_uid (dbus_uid_t uid,
00946 dbus_gid_t **group_ids,
00947 int *n_group_ids)
00948 {
00949 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00950 }
00951
00961 dbus_bool_t
00962 _dbus_unix_user_is_at_console (dbus_uid_t uid,
00963 DBusError *error)
00964 {
00965 return _dbus_is_console_user (uid, error);
00966
00967 }
00968
00976 dbus_bool_t
00977 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00978 {
00979 return uid == _dbus_geteuid ();
00980 }
00981
00989 dbus_bool_t
00990 _dbus_windows_user_is_process_owner (const char *windows_sid)
00991 {
00992 return FALSE;
00993 }
00994
00996
01008 dbus_bool_t
01009 _dbus_string_get_dirname (const DBusString *filename,
01010 DBusString *dirname)
01011 {
01012 int sep;
01013
01014 _dbus_assert (filename != dirname);
01015 _dbus_assert (filename != NULL);
01016 _dbus_assert (dirname != NULL);
01017
01018
01019 sep = _dbus_string_get_length (filename);
01020 if (sep == 0)
01021 return _dbus_string_append (dirname, ".");
01022
01023 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01024 --sep;
01025
01026 _dbus_assert (sep >= 0);
01027
01028 if (sep == 0)
01029 return _dbus_string_append (dirname, "/");
01030
01031
01032 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01033 if (sep < 0)
01034 return _dbus_string_append (dirname, ".");
01035
01036
01037 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01038 --sep;
01039
01040 _dbus_assert (sep >= 0);
01041
01042 if (sep == 0 &&
01043 _dbus_string_get_byte (filename, 0) == '/')
01044 return _dbus_string_append (dirname, "/");
01045 else
01046 return _dbus_string_copy_len (filename, 0, sep - 0,
01047 dirname, _dbus_string_get_length (dirname));
01048 }
01050
01051 static void
01052 string_squash_nonprintable (DBusString *str)
01053 {
01054 unsigned char *buf;
01055 int i, len;
01056
01057 buf = _dbus_string_get_data (str);
01058 len = _dbus_string_get_length (str);
01059
01060 for (i = 0; i < len; i++)
01061 {
01062 unsigned char c = (unsigned char) buf[i];
01063 if (c == '\0')
01064 buf[i] = ' ';
01065 else if (c < 0x20 || c > 127)
01066 buf[i] = '?';
01067 }
01068 }
01069
01084 dbus_bool_t
01085 _dbus_command_for_pid (unsigned long pid,
01086 DBusString *str,
01087 int max_len,
01088 DBusError *error)
01089 {
01090
01091 DBusString path;
01092 DBusString cmdline;
01093 int fd;
01094
01095 if (!_dbus_string_init (&path))
01096 {
01097 _DBUS_SET_OOM (error);
01098 return FALSE;
01099 }
01100
01101 if (!_dbus_string_init (&cmdline))
01102 {
01103 _DBUS_SET_OOM (error);
01104 _dbus_string_free (&path);
01105 return FALSE;
01106 }
01107
01108 if (!_dbus_string_append_printf (&path, "/proc/%ld/cmdline", pid))
01109 goto oom;
01110
01111 fd = open (_dbus_string_get_const_data (&path), O_RDONLY);
01112 if (fd < 0)
01113 {
01114 dbus_set_error (error,
01115 _dbus_error_from_errno (errno),
01116 "Failed to open \"%s\": %s",
01117 _dbus_string_get_const_data (&path),
01118 _dbus_strerror (errno));
01119 goto fail;
01120 }
01121
01122 if (!_dbus_read (fd, &cmdline, max_len))
01123 {
01124 dbus_set_error (error,
01125 _dbus_error_from_errno (errno),
01126 "Failed to read from \"%s\": %s",
01127 _dbus_string_get_const_data (&path),
01128 _dbus_strerror (errno));
01129 goto fail;
01130 }
01131
01132 if (!_dbus_close (fd, error))
01133 goto fail;
01134
01135 string_squash_nonprintable (&cmdline);
01136
01137 if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str)))
01138 goto oom;
01139
01140 _dbus_string_free (&cmdline);
01141 _dbus_string_free (&path);
01142 return TRUE;
01143 oom:
01144 _DBUS_SET_OOM (error);
01145 fail:
01146 _dbus_string_free (&cmdline);
01147 _dbus_string_free (&path);
01148 return FALSE;
01149 }
01150
01161 void
01162 _dbus_reset_process_attributes (void)
01163 {
01164
01165 #ifdef __linux__
01166 int oom_adj_fd;
01167
01168
01169
01170
01171
01172 oom_adj_fd = open ("/proc/self/oom_adj", O_WRONLY|O_SYNC);
01173 if (oom_adj_fd >= 0)
01174 {
01175
01176 write (oom_adj_fd, "0", sizeof (char));
01177 close (oom_adj_fd);
01178 }
01179 #endif
01180 }
01181