From d57e96b9ed9a308e940e85df148a01e558cfc1f9 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 10 Feb 2012 12:44:19 +0000 Subject: [PATCH] Replace a series of booleans, all (apparently) alike, with flags This makes it a bit clearer what's going on. Signed-off-by: Simon McVittie --- bus/bus.c | 22 ++++++++++++-------- bus/bus.h | 14 ++++++------ bus/main.c | 63 ++++++++++++++++++++++++++++++++++++++++-------------------- bus/test.c | 2 +- 4 files changed, 63 insertions(+), 38 deletions(-) diff --git a/bus/bus.c b/bus/bus.c index ae2a61a..e80e708 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -269,8 +269,7 @@ static dbus_bool_t process_config_first_time_only (BusContext *context, BusConfigParser *parser, const DBusString *address, - dbus_bool_t systemd_activation, - dbus_bool_t write_pidfile, + BusContextFlags flags, DBusError *error) { DBusString log_prefix; @@ -290,7 +289,10 @@ process_config_first_time_only (BusContext *context, _dbus_init_system_log (); - context->systemd_activation = systemd_activation; + if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION) + context->systemd_activation = TRUE; + else + context->systemd_activation = FALSE; /* Check for an existing pid file. Of course this is a race; * we'd have to use fcntl() locks on the pid file to @@ -298,7 +300,7 @@ process_config_first_time_only (BusContext *context, * before overwriting any existing sockets, etc. */ - if (write_pidfile) + if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE) pidfile = bus_config_parser_get_pidfile (parser); if (pidfile != NULL) @@ -698,17 +700,18 @@ process_config_postinit (BusContext *context, BusContext* bus_context_new (const DBusString *config_file, - ForceForkSetting force_fork, + BusContextFlags flags, DBusPipe *print_addr_pipe, DBusPipe *print_pid_pipe, const DBusString *address, - dbus_bool_t systemd_activation, - dbus_bool_t write_pidfile, DBusError *error) { BusContext *context; BusConfigParser *parser; + _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 || + (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0); + _DBUS_ASSERT_ERROR_IS_CLEAR (error); context = NULL; @@ -757,7 +760,7 @@ bus_context_new (const DBusString *config_file, goto failed; } - if (!process_config_first_time_only (context, parser, address, systemd_activation, write_pidfile, error)) + if (!process_config_first_time_only (context, parser, address, flags, error)) { _DBUS_ASSERT_ERROR_IS_SET (error); goto failed; @@ -852,7 +855,8 @@ bus_context_new (const DBusString *config_file, if (context->pidfile) _dbus_string_init_const (&u, context->pidfile); - if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS) + if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) || + (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS)) { _dbus_verbose ("Forking and becoming daemon\n"); diff --git a/bus/bus.h b/bus/bus.h index c476012..3597884 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -66,18 +66,18 @@ typedef struct typedef enum { - FORK_FOLLOW_CONFIG_FILE, - FORK_ALWAYS, - FORK_NEVER -} ForceForkSetting; + BUS_CONTEXT_FLAG_NONE = 0, + BUS_CONTEXT_FLAG_FORK_ALWAYS = (1 << 1), + BUS_CONTEXT_FLAG_FORK_NEVER = (1 << 2), + BUS_CONTEXT_FLAG_WRITE_PID_FILE = (1 << 3), + BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION = (1 << 4) +} BusContextFlags; BusContext* bus_context_new (const DBusString *config_file, - ForceForkSetting force_fork, + BusContextFlags flags, DBusPipe *print_addr_pipe, DBusPipe *print_pid_pipe, const DBusString *address, - dbus_bool_t systemd_activation, - dbus_bool_t write_pidfile, DBusError *error); dbus_bool_t bus_context_reload_config (BusContext *context, DBusError *error); diff --git a/bus/main.c b/bus/main.c index 1e3abae..ca0be3c 100644 --- a/bus/main.c +++ b/bus/main.c @@ -355,9 +355,7 @@ main (int argc, char **argv) int i; dbus_bool_t print_address; dbus_bool_t print_pid; - int force_fork; - dbus_bool_t systemd_activation; - dbus_bool_t write_pidfile; + BusContextFlags flags; if (!_dbus_string_init (&config_file)) return 1; @@ -373,9 +371,8 @@ main (int argc, char **argv) print_address = FALSE; print_pid = FALSE; - force_fork = FORK_FOLLOW_CONFIG_FILE; - systemd_activation = FALSE; - write_pidfile = TRUE; + + flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE; prev_arg = NULL; i = 1; @@ -386,19 +383,35 @@ main (int argc, char **argv) if (strcmp (arg, "--help") == 0 || strcmp (arg, "-h") == 0 || strcmp (arg, "-?") == 0) - usage (); + { + usage (); + } else if (strcmp (arg, "--version") == 0) - version (); + { + version (); + } else if (strcmp (arg, "--introspect") == 0) - introspect (); + { + introspect (); + } else if (strcmp (arg, "--nofork") == 0) - force_fork = FORK_NEVER; + { + flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS; + flags |= BUS_CONTEXT_FLAG_FORK_NEVER; + } else if (strcmp (arg, "--fork") == 0) - force_fork = FORK_ALWAYS; + { + flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER; + flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS; + } else if (strcmp (arg, "--nopidfile") == 0) - write_pidfile = FALSE; + { + flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE; + } else if (strcmp (arg, "--systemd-activation") == 0) - systemd_activation = TRUE; + { + flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION; + } else if (strcmp (arg, "--system") == 0) { check_two_config_files (&config_file, "system"); @@ -434,7 +447,9 @@ main (int argc, char **argv) exit (1); } else if (strcmp (arg, "--config-file") == 0) - ; /* wait for next arg */ + { + /* wait for next arg */ + } else if (strstr (arg, "--address=") == arg) { const char *file; @@ -456,7 +471,9 @@ main (int argc, char **argv) exit (1); } else if (strcmp (arg, "--address") == 0) - ; /* wait for next arg */ + { + /* wait for next arg */ + } else if (strstr (arg, "--print-address=") == arg) { const char *desc; @@ -482,7 +499,9 @@ main (int argc, char **argv) print_address = TRUE; } else if (strcmp (arg, "--print-address") == 0) - print_address = TRUE; /* and we'll get the next arg if appropriate */ + { + print_address = TRUE; /* and we'll get the next arg if appropriate */ + } else if (strstr (arg, "--print-pid=") == arg) { const char *desc; @@ -508,9 +527,13 @@ main (int argc, char **argv) print_pid = TRUE; } else if (strcmp (arg, "--print-pid") == 0) - print_pid = TRUE; /* and we'll get the next arg if appropriate */ + { + print_pid = TRUE; /* and we'll get the next arg if appropriate */ + } else - usage (); + { + usage (); + } prev_arg = arg; @@ -574,11 +597,9 @@ main (int argc, char **argv) } dbus_error_init (&error); - context = bus_context_new (&config_file, force_fork, + context = bus_context_new (&config_file, flags, &print_addr_pipe, &print_pid_pipe, _dbus_string_get_length(&address) > 0 ? &address : NULL, - systemd_activation, - write_pidfile, &error); _dbus_string_free (&config_file); if (context == NULL) diff --git a/bus/test.c b/bus/test.c index 19caa02..1ca9607 100644 --- a/bus/test.c +++ b/bus/test.c @@ -292,7 +292,7 @@ bus_context_new_test (const DBusString *test_data_dir, } dbus_error_init (&error); - context = bus_context_new (&config_file, FALSE, NULL, NULL, NULL, FALSE, FALSE, &error); + context = bus_context_new (&config_file, BUS_CONTEXT_FLAG_NONE, NULL, NULL, NULL, &error); if (context == NULL) { _DBUS_ASSERT_ERROR_IS_SET (&error); -- 1.7.9