00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbus-sysdeps.h"
00025 #include "dbus-sysdeps-unix.h"
00026 #include "dbus-internals.h"
00027 #include "dbus-protocol.h"
00028 #include "dbus-string.h"
00029 #define DBUS_USERDB_INCLUDES_PRIVATE 1
00030 #include "dbus-userdb.h"
00031 #include "dbus-test.h"
00032
00033 #include <sys/types.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <signal.h>
00037 #include <unistd.h>
00038 #include <stdio.h>
00039 #include <errno.h>
00040 #include <fcntl.h>
00041 #include <sys/stat.h>
00042 #include <grp.h>
00043 #include <sys/socket.h>
00044 #include <dirent.h>
00045 #include <sys/un.h>
00046 #ifdef HAVE_LIBAUDIT
00047 #include <sys/prctl.h>
00048 #include <sys/capability.h>
00049 #include <libaudit.h>
00050 #endif
00051
00052 #ifdef HAVE_SYS_SYSLIMITS_H
00053 #include <sys/syslimits.h>
00054 #endif
00055
00056 #ifndef O_BINARY
00057 #define O_BINARY 0
00058 #endif
00059
00073 dbus_bool_t
00074 _dbus_become_daemon (const DBusString *pidfile,
00075 DBusPipe *print_pid_pipe,
00076 DBusError *error)
00077 {
00078 const char *s;
00079 pid_t child_pid;
00080 int dev_null_fd;
00081
00082 _dbus_verbose ("Becoming a daemon...\n");
00083
00084 _dbus_verbose ("chdir to /\n");
00085 if (chdir ("/") < 0)
00086 {
00087 dbus_set_error (error, DBUS_ERROR_FAILED,
00088 "Could not chdir() to root directory");
00089 return FALSE;
00090 }
00091
00092 _dbus_verbose ("forking...\n");
00093 switch ((child_pid = fork ()))
00094 {
00095 case -1:
00096 _dbus_verbose ("fork failed\n");
00097 dbus_set_error (error, _dbus_error_from_errno (errno),
00098 "Failed to fork daemon: %s", _dbus_strerror (errno));
00099 return FALSE;
00100 break;
00101
00102 case 0:
00103 _dbus_verbose ("in child, closing std file descriptors\n");
00104
00105
00106
00107
00108
00109
00110 dev_null_fd = open ("/dev/null", O_RDWR);
00111 if (dev_null_fd >= 0)
00112 {
00113 dup2 (dev_null_fd, 0);
00114 dup2 (dev_null_fd, 1);
00115
00116 s = _dbus_getenv ("DBUS_DEBUG_OUTPUT");
00117 if (s == NULL || *s == '\0')
00118 dup2 (dev_null_fd, 2);
00119 else
00120 _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n");
00121 }
00122
00123
00124 _dbus_verbose ("setting umask\n");
00125 umask (022);
00126 break;
00127
00128 default:
00129 if (pidfile)
00130 {
00131 _dbus_verbose ("parent writing pid file\n");
00132 if (!_dbus_write_pid_file (pidfile,
00133 child_pid,
00134 error))
00135 {
00136 _dbus_verbose ("pid file write failed, killing child\n");
00137 kill (child_pid, SIGTERM);
00138 return FALSE;
00139 }
00140 }
00141
00142
00143 if (print_pid_pipe != NULL && _dbus_pipe_is_valid (print_pid_pipe))
00144 {
00145 DBusString pid;
00146 int bytes;
00147
00148 if (!_dbus_string_init (&pid))
00149 {
00150 _DBUS_SET_OOM (error);
00151 kill (child_pid, SIGTERM);
00152 return FALSE;
00153 }
00154
00155 if (!_dbus_string_append_int (&pid, child_pid) ||
00156 !_dbus_string_append (&pid, "\n"))
00157 {
00158 _dbus_string_free (&pid);
00159 _DBUS_SET_OOM (error);
00160 kill (child_pid, SIGTERM);
00161 return FALSE;
00162 }
00163
00164 bytes = _dbus_string_get_length (&pid);
00165 if (_dbus_pipe_write (print_pid_pipe, &pid, 0, bytes, error) != bytes)
00166 {
00167
00168 if (error != NULL && !dbus_error_is_set(error))
00169 {
00170 dbus_set_error (error, DBUS_ERROR_FAILED,
00171 "Printing message bus PID: did not write enough bytes\n");
00172 }
00173 _dbus_string_free (&pid);
00174 kill (child_pid, SIGTERM);
00175 return FALSE;
00176 }
00177
00178 _dbus_string_free (&pid);
00179 }
00180 _dbus_verbose ("parent exiting\n");
00181 _exit (0);
00182 break;
00183 }
00184
00185 _dbus_verbose ("calling setsid()\n");
00186 if (setsid () == -1)
00187 _dbus_assert_not_reached ("setsid() failed");
00188
00189 return TRUE;
00190 }
00191
00192
00201 dbus_bool_t
00202 _dbus_write_pid_file (const DBusString *filename,
00203 unsigned long pid,
00204 DBusError *error)
00205 {
00206 const char *cfilename;
00207 int fd;
00208 FILE *f;
00209
00210 cfilename = _dbus_string_get_const_data (filename);
00211
00212 fd = open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644);
00213
00214 if (fd < 0)
00215 {
00216 dbus_set_error (error, _dbus_error_from_errno (errno),
00217 "Failed to open \"%s\": %s", cfilename,
00218 _dbus_strerror (errno));
00219 return FALSE;
00220 }
00221
00222 if ((f = fdopen (fd, "w")) == NULL)
00223 {
00224 dbus_set_error (error, _dbus_error_from_errno (errno),
00225 "Failed to fdopen fd %d: %s", fd, _dbus_strerror (errno));
00226 _dbus_close (fd, NULL);
00227 return FALSE;
00228 }
00229
00230 if (fprintf (f, "%lu\n", pid) < 0)
00231 {
00232 dbus_set_error (error, _dbus_error_from_errno (errno),
00233 "Failed to write to \"%s\": %s", cfilename,
00234 _dbus_strerror (errno));
00235
00236 fclose (f);
00237 return FALSE;
00238 }
00239
00240 if (fclose (f) == EOF)
00241 {
00242 dbus_set_error (error, _dbus_error_from_errno (errno),
00243 "Failed to close \"%s\": %s", cfilename,
00244 _dbus_strerror (errno));
00245 return FALSE;
00246 }
00247
00248 return TRUE;
00249 }
00250
00257 dbus_bool_t
00258 _dbus_verify_daemon_user (const char *user)
00259 {
00260 DBusString u;
00261
00262 _dbus_string_init_const (&u, user);
00263
00264 return _dbus_get_user_id_and_primary_group (&u, NULL, NULL);
00265 }
00266
00274 dbus_bool_t
00275 _dbus_change_to_daemon_user (const char *user,
00276 DBusError *error)
00277 {
00278 dbus_uid_t uid;
00279 dbus_gid_t gid;
00280 DBusString u;
00281 #ifdef HAVE_LIBAUDIT
00282 dbus_bool_t we_were_root;
00283 cap_t new_caps;
00284 #endif
00285
00286 _dbus_string_init_const (&u, user);
00287
00288 if (!_dbus_get_user_id_and_primary_group (&u, &uid, &gid))
00289 {
00290 dbus_set_error (error, DBUS_ERROR_FAILED,
00291 "User '%s' does not appear to exist?",
00292 user);
00293 return FALSE;
00294 }
00295
00296 #ifdef HAVE_LIBAUDIT
00297 we_were_root = _dbus_getuid () == 0;
00298 new_caps = NULL;
00299
00300
00301
00302
00303 if (we_were_root)
00304 {
00305 cap_value_t new_cap_list[] = { CAP_AUDIT_WRITE };
00306 cap_value_t tmp_cap_list[] = { CAP_AUDIT_WRITE, CAP_SETUID, CAP_SETGID };
00307 cap_t tmp_caps = cap_init();
00308
00309 if (!tmp_caps || !(new_caps = cap_init ()))
00310 {
00311 dbus_set_error (error, DBUS_ERROR_FAILED,
00312 "Failed to initialize drop of capabilities: %s\n",
00313 _dbus_strerror (errno));
00314
00315 if (tmp_caps)
00316 cap_free (tmp_caps);
00317
00318 return FALSE;
00319 }
00320
00321
00322 cap_set_flag (new_caps, CAP_PERMITTED, 1, new_cap_list, CAP_SET);
00323 cap_set_flag (new_caps, CAP_EFFECTIVE, 1, new_cap_list, CAP_SET);
00324 cap_set_flag (tmp_caps, CAP_PERMITTED, 3, tmp_cap_list, CAP_SET);
00325 cap_set_flag (tmp_caps, CAP_EFFECTIVE, 3, tmp_cap_list, CAP_SET);
00326
00327 if (prctl (PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1)
00328 {
00329 dbus_set_error (error, _dbus_error_from_errno (errno),
00330 "Failed to set keep-capabilities: %s\n",
00331 _dbus_strerror (errno));
00332 cap_free (tmp_caps);
00333 goto fail;
00334 }
00335
00336 if (cap_set_proc (tmp_caps) == -1)
00337 {
00338 dbus_set_error (error, DBUS_ERROR_FAILED,
00339 "Failed to drop capabilities: %s\n",
00340 _dbus_strerror (errno));
00341 cap_free (tmp_caps);
00342 goto fail;
00343 }
00344 cap_free (tmp_caps);
00345 }
00346 #endif
00347
00348
00349
00350
00351
00352
00353
00354
00355 if (setgroups (0, NULL) < 0)
00356 _dbus_warn ("Failed to drop supplementary groups: %s\n",
00357 _dbus_strerror (errno));
00358
00359
00360
00361
00362 if (setgid (gid) < 0)
00363 {
00364 dbus_set_error (error, _dbus_error_from_errno (errno),
00365 "Failed to set GID to %lu: %s", gid,
00366 _dbus_strerror (errno));
00367 goto fail;
00368 }
00369
00370 if (setuid (uid) < 0)
00371 {
00372 dbus_set_error (error, _dbus_error_from_errno (errno),
00373 "Failed to set UID to %lu: %s", uid,
00374 _dbus_strerror (errno));
00375 goto fail;
00376 }
00377
00378 #ifdef HAVE_LIBAUDIT
00379 if (we_were_root)
00380 {
00381 if (cap_set_proc (new_caps))
00382 {
00383 dbus_set_error (error, DBUS_ERROR_FAILED,
00384 "Failed to drop capabilities: %s\n",
00385 _dbus_strerror (errno));
00386 goto fail;
00387 }
00388 cap_free (new_caps);
00389
00390
00391 if (prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0) == -1)
00392 {
00393 dbus_set_error (error, _dbus_error_from_errno (errno),
00394 "Failed to unset keep-capabilities: %s\n",
00395 _dbus_strerror (errno));
00396 return FALSE;
00397 }
00398 }
00399 #endif
00400
00401 return TRUE;
00402
00403 fail:
00404 #ifdef HAVE_LIBAUDIT
00405 if (!we_were_root)
00406 {
00407
00408 prctl (PR_SET_KEEPCAPS, 0, 0, 0, 0);
00409 cap_free (new_caps);
00410 }
00411 #endif
00412
00413 return FALSE;
00414 }
00415
00421 void
00422 _dbus_set_signal_handler (int sig,
00423 DBusSignalHandler handler)
00424 {
00425 struct sigaction act;
00426 sigset_t empty_mask;
00427
00428 sigemptyset (&empty_mask);
00429 act.sa_handler = handler;
00430 act.sa_mask = empty_mask;
00431 act.sa_flags = 0;
00432 sigaction (sig, &act, NULL);
00433 }
00434
00435
00443 dbus_bool_t
00444 _dbus_delete_directory (const DBusString *filename,
00445 DBusError *error)
00446 {
00447 const char *filename_c;
00448
00449 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00450
00451 filename_c = _dbus_string_get_const_data (filename);
00452
00453 if (rmdir (filename_c) != 0)
00454 {
00455 dbus_set_error (error, DBUS_ERROR_FAILED,
00456 "Failed to remove directory %s: %s\n",
00457 filename_c, _dbus_strerror (errno));
00458 return FALSE;
00459 }
00460
00461 return TRUE;
00462 }
00463
00469 dbus_bool_t
00470 _dbus_file_exists (const char *file)
00471 {
00472 return (access (file, F_OK) == 0);
00473 }
00474
00481 dbus_bool_t
00482 _dbus_user_at_console (const char *username,
00483 DBusError *error)
00484 {
00485
00486 DBusString f;
00487 dbus_bool_t result;
00488
00489 result = FALSE;
00490 if (!_dbus_string_init (&f))
00491 {
00492 _DBUS_SET_OOM (error);
00493 return FALSE;
00494 }
00495
00496 if (!_dbus_string_append (&f, DBUS_CONSOLE_AUTH_DIR))
00497 {
00498 _DBUS_SET_OOM (error);
00499 goto out;
00500 }
00501
00502
00503 if (!_dbus_string_append (&f, username))
00504 {
00505 _DBUS_SET_OOM (error);
00506 goto out;
00507 }
00508
00509 result = _dbus_file_exists (_dbus_string_get_const_data (&f));
00510
00511 out:
00512 _dbus_string_free (&f);
00513
00514 return result;
00515 }
00516
00517
00524 dbus_bool_t
00525 _dbus_path_is_absolute (const DBusString *filename)
00526 {
00527 if (_dbus_string_get_length (filename) > 0)
00528 return _dbus_string_get_byte (filename, 0) == '/';
00529 else
00530 return FALSE;
00531 }
00532
00541 dbus_bool_t
00542 _dbus_stat (const DBusString *filename,
00543 DBusStat *statbuf,
00544 DBusError *error)
00545 {
00546 const char *filename_c;
00547 struct stat sb;
00548
00549 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00550
00551 filename_c = _dbus_string_get_const_data (filename);
00552
00553 if (stat (filename_c, &sb) < 0)
00554 {
00555 dbus_set_error (error, _dbus_error_from_errno (errno),
00556 "%s", _dbus_strerror (errno));
00557 return FALSE;
00558 }
00559
00560 statbuf->mode = sb.st_mode;
00561 statbuf->nlink = sb.st_nlink;
00562 statbuf->uid = sb.st_uid;
00563 statbuf->gid = sb.st_gid;
00564 statbuf->size = sb.st_size;
00565 statbuf->atime = sb.st_atime;
00566 statbuf->mtime = sb.st_mtime;
00567 statbuf->ctime = sb.st_ctime;
00568
00569 return TRUE;
00570 }
00571
00572
00576 struct DBusDirIter
00577 {
00578 DIR *d;
00580 };
00581
00589 DBusDirIter*
00590 _dbus_directory_open (const DBusString *filename,
00591 DBusError *error)
00592 {
00593 DIR *d;
00594 DBusDirIter *iter;
00595 const char *filename_c;
00596
00597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00598
00599 filename_c = _dbus_string_get_const_data (filename);
00600
00601 d = opendir (filename_c);
00602 if (d == NULL)
00603 {
00604 dbus_set_error (error, _dbus_error_from_errno (errno),
00605 "Failed to read directory \"%s\": %s",
00606 filename_c,
00607 _dbus_strerror (errno));
00608 return NULL;
00609 }
00610 iter = dbus_new0 (DBusDirIter, 1);
00611 if (iter == NULL)
00612 {
00613 closedir (d);
00614 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00615 "Could not allocate memory for directory iterator");
00616 return NULL;
00617 }
00618
00619 iter->d = d;
00620
00621 return iter;
00622 }
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 static dbus_bool_t
00633 dirent_buf_size(DIR * dirp, size_t *size)
00634 {
00635 long name_max;
00636 # if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX)
00637 # if defined(HAVE_DIRFD)
00638 name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
00639 # elif defined(HAVE_DDFD)
00640 name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX);
00641 # else
00642 name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX);
00643 # endif
00644 if (name_max == -1)
00645 # if defined(NAME_MAX)
00646 name_max = NAME_MAX;
00647 # else
00648 return FALSE;
00649 # endif
00650 # elif defined(MAXNAMELEN)
00651 name_max = MAXNAMELEN;
00652 # else
00653 # if defined(NAME_MAX)
00654 name_max = NAME_MAX;
00655 # else
00656 # error "buffer size for readdir_r cannot be determined"
00657 # endif
00658 # endif
00659 if (size)
00660 *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1;
00661 else
00662 return FALSE;
00663
00664 return TRUE;
00665 }
00666
00677 dbus_bool_t
00678 _dbus_directory_get_next_file (DBusDirIter *iter,
00679 DBusString *filename,
00680 DBusError *error)
00681 {
00682 struct dirent *d, *ent;
00683 size_t buf_size;
00684 int err;
00685
00686 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
00687
00688 if (!dirent_buf_size (iter->d, &buf_size))
00689 {
00690 dbus_set_error (error, DBUS_ERROR_FAILED,
00691 "Can't calculate buffer size when reading directory");
00692 return FALSE;
00693 }
00694
00695 d = (struct dirent *)dbus_malloc (buf_size);
00696 if (!d)
00697 {
00698 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00699 "No memory to read directory entry");
00700 return FALSE;
00701 }
00702
00703 again:
00704 err = readdir_r (iter->d, d, &ent);
00705 if (err || !ent)
00706 {
00707 if (err != 0)
00708 dbus_set_error (error,
00709 _dbus_error_from_errno (err),
00710 "%s", _dbus_strerror (err));
00711
00712 dbus_free (d);
00713 return FALSE;
00714 }
00715 else if (ent->d_name[0] == '.' &&
00716 (ent->d_name[1] == '\0' ||
00717 (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
00718 goto again;
00719 else
00720 {
00721 _dbus_string_set_length (filename, 0);
00722 if (!_dbus_string_append (filename, ent->d_name))
00723 {
00724 dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
00725 "No memory to read directory entry");
00726 dbus_free (d);
00727 return FALSE;
00728 }
00729 else
00730 {
00731 dbus_free (d);
00732 return TRUE;
00733 }
00734 }
00735 }
00736
00740 void
00741 _dbus_directory_close (DBusDirIter *iter)
00742 {
00743 closedir (iter->d);
00744 dbus_free (iter);
00745 }
00746
00747 static dbus_bool_t
00748 fill_user_info_from_group (struct group *g,
00749 DBusGroupInfo *info,
00750 DBusError *error)
00751 {
00752 _dbus_assert (g->gr_name != NULL);
00753
00754 info->gid = g->gr_gid;
00755 info->groupname = _dbus_strdup (g->gr_name);
00756
00757
00758
00759 if (info->groupname == NULL)
00760 {
00761 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00762 return FALSE;
00763 }
00764
00765 return TRUE;
00766 }
00767
00768 static dbus_bool_t
00769 fill_group_info (DBusGroupInfo *info,
00770 dbus_gid_t gid,
00771 const DBusString *groupname,
00772 DBusError *error)
00773 {
00774 const char *group_c_str;
00775
00776 _dbus_assert (groupname != NULL || gid != DBUS_GID_UNSET);
00777 _dbus_assert (groupname == NULL || gid == DBUS_GID_UNSET);
00778
00779 if (groupname)
00780 group_c_str = _dbus_string_get_const_data (groupname);
00781 else
00782 group_c_str = NULL;
00783
00784
00785
00786
00787
00788
00789 #if defined (HAVE_POSIX_GETPWNAM_R) || defined (HAVE_NONPOSIX_GETPWNAM_R)
00790 {
00791 struct group *g;
00792 int result;
00793 char buf[1024];
00794 struct group g_str;
00795
00796 g = NULL;
00797 #ifdef HAVE_POSIX_GETPWNAM_R
00798
00799 if (group_c_str)
00800 result = getgrnam_r (group_c_str, &g_str, buf, sizeof (buf),
00801 &g);
00802 else
00803 result = getgrgid_r (gid, &g_str, buf, sizeof (buf),
00804 &g);
00805 #else
00806 g = getgrnam_r (group_c_str, &g_str, buf, sizeof (buf));
00807 result = 0;
00808 #endif
00809 if (result == 0 && g == &g_str)
00810 {
00811 return fill_user_info_from_group (g, info, error);
00812 }
00813 else
00814 {
00815 dbus_set_error (error, _dbus_error_from_errno (errno),
00816 "Group %s unknown or failed to look it up\n",
00817 group_c_str ? group_c_str : "???");
00818 return FALSE;
00819 }
00820 }
00821 #else
00822 {
00823
00824 struct group *g;
00825
00826 g = getgrnam (group_c_str);
00827
00828 if (g != NULL)
00829 {
00830 return fill_user_info_from_group (g, info, error);
00831 }
00832 else
00833 {
00834 dbus_set_error (error, _dbus_error_from_errno (errno),
00835 "Group %s unknown or failed to look it up\n",
00836 group_c_str ? group_c_str : "???");
00837 return FALSE;
00838 }
00839 }
00840 #endif
00841 }
00842
00852 dbus_bool_t
00853 _dbus_group_info_fill (DBusGroupInfo *info,
00854 const DBusString *groupname,
00855 DBusError *error)
00856 {
00857 return fill_group_info (info, DBUS_GID_UNSET,
00858 groupname, error);
00859
00860 }
00861
00871 dbus_bool_t
00872 _dbus_group_info_fill_gid (DBusGroupInfo *info,
00873 dbus_gid_t gid,
00874 DBusError *error)
00875 {
00876 return fill_group_info (info, gid, NULL, error);
00877 }
00878
00887 dbus_bool_t
00888 _dbus_parse_unix_user_from_config (const DBusString *username,
00889 dbus_uid_t *uid_p)
00890 {
00891 return _dbus_get_user_id (username, uid_p);
00892
00893 }
00894
00903 dbus_bool_t
00904 _dbus_parse_unix_group_from_config (const DBusString *groupname,
00905 dbus_gid_t *gid_p)
00906 {
00907 return _dbus_get_group_id (groupname, gid_p);
00908 }
00909
00920 dbus_bool_t
00921 _dbus_unix_groups_from_uid (dbus_uid_t uid,
00922 dbus_gid_t **group_ids,
00923 int *n_group_ids)
00924 {
00925 return _dbus_groups_from_uid (uid, group_ids, n_group_ids);
00926 }
00927
00937 dbus_bool_t
00938 _dbus_unix_user_is_at_console (dbus_uid_t uid,
00939 DBusError *error)
00940 {
00941 return _dbus_is_console_user (uid, error);
00942
00943 }
00944
00952 dbus_bool_t
00953 _dbus_unix_user_is_process_owner (dbus_uid_t uid)
00954 {
00955 return uid == _dbus_getuid ();
00956 }
00957
00965 dbus_bool_t
00966 _dbus_windows_user_is_process_owner (const char *windows_sid)
00967 {
00968 return FALSE;
00969 }
00970
00972
00984 dbus_bool_t
00985 _dbus_string_get_dirname (const DBusString *filename,
00986 DBusString *dirname)
00987 {
00988 int sep;
00989
00990 _dbus_assert (filename != dirname);
00991 _dbus_assert (filename != NULL);
00992 _dbus_assert (dirname != NULL);
00993
00994
00995 sep = _dbus_string_get_length (filename);
00996 if (sep == 0)
00997 return _dbus_string_append (dirname, ".");
00998
00999 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01000 --sep;
01001
01002 _dbus_assert (sep >= 0);
01003
01004 if (sep == 0)
01005 return _dbus_string_append (dirname, "/");
01006
01007
01008 _dbus_string_find_byte_backward (filename, sep, '/', &sep);
01009 if (sep < 0)
01010 return _dbus_string_append (dirname, ".");
01011
01012
01013 while (sep > 0 && _dbus_string_get_byte (filename, sep - 1) == '/')
01014 --sep;
01015
01016 _dbus_assert (sep >= 0);
01017
01018 if (sep == 0 &&
01019 _dbus_string_get_byte (filename, 0) == '/')
01020 return _dbus_string_append (dirname, "/");
01021 else
01022 return _dbus_string_copy_len (filename, 0, sep - 0,
01023 dirname, _dbus_string_get_length (dirname));
01024 }
01026