[systemd-commits] 3 commits - man/systemd-run.xml src/core src/run src/shared src/test TODO

Zbigniew Jędrzejewski-Szmek zbyszek at kemper.freedesktop.org
Mon Sep 9 11:57:25 PDT 2013


 TODO                      |    2 
 man/systemd-run.xml       |   17 ++++++
 src/core/dbus-service.c   |   21 +++-----
 src/core/manager.c        |    6 --
 src/run/run.c             |   11 +++-
 src/shared/path-util.c    |   61 +++++++++++++++++++++---
 src/shared/path-util.h    |    8 +++
 src/shared/strv.c         |   37 ++++++++++++++
 src/shared/strv.h         |    1 
 src/test/test-path-util.c |   24 +++++++++
 src/test/test-strv.c      |  114 +++++++++++++++++++++++++++++++---------------
 11 files changed, 240 insertions(+), 62 deletions(-)

New commits:
commit c9d954b27ee125c3c90a6d2951c62eec4abb160b
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Sep 8 07:51:39 2013 -0400

    run: allow non-absolute paths as command

diff --git a/TODO b/TODO
index ff29cba..b83fd67 100644
--- a/TODO
+++ b/TODO
@@ -715,6 +715,8 @@ Features:
    - document initcall_debug
    - kernel cmdline "bootchart" option for simplicity?
 
+* systemd-run is missing completion scripts
+
 External:
 
 * dbus:
diff --git a/man/systemd-run.xml b/man/systemd-run.xml
index 6b0189c..e76a402 100644
--- a/man/systemd-run.xml
+++ b/man/systemd-run.xml
@@ -188,6 +188,23 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
   </refsect1>
 
   <refsect1>
+    <title>Example</title>
+
+    <para>The following command will log the environment variables
+    provided by systemd to services:</para>
+
+    <programlisting># systemd-run env
+Running as unit run-19945.service.
+# journalctl -u run-19945.service
+Sep 08 07:37:21 bupkis systemd[1]: Starting /usr/bin/env...
+Sep 08 07:37:21 bupkis systemd[1]: Started /usr/bin/env.
+Sep 08 07:37:21 bupkis env[19948]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
+Sep 08 07:37:21 bupkis env[19948]: LANG=en_US.UTF-8
+Sep 08 07:37:21 bupkis env[19948]: BOOT_IMAGE=/vmlinuz-3.11.0-0.rc5.git6.2.fc20.x86_64
+    </programlisting>
+  </refsect1>
+
+  <refsect1>
     <title>See Also</title>
     <para>
       <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
diff --git a/src/core/manager.c b/src/core/manager.c
index 10ccffb..669af15 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -456,11 +456,7 @@ static int manager_setup_signals(Manager *m) {
 }
 
 static int manager_default_environment(Manager *m) {
-#ifdef HAVE_SPLIT_USR
-        const char *path = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
-#else
-        const char *path = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin";
-#endif
+        const char *path = "PATH=" DEFAULT_PATH;
 
         assert(m);
 
diff --git a/src/run/run.c b/src/run/run.c
index c5d314b..da8c788 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -39,7 +39,7 @@ static bool arg_send_sighup = false;
 
 static int help(void) {
 
-        printf("%s [OPTIONS...] [COMMAND LINE...]\n\n"
+        printf("%s [OPTIONS...] COMMAND [ARGS...]\n\n"
                "Run the specified command in a transient scope or service unit.\n\n"
                "  -h --help               Show this help\n"
                "     --version            Show package version\n"
@@ -324,7 +324,7 @@ static int start_transient_scope(
 int main(int argc, char* argv[]) {
         sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_bus_unref_ sd_bus *bus = NULL;
-        _cleanup_free_ char *description = NULL;
+        _cleanup_free_ char *description = NULL, *command = NULL;
         int r;
 
         log_parse_environment();
@@ -334,6 +334,13 @@ int main(int argc, char* argv[]) {
         if (r <= 0)
                 goto fail;
 
+        r = find_binary(argv[optind], &command);
+        if (r < 0) {
+                log_error("Failed to find executable %s: %s", argv[optind], strerror(-r));
+                goto fail;
+        }
+        argv[optind] = command;
+
         if (!arg_description) {
                 description = strv_join(argv + optind, " ");
                 if (!description) {
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 6888135..8e108db 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -425,3 +425,51 @@ int path_is_os_tree(const char *path) {
 
         return r < 0 ? 0 : 1;
 }
+
+int find_binary(const char *name, char **filename) {
+        assert(name);
+        if (strchr(name, '/')) {
+                char *p;
+
+                if (path_is_absolute(name))
+                        p = strdup(name);
+                else
+                        p = path_make_absolute_cwd(name);
+                if (!p)
+                        return -ENOMEM;
+
+                *filename = p;
+                return 0;
+        } else {
+                const char *path;
+                char *state, *w;
+                size_t l;
+
+                /**
+                 * Plain getenv, not secure_getenv, because we want
+                 * to actually allow the user to pick the binary.
+                 */
+                path = getenv("PATH");
+                if (!path)
+                        path = DEFAULT_PATH;
+
+                FOREACH_WORD_SEPARATOR(w, l, path, ":", state) {
+                        char *p;
+
+                        if (asprintf(&p, "%.*s/%s", l, w, name) < 0)
+                                return -ENOMEM;
+
+                        if (access(p, X_OK) < 0) {
+                                free(p);
+                                continue;
+                        }
+
+                        path_kill_slashes(p);
+                        *filename = p;
+
+                        return 0;
+                }
+
+                return -ENOENT;
+        }
+}
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index d187743..9452931 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -25,6 +25,12 @@
 
 #include "macro.h"
 
+#ifdef HAVE_SPLIT_USR
+#  define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
+#else
+#  define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
+#endif
+
 bool is_path(const char *p) _pure_;
 char** path_split_and_make_absolute(const char *p);
 char* path_get_file_name(const char *p) _pure_;
@@ -43,3 +49,5 @@ char** path_strv_canonicalize_uniq(char **l);
 int path_is_mount_point(const char *path, bool allow_symlink);
 int path_is_read_only_fs(const char *path);
 int path_is_os_tree(const char *path);
+
+int find_binary(const char *name, char **filename);
diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c
index f396b32..b0aeb11 100644
--- a/src/test/test-path-util.c
+++ b/src/test/test-path-util.c
@@ -83,7 +83,31 @@ static void test_path(void) {
         }
 }
 
+static void test_find_binary(void) {
+        char *p;
+
+        assert(find_binary("/bin/sh", &p) == 0);
+        puts(p);
+        assert(streq(p, "/bin/sh"));
+        free(p);
+
+        assert(find_binary("./test-path-util", &p) == 0);
+        puts(p);
+        assert(endswith(p, "/test-path-util"));
+        assert(path_is_absolute(p));
+        free(p);
+
+        assert(find_binary("sh", &p) == 0);
+        puts(p);
+        assert(endswith(p, "/sh"));
+        assert(path_is_absolute(p));
+        free(p);
+
+        assert(find_binary("xxxx-xxxx", &p) == -ENOENT);
+}
+
 int main(void) {
         test_path();
+        test_find_binary();
         return 0;
 }

commit 116cc028742836e61abce7582ec9ecf9f0aaeb53
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sun Sep 8 07:31:25 2013 -0400

    path-util.c: small modernization

diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 0c1b6a0..6888135 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -102,7 +102,8 @@ char **path_split_and_make_absolute(const char *p) {
         char **l;
         assert(p);
 
-        if (!(l = strv_split(p, ":")))
+        l = strv_split(p, ":");
+        if (!l)
                 return NULL;
 
         if (!path_strv_make_absolute_cwd(l)) {
@@ -126,7 +127,7 @@ char *path_make_absolute(const char *p, const char *prefix) {
 }
 
 char *path_make_absolute_cwd(const char *p) {
-        char *cwd, *r;
+        _cleanup_free_ char *cwd = NULL;
 
         assert(p);
 
@@ -140,10 +141,7 @@ char *path_make_absolute_cwd(const char *p) {
         if (!cwd)
                 return NULL;
 
-        r = path_make_absolute(p, cwd);
-        free(cwd);
-
-        return r;
+        return path_make_absolute(p, cwd);
 }
 
 char **path_strv_make_absolute_cwd(char **l) {
@@ -156,7 +154,8 @@ char **path_strv_make_absolute_cwd(char **l) {
         STRV_FOREACH(s, l) {
                 char *t;
 
-                if (!(t = path_make_absolute_cwd(*s)))
+                t = path_make_absolute_cwd(*s);
+                if (!t)
                         return NULL;
 
                 free(*s);

commit a6fde35332f5e7f78bff437d7b7bfded83debbaa
Author: Zbigniew Jędrzejewski-Szmek <zbyszek at in.waw.pl>
Date:   Sat Aug 31 20:28:09 2013 +0200

    systemd-run: properly escape arguments
    
    Spaces, quotes, and such, were not properly escaped. We should
    write them like we read them.
    
    https://bugs.freedesktop.org/show_bug.cgi?id=67971

diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c
index 85b13f0..696c446 100644
--- a/src/core/dbus-service.c
+++ b/src/core/dbus-service.c
@@ -273,21 +273,16 @@ static int bus_service_set_transient_property(
                         fputs("ExecStart=\n", f);
 
                         LIST_FOREACH(command, c, s->exec_command[SERVICE_EXEC_START]) {
-                                char **a;
-                                fputs("ExecStart=", f);
+                                _cleanup_free_ char *a;
 
-                                if (c->ignore)
-                                        fputc('-', f);
-
-                                fputc('@', f);
-                                fputs(c->path, f);
-
-                                STRV_FOREACH(a, c->argv) {
-                                        fputc(' ', f);
-                                        fputs(*a, f);
-                                }
+                                a = strv_join_quoted(c->argv);
+                                if (!a)
+                                        return -ENOMEM;
 
-                                fputc('\n', f);
+                                fprintf(f, "ExecStart=%s@%s %s\n",
+                                        c->ignore ? "-" : "",
+                                        c->path,
+                                        a);
                         }
 
                         fflush(f);
diff --git a/src/shared/strv.c b/src/shared/strv.c
index 3e7778d..2df478f 100644
--- a/src/shared/strv.c
+++ b/src/shared/strv.c
@@ -356,6 +356,43 @@ char *strv_join(char **l, const char *separator) {
         return r;
 }
 
+char *strv_join_quoted(char **l) {
+        char *buf = NULL;
+        char **s;
+        size_t allocated = 0, len = 0;
+
+        STRV_FOREACH(s, l) {
+                /* assuming here that escaped string cannot be more
+                 * than twice as long, and reserving space for the
+                 * separator and quotes.
+                 */
+                _cleanup_free_ char *esc = NULL;
+                size_t needed;
+
+                if (!GREEDY_REALLOC(buf, allocated,
+                                    len + strlen(*s) * 2 + 3))
+                        goto oom;
+
+                esc = cescape(*s);
+                if (!esc)
+                        goto oom;
+
+                needed = snprintf(buf + len, allocated - len, "%s\"%s\"",
+                                  len > 0 ? " " : "", esc);
+                assert(needed < allocated - len);
+                len += needed;
+        }
+
+        if (!buf)
+                buf = malloc0(1);
+
+        return buf;
+
+ oom:
+        free(buf);
+        return NULL;
+}
+
 char **strv_append(char **l, const char *s) {
         char **r, **k;
 
diff --git a/src/shared/strv.h b/src/shared/strv.h
index 4ade827..4e80ea6 100644
--- a/src/shared/strv.h
+++ b/src/shared/strv.h
@@ -68,6 +68,7 @@ char **strv_split_quoted(const char *s);
 char **strv_split_newlines(const char *s);
 
 char *strv_join(char **l, const char *separator);
+char *strv_join_quoted(char **l);
 
 char **strv_parse_nulstr(const char *s, size_t l);
 char **strv_split_nulstr(const char *s);
diff --git a/src/test/test-strv.c b/src/test/test-strv.c
index 074e1bb..25bee22 100644
--- a/src/test/test-strv.c
+++ b/src/test/test-strv.c
@@ -42,50 +42,68 @@ static void test_specifier_printf(void) {
         assert_se(streq(w, "xxx a=AAAA b=BBBB yyy"));
 }
 
-static void test_strv_find(void) {
-        const char * const input_table[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
+static const char* const input_table_multiple[] = {
+        "one",
+        "two",
+        "three",
+        NULL,
+};
+
+static const char* const input_table_one[] = {
+        "one",
+        NULL,
+};
+
+static const char* const input_table_none[] = {
+        NULL,
+};
+
+static const char* const input_table_quotes[] = {
+        "\"",
+        "'",
+        "\"\"",
+        "\\",
+        "\\\\",
+        NULL,
+};
+#define QUOTES_STRING                            \
+        "\"\\\"\" "                              \
+        "\"\\\'\" "                              \
+        "\"\\\"\\\"\" "                          \
+        "\"\\\\\" "                              \
+        "\"\\\\\\\\\""
+
+static const char * const input_table_spaces[] = {
+        " ",
+        "' '",
+        "\" ",
+        " \"",
+        " \\\\ ",
+        NULL,
+};
+#define SPACES_STRING                           \
+        "\" \" "                                \
+        "\"\\' \\'\" "                          \
+        "\"\\\" \" "                            \
+        "\" \\\"\" "                            \
+        "\" \\\\\\\\ \""
 
-        assert_se(strv_find((char **)input_table, "three"));
-        assert_se(!strv_find((char **)input_table, "four"));
+static void test_strv_find(void) {
+        assert_se(strv_find((char **)input_table_multiple, "three"));
+        assert_se(!strv_find((char **)input_table_multiple, "four"));
 }
 
 static void test_strv_find_prefix(void) {
-        const char * const input_table[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
-
-        assert_se(strv_find_prefix((char **)input_table, "o"));
-        assert_se(strv_find_prefix((char **)input_table, "one"));
-        assert_se(strv_find_prefix((char **)input_table, ""));
-        assert_se(!strv_find_prefix((char **)input_table, "xxx"));
-        assert_se(!strv_find_prefix((char **)input_table, "onee"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, "o"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, "one"));
+        assert_se(strv_find_prefix((char **)input_table_multiple, ""));
+        assert_se(!strv_find_prefix((char **)input_table_multiple, "xxx"));
+        assert_se(!strv_find_prefix((char **)input_table_multiple, "onee"));
 }
 
 static void test_strv_join(void) {
         _cleanup_free_ char *p = NULL, *q = NULL, *r = NULL, *s = NULL, *t = NULL;
 
-        const char * const input_table_multiple[] = {
-                "one",
-                "two",
-                "three",
-                NULL
-        };
-        const char * const input_table_one[] = {
-                "one",
-                NULL
-        };
-        const char * const input_table_none[] = {
-                NULL
-        };
-
         p = strv_join((char **)input_table_multiple, ", ");
         assert_se(p);
         assert_se(streq(p, "one, two, three"));
@@ -107,6 +125,25 @@ static void test_strv_join(void) {
         assert_se(streq(t, ""));
 }
 
+static void test_strv_quote_unquote(const char* const *split, const char *quoted) {
+        _cleanup_free_ char *p;
+        _cleanup_strv_free_ char **s;
+        char **t;
+
+        p = strv_join_quoted((char **)split);
+        printf("-%s- --- -%s-\n", p, quoted); /* fprintf deals with NULL, puts does not */
+        assert_se(p);
+        assert_se(streq(p, quoted));
+
+        s = strv_split_quoted(quoted);
+        assert_se(s);
+        STRV_FOREACH(t, s) {
+                assert_se(*t);
+                assert_se(streq(*t, *split));
+                split++;
+        }
+}
+
 static void test_strv_split_nulstr(void) {
         _cleanup_strv_free_ char **l = NULL;
         const char nulstr[] = "str0\0str1\0str2\0str3\0";
@@ -253,6 +290,13 @@ int main(int argc, char *argv[]) {
         test_strv_find();
         test_strv_find_prefix();
         test_strv_join();
+
+        test_strv_quote_unquote(input_table_multiple, "\"one\" \"two\" \"three\"");
+        test_strv_quote_unquote(input_table_one, "\"one\"");
+        test_strv_quote_unquote(input_table_none, "");
+        test_strv_quote_unquote(input_table_quotes, QUOTES_STRING);
+        test_strv_quote_unquote(input_table_spaces, SPACES_STRING);
+
         test_strv_split_nulstr();
         test_strv_parse_nulstr();
         test_strv_overlap();



More information about the systemd-commits mailing list