+static int create_edit_temp_file(const char *new_path, const char *original_path, char **ret_tmp_fn) {
+ char *t;
+ int r;
+
+ assert(new_path);
+ assert(original_path);
+ assert(ret_tmp_fn);
+
+ r = tempfn_random(new_path, &t);
+ if (r < 0)
+ return log_error_errno(r, "Failed to determine temporary filename for %s: %m", new_path);
+
+ r = mkdir_parents(new_path, 0755);
+ if (r < 0) {
+ log_error_errno(r, "Failed to create directories for %s: %m", new_path);
+ free(t);
+ return r;
+ }
+
+ r = copy_file(original_path, t, 0, 0644, 0);
+ if (r == -ENOENT) {
+ r = touch(t);
+ if (r < 0) {
+ log_error_errno(r, "Failed to create temporary file %s: %m", t);
+ free(t);
+ return r;
+ }
+ } else if (r < 0) {
+ log_error_errno(r, "Failed to copy %s to %s: %m", original_path, t);
+ free(t);
+ return r;
+ }
+
+ *ret_tmp_fn = t;
+
+ return 0;
+}
+
+static int get_file_to_edit(const char *name, const char *user_home, const char *user_runtime, char **ret_path) {
+ _cleanup_free_ char *path = NULL, *path2 = NULL, *run = NULL;
+
+ switch (arg_scope) {
+ case UNIT_FILE_SYSTEM:
+ path = path_join(arg_root, SYSTEM_CONFIG_UNIT_PATH, name);
+ if (arg_runtime)
+ run = path_join(arg_root, "/run/systemd/system/", name);
+ break;
+ case UNIT_FILE_GLOBAL:
+ path = path_join(arg_root, USER_CONFIG_UNIT_PATH, name);
+ if (arg_runtime)
+ run = path_join(arg_root, "/run/systemd/user/", name);
+ break;
+ case UNIT_FILE_USER:
+ assert(user_home);
+ assert(user_runtime);
+
+ path = path_join(arg_root, user_home, name);
+ if (arg_runtime) {
+ path2 = path_join(arg_root, USER_CONFIG_UNIT_PATH, name);
+ if (!path2)
+ return log_oom();
+ run = path_join(arg_root, user_runtime, name);
+ }
+ break;
+ default:
+ assert_not_reached("Invalid scope");
+ }
+ if (!path || (arg_runtime && !run))
+ return log_oom();
+
+ if (arg_runtime) {
+ if (access(path, F_OK) >= 0)
+ return log_error_errno(EEXIST, "Refusing to create \"%s\" because it would be overriden by \"%s\" anyway.",
+ run, path);
+ if (path2 && access(path2, F_OK) >= 0)
+ return log_error_errno(EEXIST, "Refusing to create \"%s\" because it would be overriden by \"%s\" anyway.",
+ run, path2);
+ *ret_path = run;
+ run = NULL;
+ } else {
+ *ret_path = path;
+ path = NULL;
+ }
+
+ return 0;
+}
+
+
+static int unit_file_create_dropin(const char *unit_name, const char *user_home, const char *user_runtime, char **ret_new_path, char **ret_tmp_path) {
+ char *tmp_new_path, *ending;
+ char *tmp_tmp_path;
+ int r;
+
+ assert(unit_name);
+ assert(ret_new_path);
+ assert(ret_tmp_path);
+
+ ending = strappenda(unit_name, ".d/override.conf");
+ r = get_file_to_edit(ending, user_home, user_runtime, &tmp_new_path);
+ if (r < 0)
+ return r;
+
+ r = create_edit_temp_file(tmp_new_path, tmp_new_path, &tmp_tmp_path);
+ if (r < 0) {
+ free(tmp_new_path);
+ return r;
+ }
+
+ *ret_new_path = tmp_new_path;
+ *ret_tmp_path = tmp_tmp_path;
+
+ return 0;
+}
+
+static int unit_file_create_copy(const char *unit_name,
+ const char *fragment_path,
+ const char *user_home,
+ const char *user_runtime,
+ char **ret_new_path,
+ char **ret_tmp_path) {
+ char *tmp_new_path;
+ char *tmp_tmp_path;
+ int r;
+
+ assert(fragment_path);
+ assert(unit_name);
+ assert(ret_new_path);
+ assert(ret_tmp_path);
+
+ r = get_file_to_edit(unit_name, user_home, user_runtime, &tmp_new_path);
+ if (r < 0)
+ return r;
+
+ if (!path_equal(fragment_path, tmp_new_path) && access(tmp_new_path, F_OK) == 0) {
+ char response;
+
+ r = ask_char(&response, "yn", "%s already exists, are you sure to overwrite it with %s? [(y)es, (n)o] ", tmp_new_path, fragment_path);
+ if (r < 0) {
+ free(tmp_new_path);
+ return r;
+ }
+ if (response != 'y') {
+ log_warning("%s ignored", unit_name);
+ free(tmp_new_path);
+ return -1;
+ }
+ }
+
+ r = create_edit_temp_file(tmp_new_path, fragment_path, &tmp_tmp_path);
+ if (r < 0) {
+ log_error_errno(r, "Failed to create temporary file for %s: %m", tmp_new_path);
+ free(tmp_new_path);
+ return r;
+ }
+
+ *ret_new_path = tmp_new_path;
+ *ret_tmp_path = tmp_tmp_path;
+
+ return 0;
+}
+
+static int run_editor(char **paths) {
+ pid_t pid;
+ int r;
+
+ assert(paths);
+
+ pid = fork();
+ if (pid < 0) {
+ log_error_errno(errno, "Failed to fork: %m");
+ return -errno;
+ }
+
+ if (pid == 0) {
+ const char **args;
+ char **backup_editors = STRV_MAKE("nano", "vim", "vi");
+ char *editor;
+ char **tmp_path, **original_path, **p;
+ unsigned i = 1;
+ size_t argc;
+
+ argc = strv_length(paths)/2 + 1;
+ args = newa(const char*, argc + 1);
+
+ args[0] = NULL;
+ STRV_FOREACH_PAIR(original_path, tmp_path, paths) {
+ args[i] = *tmp_path;
+ i++;
+ }
+ args[argc] = NULL;
+
+ /* SYSTEMD_EDITOR takes precedence over EDITOR which takes precedence over VISUAL
+ * If neither SYSTEMD_EDITOR nor EDITOR nor VISUAL are present,
+ * we try to execute well known editors
+ */
+ editor = getenv("SYSTEMD_EDITOR");
+ if (!editor)
+ editor = getenv("EDITOR");
+ if (!editor)
+ editor = getenv("VISUAL");
+
+ if (!isempty(editor)) {
+ args[0] = editor;
+ execvp(editor, (char* const*) args);
+ }
+
+ STRV_FOREACH(p, backup_editors) {
+ args[0] = *p;
+ execvp(*p, (char* const*) args);
+ /* We do not fail if the editor doesn't exist
+ * because we want to try each one of them before
+ * failing.
+ */
+ if (errno != ENOENT) {
+ log_error("Failed to execute %s: %m", editor);
+ _exit(EXIT_FAILURE);
+ }
+ }
+
+ log_error("Cannot edit unit(s), no editor available. Please set either $SYSTEMD_EDITOR or $EDITOR or $VISUAL.");
+ _exit(EXIT_FAILURE);
+ }
+
+ r = wait_for_terminate_and_warn("editor", pid, true);
+ if (r < 0)
+ return log_error_errno(r, "Failed to wait for child: %m");
+
+ return r;
+}
+
+static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
+ _cleanup_free_ char *user_home = NULL;
+ _cleanup_free_ char *user_runtime = NULL;
+ _cleanup_lookup_paths_free_ LookupPaths lp = {};
+ bool avoid_bus_cache;
+ char **name;
+ int r;
+
+ assert(names);
+ assert(paths);
+
+ r = init_home_and_lookup_paths(&user_home, &user_runtime, &lp);
+ if (r < 0)
+ return r;
+
+ avoid_bus_cache = !bus || avoid_bus();
+
+ STRV_FOREACH(name, names) {
+ _cleanup_free_ char *path = NULL;
+ char *new_path, *tmp_path;
+
+ r = unit_find_paths(bus, *name, avoid_bus_cache, &lp, &path, NULL);
+ if (r < 0)
+ return r;
+ else if (r == 0 || !path)
+ // FIXME: support units with path==NULL (no FragmentPath)
+ return log_error_errno(ENOENT, "Unit %s not found, cannot edit.", *name);
+
+ if (arg_full)
+ r = unit_file_create_copy(*name, path, user_home, user_runtime, &new_path, &tmp_path);
+ else
+ r = unit_file_create_dropin(*name, user_home, user_runtime, &new_path, &tmp_path);
+ if (r < 0)
+ return r;
+
+ r = strv_push_pair(paths, new_path, tmp_path);
+ if (r < 0)
+ return log_oom();
+ }
+
+ return 0;
+}
+
+static int edit(sd_bus *bus, char **args) {
+ _cleanup_strv_free_ char **names = NULL;
+ _cleanup_strv_free_ char **paths = NULL;
+ char **original, **tmp;
+ int r;
+
+ assert(args);
+
+ if (!on_tty()) {
+ log_error("Cannot edit units if we are not on a tty");
+ return -EINVAL;
+ }
+
+ if (arg_transport != BUS_TRANSPORT_LOCAL) {
+ log_error("Cannot remotely edit units");
+ return -EINVAL;
+ }
+
+ r = expand_names(bus, args + 1, NULL, &names);
+ if (r < 0)
+ return log_error_errno(r, "Failed to expand names: %m");
+
+ if (!names) {
+ log_error("No unit name found by expanding names");
+ return -ENOENT;
+ }
+
+ r = find_paths_to_edit(bus, names, &paths);
+ if (r < 0)
+ return r;
+
+ if (strv_isempty(paths)) {
+ log_error("Cannot find any units to edit");
+ return -ENOENT;
+ }
+
+ r = run_editor(paths);
+ if (r < 0)
+ goto end;
+
+ STRV_FOREACH_PAIR(original, tmp, paths) {
+ /* If the temporary file is empty we ignore it.
+ * It's useful if the user wants to cancel its modification
+ */
+ if (null_or_empty_path(*tmp)) {
+ log_warning("Edition of %s canceled: temporary file empty", *original);
+ continue;
+ }
+ r = rename(*tmp, *original);
+ if (r < 0) {
+ r = log_error_errno(errno, "Failed to rename %s to %s: %m", *tmp, *original);
+ goto end;
+ }
+ }
+
+ if (!arg_no_reload && bus && !avoid_bus())
+ r = daemon_reload(bus, args);
+
+end:
+ STRV_FOREACH_PAIR(original, tmp, paths)
+ unlink_noerrno(*tmp);
+
+ return r;
+}
+