X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dbus%2Fdbus-sysdeps-util-unix.c;h=6265e2b5d5ad2c8c7a715cc3e975edfb225cfedf;hb=757b80b9711d9733798c927495d74c7323e95400;hp=5904950de69c6441a8a5597637b0770aa47db4a2;hpb=58fe0adefd1548bda0a55ece356d3741056ccd1c;p=platform%2Fupstream%2Fdbus.git diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 5904950..6265e2b 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -42,16 +42,24 @@ #include #include #include +#ifdef HAVE_SYS_RESOURCE_H +#include +#endif #include #include #include #include + +#ifdef HAVE_SYSLOG_H #include +#endif #ifdef HAVE_SYS_SYSLIMITS_H #include #endif +#include "sd-daemon.h" + #ifndef O_BINARY #define O_BINARY 0 #endif @@ -120,6 +128,7 @@ _dbus_become_daemon (const DBusString *pidfile, dup2 (dev_null_fd, 2); else _dbus_verbose ("keeping stderr open due to DBUS_DEBUG_OUTPUT\n"); + close (dev_null_fd); } if (!keep_umask) @@ -251,8 +260,8 @@ _dbus_write_pid_to_file_and_pipe (const DBusString *pidfile, DBusString pid; int bytes; - _dbus_verbose ("writing our pid to pipe %"PRIuPTR"\n", - print_pid_pipe->fd_or_handle); + _dbus_verbose ("writing our pid to pipe %d\n", + print_pid_pipe->fd); if (!_dbus_string_init (&pid)) { @@ -369,11 +378,72 @@ _dbus_change_to_daemon_user (const char *user, } #endif /* !HAVE_LIBAUDIT */ -void -_dbus_init_system_log (void) + +/** + * Attempt to ensure that the current process can open + * at least @limit file descriptors. + * + * If @limit is lower than the current, it will not be + * lowered. No error is returned if the request can + * not be satisfied. + * + * @limit Number of file descriptors + */ +void +_dbus_request_file_descriptor_limit (unsigned int limit) +{ +#ifdef HAVE_SETRLIMIT + struct rlimit lim; + struct rlimit target_lim; + + /* No point to doing this practically speaking + * if we're not uid 0. We expect the system + * bus to use this before we change UID, and + * the session bus takes the Linux default + * of 1024 for both cur and max. + */ + if (getuid () != 0) + return; + + if (getrlimit (RLIMIT_NOFILE, &lim) < 0) + return; + + if (lim.rlim_cur >= limit) + return; + + /* Ignore "maximum limit", assume we have the "superuser" + * privileges. On Linux this is CAP_SYS_RESOURCE. + */ + target_lim.rlim_cur = target_lim.rlim_max = limit; + /* Also ignore errors; if we fail, we will at least work + * up to whatever limit we had, which seems better than + * just outright aborting. + * + * However, in the future we should probably log this so OS builders + * have a chance to notice any misconfiguration like dbus-daemon + * being started without CAP_SYS_RESOURCE. + */ + setrlimit (RLIMIT_NOFILE, &target_lim); +#endif +} + +void +_dbus_init_system_log (dbus_bool_t is_daemon) { - openlog ("dbus", LOG_PID, LOG_DAEMON); +#ifdef HAVE_SYSLOG_H + int logopts = LOG_PID; + +#if HAVE_DECL_LOG_PERROR +#ifdef HAVE_SYSTEMD + if (!is_daemon || sd_booted () <= 0) +#endif + logopts |= LOG_PERROR; +#endif + + openlog ("dbus", logopts, LOG_DAEMON); +#endif } + /** * Log a message to the system log file (e.g. syslog on Unix). * @@ -407,6 +477,8 @@ _dbus_system_log (DBusSystemLogSeverity severity, const char *msg, ...) void _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args) { + va_list tmp; +#ifdef HAVE_SYSLOG_H int flags; switch (severity) { @@ -418,11 +490,26 @@ _dbus_system_logv (DBusSystemLogSeverity severity, const char *msg, va_list args break; case DBUS_SYSTEM_LOG_FATAL: flags = LOG_DAEMON|LOG_CRIT; + break; default: return; } - vsyslog (flags, msg, args); + DBUS_VA_COPY (tmp, args); + vsyslog (flags, msg, tmp); + va_end (tmp); +#endif + +#if !defined(HAVE_SYSLOG_H) || !HAVE_DECL_LOG_PERROR + { + /* vsyslog() won't write to stderr, so we'd better do it */ + DBUS_VA_COPY (tmp, args); + fprintf (stderr, "dbus[" DBUS_PID_FORMAT "]: ", _dbus_getpid ()); + vfprintf (stderr, msg, tmp); + fputc ('\n', stderr); + va_end (tmp); + } +#endif if (severity == DBUS_SYSTEM_LOG_FATAL) exit (1); @@ -469,7 +556,7 @@ _dbus_user_at_console (const char *username, DBusError *error) { - DBusString f; + DBusString u, f; dbus_bool_t result; result = FALSE; @@ -485,8 +572,9 @@ _dbus_user_at_console (const char *username, goto out; } + _dbus_string_init_const (&u, username); - if (!_dbus_string_append (&f, username)) + if (!_dbus_concat_dir_and_file (&f, &u)) { _DBUS_SET_OOM (error); goto out; @@ -607,54 +695,14 @@ _dbus_directory_open (const DBusString *filename, return iter; } -/* Calculate the required buffer size (in bytes) for directory - * entries read from the given directory handle. Return -1 if this - * this cannot be done. - * - * If you use autoconf, include fpathconf and dirfd in your - * AC_CHECK_FUNCS list. Otherwise use some other method to detect - * and use them where available. - */ -static dbus_bool_t -dirent_buf_size(DIR * dirp, size_t *size) -{ - long name_max; -# if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX) -# if defined(HAVE_DIRFD) - name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX); -# elif defined(HAVE_DDFD) - name_max = fpathconf(dirp->dd_fd, _PC_NAME_MAX); -# else - name_max = fpathconf(dirp->__dd_fd, _PC_NAME_MAX); -# endif /* HAVE_DIRFD */ - if (name_max == -1) -# if defined(NAME_MAX) - name_max = NAME_MAX; -# else - return FALSE; -# endif -# elif defined(MAXNAMELEN) - name_max = MAXNAMELEN; -# else -# if defined(NAME_MAX) - name_max = NAME_MAX; -# else -# error "buffer size for readdir_r cannot be determined" -# endif -# endif - if (size) - *size = (size_t)offsetof(struct dirent, d_name) + name_max + 1; - else - return FALSE; - - return TRUE; -} - /** * Get next file in the directory. Will not return "." or ".." on * UNIX. If an error occurs, the contents of "filename" are * undefined. The error is never set if the function succeeds. * + * This function is not re-entrant, and not necessarily thread-safe. + * Only use it for test code or single-threaded utilities. + * * @param iter the iterator * @param filename string to be set to the next file in the dir * @param error return location for error @@ -665,37 +713,24 @@ _dbus_directory_get_next_file (DBusDirIter *iter, DBusString *filename, DBusError *error) { - struct dirent *d, *ent; - size_t buf_size; + struct dirent *ent; int err; _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (!dirent_buf_size (iter->d, &buf_size)) - { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Can't calculate buffer size when reading directory"); - return FALSE; - } - - d = (struct dirent *)dbus_malloc (buf_size); - if (!d) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, - "No memory to read directory entry"); - return FALSE; - } again: - err = readdir_r (iter->d, d, &ent); - if (err || !ent) + errno = 0; + ent = readdir (iter->d); + + if (!ent) { + err = errno; + if (err != 0) dbus_set_error (error, _dbus_error_from_errno (err), "%s", _dbus_strerror (err)); - dbus_free (d); return FALSE; } else if (ent->d_name[0] == '.' && @@ -709,12 +744,10 @@ _dbus_directory_get_next_file (DBusDirIter *iter, { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, "No memory to read directory entry"); - dbus_free (d); return FALSE; } else { - dbus_free (d); return TRUE; } } @@ -1059,11 +1092,11 @@ string_squash_nonprintable (DBusString *str) for (i = 0; i < len; i++) { - unsigned char c = (unsigned char) buf[i]; + unsigned char c = (unsigned char) buf[i]; if (c == '\0') - c = ' '; + buf[i] = ' '; else if (c < 0x20 || c > 127) - c = '?'; + buf[i] = '?'; } } @@ -1133,10 +1166,10 @@ _dbus_command_for_pid (unsigned long pid, goto fail; string_squash_nonprintable (&cmdline); - + if (!_dbus_string_copy (&cmdline, 0, str, _dbus_string_get_length (str))) goto oom; - + _dbus_string_free (&cmdline); _dbus_string_free (&path); return TRUE;