From: Mike Yuan Date: Sat, 17 Dec 2022 13:07:32 +0000 (+0800) Subject: systemctl: new option --drop-in for specifying drop-in filename X-Git-Tag: v253-rc1~239 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f206809b9740aa601c5bb73e80e6ed20fa76ed0f;p=thirdparty%2Fsystemd.git systemctl: new option --drop-in for specifying drop-in filename 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 --- diff --git a/man/systemctl.xml b/man/systemctl.xml index 193f6b98004..a4023349d5f 100644 --- a/man/systemctl.xml +++ b/man/systemctl.xml @@ -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. + If is specified, the given drop-in file name + will be used instead of the default override.conf. + If is specified, this will copy the original units instead of creating drop-in files. @@ -2417,6 +2420,15 @@ Jan 12 10:46:45 example.com bluetoothd[8900]: gatt-time-server: Input/output err When used with bind, creates a read-only bind mount. + + + + + When used with edit, use the given drop-in file name instead of + override.conf. + + + diff --git a/src/systemctl/systemctl-edit.c b/src/systemctl/systemctl-edit.c index 3d519ccb8d3..133bce66314 100644 --- a/src/systemctl/systemctl-edit.c +++ b/src/systemctl/systemctl-edit.c @@ -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); } diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index d13c7867e22..ae9b95620e5 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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); diff --git a/src/systemctl/systemctl.h b/src/systemctl/systemctl.h index 1a7a6e28d39..9f9b8faa696 100644 --- a/src/systemctl/systemctl.h +++ b/src/systemctl/systemctl.h @@ -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";