]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: new option --drop-in for specifying drop-in filename
authorMike Yuan <me@yhndnzj.com>
Sat, 17 Dec 2022 13:07:32 +0000 (21:07 +0800)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 20 Dec 2022 14:59:11 +0000 (15:59 +0100)
Previously 'systemctl edit' would only operate on
'override.conf', but users may need more than that.
Thus the new option '--drop-in' is added to allow
users to specify the drop-in file name.

Closes #25767

man/systemctl.xml
src/systemctl/systemctl-edit.c
src/systemctl/systemctl.c
src/systemctl/systemctl.h

index 193f6b9800476f4c4b9e488c21cca00c28be291d..a4023349d5f5e6dd88cc06b5d90b3272a5afcc8d 100644 (file)
@@ -1061,6 +1061,9 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
             temporary files which will be written to the real location if the
             editor exits successfully.</para>
 
+            <para>If <option>--drop-in=</option> is specified, the given drop-in file name
+            will be used instead of the default <filename>override.conf</filename>.</para>
+
             <para>If <option>--full</option> is specified, this will copy the
             original units instead of creating drop-in files.</para>
 
@@ -2417,6 +2420,15 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err
         <listitem><para>When used with <command>bind</command>, creates a read-only bind mount.</para></listitem>
       </varlistentry>
 
+      <varlistentry>
+        <term><option>--drop-in=</option></term>
+
+        <listitem>
+          <para>When used with <command>edit</command>, use the given drop-in file name instead of
+          <filename>override.conf</filename>.</para>
+        </listitem>
+      </varlistentry>
+
       <xi:include href="user-system-options.xml" xpointer="host" />
       <xi:include href="user-system-options.xml" xpointer="machine" />
 
index 3d519ccb8d3d1d84450d0fc76ffda9d991979864..133bce663142048d01d228342b55af0c529c6fc5 100644 (file)
@@ -426,12 +426,32 @@ static int find_paths_to_edit(
         _cleanup_(hashmap_freep) Hashmap *cached_name_map = NULL, *cached_id_map = NULL;
         _cleanup_(edit_file_free_all) EditFile *edit_files = NULL;
         _cleanup_(lookup_paths_free) LookupPaths lp = {};
+        _cleanup_free_ char *drop_in_alloc = NULL, *suffix = NULL;
+        const char *drop_in;
         size_t n_edit_files = 0;
         int r;
 
         assert(names);
         assert(ret_edit_files);
 
+        if (isempty(arg_drop_in))
+                drop_in = "override.conf";
+        else if (!endswith(arg_drop_in, ".conf")) {
+                drop_in_alloc = strjoin(arg_drop_in, ".conf");
+                if (!drop_in_alloc)
+                        return log_oom();
+
+                drop_in = drop_in_alloc;
+        } else
+                drop_in = arg_drop_in;
+
+        if (!filename_is_valid(drop_in))
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid drop-in file name '%s'.", drop_in);
+
+        suffix = strjoin(".d/", drop_in);
+        if (!suffix)
+                return log_oom();
+
         r = lookup_paths_init(&lp, arg_scope, 0, arg_root);
         if (r < 0)
                 return r;
@@ -468,7 +488,7 @@ static int find_paths_to_edit(
                         r = unit_file_create_new(
                                         &lp,
                                         *name,
-                                        arg_full ? NULL : ".d/override.conf",
+                                        arg_full ? NULL : suffix,
                                         NULL,
                                         edit_files + n_edit_files);
                 } else {
@@ -508,7 +528,7 @@ static int find_paths_to_edit(
                                 r = unit_file_create_new(
                                                 &lp,
                                                 unit_name,
-                                                ".d/override.conf",
+                                                suffix,
                                                 unit_paths,
                                                 edit_files + n_edit_files);
                         }
index d13c7867e228e844b46227a13fa28924eb5dec5e..ae9b95620e59ea8fd6fca51f2976bb1d18d0174f 100644 (file)
@@ -118,6 +118,7 @@ TimestampStyle arg_timestamp_style = TIMESTAMP_PRETTY;
 bool arg_read_only = false;
 bool arg_mkdir = false;
 bool arg_marked = false;
+const char *arg_drop_in = NULL;
 
 STATIC_DESTRUCTOR_REGISTER(arg_types, strv_freep);
 STATIC_DESTRUCTOR_REGISTER(arg_states, strv_freep);
@@ -131,6 +132,7 @@ STATIC_DESTRUCTOR_REGISTER(arg_reboot_argument, unsetp);
 STATIC_DESTRUCTOR_REGISTER(arg_host, unsetp);
 STATIC_DESTRUCTOR_REGISTER(arg_boot_loader_entry, unsetp);
 STATIC_DESTRUCTOR_REGISTER(arg_clean_what, strv_freep);
+STATIC_DESTRUCTOR_REGISTER(arg_drop_in, unsetp);
 
 static int systemctl_help(void) {
         _cleanup_free_ char *link = NULL;
@@ -316,6 +318,7 @@ static int systemctl_help(void) {
                "     --read-only         Create read-only bind mount\n"
                "     --mkdir             Create directory before mounting, if missing\n"
                "     --marked            Restart/reload previously marked units\n"
+               "     --drop-in=NAME      Edit unit files using the specified drop-in file name\n"
                "\nSee the %2$s for details.\n",
                program_invocation_short_name,
                link,
@@ -438,6 +441,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 ARG_MKDIR,
                 ARG_MARKED,
                 ARG_NO_WARN,
+                ARG_DROP_IN,
         };
 
         static const struct option options[] = {
@@ -500,6 +504,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 { "read-only",           no_argument,       NULL, ARG_READ_ONLY           },
                 { "mkdir",               no_argument,       NULL, ARG_MKDIR               },
                 { "marked",              no_argument,       NULL, ARG_MARKED              },
+                { "drop-in",             required_argument, NULL, ARG_DROP_IN             },
                 {}
         };
 
@@ -936,6 +941,10 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         arg_no_warn = true;
                         break;
 
+                case ARG_DROP_IN:
+                        arg_drop_in = optarg;
+                        break;
+
                 case '.':
                         /* Output an error mimicking getopt, and print a hint afterwards */
                         log_error("%s: invalid option -- '.'", program_invocation_name);
index 1a7a6e28d39c961c7320bc8c8c3fe18566e4f399..9f9b8faa6963e36125a73f73f7729c378e12dafb 100644 (file)
@@ -97,6 +97,7 @@ extern TimestampStyle arg_timestamp_style;
 extern bool arg_read_only;
 extern bool arg_mkdir;
 extern bool arg_marked;
+extern const char *arg_drop_in;
 
 static inline const char* arg_job_mode(void) {
         return _arg_job_mode ?: "replace";