+ r = 1;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int reset_maintenance(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ unsigned i;
+ int r;
+ DBusError error;
+
+ assert(bus);
+ dbus_error_init(&error);
+
+ if (n <= 1)
+ return daemon_reload(bus, args, n);
+
+ for (i = 1; i < n; i++) {
+
+ if (!(m = dbus_message_new_method_call(
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "ResetMaintenanceUnit"))) {
+ log_error("Could not allocate message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!dbus_message_append_args(m,
+ DBUS_TYPE_STRING, args + i,
+ DBUS_TYPE_INVALID)) {
+ log_error("Could not append arguments to message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_unref(m);
+ dbus_message_unref(reply);
+ m = reply = NULL;
+ }
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int show_enviroment(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ DBusError error;
+ DBusMessageIter iter, sub, sub2;
+ int r;
+ const char
+ *interface = "org.freedesktop.systemd1.Manager",
+ *property = "Environment";
+
+ dbus_error_init(&error);
+
+ if (!(m = dbus_message_new_method_call(
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.DBus.Properties",
+ "Get"))) {
+ log_error("Could not allocate message.");
+ return -ENOMEM;
+ }
+
+ if (!dbus_message_append_args(m,
+ DBUS_TYPE_STRING, &interface,
+ DBUS_TYPE_STRING, &property,
+ DBUS_TYPE_INVALID)) {
+ log_error("Could not append arguments to message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ if (!dbus_message_iter_init(reply, &iter) ||
+ dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&iter, &sub);
+
+ if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_ARRAY ||
+ dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_STRING) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_recurse(&sub, &sub2);
+
+ while (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_INVALID) {
+ const char *text;
+
+ if (dbus_message_iter_get_arg_type(&sub2) != DBUS_TYPE_STRING) {
+ log_error("Failed to parse reply.");
+ r = -EIO;
+ goto finish;
+ }
+
+ dbus_message_iter_get_basic(&sub2, &text);
+ printf("%s\n", text);
+
+ dbus_message_iter_next(&sub2);
+ }
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+static int set_environment(DBusConnection *bus, char **args, unsigned n) {
+ DBusMessage *m = NULL, *reply = NULL;
+ DBusError error;
+ int r;
+ const char *method;
+ DBusMessageIter iter, sub;
+ unsigned i;
+
+ dbus_error_init(&error);
+
+ method = streq(args[0], "set-environment")
+ ? "SetEnvironment"
+ : "UnsetEnvironment";
+
+ if (!(m = dbus_message_new_method_call(
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ method))) {
+
+ log_error("Could not allocate message.");
+ return -ENOMEM;
+ }
+
+ dbus_message_iter_init_append(m, &iter);
+
+ if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
+ log_error("Could not append arguments to message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ for (i = 1; i < n; i++)
+ if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &args[i])) {
+ log_error("Could not append arguments to message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!dbus_message_iter_close_container(&iter, &sub)) {
+ log_error("Could not append arguments to message.");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+ log_error("Failed to issue method call: %s", bus_error_message(&error));
+ r = -EIO;
+ goto finish;
+ }
+
+ r = 0;
+
+finish:
+ if (m)
+ dbus_message_unref(m);
+
+ if (reply)
+ dbus_message_unref(reply);
+
+ dbus_error_free(&error);
+
+ return r;
+}
+
+typedef struct {
+ char *name;
+ char *path;
+
+ char **aliases;
+ char **wanted_by;
+} InstallInfo;
+
+static Hashmap *will_install = NULL, *have_installed = NULL;
+static Set *remove_symlinks_to = NULL;
+
+static void install_info_free(InstallInfo *i) {
+ assert(i);
+
+ free(i->name);
+ free(i->path);
+ strv_free(i->aliases);
+ strv_free(i->wanted_by);
+ free(i);
+}
+
+static void install_info_hashmap_free(Hashmap *m) {
+ InstallInfo *i;
+
+ while ((i = hashmap_steal_first(m)))
+ install_info_free(i);
+
+ hashmap_free(m);
+}
+
+static bool unit_name_valid(const char *name) {
+
+ /* This is a minimal version of unit_name_valid() from
+ * unit-name.c */
+
+ if (!*name)
+ return false;
+
+ if (ignore_file(name))
+ return false;
+
+ return true;
+}
+
+static int install_info_add(const char *name) {
+ InstallInfo *i;
+ int r;
+
+ assert(will_install);
+
+ if (!unit_name_valid(name))
+ return -EINVAL;
+
+ if (hashmap_get(have_installed, name) ||
+ hashmap_get(will_install, name))
+ return 0;
+
+ if (!(i = new0(InstallInfo, 1))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if (!(i->name = strdup(name))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if ((r = hashmap_put(will_install, i->name, i)) < 0)
+ goto fail;
+
+ return 0;
+
+fail:
+ if (i)
+ install_info_free(i);
+
+ return r;
+}
+
+static int config_parse_also(
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ char *w;
+ size_t l;
+ char *state;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+
+ FOREACH_WORD_QUOTED(w, l, rvalue, state) {
+ char *n;
+ int r;
+
+ if (!(n = strndup(w, l)))
+ return -ENOMEM;
+
+ r = install_info_add(n);
+ free(n);
+
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int mark_symlink_for_removal(const char *p) {
+ char *n;
+ int r;
+
+ assert(p);
+ assert(path_is_absolute(p));
+
+ if (!remove_symlinks_to)
+ return 0;
+
+ if (!(n = strdup(p)))
+ return -ENOMEM;
+
+ path_kill_slashes(n);
+
+ if ((r = set_put(remove_symlinks_to, n)) < 0) {
+ free(n);
+ return r == -EEXIST ? 0 : r;
+ }
+
+ return 0;
+}
+
+static int remove_marked_symlinks_fd(int fd, const char *config_path, const char *root, bool *deleted) {
+ int r = 0;
+ DIR *d;
+ struct dirent *de;
+
+ assert(fd >= 0);
+ assert(root);
+ assert(deleted);
+
+ if (!(d = fdopendir(fd))) {
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ rewinddir(d);
+
+ while ((de = readdir(d))) {
+ bool is_dir = false, is_link = false;
+
+ if (ignore_file(de->d_name))
+ continue;
+
+ if (de->d_type == DT_LNK)
+ is_link = true;
+ else if (de->d_type == DT_DIR)
+ is_dir = true;
+ else if (de->d_type == DT_UNKNOWN) {
+ struct stat st;
+
+ if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
+ log_error("Failed to stat %s/%s: %m", root, de->d_name);
+
+ if (r == 0)
+ r = -errno;
+ continue;
+ }
+
+ is_link = S_ISLNK(st.st_mode);
+ is_dir = S_ISDIR(st.st_mode);
+ } else
+ continue;
+
+ if (is_dir) {
+ int nfd, q;
+ char *p;
+
+ if ((nfd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0) {
+ log_error("Failed to open %s/%s: %m", root, de->d_name);
+
+ if (r == 0)
+ r = -errno;
+ continue;
+ }
+
+ if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
+ log_error("Failed to allocate directory string.");
+ close_nointr_nofail(nfd);
+ r = -ENOMEM;
+ break;
+ }
+
+ /* This will close nfd, regardless whether it succeeds or not */
+ q = remove_marked_symlinks_fd(nfd, config_path, p, deleted);
+ free(p);
+
+ if (r == 0)
+ r = q;
+
+ } else if (is_link) {
+ char *p, *dest, *c;
+ int q;
+
+ if (asprintf(&p, "%s/%s", root, de->d_name) < 0) {
+ log_error("Failed to allocate symlink string.");
+ r = -ENOMEM;
+ break;
+ }
+
+ if ((q = readlink_and_make_absolute(p, &dest)) < 0) {
+ log_error("Cannot read symlink %s: %s", p, strerror(-q));
+ free(p);
+
+ if (r == 0)
+ r = q;
+ continue;
+ }
+
+ if ((c = canonicalize_file_name(dest))) {
+ /* This might fail if the destination
+ * is already removed */
+
+ free(dest);
+ dest = c;
+ }
+
+ path_kill_slashes(dest);
+ if (set_get(remove_symlinks_to, dest)) {
+
+ if (!arg_quiet)
+ log_info("rm '%s'", p);
+
+ if (unlink(p) < 0) {
+ log_error("Cannot unlink symlink %s: %m", p);
+
+ if (r == 0)
+ r = -errno;
+ } else {
+ rmdir_parents(p, config_path);
+ path_kill_slashes(p);
+
+ if (!set_get(remove_symlinks_to, p)) {
+
+ if ((r = mark_symlink_for_removal(p)) < 0) {
+ if (r == 0)
+ r = q;
+ } else
+ *deleted = true;
+ }
+ }
+ }
+
+ free(p);
+ free(dest);
+ }
+ }
+
+ closedir(d);
+
+ return r;
+}
+
+static int remove_marked_symlinks(const char *config_path) {
+ int fd, r = 0;
+ bool deleted;
+
+ assert(config_path);
+
+ if (set_size(remove_symlinks_to) <= 0)
+ return 0;
+
+ if ((fd = open(config_path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW)) < 0)
+ return -errno;
+
+ do {
+ int q, cfd;
+ deleted = false;
+
+ if ((cfd = dup(fd)) < 0) {
+ r = -errno;
+ break;
+ }
+
+ /* This takes possession of cfd and closes it */
+ if ((q = remove_marked_symlinks_fd(cfd, config_path, config_path, &deleted)) < 0) {
+ if (r == 0)
+ r = q;
+ }
+ } while (deleted);
+
+ close_nointr_nofail(fd);
+
+ return r;
+}
+
+static int create_symlink(const char *verb, const char *old_path, const char *new_path) {
+ int r;
+
+ assert(old_path);
+ assert(new_path);
+ assert(verb);
+
+ if (streq(verb, "enable")) {
+ char *dest;
+
+ mkdir_parents(new_path, 0755);
+
+ if (symlink(old_path, new_path) >= 0) {
+
+ if (!arg_quiet)
+ log_info("ln -s '%s' '%s'", old_path, new_path);
+
+ return 0;
+ }
+
+ if (errno != EEXIST) {
+ log_error("Cannot link %s to %s: %m", old_path, new_path);
+ return -errno;
+ }
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+
+ if (errno == EINVAL) {
+ log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
+ return -EEXIST;
+ }
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (streq(dest, old_path)) {
+ free(dest);
+ return 0;
+ }
+
+ if (!arg_force) {
+ log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
+ free(dest);
+ return -EEXIST;
+ }
+
+ free(dest);
+ unlink(new_path);
+
+ if (!arg_quiet)
+ log_info("ln -s '%s' '%s'", old_path, new_path);
+
+ if (symlink(old_path, new_path) >= 0)
+ return 0;
+
+ log_error("Cannot link %s to %s: %m", old_path, new_path);
+ return -errno;
+
+ } else if (streq(verb, "disable")) {
+ char *dest;
+
+ if ((r = mark_symlink_for_removal(old_path)) < 0)
+ return r;
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+ if (errno == ENOENT)
+ return 0;
+
+ if (errno == EINVAL) {
+ log_warning("File %s not a symlink, ignoring.", old_path);
+ return 0;
+ }
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (!streq(dest, old_path)) {
+ log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
+ free(dest);
+ return 0;
+ }
+
+ free(dest);
+
+ if ((r = mark_symlink_for_removal(new_path)) < 0)
+ return r;
+
+ if (!arg_quiet)
+ log_info("rm '%s'", new_path);
+
+ if (unlink(new_path) >= 0)
+ return 0;
+
+ log_error("Cannot unlink %s: %m", new_path);
+ return -errno;
+
+ } else if (streq(verb, "is-enabled")) {
+ char *dest;
+
+ if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
+
+ if (errno == ENOENT || errno == EINVAL)
+ return 0;
+
+ log_error("readlink() failed: %s", strerror(-r));
+ return r;
+ }
+
+ if (streq(dest, old_path)) {
+ free(dest);
+ return 1;
+ }
+
+ return 0;
+ }
+
+ assert_not_reached("Unknown action.");
+}
+
+static int install_info_symlink_alias(const char *verb, InstallInfo *i, const char *config_path) {
+ char **s;
+ char *alias_path = NULL;
+ int r;
+
+ assert(verb);
+ assert(i);
+ assert(config_path);
+
+ STRV_FOREACH(s, i->aliases) {
+
+ if (!unit_name_valid(*s)) {
+ log_error("Invalid name %s.", *s);
+ r = -EINVAL;
+ goto finish;
+ }
+
+ free(alias_path);
+ if (!(alias_path = path_make_absolute(*s, config_path))) {
+ log_error("Out of memory");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if ((r = create_symlink(verb, i->path, alias_path)) != 0)
+ goto finish;
+
+ if (streq(verb, "disable"))
+ rmdir_parents(alias_path, config_path);
+ }
+
+ r = 0;
+
+finish:
+ free(alias_path);
+
+ return r;
+}
+
+static int install_info_symlink_wants(const char *verb, InstallInfo *i, const char *config_path) {
+ char **s;
+ char *alias_path = NULL;
+ int r;
+
+ assert(verb);
+ assert(i);
+ assert(config_path);
+
+ STRV_FOREACH(s, i->wanted_by) {
+ if (!unit_name_valid(*s)) {
+ log_error("Invalid name %s.", *s);
+ r = -EINVAL;
+ goto finish;
+ }
+
+ free(alias_path);
+ alias_path = NULL;
+
+ if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
+ log_error("Out of memory");
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if ((r = create_symlink(verb, i->path, alias_path)) != 0)
+ goto finish;
+
+ if (streq(verb, "disable"))
+ rmdir_parents(alias_path, config_path);
+ }
+
+ r = 0;