chiark / gitweb /
run: allow non-absolute paths as command
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 8 Sep 2013 11:51:39 +0000 (07:51 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 9 Sep 2013 17:49:30 +0000 (13:49 -0400)
TODO
man/systemd-run.xml
src/core/manager.c
src/run/run.c
src/shared/path-util.c
src/shared/path-util.h
src/test/test-path-util.c

diff --git a/TODO b/TODO
index ff29cba8f9dfda94296fd2e8c8344bbe5e311309..b83fd67b8642bae0b317be4801fa43315668866d 100644 (file)
--- a/TODO
+++ b/TODO
@@ -715,6 +715,8 @@ Features:
    - document initcall_debug
    - kernel cmdline "bootchart" option for simplicity?
 
    - document initcall_debug
    - kernel cmdline "bootchart" option for simplicity?
 
+* systemd-run is missing completion scripts
+
 External:
 
 * dbus:
 External:
 
 * dbus:
index 6b0189c25d60f4851d01bbc7d0e19b44f8dfbc6d..e76a4020036c5bec361df55ae3271f990698dd1d 100644 (file)
@@ -187,6 +187,23 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
     code otherwise.</para>
   </refsect1>
 
     code otherwise.</para>
   </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>
   <refsect1>
     <title>See Also</title>
     <para>
index 10ccffb4044ded10e616a12ad5049a7faac58b2c..669af1524f30b7f50bd29473062fe21c94720cd9 100644 (file)
@@ -456,11 +456,7 @@ static int manager_setup_signals(Manager *m) {
 }
 
 static int manager_default_environment(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);
 
 
         assert(m);
 
index c5d314bdf1974701720017cbe7091dfa80a98275..da8c788eea1fee8bfd894347a47880f929c8dad8 100644 (file)
@@ -39,7 +39,7 @@ static bool arg_send_sighup = false;
 
 static int help(void) {
 
 
 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"
                "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;
 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();
         int r;
 
         log_parse_environment();
@@ -334,6 +334,13 @@ int main(int argc, char* argv[]) {
         if (r <= 0)
                 goto fail;
 
         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) {
         if (!arg_description) {
                 description = strv_join(argv + optind, " ");
                 if (!description) {
index 68881357784eba69f4cd8d165b32f903f058323e..8e108db53173f1c39ef1ae2965c7081031ee6fce 100644 (file)
@@ -425,3 +425,51 @@ int path_is_os_tree(const char *path) {
 
         return r < 0 ? 0 : 1;
 }
 
         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;
+        }
+}
index d187743769f85b9e5a88b4e1e3789904954fdbb5..9452931586836e007d19e5dc861e60d5e8a76eb2 100644 (file)
 
 #include "macro.h"
 
 
 #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_;
 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 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);
index f396b32ffeb72b4e15bb648c6afeb32e6df7ac76..b0aeb11a63f239e117aef0f16a6ecc8c8cf71f63 100644 (file)
@@ -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();
 int main(void) {
         test_path();
+        test_find_binary();
         return 0;
 }
         return 0;
 }