diff options
author | Havoc Pennington <hp@redhat.com> | 2003-05-04 08:54:24 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2003-05-04 08:54:24 +0000 |
commit | df01c98cc7e83f3336e501fcf2eeee52c95464fb (patch) | |
tree | e92ccc82056908b25eb9cab6e9ad763b8febd9dc /bus | |
parent | c0158234d046381a6bc8c004c82577576977d0d7 (diff) |
2003-05-04 Havoc Pennington <hp@pobox.com>
* tools/dbus-launch.c: implement
* bus/main.c (main), bus/bus.c (bus_context_new):
implement --print-pid and --fork
Diffstat (limited to 'bus')
-rw-r--r-- | bus/bus.c | 40 | ||||
-rw-r--r-- | bus/bus.h | 2 | ||||
-rw-r--r-- | bus/dbus-daemon-1.1.in | 16 | ||||
-rw-r--r-- | bus/main.c | 76 | ||||
-rw-r--r-- | bus/test.c | 2 |
5 files changed, 130 insertions, 6 deletions
@@ -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 */ @@ -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 "<listen>" @@ -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) { @@ -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); |