1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "path-lookup.h"
33 #include "conf-parser.h"
35 static bool arg_force = false;
41 } arg_where = WHERE_SYSTEM;
48 } arg_action = ACTION_INVALID;
58 Hashmap *will_install = NULL, *have_installed = NULL;
60 static int help(void) {
62 printf("%s [options]\n\n"
63 " -h --help Show this help\n"
64 " --force Override existing links\n"
65 " --system Install into system\n"
66 " --session Install into session\n"
67 " --global Install into all sessions\n"
69 " enable [NAME...] Enable one or more units\n"
70 " disable [NAME...] Disable one or more units\n"
71 " test [NAME...] Test whether any of the specified units are enabled\n",
77 static int parse_argv(int argc, char *argv[]) {
86 static const struct option options[] = {
87 { "help", no_argument, NULL, 'h' },
88 { "session", no_argument, NULL, ARG_SESSION },
89 { "system", no_argument, NULL, ARG_SYSTEM },
90 { "global", no_argument, NULL, ARG_GLOBAL },
91 { "force", no_argument, NULL, ARG_FORCE },
100 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
109 arg_where = WHERE_SESSION;
113 arg_where = WHERE_SYSTEM;
117 arg_where = WHERE_GLOBAL;
128 log_error("Unknown option code %c", c);
133 if (optind >= argc) {
134 log_error("Missing verb.");
138 if (streq(argv[optind], "enable"))
139 arg_action = ACTION_ENABLE;
140 else if (streq(argv[optind], "disable"))
141 arg_action = ACTION_DISABLE;
142 else if (streq(argv[optind], "test"))
143 arg_action = ACTION_TEST;
145 log_error("Unknown verb %s", argv[optind]);
151 if (optind >= argc) {
152 log_error("Missing unit name.");
159 static void install_info_free(InstallInfo *i) {
164 strv_free(i->aliases);
165 strv_free(i->wanted_by);
169 static void install_info_hashmap_free(Hashmap *m) {
172 while ((i = hashmap_steal_first(m)))
173 install_info_free(i);
178 static bool unit_name_valid(const char *name) {
180 /* This is a minimal version of unit_name_valid() from
183 if (strchr(name, '/'))
189 if (ignore_file(name))
195 static int install_info_add(const char *name) {
199 if (!unit_name_valid(name))
202 if (hashmap_get(have_installed, name) ||
203 hashmap_get(will_install, name))
206 if (!(i = new0(InstallInfo, 1))) {
211 if (!(i->name = strdup(name))) {
216 if ((r = hashmap_put(will_install, i->name, i)) < 0)
223 install_info_free(i);
228 static int config_parse_also(
229 const char *filename,
245 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
249 if (!(n = strndup(w, l)))
252 r = install_info_add(n);
262 static int create_symlink(const char *old_path, const char *new_path) {
268 if (arg_action == ACTION_ENABLE) {
271 mkdir_parents(new_path, 0755);
273 if (symlink(old_path, new_path) >= 0)
276 if (errno != EEXIST) {
277 log_error("Cannot link %s to %s: %m", old_path, new_path);
281 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
283 if (errno == EINVAL) {
284 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
288 log_error("readlink() failed: %s", strerror(-r));
292 if (streq(dest, old_path)) {
298 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
306 if (symlink(old_path, new_path) >= 0)
309 log_error("Cannot link %s to %s: %m", old_path, new_path);
312 } else if (arg_action == ACTION_DISABLE) {
315 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
319 if (errno == EINVAL) {
320 log_warning("File %s not a symlink, ignoring.", old_path);
324 log_error("readlink() failed: %s", strerror(-r));
328 if (!streq(dest, old_path)) {
329 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
335 if (unlink(new_path) >= 0)
338 log_error("Cannot unlink %s: %m", new_path);
341 } else if (arg_action == ACTION_TEST) {
344 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
346 if (errno == ENOENT || errno == EINVAL)
349 log_error("readlink() failed: %s", strerror(-r));
353 if (streq(dest, old_path)) {
361 assert_not_reached("Unknown action.");
364 static int install_info_symlink_alias(InstallInfo *i, const char *config_path) {
366 char *alias_path = NULL;
371 STRV_FOREACH(s, i->aliases) {
373 if (!unit_name_valid(*s)) {
374 log_error("Invalid name %s.", *s);
380 if (!(alias_path = path_make_absolute(*s, config_path))) {
381 log_error("Out of memory");
386 if ((r = create_symlink(i->path, alias_path)) != 0)
398 static int install_info_symlink_wants(InstallInfo *i, const char *config_path) {
400 char *alias_path = NULL;
405 STRV_FOREACH(s, i->wanted_by) {
406 if (!unit_name_valid(*s)) {
407 log_error("Invalid name %s.", *s);
415 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
416 log_error("Out of memory");
421 if ((r = create_symlink(i->path, alias_path)) != 0)
424 if (arg_action == ACTION_DISABLE) {
427 /* Try to remove .wants dir if we don't need it anymore */
428 if (asprintf(&t, "%s/%s.wants", config_path, *s) >= 0) {
443 static int install_info_apply(LookupPaths *paths, InstallInfo *i, const char *config_path) {
445 const ConfigItem items[] = {
446 { "Alias", config_parse_strv, &i->aliases, "Install" },
447 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
448 { "Also", config_parse_also, NULL, "Install" },
450 { NULL, NULL, NULL, NULL }
454 char *filename = NULL;
461 STRV_FOREACH(p, paths->unit_path) {
463 if (!(filename = path_make_absolute(i->name, *p))) {
464 log_error("Out of memory");
468 if ((f = fopen(filename, "re")))
474 if (errno != ENOENT) {
475 log_error("Failed to open %s: %m", filename);
481 log_error("Couldn't find %s.", i->name);
487 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
494 if ((r = install_info_symlink_alias(i, config_path)) != 0)
497 if ((r = install_info_symlink_wants(i, config_path)) != 0)
503 static char *get_config_path(void) {
508 return strdup(SYSTEM_CONFIG_UNIT_PATH);
511 return strdup(SESSION_CONFIG_UNIT_PATH);
513 case WHERE_SESSION: {
516 if (session_config_home(&p) < 0)
523 assert_not_reached("Unknown config path.");
527 int main(int argc, char *argv[]) {
528 int r, retval = 1, j;
531 char *config_path = NULL;
535 log_set_target(LOG_TARGET_CONSOLE);
536 log_set_max_level(LOG_INFO);
537 log_parse_environment();
539 if ((r = parse_argv(argc, argv)) < 0)
546 if ((r = lookup_paths_init(&paths, arg_where == WHERE_SYSTEM ? MANAGER_INIT : MANAGER_SESSION)) < 0) {
547 log_error("Failed to determine lookup paths: %s", strerror(-r));
551 if (!(config_path = get_config_path())) {
552 log_error("Failed to determine config path");
556 will_install = hashmap_new(string_hash_func, string_compare_func);
557 have_installed = hashmap_new(string_hash_func, string_compare_func);
559 if (!will_install || !have_installed) {
560 log_error("Failed to allocate unit sets.");
564 for (j = optind; j < argc; j++)
565 if ((r = install_info_add(argv[j])) < 0)
568 while ((i = hashmap_first(will_install))) {
569 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
571 if ((r = install_info_apply(&paths, i, config_path)) != 0) {
576 /* In test mode and found something */
582 retval = arg_action == ACTION_TEST ? 1 : 0;
585 install_info_hashmap_free(will_install);
586 install_info_hashmap_free(have_installed);
588 lookup_paths_free(&paths);