]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
generators: accept one or three args, do not write to /tmp
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Jul 2022 16:19:04 +0000 (18:19 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Jul 2022 11:10:39 +0000 (13:10 +0200)
Since the general generator logic was established in the rewrite in
07719a21b6425d378b36bb8d7f47ad5ec5296d28, generators would always write to /tmp
by default. I think this not a good default at all, because generators write a
bunch of files and would create a mess in /tmp. And for debugging, one
generally needs to remove all the files in the output directory, because
generators will complain in the output paths are already present. Thus the
approach of disabling console logging and writing many files to /tmp when
invoked with no arguments is not nice, so let's disallow operation with no
args.

But when debugging, one generally does not care about the separate output dirs
(most generators use only one). Thus the general pattern I use is something
like:
  rm -rf /tmp/x && mkdir /tmp/x && build/some-generator /tmp/{x,x,x}
This commit allows only one directory to be specified and simplifies this to:
  rm -rf /tmp/x && mkdir /tmp/x && build/some-generator /tmp/x

man/systemd.generator.xml
src/shared/generator.h

index 1f916ac65eb11cdfadfd466e9d6822c31386abf9..d837afb6f9df5519151efe0b57e2b89ee8484c6c 100644 (file)
@@ -26,8 +26,8 @@
     <cmdsynopsis>
       <command index='false'>/path/to/generator</command>
       <arg choice="plain"><replaceable>normal-dir</replaceable></arg>
-      <arg choice="plain"><replaceable>early-dir</replaceable></arg>
-      <arg choice="plain"><replaceable>late-dir</replaceable></arg>
+      <arg choice="option"><replaceable>early-dir</replaceable></arg>
+      <arg choice="option"><replaceable>late-dir</replaceable></arg>
     </cmdsynopsis>
 
     <para>
     that they can extend the unit file hierarchy the service manager subsequently loads and operates
     on.</para>
 
-    <para>Each generator is called with three directory paths that are to be used for generator output. In
-    these three directories, generators may dynamically generate unit files (regular ones, instances, as well
-    as templates), unit file <filename>.d/</filename> drop-ins, and create symbolic links to unit files to
-    add additional dependencies, create aliases, or instantiate existing templates. Those directories are
-    included in the unit load path of
-    <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>, allowing
-    generated configuration to extend or override existing definitions.</para>
+    <para><command>systemd</command> will call each generator with three directory paths that are to be used
+    for generator output. In these three directories, generators may dynamically generate unit files (regular
+    ones, instances, as well as templates), unit file <filename>.d/</filename> drop-ins, and create symbolic
+    links to unit files to add additional dependencies, create aliases, or instantiate existing templates.
+    Those directories are included in the unit load path, allowing generated configuration to extend or
+    override existing definitions. For tests, generators may be called with just one argument; the generator
+    should assume that all three paths are the same in that case.</para>
 
     <para>Directory paths for generator output differ by priority: <filename>…/generator.early</filename> has
     priority higher than the admin configuration in <filename>/etc/</filename>, while
@@ -96,7 +96,8 @@
     <para>Generators are invoked with three arguments: paths to directories where generators can place their
     generated unit files or symlinks. By default those paths are runtime directories that are included in the
     search path of <command>systemd</command>, but a generator may be called with different paths for
-    debugging purposes.</para>
+    debugging purposes. If only one argument is provided, the generator should use the same directory as the
+    the three output paths.</para>
 
     <orderedlist>
       <listitem>
index bd768daf491c2990ff3f705a79e097ee3110d66a..1b4f36ac53c6796713690dad7e122d0c36287355 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <stdio.h>
 
+#include "macro.h"
 #include "main-func.h"
 
 int generator_open_unit_file(
@@ -86,11 +87,11 @@ void log_setup_generator(void);
         _DEFINE_MAIN_FUNCTION(                                          \
                 ({                                                      \
                         log_setup_generator();                          \
-                        if (argc > 1 && argc != 4)                      \
+                        if (!IN_SET(argc, 2, 4))                        \
                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), \
-                                                       "This program takes zero or three arguments."); \
+                                                       "This program takes one or three arguments."); \
                 }),                                                     \
-                impl(argc > 1 ? argv[1] : "/tmp",                       \
-                     argc > 1 ? argv[2] : "/tmp",                       \
-                     argc > 1 ? argv[3] : "/tmp"),                      \
+                impl(argv[1],                                           \
+                     argv[argc == 4 ? 2 : 1],                           \
+                     argv[argc == 4 ? 3 : 1]),                          \
                 r < 0 ? EXIT_FAILURE : EXIT_SUCCESS)