chiark / gitweb /
util: when using basename() for creating temporary files, verify the resulting name...
[elogind.git] / src / systemctl / systemctl.c
index d356686f78e482c23e3717c47d040e8212a4d0dd..5ed430c82f21e8de0a0aae8187f40365f6aa52a2 100644 (file)
@@ -72,7 +72,7 @@
 #include "bus-util.h"
 #include "bus-message.h"
 #include "bus-error.h"
-#include "bus-errors.h"
+#include "bus-common-errors.h"
 #include "mkdir.h"
 
 static char **arg_types = NULL;
@@ -752,7 +752,7 @@ struct socket_info {
 
         /* Note: triggered is a list here, although it almost certainly
          * will always be one unit. Nevertheless, dbus API allows for multiple
-         * values, so let's follow that.*/
+         * values, so let's follow that. */
         char** triggered;
 
         /* The strv above is shared. free is set only in the first one. */
@@ -5143,7 +5143,7 @@ static int enable_sysv_units(const char *verb, char **args) {
         int r = 0;
 
 #if defined(HAVE_SYSV_COMPAT) && defined(HAVE_CHKCONFIG)
-        unsigned f = 1, t = 1;
+        unsigned f = 0;
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
 
         if (arg_scope != UNIT_FILE_SYSTEM)
@@ -5162,7 +5162,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 return r;
 
         r = 0;
-        for (f = 0; args[f]; f++) {
+        while (args[f]) {
                 const char *name;
                 _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
                 bool found_native = false, found_sysv;
@@ -5173,7 +5173,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 pid_t pid;
                 siginfo_t status;
 
-                name = args[f];
+                name = args[f++];
 
                 if (!endswith(name, ".service"))
                         continue;
@@ -5205,9 +5205,6 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (!found_sysv)
                         continue;
 
-                /* Mark this entry, so that we don't try enabling it as native unit */
-                args[f] = (char*) "";
-
                 log_info("%s is not a native service, redirecting to /sbin/chkconfig.", name);
 
                 if (!isempty(arg_root))
@@ -5256,19 +5253,12 @@ static int enable_sysv_units(const char *verb, char **args) {
                                 return -EINVAL;
                 } else
                         return -EPROTO;
-        }
-
-        /* Drop all SysV units */
-        for (f = 0, t = 0; args[f]; f++) {
 
-                if (isempty(args[f]))
-                        continue;
-
-                args[t++] = args[f];
+                /* Remove this entry, so that we don't try enabling it as native unit */
+                assert(f > 0 && streq(args[f-1], name));
+                assert_se(strv_remove(args + f - 1, name));
         }
 
-        args[t] = NULL;
-
 #endif
         return r;
 }
@@ -5740,16 +5730,16 @@ static int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **un
 }
 
 static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) {
-        int r;
         char *t;
+        int r;
 
         assert(new_path);
         assert(original_path);
         assert(ret_tmp_fn);
 
-        t = tempfn_random(new_path);
-        if (!t)
-                return log_oom();
+        r = tempfn_random(new_path, &t);
+        if (r < 0)
+                return log_error_errno(r, "Failed to determine temporary filename for %s: %m", new_path);
 
         r = mkdir_parents(new_path, 0755);
         if (r < 0) {
@@ -7246,12 +7236,9 @@ static int talk_initctl(void) {
                 return -errno;
         }
 
-        errno = 0;
-        r = loop_write(fd, &request, sizeof(request), false) != sizeof(request);
-        if (r) {
-                log_error_errno(errno, "Failed to write to "INIT_FIFO": %m");
-                return errno > 0 ? -errno : -EIO;
-        }
+        r = loop_write(fd, &request, sizeof(request), false);
+        if (r < 0)
+                return log_error_errno(r, "Failed to write to "INIT_FIFO": %m");
 
         return 1;
 }