[systemd-devel] [PATCH] systemctl: check the argument to -t for invalid values

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed May 23 09:24:05 PDT 2012


Systemctl accepted anything as the argument for -t, and simply said '0
units found'. It is better to catch this user error early.
---
A small thing thing I noticed today.

 src/systemctl/systemctl.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 66f4113..d353071 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -4325,6 +4325,19 @@ static int runlevel_help(void) {
         return 0;
 }
 
+static int arg_type_ok(const char *type) {
+
+        static const char *types[] = {"service", "socket", "device", "mount",
+                                      "swap", "automount", "target", "path",
+                                      "timer", "snapshot"};
+        unsigned i;
+
+        for (i=0; i<ELEMENTSOF(types); i++)
+                if (!strcmp(type, types[i]))
+                        return true;
+        return false;
+}
+
 static int systemctl_parse_argv(int argc, char *argv[]) {
 
         enum {
@@ -4408,6 +4421,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         return 0;
 
                 case 't':
+                        if (!arg_type_ok(optarg)) {
+                                log_error("Invalid unit type '%s'", optarg);
+                                return -EINVAL;
+                        }
                         arg_type = optarg;
                         break;
 
-- 
1.7.10.2.667.gf1138e2



More information about the systemd-devel mailing list