[systemd-devel] [PATCH 1/7] bus_open_transport: use SystemdRunningAs instead of bool

Hristo Venev hristo at venev.name
Sun Dec 1 11:25:52 PST 2013


SYSTEMD_USER/SYSTEMD_SYSTEM is far more clear than true/false
---
 src/analyze/analyze.c         |  8 ++++----
 src/hostname/hostnamectl.c    |  2 +-
 src/libsystemd-bus/bus-util.c | 39 ++++++++++++++++++++++++---------------
 src/libsystemd-bus/bus-util.h |  5 +++--
 src/libsystemd-bus/busctl.c   |  8 ++++----
 src/locale/localectl.c        |  2 +-
 src/login/loginctl.c          |  2 +-
 src/machine/machinectl.c      |  2 +-
 src/run/run.c                 | 10 +++++-----
 src/systemctl/systemctl.c     |  4 ++++
 src/timedate/timedatectl.c    |  2 +-
 11 files changed, 49 insertions(+), 35 deletions(-)

diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index eb85276..3ffd85d 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -73,7 +73,7 @@ static usec_t arg_fuzz = 0;
 static bool arg_no_pager = false;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
 static char *arg_host = NULL;
-static bool arg_user = false;
+static bool arg_as = SYSTEMD_SYSTEM;
 
 struct boot_times {
         usec_t firmware_time;
@@ -1200,11 +1200,11 @@ static int parse_argv(int argc, char *argv[]) {
                         return 0;
 
                 case ARG_USER:
-                        arg_user = true;
+                        arg_as = SYSTEMD_USER;
                         break;
 
                 case ARG_SYSTEM:
-                        arg_user = false;
+                        arg_as = SYSTEMD_SYSTEM;
                         break;
 
                 case ARG_ORDER:
@@ -1271,7 +1271,7 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
+        r = bus_open_transport(arg_transport, arg_host, arg_as, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
index 3e51778..569b253 100644
--- a/src/hostname/hostnamectl.c
+++ b/src/hostname/hostnamectl.c
@@ -476,7 +476,7 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, false, &bus);
+        r = bus_open_transport(arg_transport, arg_host, SYSTEMD_SYSTEM, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/libsystemd-bus/bus-util.c b/src/libsystemd-bus/bus-util.c
index 5069aaa..9459e6f 100644
--- a/src/libsystemd-bus/bus-util.c
+++ b/src/libsystemd-bus/bus-util.c
@@ -953,7 +953,7 @@ int bus_map_all_properties(sd_bus *bus,
         return r;
 }
 
-int bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus) {
+int bus_open_transport(BusTransport transport, const char *host, SystemdRunningAs running_as, sd_bus **bus) {
         int r;
 
         assert(transport >= 0);
@@ -961,18 +961,22 @@ int bus_open_transport(BusTransport transport, const char *host, bool user, sd_b
         assert(bus);
 
         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
-        assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
+        assert_return(transport == BUS_TRANSPORT_LOCAL || running_as == SYSTEMD_SYSTEM, -ENOTSUP);
 
         switch (transport) {
 
         case BUS_TRANSPORT_LOCAL:
-                if (user)
-                        r = sd_bus_default_user(bus);
-                else
-                        r = sd_bus_default_system(bus);
-
+                switch (running_as) {
+                        case SYSTEMD_SYSTEM:
+                                r = sd_bus_default_system(bus);
+                                break;
+                        case SYSTEMD_USER:
+                                r = sd_bus_default_user(bus);
+                                break;
+                        default:
+                                assert_not_reached("Unknown running_as.");
+                }
                 break;
-
         case BUS_TRANSPORT_REMOTE:
                 r = sd_bus_open_system_remote(host, bus);
                 break;
@@ -988,7 +992,7 @@ int bus_open_transport(BusTransport transport, const char *host, bool user, sd_b
         return r;
 }
 
-int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
+int bus_open_transport_systemd(BusTransport transport, const char *host, SystemdRunningAs running_as, sd_bus **bus) {
         int r;
 
         assert(transport >= 0);
@@ -996,16 +1000,21 @@ int bus_open_transport_systemd(BusTransport transport, const char *host, bool us
         assert(bus);
 
         assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
-        assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP);
+        assert_return(transport == BUS_TRANSPORT_LOCAL || running_as == SYSTEMD_SYSTEM, -ENOTSUP);
 
         switch (transport) {
 
         case BUS_TRANSPORT_LOCAL:
-                if (user)
-                        r = bus_open_user_systemd(bus);
-                else
-                        r = bus_open_system_systemd(bus);
-
+                switch (running_as) {
+                        case SYSTEMD_SYSTEM:
+                                r = bus_open_system_systemd(bus);
+                                break;
+                        case SYSTEMD_USER:
+                                r = bus_open_user_systemd(bus);
+                                break;
+                        default:
+                                assert_not_reached("Unknown running_as.");
+                }
                 break;
 
         case BUS_TRANSPORT_REMOTE:
diff --git a/src/libsystemd-bus/bus-util.h b/src/libsystemd-bus/bus-util.h
index 9d49237..32ad8f9 100644
--- a/src/libsystemd-bus/bus-util.h
+++ b/src/libsystemd-bus/bus-util.h
@@ -26,6 +26,7 @@
 #include "hashmap.h"
 #include "time-util.h"
 #include "util.h"
+#include "path-lookup.h"
 
 typedef enum BusTransport {
         BUS_TRANSPORT_LOCAL,
@@ -68,8 +69,8 @@ void bus_verify_polkit_async_registry_free(sd_bus *bus, Hashmap *registry);
 int bus_open_system_systemd(sd_bus **_bus);
 int bus_open_user_systemd(sd_bus **_bus);
 
-int bus_open_transport(BusTransport transport, const char *host, bool user, sd_bus **bus);
-int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus);
+int bus_open_transport(BusTransport transport, const char *host, SystemdRunningAs running_as, sd_bus **bus);
+int bus_open_transport_systemd(BusTransport transport, const char *host, SystemdRunningAs running_as, sd_bus **bus);
 
 int bus_print_property(const char *name, sd_bus_message *property, bool all);
 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool all);
diff --git a/src/libsystemd-bus/busctl.c b/src/libsystemd-bus/busctl.c
index f557e50..853970a 100644
--- a/src/libsystemd-bus/busctl.c
+++ b/src/libsystemd-bus/busctl.c
@@ -40,7 +40,7 @@ static bool arg_no_machine = false;
 static char **arg_matches = NULL;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
 static char *arg_host = NULL;
-static bool arg_user = false;
+static SystemdRunningAs arg_as = SYSTEMD_SYSTEM;
 
 static void pager_open_if_enabled(void) {
 
@@ -292,11 +292,11 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_USER:
-                        arg_user = true;
+                        arg_as = SYSTEMD_USER;
                         break;
 
                 case ARG_SYSTEM:
-                        arg_user = false;
+                        arg_as = SYSTEMD_SYSTEM;
                         break;
 
                 case ARG_ADDRESS:
@@ -386,7 +386,7 @@ int main(int argc, char *argv[]) {
 
                 r = sd_bus_start(bus);
         } else
-                r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
+                r = bus_open_transport(arg_transport, arg_host, arg_as, &bus);
 
         if (r < 0) {
                 log_error("Failed to connect to bus: %s", strerror(-r));
diff --git a/src/locale/localectl.c b/src/locale/localectl.c
index 76a53f6..cec1d64 100644
--- a/src/locale/localectl.c
+++ b/src/locale/localectl.c
@@ -803,7 +803,7 @@ int main(int argc, char*argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, false, &bus);
+        r = bus_open_transport(arg_transport, arg_host, SYSTEMD_SYSTEM, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/login/loginctl.c b/src/login/loginctl.c
index 5547cb2..98af1a1 100644
--- a/src/login/loginctl.c
+++ b/src/login/loginctl.c
@@ -1281,7 +1281,7 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, false, &bus);
+        r = bus_open_transport(arg_transport, arg_host, SYSTEMD_SYSTEM, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 61fb813..ad0716d 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -842,7 +842,7 @@ int main(int argc, char*argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, false, &bus);
+        r = bus_open_transport(arg_transport, arg_host, SYSTEMD_SYSTEM, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/run/run.c b/src/run/run.c
index 2e0cd1a..ad23e83 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -38,7 +38,7 @@ static const char *arg_slice = NULL;
 static bool arg_send_sighup = false;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
 static char *arg_host = NULL;
-static bool arg_user = false;
+static SystemdRunningAs arg_as = SYSTEMD_SYSTEM;
 
 static int help(void) {
 
@@ -107,11 +107,11 @@ static int parse_argv(int argc, char *argv[]) {
                         return 0;
 
                 case ARG_USER:
-                        arg_user = true;
+                        arg_as = SYSTEMD_USER;
                         break;
 
                 case ARG_SYSTEM:
-                        arg_user = false;
+                        arg_as = SYSTEMD_SYSTEM;
                         break;
 
                 case ARG_SCOPE:
@@ -161,7 +161,7 @@ static int parse_argv(int argc, char *argv[]) {
                 return -EINVAL;
         }
 
-        if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) {
+        if (arg_as != SYSTEMD_SYSTEM && arg_transport != BUS_TRANSPORT_LOCAL) {
                 log_error("Execution in user context is not supported on non-local systems.");
                 return -EINVAL;
         }
@@ -395,7 +395,7 @@ int main(int argc, char* argv[]) {
                 arg_description = description;
         }
 
-        r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
+        r = bus_open_transport(arg_transport, arg_host, arg_as, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 5dcefd7..a6f95d6 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -85,6 +85,7 @@ static enum dependency {
 } arg_dependency = DEPENDENCY_FORWARD;
 static const char *arg_job_mode = "replace";
 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
+static SystemdRunningAs arg_as = SYSTEMD_SYSTEM;
 static bool arg_no_block = false;
 static bool arg_no_legend = false;
 static bool arg_no_pager = false;
@@ -5126,14 +5127,17 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
 
                 case ARG_USER:
                         arg_scope = UNIT_FILE_USER;
+                        arg_as = SYSTEMD_USER;
                         break;
 
                 case ARG_SYSTEM:
                         arg_scope = UNIT_FILE_SYSTEM;
+                        arg_as = SYSTEMD_SYSTEM;
                         break;
 
                 case ARG_GLOBAL:
                         arg_scope = UNIT_FILE_GLOBAL;
+                        arg_as = SYSTEMD_SYSTEM;
                         break;
 
                 case ARG_NO_BLOCK:
diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c
index 6a36af0..0a0e884 100644
--- a/src/timedate/timedatectl.c
+++ b/src/timedate/timedatectl.c
@@ -592,7 +592,7 @@ int main(int argc, char *argv[]) {
         if (r <= 0)
                 goto finish;
 
-        r = bus_open_transport(arg_transport, arg_host, false, &bus);
+        r = bus_open_transport(arg_transport, arg_host, SYSTEMD_SYSTEM, &bus);
         if (r < 0) {
                 log_error("Failed to create bus connection: %s", strerror(-r));
                 goto finish;
-- 
1.8.4.4



More information about the systemd-devel mailing list