From df01c98cc7e83f3336e501fcf2eeee52c95464fb Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Sun, 4 May 2003 08:54:24 +0000 Subject: 2003-05-04 Havoc Pennington * tools/dbus-launch.c: implement * bus/main.c (main), bus/bus.c (bus_context_new): implement --print-pid and --fork --- bus/bus.c | 40 +++++++++++++++++++++++++- bus/bus.h | 2 ++ bus/dbus-daemon-1.1.in | 16 +++++++++-- bus/main.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++-- bus/test.c | 2 +- 5 files changed, 130 insertions(+), 6 deletions(-) (limited to 'bus') diff --git a/bus/bus.c b/bus/bus.c index cabc0b18..85d737da 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -280,7 +280,9 @@ setup_server (BusContext *context, BusContext* bus_context_new (const DBusString *config_file, + dbus_bool_t force_fork, int print_addr_fd, + int print_pid_fd, DBusError *error) { BusContext *context; @@ -540,7 +542,7 @@ bus_context_new (const DBusString *config_file, _dbus_assert (context->policy != NULL); /* Now become a daemon if appropriate */ - if (bus_config_parser_get_fork (parser)) + if (force_fork || bus_config_parser_get_fork (parser)) { DBusString u; @@ -567,6 +569,42 @@ bus_context_new (const DBusString *config_file, /* keep around the pid filename so we can delete it later */ context->pidfile = _dbus_strdup (pidfile); + /* Write PID if requested */ + if (print_pid_fd >= 0) + { + DBusString pid; + int bytes; + + if (!_dbus_string_init (&pid)) + { + BUS_SET_OOM (error); + goto failed; + } + + if (!_dbus_string_append_int (&pid, _dbus_getpid ()) || + !_dbus_string_append (&pid, "\n")) + { + _dbus_string_free (&pid); + BUS_SET_OOM (error); + goto failed; + } + + bytes = _dbus_string_get_length (&pid); + if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Printing message bus PID: %s\n", + _dbus_strerror (errno)); + _dbus_string_free (&pid); + goto failed; + } + + if (print_pid_fd > 2) + _dbus_close (print_pid_fd, NULL); + + _dbus_string_free (&pid); + } + /* Here we change our credentials if required, * as soon as we've set up our sockets and pidfile */ diff --git a/bus/bus.h b/bus/bus.h index 747e009f..8f32d7a9 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -57,7 +57,9 @@ typedef struct } BusLimits; BusContext* bus_context_new (const DBusString *config_file, + dbus_bool_t force_fork, int print_addr_fd, + int print_pid_fd, DBusError *error); void bus_context_shutdown (BusContext *context); void bus_context_ref (BusContext *context); diff --git a/bus/dbus-daemon-1.1.in b/bus/dbus-daemon-1.1.in index 3339aebc..73a88c90 100644 --- a/bus/dbus-daemon-1.1.in +++ b/bus/dbus-daemon-1.1.in @@ -9,7 +9,7 @@ dbus-daemon-1 \- Message bus daemon .PP .B dbus-daemon-1 dbus-daemon-1 [\-\-version] [\-\-session] [\-\-system] [\-\-config-file=FILE] -[\-\-print-address[=DESCRIPTOR]] +[\-\-print-address[=DESCRIPTOR]] [\-\-print-pid[=DESCRIPTOR]] [\-\-fork] .SH DESCRIPTION @@ -62,11 +62,22 @@ The following options are supported: .I "--config-file=FILE" Use the given configuration file. .TP +.I "--fork" +Force the message bus to fork and become a daemon, even if +the configuration file does not specify that it should. +In most contexts the configuration file already gets this +right, though. +.TP .I "--print-address[=DESCRIPTOR]" Print the address of the message bus to standard output, or to the given file descriptor. This is used by programs that launch the message bus. .TP +.I "--print-pid[=DESCRIPTOR]" +Print the process ID of the message bus to standard output, or +to the given file descriptor. This is used by programs that +launch the message bus. +.TP .I "--session" Use the standard configuration file for the per-login-session message bus. @@ -185,7 +196,8 @@ privileges for writing. .PP If present, the bus daemon becomes a real daemon (forks -into the background, etc.). +into the background, etc.). This is generally used +rather than the \-\-fork command line option. .TP .I "" diff --git a/bus/main.c b/bus/main.c index de78368b..8c605be3 100644 --- a/bus/main.c +++ b/bus/main.c @@ -47,7 +47,7 @@ signal_handler (int sig) static void usage (void) { - fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]]\n"); + fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n"); exit (1); } @@ -86,24 +86,45 @@ check_two_addr_descriptors (const DBusString *addr_fd, } } +static void +check_two_pid_descriptors (const DBusString *pid_fd, + const char *extra_arg) +{ + if (_dbus_string_get_length (pid_fd) > 0) + { + fprintf (stderr, "--%s specified but printing pid to %s already requested\n", + extra_arg, _dbus_string_get_const_data (pid_fd)); + exit (1); + } +} + int main (int argc, char **argv) { DBusError error; DBusString config_file; DBusString addr_fd; + DBusString pid_fd; const char *prev_arg; int print_addr_fd; + int print_pid_fd; int i; dbus_bool_t print_address; + dbus_bool_t print_pid; + dbus_bool_t force_fork; if (!_dbus_string_init (&config_file)) return 1; if (!_dbus_string_init (&addr_fd)) return 1; + + if (!_dbus_string_init (&pid_fd)) + return 1; print_address = FALSE; + print_pid = FALSE; + force_fork = FALSE; prev_arg = NULL; i = 1; @@ -117,6 +138,8 @@ main (int argc, char **argv) usage (); else if (strcmp (arg, "--version") == 0) version (); + else if (strcmp (arg, "--fork") == 0) + force_fork = TRUE; else if (strcmp (arg, "--system") == 0) { check_two_config_files (&config_file, "system"); @@ -179,6 +202,32 @@ main (int argc, char **argv) } else if (strcmp (arg, "--print-address") == 0) print_address = TRUE; /* and we'll get the next arg if appropriate */ + else if (strstr (arg, "--print-pid=") == arg) + { + const char *desc; + + check_two_pid_descriptors (&pid_fd, "print-pid"); + + desc = strchr (arg, '='); + ++desc; + + if (!_dbus_string_append (&pid_fd, desc)) + exit (1); + + print_pid = TRUE; + } + else if (prev_arg && + strcmp (prev_arg, "--print-pid") == 0) + { + check_two_pid_descriptors (&pid_fd, "print-pid"); + + if (!_dbus_string_append (&pid_fd, arg)) + exit (1); + + print_pid = TRUE; + } + else if (strcmp (arg, "--print-pid") == 0) + print_pid = TRUE; /* and we'll get the next arg if appropriate */ else usage (); @@ -213,9 +262,32 @@ main (int argc, char **argv) print_addr_fd = val; } } + + print_pid_fd = -1; + if (print_pid) + { + print_pid_fd = 1; /* stdout */ + if (_dbus_string_get_length (&pid_fd) > 0) + { + long val; + int end; + if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) || + end != _dbus_string_get_length (&pid_fd) || + val < 0 || val > _DBUS_INT_MAX) + { + fprintf (stderr, "Invalid file descriptor: \"%s\"\n", + _dbus_string_get_const_data (&pid_fd)); + exit (1); + } + + print_pid_fd = val; + } + } dbus_error_init (&error); - context = bus_context_new (&config_file, print_addr_fd, &error); + context = bus_context_new (&config_file, force_fork, + print_addr_fd, print_pid_fd, + &error); _dbus_string_free (&config_file); if (context == NULL) { diff --git a/bus/test.c b/bus/test.c index 012d1d0e..f8d4c5f3 100644 --- a/bus/test.c +++ b/bus/test.c @@ -377,7 +377,7 @@ bus_context_new_test (const DBusString *test_data_dir, } dbus_error_init (&error); - context = bus_context_new (&config_file, -1, &error); + context = bus_context_new (&config_file, FALSE, -1, -1, &error); if (context == NULL) { _DBUS_ASSERT_ERROR_IS_SET (&error); -- cgit