Commit df01c98c authored by Havoc Pennington's avatar Havoc Pennington

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
parent c0158234
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
2003-05-03 Havoc Pennington <hp@redhat.com>
* dbus/dbus-address.c (dbus_parse_address): fix bug when a key in
......
......@@ -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);
......
......@@ -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);
......
......@@ -440,10 +440,6 @@ AC_SUBST(DBUS_QT_LIBS)
### X11 detection
AC_PATH_XTRA
DBUS_X_LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
DBUS_X_CFLAGS="$X_CFLAGS"
AC_SUBST(DBUS_X_CFLAGS)
AC_SUBST(DBUS_X_LIBS)
## for now enable_x11 just tracks have_x11,
## there's no --enable-x11
......@@ -457,8 +453,16 @@ fi
if test x$enable_x11 = xyes ; then
AC_DEFINE(DBUS_BUILD_X11,1,[Build X11-dependent code])
DBUS_X_LIBS="$X_LIBS $X_PRE_LIBS -lX11 $X_EXTRA_LIBS"
DBUS_X_CFLAGS="$X_CFLAGS"
else
DBUS_X_LIBS=
DBUS_X_CFLAGS=
fi
AC_SUBST(DBUS_X_CFLAGS)
AC_SUBST(DBUS_X_LIBS)
### Documentation
AC_PATH_PROG(DOXYGEN, doxygen, no)
......
......@@ -19,7 +19,8 @@ dbus_launch_SOURCES= \
dbus_send_LDADD= $(top_builddir)/dbus/libdbus-1.la
dbus_monitor_LDADD= $(top_builddir)/glib/libdbus-glib-1.la
dbus_launch_LDADD= $(top_builddir)/dbus/libdbus-1.la
## dbus-launch doesn't link to anything
dbus_launch_LDADD= $(DBUS_X_LIBS)
man_MANS = dbus-send.1 dbus-monitor.1 dbus-launch.1
EXTRA_DIST = $(man_MANS)
......@@ -24,14 +24,19 @@ about D-BUS. See also the man page for \fIdbus-daemon-1\fP.
.PP
Here is an example of how to use \fIdbus-launch\fP with an
sh-compatible shell:
sh-compatible shell to start the per-session bus daemon:
.nf
VARIABLES=`dbus-launch`
eval $VARIABLES
echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
## test for an existing bus daemon, just to be safe
if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
## if not found, launch a new one
eval `dbus-launch --exit-with-session`
echo "D-BUS per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
export DBUS_SESSION_BUS_ADDRESS
fi
.fi
You might run something like that in your login scripts.
.SH OPTIONS
The following options are supported:
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment