From 0bc3c8a0f6bf2ba41b9b3e83f8ead254e593cfbb Mon Sep 17 00:00:00 2001 From: Chengwei Yang Date: Mon, 26 Aug 2013 16:00:33 +0800 Subject: [PATCH] Set FD_CLOEXEC for dbus-daemon stdin/stdout/stderr If dbus-daemon isn't running in daemonized mode, it's stdin/stdout/stderr keep open when spawning service, so the child service will inherit it's stdin/stdout/stderr and so that the child output to stdout/stderr will to dbus-daemon' stdout/stderr. That's result a very confusing output in systemd environment. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68559 --- bus/bus.c | 4 ++++ dbus/dbus-sysdeps-util-unix.c | 24 ++++++++++++++++++++++++ dbus/dbus-sysdeps.h | 2 ++ 3 files changed, 30 insertions(+) diff --git a/bus/bus.c b/bus/bus.c index 307c158..53d005d 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -873,6 +873,10 @@ bus_context_new (const DBusString *config_file, { _dbus_verbose ("Fork not requested\n"); +#ifdef DBUS_UNIX + _dbus_std_cloexec (); +#endif + /* Need to write PID file and to PID pipe for ourselves, * not for the child process. This is a no-op if the pidfile * is NULL and print_pid_pipe is NULL. diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c index 9ad63b4..90c6b9f 100644 --- a/dbus/dbus-sysdeps-util-unix.c +++ b/dbus/dbus-sysdeps-util-unix.c @@ -164,6 +164,30 @@ _dbus_become_daemon (const DBusString *pidfile, /** + * Set close at exec flag for dbus-daemon stdin/stdout/stderr to + * prevent inherited by activated child service. + * Bug: + */ +void +_dbus_std_cloexec (void) +{ + int fd_flags = -1; + + fd_flags = fcntl (0, F_GETFD); + if (fd_flags >= 0) + fcntl (0, F_SETFD, fd_flags | FD_CLOEXEC); + + fd_flags = fcntl (1, F_GETFD); + if (fd_flags >= 0) + fcntl (1, F_SETFD, fd_flags | FD_CLOEXEC); + + fd_flags = fcntl (2, F_GETFD); + if (fd_flags >= 0) + fcntl (2, F_SETFD, fd_flags | FD_CLOEXEC); +} + + +/** * Creates a file containing the process ID. * * @param filename the filename to write to diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index e586946..9227415 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -425,6 +425,8 @@ dbus_bool_t _dbus_become_daemon (const DBusString *pidfile, DBusError *error, dbus_bool_t keep_umask); +void _dbus_std_cloexec (void); + dbus_bool_t _dbus_verify_daemon_user (const char *user); dbus_bool_t _dbus_change_to_daemon_user (const char *user, DBusError *error); -- 1.7.9.5