chiark / gitweb /
install: various improvements
authorLennart Poettering <lennart@poettering.net>
Thu, 8 Jul 2010 19:34:51 +0000 (21:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 8 Jul 2010 19:39:01 +0000 (21:39 +0200)
Rename --start to --realize, to make things less confusing when doing
"systemctl stop --realize foo.service".

Introduce --realize=reload.

Don't talk to systemd when run within a chroot, or when systemd isn't
running.

Makefile.am
man/daemon.xml
man/systemd-install.xml
src/install.c
src/socket.c
src/util.c
src/util.h

index debe4759671569370deb323e0514b8a669c206a0..52339718a1d0fcd3ba0ddd11b9352963797cc63e 100644 (file)
@@ -503,7 +503,8 @@ systemd_notify_LDADD = \
 systemd_install_SOURCES = \
        src/install.c \
        src/path-lookup.c \
 systemd_install_SOURCES = \
        src/install.c \
        src/path-lookup.c \
-       src/dbus-common.c
+       src/dbus-common.c \
+       src/sd-daemon.c
 
 systemd_install_LDADD = \
        libsystemd-basic.la \
 
 systemd_install_LDADD = \
        libsystemd-basic.la \
index dfee39016479671c416cff91727efd9cea422c17..b708bdcfbaae0386342606aa6044c6fc76c4df68 100644 (file)
@@ -793,11 +793,11 @@ endif</programlisting>
                         package managers:</para>
 
                         <programlisting>%post
                         package managers:</para>
 
                         <programlisting>%post
-/usr/bin/systemd-install --start enable foobar.service foobar.socket >/dev/null 2>&amp;1 || :
+/usr/bin/systemd-install --realize enable foobar.service foobar.socket >/dev/null 2>&amp;1 || :
 
 %preun
 if [ "$1" -eq 0 ]; then
 
 %preun
 if [ "$1" -eq 0 ]; then
-        /usr/bin/systemd-install --start disable foobar.service foobar.socket >/dev/null 2>&amp;1 || :
+        /usr/bin/systemd-install --realize disable foobar.service foobar.socket >/dev/null 2>&amp;1 || :
 fi</programlisting>
 
                 </refsect2>
 fi</programlisting>
 
                 </refsect2>
index c3ec4bdcd8f24988c459494340919dcbfebd5531..28415d19e11cf3283813f4c78dbc9b7a7013d8e6 100644 (file)
                         </varlistentry>
 
                         <varlistentry>
                         </varlistentry>
 
                         <varlistentry>
-                                <term><option>--start[=MODE]</option></term>
+                                <term><option>--realize[=MODE]</option></term>
 
                                 <listitem><para>After
 
                                 <listitem><para>After
-                                enabling/disabling stop/restart the
-                                unit and reload manager
+                                enabling/disabling stop/restart/stop
+                                the unit and reload manager
                                 configuration. Optionally, takes one
                                 of <option>no</option>,
                                 configuration. Optionally, takes one
                                 of <option>no</option>,
+                                <option>reload</option>,
                                 <option>minimal</option>,
                                 <option>maybe</option> or
                                 <option>yes</option>. If
                                 <option>minimal</option>,
                                 <option>maybe</option> or
                                 <option>yes</option>. If
                                 configuration and no service will be
                                 started or stopped after
                                 enabling/disabling of the unit
                                 configuration and no service will be
                                 started or stopped after
                                 enabling/disabling of the unit
-                                files. If <option>minimal</option> is
-                                passed and a unit is being enabled it
-                                will also be restarted should it
-                                already be running. If a unit is being
-                                disabled it will be stopped should it
-                                be running. In either case the
-                                daemon configuration is
+                                files. If <option>reload</option> is
+                                passed the daemon configuration is
+                                reloaded but the unit otherwise not
+                                started/stopped/restarted. If
+                                <option>minimal</option> is passed and
+                                a unit is being enabled it will also
+                                be restarted should it already be
+                                running. If a unit is being disabled
+                                it will be stopped should it be
+                                running. In either case the daemon
+                                configuration is
                                 reloaded. <option>maybe</option> is
                                 similar to this, but the unit will
                                 also be started if it is being enabled
                                 reloaded. <option>maybe</option> is
                                 similar to this, but the unit will
                                 also be started if it is being enabled
                                 <option>maybe</option>. This option
                                 has no effect when
                                 <option>--global</option> or
                                 <option>maybe</option>. This option
                                 has no effect when
                                 <option>--global</option> or
-                                <command>test</command> is
-                                used.</para></listitem>
+                                <command>test</command> is used, or
+                                when systemd is not running or the
+                                command is executed in a
+                                <citerefentry><refentrytitle>chroot</refentrytitle><manvolnum>2</manvolnum></citerefentry>
+                                environment.</para></listitem>
                         </varlistentry>
                 </variablelist>
 
                         </varlistentry>
                 </variablelist>
 
index 3b70bd1d3db2354dfd2d0ee04cd782074dc3cc82..a05002d073e805ab1b11e3f693d07963fd47903c 100644 (file)
@@ -32,6 +32,7 @@
 #include "strv.h"
 #include "conf-parser.h"
 #include "dbus-common.h"
 #include "strv.h"
 #include "conf-parser.h"
 #include "dbus-common.h"
+#include "sd-daemon.h"
 
 static bool arg_force = false;
 
 
 static bool arg_force = false;
 
@@ -49,11 +50,12 @@ static enum {
 } arg_action = ACTION_INVALID;
 
 static enum {
 } arg_action = ACTION_INVALID;
 
 static enum {
-        START_NO,        /* Don't start/stop or anything */
-        START_MINIMAL,   /* Only shutdown/restart if running. */
-        START_MAYBE,     /* Start if WantedBy= suggests */
-        START_YES        /* Start unconditionally */
-} arg_start = START_NO;
+        REALIZE_NO,        /* Don't reload/start/stop or anything */
+        REALIZE_RELOAD,    /* Only reload daemon config, don't stop/start */
+        REALIZE_MINIMAL,   /* Only shutdown/restart if running. */
+        REALIZE_MAYBE,     /* Start if WantedBy= suggests */
+        REALIZE_YES        /* Start unconditionally */
+} arg_realize = REALIZE_NO;
 
 typedef struct {
         char *name;
 
 typedef struct {
         char *name;
@@ -69,13 +71,13 @@ static int help(void) {
 
         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
                "Install init system units.\n\n"
 
         printf("%s [OPTIONS...] {COMMAND} ...\n\n"
                "Install init system units.\n\n"
-               "  -h --help         Show this help\n"
-               "     --force        Override existing links\n"
-               "     --system       Install into system\n"
-               "     --session      Install into session\n"
-               "     --global       Install into all sessions\n"
-               "     --start[=MODE] Start/stop/restart unit after installation\n"
-               "                    Takes 'no', 'minimal', 'maybe' or 'yes'\n\n"
+               "  -h --help           Show this help\n"
+               "     --force          Override existing links\n"
+               "     --system         Install into system\n"
+               "     --session        Install into session\n"
+               "     --global         Install into all sessions\n"
+               "     --realize[=MODE] Start/stop/restart unit after installation\n"
+               "                      Takes 'no', 'minimal', 'maybe' or 'yes'\n\n"
                "Commands:\n"
                "  enable [NAME...]    Enable one or more units\n"
                "  disable [NAME...]   Disable one or more units\n"
                "Commands:\n"
                "  enable [NAME...]    Enable one or more units\n"
                "  disable [NAME...]   Disable one or more units\n"
@@ -92,7 +94,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_SYSTEM,
                 ARG_GLOBAL,
                 ARG_FORCE,
                 ARG_SYSTEM,
                 ARG_GLOBAL,
                 ARG_FORCE,
-                ARG_START
+                ARG_REALIZE
         };
 
         static const struct option options[] = {
         };
 
         static const struct option options[] = {
@@ -101,7 +103,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "system",    no_argument,       NULL, ARG_SYSTEM  },
                 { "global",    no_argument,       NULL, ARG_GLOBAL  },
                 { "force",     no_argument,       NULL, ARG_FORCE   },
                 { "system",    no_argument,       NULL, ARG_SYSTEM  },
                 { "global",    no_argument,       NULL, ARG_GLOBAL  },
                 { "force",     no_argument,       NULL, ARG_FORCE   },
-                { "start",     optional_argument, NULL, ARG_START   },
+                { "realize",   optional_argument, NULL, ARG_REALIZE },
                 { NULL,        0,                 NULL, 0           }
         };
 
                 { NULL,        0,                 NULL, 0           }
         };
 
@@ -134,20 +136,20 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_force = true;
                         break;
 
                         arg_force = true;
                         break;
 
-                case ARG_START:
+                case ARG_REALIZE:
 
                         if (!optarg)
 
                         if (!optarg)
-                                arg_start = START_MAYBE;
+                                arg_realize = REALIZE_MAYBE;
                         else if (streq(optarg, "no"))
                         else if (streq(optarg, "no"))
-                                arg_start = START_NO;
+                                arg_realize = REALIZE_NO;
                         else if (streq(optarg, "minimal"))
                         else if (streq(optarg, "minimal"))
-                                arg_start = START_MINIMAL;
+                                arg_realize = REALIZE_MINIMAL;
                         else if (streq(optarg, "maybe"))
                         else if (streq(optarg, "maybe"))
-                                arg_start = START_MAYBE;
+                                arg_realize = REALIZE_MAYBE;
                         else if (streq(optarg, "yes"))
                         else if (streq(optarg, "yes"))
-                                arg_start = START_YES;
+                                arg_realize = REALIZE_YES;
                         else {
                         else {
-                                log_error("Invalid --start argument %s", optarg);
+                                log_error("Invalid --realize argument %s", optarg);
                                 return -EINVAL;
                         }
 
                                 return -EINVAL;
                         }
 
@@ -306,7 +308,7 @@ static int install_info_run(DBusConnection *bus, InstallInfo *i) {
 
         if (arg_action == ACTION_ENABLE) {
 
 
         if (arg_action == ACTION_ENABLE) {
 
-                if (arg_start == START_MAYBE) {
+                if (arg_realize == REALIZE_MAYBE) {
                         char **k;
                         bool yes_please = false;
 
                         char **k;
                         bool yes_please = false;
 
@@ -416,7 +418,7 @@ static int install_info_run(DBusConnection *bus, InstallInfo *i) {
                                       "org.freedesktop.systemd1",
                                       "/org/freedesktop/systemd1",
                                       "org.freedesktop.systemd1.Manager",
                                       "org.freedesktop.systemd1",
                                       "/org/freedesktop/systemd1",
                                       "org.freedesktop.systemd1.Manager",
-                                      arg_start == START_MINIMAL ? "TryRestartUnit" : "RestartUnit"))) {
+                                      arg_realize == REALIZE_MINIMAL ? "TryRestartUnit" : "RestartUnit"))) {
                         log_error("Could not allocate message.");
                         r = -ENOMEM;
                         goto finish;
                         log_error("Could not allocate message.");
                         r = -ENOMEM;
                         goto finish;
@@ -778,16 +780,26 @@ static int do_run(void) {
 
         dbus_error_init(&error);
 
 
         dbus_error_init(&error);
 
-        if (arg_start == START_NO)
+        if (arg_realize == REALIZE_NO)
                 return 0;
 
         if (arg_where == WHERE_GLOBAL) {
                 return 0;
 
         if (arg_where == WHERE_GLOBAL) {
-                log_warning("Warning: --start has no effect with --global.");
+                log_warning("Warning: --realize has no effect with --global.");
                 return 0;
         }
 
         if (arg_action != ACTION_ENABLE && arg_action != ACTION_DISABLE) {
                 return 0;
         }
 
         if (arg_action != ACTION_ENABLE && arg_action != ACTION_DISABLE) {
-                log_warning("Warning: --start has no effect with test.");
+                log_warning("Warning: --realize has no effect with test.");
+                return 0;
+        }
+
+        if (arg_where == WHERE_SYSTEM && sd_booted() <= 0) {
+                log_info("systemd is not running, --realize has not effect.");
+                return 0;
+        }
+
+        if (arg_where == WHERE_SYSTEM && running_in_chroot() > 0) {
+                log_info("Running in a chroot() environment, --realize has no effect.");
                 return 0;
         }
 
                 return 0;
         }
 
@@ -802,9 +814,11 @@ static int do_run(void) {
                 if ((r = daemon_reload(bus)) < 0)
                         goto finish;
 
                 if ((r = daemon_reload(bus)) < 0)
                         goto finish;
 
-        HASHMAP_FOREACH(j, have_installed, i)
-                if ((q = install_info_run(bus, j)) < 0)
-                        r = q;
+        if (arg_realize != REALIZE_RELOAD) {
+                HASHMAP_FOREACH(j, have_installed, i)
+                        if ((q = install_info_run(bus, j)) < 0)
+                                r = q;
+        }
 
         if (arg_action == ACTION_DISABLE)
                 if ((q = daemon_reload(bus)) < 0)
 
         if (arg_action == ACTION_DISABLE)
                 if ((q = daemon_reload(bus)) < 0)
index ee75de04d647b09210e50f71cc6f86d2a57d5caf..acd49a50457cdb49ba06b278922a56243026a40a 100644 (file)
@@ -1039,7 +1039,7 @@ static void socket_enter_running(Socket *s, int cfd) {
                 socket_set_state(s, SOCKET_RUNNING);
         } else {
                 Unit *u;
                 socket_set_state(s, SOCKET_RUNNING);
         } else {
                 Unit *u;
-                char *prefix, *instance, *name;
+                char *prefix, *instance = NULL, *name;
 
                 if (s->n_connections >= s->max_connections) {
                         log_warning("Too many incoming connections (%u)", s->n_connections);
 
                 if (s->n_connections >= s->max_connections) {
                         log_warning("Too many incoming connections (%u)", s->n_connections);
index 2e5827f32dcf3671a26545d5250312e265bcb8c3..7f5fa05dca4d1beb15da5b82db6a6792198dec9b 100644 (file)
@@ -2825,6 +2825,25 @@ int columns(void) {
         return parsed_columns;
 }
 
         return parsed_columns;
 }
 
+int running_in_chroot(void) {
+        struct stat a, b;
+
+        zero(a);
+        zero(b);
+
+        /* Only works as root */
+
+        if (stat("/proc/1/root", &a) < 0)
+                return -errno;
+
+        if (stat("/", &b) < 0)
+                return -errno;
+
+        return
+                a.st_dev != b.st_dev ||
+                a.st_ino != b.st_ino;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
index 5d51c07e2f27d691bdb119fa80c4865444f24f68..1a4c440ba5494c3a5a1d97dbeb378580ed5c4ba2 100644 (file)
@@ -330,6 +330,8 @@ void status_welcome(void);
 
 int columns(void);
 
 
 int columns(void);
 
+int running_in_chroot(void);
+
 const char *ioprio_class_to_string(int i);
 int ioprio_class_from_string(const char *s);
 
 const char *ioprio_class_to_string(int i);
 int ioprio_class_from_string(const char *s);