Commit graph

78 commits

Author SHA1 Message Date
Alex Richardson
6c1c7e539f Fix no-X11 build after d847b5f6ae
Since that commit the error variable is used in all cases not only the
DBUS_BUILD_X11 #ifdef branches.

Fixes: https://gitlab.freedesktop.org/dbus/dbus/-/issues/392
2022-04-16 16:04:57 +00:00
Simon McVittie
363919696a dbus-launch: If no X11, look for existing bus before failing
What we are aiming for here is that `dbus-launch --autolaunch` will
exit with an error if it would have been unable to advertise the bus
in a way that will be picked up by other instances of itself, so that
we don't get a proliferation of autolaunched dbus-launch instances,
each with its own dbus-daemon, and none of them talking to the others.

However, we don't need to do that *immediately*: we can try to look for
an existing instance first. If we find one, then we can assume that
it's suitable for use, even if X11 autolaunching was disabled at
compile-time or if connecting to the X server failed at runtime. We
only need to bail out if we get to the point where we are ready to start
forking child processes.

This lets systemd users run `dbus-launch --autolaunch` to find a
systemd-managed dbus-daemon, even if X11 autolaunching is disabled.
When combined with the previous commit, it also lets macOS users run
`dbus-launch --autolaunch` to find a launchd-managed dbus-daemon.

Resolves: dbus/dbus#385
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-03-29 14:38:43 +00:00
Simon McVittie
d847b5f6ae dbus-launch: Generalize support for discovering an existing bus
Previously we were looking for an existing bus via
_dbus_lookup_user_bus(), meaning a user bus $XDG_RUNTIME_DIR/bus
(in practice this is managed by systemd --user, although in principle
there's nothing to stop other session frameworks from providing the
same thing).

_dbus_lookup_session_address() looks for an externally-managed
dbus-daemon in a more general way: on macOS it uses launchd, and on other
Unix it's a thin wrapper around _dbus_lookup_user_bus(). Let's try that,
so that macOS users can get their existing dbus-daemon from launchd.

This partially resolves dbus/dbus#385, although initially only for macOS
users who have (unusually) enabled X11 autolaunching support.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-03-29 14:38:43 +00:00
Simon McVittie
94806fb2c7 Don't let dbus-daemon and its subprocesses inherit unnecessary fds
This should avoid test failures under CMake in which the
dbus-daemon inherits an unwanted fd from CMake's test framework, causing
the close-on-exec check before executing activated services to fail.

The dbus-daemon now marks all fds that it inherits, except for its
stdin, stdout and stderr, to be closed on exec. For completeness, the
dbus-daemons run by dbus-run-session and dbus-launch also now inherit
stdin, stdout, stderr and the pipes used to communicate with their
callers, but nothing else.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2018-11-20 19:09:18 +00:00
Simon McVittie
93b7bb392b dbus-launch: Fix unused variable warning when libX11 not present
Closes: https://gitlab.freedesktop.org/dbus/dbus/issues/228
Signed-off-by: Simon McVittie <smcv@collabora.com>
2018-10-18 15:30:56 +00:00
Simon McVittie
13b640544d Unix: Flush stdout and stderr streams before forking
stdout and stderr are close-on-exec and buffered, so we can't rely on
their buffers being empty. If we continue to execute application code
after forking (as opposed to immediately exec()ing), then the child
process might later flush the libc stdio buffers, resulting in
output that is printed by the parent also being printed by the child.

In particular, test-bus.log sometimes grows extremely large for
this reason, because this test repeatedly attempts to carry out
legacy activation.

Reviewed-by: Philip Withnall <withnall@endlessm.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=103601
2017-11-15 12:12:18 +00:00
Simon McVittie
e98d907f7e dbus-launch: Use dbus_try_get_local_machine_id()
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=13194
2017-06-09 13:35:31 +01:00
Simon McVittie
053c48c035 dbus-launch: clarify signal handler
We only register signal_handler() for the three signals that we want
to handle as "kill dbus-daemon and exit", so there's no point in the
switch. Silence -Wswitch-default by removing it altogether.

The variable name got_fatal_signal and the verbose message are both
misleading, because actually this is a handler for multiple signals,
not just SIGHUP. Rename them to be generic.

Based on part of a patch from Thomas Zimmermann.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=98191
2017-04-07 12:07:43 +01:00
Simon McVittie
40560fa0c7 test, tools: assert impossible values of local enums are not reached
Based on part of a patch from Thomas Zimmermann.

Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=98191
2017-04-07 12:07:01 +01:00
Simon McVittie
7959d90719 Add missing function attributes suggested by clang (but not by gcc)
clang is a little more enthusiastic about suggesting these.

Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2016-10-13 22:44:13 +01:00
Thomas Zimmermann
5350b54de7 Fix warnings from compiler option '-Wsuggest-attribute=noreturn'
This patch fixes warnings from '-Wsuggest-attribute=noreturn'. We cannot
enable it unconditionally as it would break libtool.

Signed-off-by: Thomas Zimmermann <tdz@users.sourceforge.net>
2016-10-10 15:11:24 +01:00
Simon McVittie
7c837b1045 dbus-launch: redirect dbus-daemon output to syslog
Otherwise, it won't go anywhere at all.

Signed-off-by: Simon McVittie <smcv@debian.org>
2016-09-30 19:36:50 +01:00
Thomas Zimmermann
6ac1dfb343 dbus-launch: Protect concat2 by DBUS_ENABLE_EMBEDDED_TESTS
Signed-off-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97282
2016-08-11 16:40:58 +01:00
Simon McVittie
fef477f150 dbus-launch: use _dbus_ensure_standard_fds when closing stderr
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97008
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thiago Macieira <thiago@kde.org>
2016-07-25 11:27:03 +01:00
Simon McVittie
f0819f926f dbus-launch: use _dbus_ensure_standard_fds() in the babysitter
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97008
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thiago Macieira <thiago@kde.org>
2016-07-25 11:26:52 +01:00
Simon McVittie
c8f73a2a3a dbus-daemon, dbus-launch: cope with callers having closed standard fds
In Debian bug <https://bugs.debian.org/829348>, lightdm appears to
have been starting dbus-launch with at least one of the three
standard fds 0, 1, 2 (stdin, stdout, stderr) closed. This resulted
in the dbus-daemon's epoll_create1() returning a fd less than 3.
Later, _dbus_become_daemon() replaces fds 0-2 with /dev/null. As a
result, a subsequent call to _dbus_loop_add_watch() for the reload
pipe resulted in calling epoll_ctl on the non-epoll fd pointing to
/dev/null, which fails with EINVAL, resulting in the dbus-daemon
exiting unsuccessfully.

Unix programs are not normally expected to behave correctly when
launched with the standard fds not already open; but at the same time,
X11 autolaunching means that dbus-launch (and hence the dbus-daemon)
can get started from an arbitrarily precarious situation.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97008
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thiago Macieira <thiago@kde.org>
2016-07-25 11:26:36 +01:00
Simon McVittie
b04d9324fb dbus-launch: add --exit-with-x11 option
This is more suitable for distributions' Xsession scripts: it verifies
that X is already available, and so never results in an attempt to poll
stdin.

We read the machine UUID because it is needed to set the X atoms.
x11_init() assumes that the machine UUID (global variable) has been
set, either via read_machine_uuid_if_needed() or save_machine_uuid().
This is pretty tangled, but to make The Right Thing happen
automatically, we'd need to redo dbus-launch in terms of DBusError.

Reviewed-by: Will Thompson
Reviewed-by: Thiago Macieira
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39197
Signed-off-by: Simon McVittie <smcv@debian.org>
2016-07-20 13:32:36 +01:00
Simon McVittie
c7f3df0271 Replace $DBUS_USE_TEST_BINARY with $DBUS_TEST_DBUS_LAUNCH
Instead of using $DBUS_USE_TEST_BINARY to control whether to use the
hard-coded test binary TEST_BUS_LAUNCH_BINARY, we can just use
$DBUS_TEST_DBUS_LAUNCH to control what we launch directly, as we
were already doing for $DBUS_TEST_DAEMON.

Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=92899
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
2016-02-12 15:26:23 +00:00
Simon McVittie
160f5bb9d2 dbus-launch: if autolaunching, use XDG_RUNTIME_DIR/bus if available
This provides backwards-compatible autolaunching behaviour, as long
as dbus-launch inherits the XDG_RUNTIME_DIR (which it presumably did
if it's going to work at all, since it must also have inherited the
DISPLAY). In particular, we go through the motions of starting the
dbus-daemon, so that we can start the "babysitter" process that will
maintain the X11 window to store the bus address.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=61301
Reviewed-by: Philip Withnall <philip.withnall@collabora.co.uk>
[smcv: decorate _dbus_lookup_user_bus with DBUS_PRIVATE_EXPORT so we
can still call it after fixing fd.o#83115; update cmake to match Autotools]
2015-02-24 11:25:50 +00:00
Simon McVittie
71c11a9e48 dbus-launch: use libdbus to read the UUID
As a side benefit, this means that dbus-launch now understands
/etc/machine-id and not just /var/lib/dbus/machine-id.

Since machine_uuid comes out of libdbus allocated with dbus_malloc,
to avoid having to copy it from malloc-allocated to
dbus_malloc-allocated storage, it makes sense to change it to be
consistently dbus_malloc-allocated (particularly now that Bug #83115
has made use of internal symbols relatively painless). However, I'm
deliberately not changing the allocation model of any other strings
in dbus-launch right now; that's a larger yak-shaving exercise.
2015-02-24 11:16:13 +00:00
Роман Донченко
5c7ea35c3d dbus-launch: kill bus if we can't attach to a session when requested
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=74698
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2014-06-11 11:38:24 +01:00
Роман Донченко
3be6063729 Avoid killing all available processes if an X error arrives early on
The timeline of events in dbus-launch's main process goes something like this:

* do initial X calls
[1]
* do some other stuff
* fork
    (child process starts doing some other stuff)
* return "intermediate parent" pid from fork()
* obtain bus daemon pid from bus_pid_to_launcher_pipe
[2]
* do things that might include X11 calls or killing the dbus-daemon

Meanwhile, the "babysitter" child goes like this:

* return 0 from fork()
[3]
* obtain bus daemon pid from parent process via bus_pid_to_babysitter_pipe
[4]
* do things that might include X11 calls or killing the bus daemon

Before [1] or [3], the right thing to do about an X error is to just
exit. The current implementation called kill(-1) first, which is
undesirable: it kills unrelated processes. With this change, we
just exit.

After [2] or [4], the right thing to do is to kill the dbus-daemon,
and that's what the existing code did.

Between [1] and [2], or between [3] and [4], there is no correct thing
that we can do immediately: we would have to wait for the end of the
"critical section", *then* kill the dbus-daemon. This has not yet been
implemented, so this patch relies for its correctness on the fact that
there are no libX11 calls between those points, so we cannot receive
an X error between them.

dbus-launch deserves more comments, or a reimplementation that is easier to
understand, but this change is certainly better than nothing.

[Commit message added, summarizing reviewers' comments -smcv]
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=74698
Reviewed-by: Simon McVittie
Reviewed-by: Thiago Macieira
2014-04-30 19:11:56 +01:00
Chengwei Yang
47df159cc5 Unify the way to find dbus-daemon test binary
There are two ways to find the dbus-daemon for testing. The first one is
defined as string at compile stage and the second one is export it from
test environment.

The first way has limitation that after defined, it's static string, so
it's impossible to run installable check. So let's unify to the second
way.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37849
[added missing "}" -smcv]
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2013-10-08 16:08:57 +01:00
Simon McVittie
f81cd4a18b When using dbus-launch for tests, fail hard if test binary is missing
We want to test the version-under-test, not the system version.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37849
Reviewed-by: Chengwei Yang <chengwei.yang@intel.com>
2013-10-08 11:06:18 +01:00
Simon McVittie
580ed1c8fb dbus-launch: avoid asprintf(), and die gracefully on out-of-memory
asprintf() is a GNU extension (non-portable).

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37849
Reviewed-by: Chengwei Yang <chengwei.yang@intel.com>
2013-10-08 11:06:18 +01:00
Chengwei Yang
fd5271f839 Fix build on NetBSD 6.1.1 with gcc 4.5.3
There are two build failure on NetBSD 6.1.1 with gcc 4.5.3, the first
one is char to int, warning treated as error. The second one is a mismatch
between format string and arguments.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=69842
[adjusted commit message -smcv]
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2013-10-08 10:29:20 +01:00
Chengwei Yang
ee0f28f6a5 Use test binaries in build dir to do test
When do autolaunch testing, libdbus will try to start dbus-launch in
installed direcotry, if fail then fall back to dbus-launch in $PATH.
dbus-launch does a relative better thing to start dbus-daemon in build
directory, however, in most of case, the build $prefix is different from
the real prefix where dbus-daemon installed. So dbus-daemon will fail to
start due to can't find its config file. And then dbus-launch will fall
back to finally the installed dbus-daemon.

This patch fix this behavior and will start dbus-launch and dbus-daemon
in build directory in test environment.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=37849
2013-09-13 14:29:45 +01:00
Chengwei Yang
fbeac8820a dbus-launch: unconditionally use SIGHUP and free memory on OOM
In a previous patch, it check SIGHUP for windows, however, in fact there
is dbus-launch-win.c supposed to be used on windows. So just use SIGHUP
unconditionally.

Also free memory on OOM, although this doesn't make much sense since
this is a oneshort program, rather than a daemon.

Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
[fixed whitespace -smcv]
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66068
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2013-09-05 14:01:46 +01:00
Chengwei Yang
11c5dc126e tests to embedded tests: replaced in tools
Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66291
2013-06-28 12:14:00 +01:00
Chengwei Yang
eec093bbdb dbus-launch: do not verbose output if build with verbose mode disabled
Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66068
2013-06-26 15:23:35 +01:00
Chengwei Yang
8aa07c7e48 dbus-launch: align document
Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66068
2013-06-26 15:23:11 +01:00
Chengwei Yang
973684f022 dbus-launch: fix coding style
Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=66068
2013-06-26 15:22:35 +01:00
Simon McVittie
57db30169f Fix Werror=unused-function if build without X11
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=65712
Signed-off-by: Chengwei Yang <chengwei.yang@intel.com>
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2013-06-13 16:18:30 +01:00
Simon McVittie
863c989bb6 Fix compiler warnings when X11 autolaunch and launchd are both disabled
From the department of "if it isn't tested, it doesn't work". I tried
compiling dbus without an assortment of optional features:

    in_builddir ~/build/dbus/legacy ${MR_REPO}/configure \
        --enable-developer --enable-maintainer-mode --enable-tests \
        dbus_cv_sync_sub_and_fetch=no \
        --disable-selinux \
        --disable-inotify \
        --disable-dnotify \
        --disable-epoll \
        --disable-kqueue \
        --disable-launchd \
        --disable-systemd \
        --disable-libaudit \
        --without-valgrind \
        --disable-x11-autolaunch \
        && ...

and it resulted in -Wunused warnings.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=64362
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
Reviewed-by: Thiago Macieira <thiago@kde.org>
2013-05-10 11:34:42 +01:00
Michel HERMIER
9a9b0e2736 Don't leak temporary fds pointing to /dev/null
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=56927
[commit message added -smcv]
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
2012-11-09 16:02:01 +00:00
Simon McVittie
6ec9900bc1 Revert "dbus-launch: add --exit-with-x11 option"
This reverts commit fcc656d430.
2012-06-27 11:31:22 +01:00
Simon McVittie
fcc656d430 dbus-launch: add --exit-with-x11 option
This is more suitable for distributions' Xsession scripts: it verifies
that X is already available, and so never results in an attempt to poll
stdin.

Reviewed-by: Will Thompson <will.thompson@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39197
2012-06-15 13:10:16 +01:00
Simon McVittie
1785b37e43 dbus-launch: if using X to define the session lifetime, do not poll stdin
dbus-launch --exit-with-session attempts to scope the session length
to various things:

- if DISPLAY points to an X server, exit when the X session ends
- if stdin is a terminal, exit when end-of-file is reached
- if both are true, exit when one of them happens, whichever is first
- if neither is true, fail

These are not particularly useful semantics: if the session is scoped to
the X session, then the terminal from which dbus-launch was launched
is irrelevant. This also causes practical problems when dbus-launch
consumes characters from the terminal from which it happens to have
been launched (some display managers, like slim and nodm, run users' X
sessions with stdin pointing to the terminal from which the init daemon
happens to have started the display manager during boot, usually tty1
on Linux).

Reviewed-by: Will Thompson <will.thompson@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39197
2012-06-15 13:09:58 +01:00
Simon McVittie
ab0cebd883 document how the various processes in dbus-launch interact
Reviewed-by: Will Thompson <will.thompson@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=39197
2012-06-15 13:09:14 +01:00
Simon McVittie
cea055514a Check for X even if X11 autolaunching is disabled
DBUS_ENABLE_X11_AUTOLAUNCH obviously requires DBUS_BUILD_X11. However,
the converse is not true.

If DBUS_BUILD_X11 is defined, dbus-launch will be able to connect to
the X server to determine when the session ends; most distributors will
want this, but it can be disabled with the standard Autoconf option
--without-x.

If DBUS_ENABLE_X11_AUTOLAUNCH is *also* defined, dbus-launch and libdbus
will be willing to perform autolaunch. Again, most distributors will want
this, but it can be disabled with --disable-x11-autolaunch.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=19997
Reviewed-by: Colin Walters <walters@verbum.org>
2011-05-25 17:38:44 +01:00
Simon McVittie
c559ec0525 dbus-launch: pass_info: always free strings on OOM
This doesn't really do anything, because we're about to exit anyway, but
it placates static analysis tools.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=29881
Bug-NB: NB#180486
Reviewed-by: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
2011-05-25 16:13:19 +01:00
Christian Dywan
b1f947da1a Free envvar and args in pass_info in the oom case 2010-10-05 13:47:07 +01:00
Havoc Pennington
ade5c7ad33 fix some "unused result" warnings in dbus-launch and dbus-spawn
Needed to avoid -Werror
2010-09-06 19:30:42 +02:00
Marcus Brinkmann
dbecdeabb2 Consistently include <config.h> in all C source files and never in header files. 2010-03-19 20:11:48 +01:00
Sascha Silbe
9b2c196ef3 Bug 23977 - dbus-launch --exit-with-session not killing dbus-daemon on SIGINT
Handle SIGINT in the same way we handle SIGTERM.
2009-10-16 15:20:43 -04:00
Tobias Mueller
dc33f4f774 Bug 21161 - Update the FSF address
No comment.

Signed-off-by: Colin Walters <walters@verbum.org>
(cherry picked from commit 5baf2f856a)
2009-07-14 15:39:47 -04:00
Kjartan Maraas
eb3b99e7c6 Bug 19502 - Sparse warning cleanups
This patch makes various things that should be static static,
corrects some "return FALSE" where it should be NULL, etc.

Signed-off-by: Colin Walters <walters@verbum.org>
2009-05-06 12:51:18 -04:00
Tor Lillqvist
1000270072 [win32] Protect usage of SIGHUP with #ifdef
Signed-off-by: Colin Walters <walters@verbum.org>
2009-01-06 18:21:25 -05:00
Colin Walters
0bfc5e8d23 Check for X11 events before selecting (FDO bug #15293) 2008-03-31 18:20:07 -04:00
John (J5) Palmieri
46b48ac654 tools/dbus-launch.c: wrap X'ism in #ifdef so we can compile without X 2008-03-04 10:03:14 -05:00