From 97dc7aae94865ed505899ad397735142575c5c1c Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sun, 19 Dec 2010 13:24:55 -0500 Subject: [PATCH] dbus-launch: Refactor duplicate code De-duplicate execl() invocations of dbus-daemon. https://bugs.freedesktop.org/show_bug.cgi?id=32509 --- tools/dbus-launch.c | 86 +++++++++++++++++++++++++++++++++------------------ 1 files changed, 56 insertions(+), 30 deletions(-) diff --git a/tools/dbus-launch.c b/tools/dbus-launch.c index ce9c7b1..00815f9 100644 --- a/tools/dbus-launch.c +++ b/tools/dbus-launch.c @@ -38,6 +38,8 @@ #include #include +#include + #ifdef DBUS_BUILD_X11 #include extern Display *xdisplay; @@ -748,6 +750,42 @@ oom: #define READ_END 0 #define WRITE_END 1 +static int +exec_dbus_daemon (dbus_bool_t use_path, + const char *binary, + dbus_bool_t autolaunch, + const char *write_pid_fd_as_string, + const char *write_address_fd_as_string, + const char *config_file) +{ + const char *argv[24]; + int i; + + i = 0; + argv[i++] = binary; + argv[i++] = "--fork"; + argv[i++] = "--print-pid"; + argv[i++] = write_pid_fd_as_string, + argv[i++] = "--print-address"; + argv[i++] = write_address_fd_as_string; + if (config_file) + { + argv[i++] = "--config-file"; + argv[i++] = config_file; + } + else + { + argv[i++] = "--session"; + } + + argv[i++] = NULL; + + if (use_path) + return execvp (binary, (char **)argv); + else + return execv (binary, (char **)argv); +} + int main (int argc, char **argv) { @@ -1045,14 +1083,12 @@ main (int argc, char **argv) /* exec from testdir */ if (getenv("DBUS_USE_TEST_BINARY") != NULL) { - execl (TEST_BUS_BINARY, - TEST_BUS_BINARY, - "--fork", - "--print-pid", write_pid_fd_as_string, - "--print-address", write_address_fd_as_string, - config_file ? "--config-file" : "--session", - config_file, /* has to be last in this varargs list */ - NULL); + exec_dbus_daemon (FALSE, + TEST_BUS_BINARY, + autolaunch, + write_pid_fd_as_string, + write_address_fd_as_string, + config_file); fprintf (stderr, "Failed to execute test message bus daemon %s: %s. Will try again with the system path.\n", @@ -1060,33 +1096,23 @@ main (int argc, char **argv) } #endif /* DBUS_BUILD_TESTS */ - execl (DBUS_DAEMONDIR"/dbus-daemon", - DBUS_DAEMONDIR"/dbus-daemon", - "--fork", - "--print-pid", write_pid_fd_as_string, - "--print-address", write_address_fd_as_string, - config_file ? "--config-file" : "--session", - config_file, /* has to be last in this varargs list */ - NULL); + exec_dbus_daemon (FALSE, + DBUS_DAEMONDIR "/dbus-daemon", + autolaunch, + write_pid_fd_as_string, + write_address_fd_as_string, + config_file); fprintf (stderr, "Failed to execute message bus daemon %s: %s. Will try again without full path.\n", DBUS_DAEMONDIR"/dbus-daemon", strerror (errno)); - /* - * If it failed, try running without full PATH. Note this is needed - * because the build process builds the run-with-tmp-session-bus.conf - * file and the dbus-daemon will not be in the install location during - * build time. - */ - execlp ("dbus-daemon", - "dbus-daemon", - "--fork", - "--print-pid", write_pid_fd_as_string, - "--print-address", write_address_fd_as_string, - config_file ? "--config-file" : "--session", - config_file, /* has to be last in this varargs list */ - NULL); + exec_dbus_daemon (TRUE, + "dbus-daemon", + autolaunch, + write_pid_fd_as_string, + write_address_fd_as_string, + config_file); fprintf (stderr, "Failed to execute message bus daemon: %s\n", -- 1.7.3.3