[systemd-commits] 3 commits - fixme src/load-fragment.c src/strv.c src/strv.h src/systemctl.c src/utmp-wtmp.c

Lennart Poettering lennart at kemper.freedesktop.org
Fri Jun 18 10:18:10 PDT 2010


 fixme               |   10 +++-
 src/load-fragment.c |   63 ++++++++++++++++++++++++++++++
 src/strv.c          |   28 +++++++++++++
 src/strv.h          |    2 
 src/systemctl.c     |  109 ++++++++++++++++++++++++++++++++++++++++++++++------
 src/utmp-wtmp.c     |    2 
 6 files changed, 200 insertions(+), 14 deletions(-)

New commits:
commit 629c210d3f080854d2d90386f9da1423e53e114e
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 18 19:18:03 2010 +0200

    utmp: properly initialize local variables

diff --git a/src/utmp-wtmp.c b/src/utmp-wtmp.c
index 5aafb7b..d7cda82 100644
--- a/src/utmp-wtmp.c
+++ b/src/utmp-wtmp.c
@@ -291,7 +291,7 @@ finish:
 int utmp_wall(const char *message) {
         struct utmpx *u;
         char date[26];
-        char *text, *hn, *un, *tty;
+        char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
         int r;
         time_t t;
 
commit d55ae9e6d7a20bb79f60a154bfcf4348dfb06852
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 18 19:16:14 2010 +0200

    systemctl: add compat support for shutting down the system via upstart

diff --git a/fixme b/fixme
index 709534b..374ff41 100644
--- a/fixme
+++ b/fixme
@@ -59,8 +59,6 @@
 
 * User= and friends needs to understand %i and similar replacements
 
-* EnvironmentFile=
-
 * make systemd bus activatable
 
 * pin /cgroup/systemd
@@ -71,6 +69,14 @@
 
 * upstart fallback in systemctl
 
+* abstract namespace dbus socket
+
+* /sbin/shutdown argv[2..] message
+
+* exec /sbin/telinit from init
+
+* discuss NOTIFY_SOCKET, make it configurable? security implications?
+
 Regularly:
 
 * look for close() vs. close_nointr() vs. close_nointr_nofail()
diff --git a/src/systemctl.c b/src/systemctl.c
index ec92504..447a244 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -1893,12 +1893,7 @@ static int parse_argv(int argc, char *argv[]) {
         return systemctl_parse_argv(argc, argv);
 }
 
-static int talk_upstart(DBusConnection *bus) {
-        log_error("Talking upstart");
-        return 0;
-}
-
-static int talk_initctl(void) {
+static int action_to_runlevel(void) {
 
         static const char table[_ACTION_MAX] = {
                 [ACTION_HALT] =      '0',
@@ -1911,26 +1906,114 @@ static int talk_initctl(void) {
                 [ACTION_RESCUE] =    '1'
         };
 
+        assert(arg_action < _ACTION_MAX);
+
+        return table[arg_action];
+}
+
+static int talk_upstart(DBusConnection *bus) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        int previous, rl, r;
+        char
+                env1_buf[] = "RUNLEVEL=X",
+                env2_buf[] = "PREVLEVEL=X";
+        char *env1 = env1_buf, *env2 = env2_buf;
+        const char *emit = "runlevel";
+        dbus_bool_t b_false = FALSE;
+        DBusMessageIter iter, sub;
+
+        dbus_error_init(&error);
+
+        if (!(rl = action_to_runlevel()))
+                return 0;
+
+        if (utmp_get_runlevel(&previous, NULL) < 0)
+                previous = 'N';
+
+        if (!(m = dbus_message_new_method_call(
+                              "com.ubuntu.Upstart",
+                              "/com/ubuntu/Upstart",
+                              "com.ubuntu.Upstart0_6",
+                              "EmitEvent"))) {
+
+                log_error("Could not allocate message.");
+                return -ENOMEM;
+        }
+
+        dbus_message_iter_init_append(m, &iter);
+
+        env1_buf[sizeof(env1_buf)-2] = rl;
+        env2_buf[sizeof(env2_buf)-2] = previous;
+
+        if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &emit) ||
+            !dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub) ||
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env1) ||
+            !dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &env2) ||
+            !dbus_message_iter_close_container(&iter, &sub) ||
+            !dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b_false)) {
+                log_error("Could not append arguments to message.");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+
+                if (error_is_no_service(&error)) {
+                        r = 0;
+                        goto finish;
+                }
+
+                log_error("Failed to issue method call: %s", error.message);
+                r = -EIO;
+                goto finish;
+        }
+
+        r = 1;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        return r;
+}
+
+static int talk_initctl(void) {
         struct init_request request;
         int r, fd;
+        char rl;
 
-        if (!table[arg_action])
+        if (!(rl = action_to_runlevel()))
                 return 0;
 
         zero(request);
         request.magic = INIT_MAGIC;
         request.sleeptime = 0;
         request.cmd = INIT_CMD_RUNLVL;
-        request.runlevel = table[arg_action];
+        request.runlevel = rl;
+
+        if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0) {
+
+                if (errno == ENOENT)
+                        return 0;
 
-        if ((fd = open(INIT_FIFO, O_WRONLY|O_NDELAY|O_CLOEXEC|O_NOCTTY)) < 0)
+                log_error("Failed to open "INIT_FIFO": %m");
                 return -errno;
+        }
 
+        errno = 0;
         r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
         close_nointr_nofail(fd);
 
-        if (r < 0)
+        if (r < 0) {
+                log_error("Failed to write to "INIT_FIFO": %m");
                 return errno ? -errno : -EIO;
+        }
 
         return 1;
 }
@@ -2068,7 +2151,11 @@ static int start_with_fallback(DBusConnection *bus) {
 
         /* Nothing else worked, so let's try
          * /dev/initctl */
-        return talk_initctl();
+        if ((r = talk_initctl()) != 0)
+                return 0;
+
+        log_error("Failed to talk to init daemon.");
+        return -EIO;
 }
 
 static int halt_main(DBusConnection *bus) {
commit ddb26e1818f67c2b97313d2ccf7468b2240ec086
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Jun 18 06:06:24 2010 +0200

    execute: add EnvironmentFile= option

diff --git a/src/load-fragment.c b/src/load-fragment.c
index 1f082d5..7a51bf8 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -40,6 +40,9 @@
 #include "missing.h"
 #include "unit-name.h"
 
+#define COMMENTS "#;\n"
+#define LINE_MAX 4096
+
 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
         static int function(                                            \
                         const char *filename,                           \
@@ -1143,6 +1146,64 @@ static int config_parse_path_unit(
         return 0;
 }
 
+static int config_parse_env_file(
+                const char *filename,
+                unsigned line,
+                const char *section,
+                const char *lvalue,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        FILE *f;
+        int r;
+        char ***env = data;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        if (!(f = fopen(rvalue, "re"))) {
+                log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue);
+                return -errno;
+        }
+
+        while (!feof(f)) {
+                char l[LINE_MAX], *p;
+                char **t;
+
+                if (!fgets(l, sizeof(l), f)) {
+                        if (feof(f))
+                                break;
+
+                        r = -errno;
+                        log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue);
+                        goto finish;
+                }
+
+                p = strstrip(l);
+
+                if (!*p)
+                        continue;
+
+                if (strchr(COMMENTS, *p))
+                        continue;
+
+                t = strv_env_set(*env, p);
+                strv_free(*env);
+                *env = t;
+        }
+
+        r = 0;
+
+finish:
+        if (f)
+                fclose(f);
+
+        return r;
+}
+
 #define FOLLOW_MAX 8
 
 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
@@ -1272,6 +1333,7 @@ static void dump_items(FILE *f, const ConfigItem *items) {
                 { config_parse_cpu_sched_prio,   "CPUSCHEDPRIO" },
                 { config_parse_cpu_affinity,     "CPUAFFINITY" },
                 { config_parse_mode,             "MODE" },
+                { config_parse_env_file,         "FILE" },
                 { config_parse_output,           "OUTPUT" },
                 { config_parse_input,            "INPUT" },
                 { config_parse_facility,         "FACILITY" },
@@ -1358,6 +1420,7 @@ static int load_from_path(Unit *u, const char *path) {
                 { "CPUAffinity",            config_parse_cpu_affinity,    &(context),                                      section   }, \
                 { "UMask",                  config_parse_mode,            &(context).umask,                                section   }, \
                 { "Environment",            config_parse_strv,            &(context).environment,                          section   }, \
+                { "EnvironmentFile",        config_parse_env_file,        &(context).environment,                          section   }, \
                 { "StandardInput",          config_parse_input,           &(context).std_input,                            section   }, \
                 { "StandardOutput",         config_parse_output,          &(context).std_output,                           section   }, \
                 { "StandardError",          config_parse_output,          &(context).std_error,                            section   }, \
diff --git a/src/strv.c b/src/strv.c
index 85599fe..01464e1 100644
--- a/src/strv.c
+++ b/src/strv.c
@@ -511,3 +511,31 @@ char **strv_env_delete(char **x, unsigned n_lists, ...) {
 
         return r;
 }
+
+char **strv_env_set(char **x, const char *p) {
+
+        char **k, **r;
+
+        if (!(r = new(char*, strv_length(x)+2)))
+                return NULL;
+
+        k = r;
+        if (env_append(r, &k, x) < 0)
+                goto fail;
+
+        if (!(*(k++) = strdup(p)))
+                goto fail;
+
+        *k = NULL;
+
+        return r;
+
+fail:
+        for (k--; k >= r; k--)
+                free(*k);
+
+        free(r);
+
+        return NULL;
+
+}
diff --git a/src/strv.h b/src/strv.h
index af13983..0d50b02 100644
--- a/src/strv.h
+++ b/src/strv.h
@@ -58,6 +58,8 @@ char *strv_join(char **l, const char *separator) _malloc_;
 char **strv_env_merge(unsigned n_lists, ...);
 char **strv_env_delete(char **x, unsigned n_lists, ...);
 
+char **strv_env_set(char **x, const char *p);
+
 #define STRV_FOREACH(s, l)                      \
         for ((s) = (l); (s) && *(s); (s)++)
 


More information about the systemd-commits mailing list