]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysusers: take configuration as positional arguments
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 29 Jan 2018 13:47:01 +0000 (14:47 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 2 Feb 2018 09:18:13 +0000 (10:18 +0100)
If the configuration is included in a script, this is more convient.
I thought it would be possible to use this for rpm scriptlets with
'%pre -p systemd-sysuser "..."', but apparently there is no way to pass
arguments to the executable ($1 is used for the package installation count).
But this functionality seems generally useful, e.g. for testing and one-off
scripts, so let's keep it.

There's a slight change in behaviour when files are given on the command line:
if we cannot parse them, error out instead of ignoring the failure. When trying
to parse all configuration files, we don't want to fail even if some config
files are broken, but when parsing a list of items specified explicitly, we
should.

v2:
- rename --direct to --inline

man/systemd-sysusers.xml
src/sysusers/sysusers.c

index 73ba4e4a84018e318f858dd78e993551cfaa78e9..781635688917de38e3b85779a3bc05657329b041 100644 (file)
         paths. </para></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--inline</option></term>
+        <listitem><para>Treat each positional argument as a separate configuration
+        line instead of a file name.</para></listitem>
+      </varlistentry>
+
       <xi:include href="standard-options.xml" xpointer="help" />
       <xi:include href="standard-options.xml" xpointer="version" />
     </variablelist>
index 288f6a16658ed48c8d4e29319a0f2998868dade7..340e0db7983338aeafc9ff60d63878eb59925d08 100644 (file)
@@ -74,6 +74,7 @@ typedef struct Item {
 } Item;
 
 static char *arg_root = NULL;
+static bool arg_inline = false;
 
 static const char conf_file_dirs[] = CONF_PATHS_NULSTR("sysusers.d");
 
@@ -1718,6 +1719,7 @@ static void help(void) {
                "  -h --help                 Show this help\n"
                "     --version              Show package version\n"
                "     --root=PATH            Operate on an alternate filesystem root\n"
+               "     --inline               Treat arguments as configuration lines\n"
                , program_invocation_short_name);
 }
 
@@ -1726,12 +1728,14 @@ static int parse_argv(int argc, char *argv[]) {
         enum {
                 ARG_VERSION = 0x100,
                 ARG_ROOT,
+                ARG_INLINE,
         };
 
         static const struct option options[] = {
                 { "help",    no_argument,       NULL, 'h'         },
                 { "version", no_argument,       NULL, ARG_VERSION },
                 { "root",    required_argument, NULL, ARG_ROOT    },
+                { "inline",  no_argument,       NULL, ARG_INLINE  },
                 {}
         };
 
@@ -1757,6 +1761,10 @@ static int parse_argv(int argc, char *argv[]) {
                                 return r;
                         break;
 
+                case ARG_INLINE:
+                        arg_inline = true;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -1795,9 +1803,13 @@ int main(int argc, char *argv[]) {
                 int j;
 
                 for (j = optind; j < argc; j++) {
-                        k = read_config_file(argv[j], false);
-                        if (k < 0 && r == 0)
-                                r = k;
+                        if (arg_inline)
+                                /* Use (argument):n, where n==1 for the first positional arg */
+                                r = parse_line("(argument)", j - optind + 1, argv[j]);
+                        else
+                                r = read_config_file(argv[j], false);
+                        if (r < 0)
+                                goto finish;
                 }
         } else {
                 _cleanup_strv_free_ char **files = NULL;