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 "Install init system units.\n\n"
64 " -h --help Show this help\n"
65 " --force Override existing links\n"
66 " --system Install into system\n"
67 " --session Install into session\n"
68 " --global Install into all sessions\n"
70 " enable [NAME...] Enable one or more units\n"
71 " disable [NAME...] Disable one or more units\n"
72 " test [NAME...] Test whether any of the specified units are enabled\n",
73 program_invocation_short_name);
78 static int parse_argv(int argc, char *argv[]) {
87 static const struct option options[] = {
88 { "help", no_argument, NULL, 'h' },
89 { "session", no_argument, NULL, ARG_SESSION },
90 { "system", no_argument, NULL, ARG_SYSTEM },
91 { "global", no_argument, NULL, ARG_GLOBAL },
92 { "force", no_argument, NULL, ARG_FORCE },
101 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
110 arg_where = WHERE_SESSION;
114 arg_where = WHERE_SYSTEM;
118 arg_where = WHERE_GLOBAL;
129 log_error("Unknown option code %c", c);
134 if (optind >= argc) {
135 log_error("Missing verb.");
139 if (streq(argv[optind], "enable"))
140 arg_action = ACTION_ENABLE;
141 else if (streq(argv[optind], "disable"))
142 arg_action = ACTION_DISABLE;
143 else if (streq(argv[optind], "test"))
144 arg_action = ACTION_TEST;
146 log_error("Unknown verb %s", argv[optind]);
152 if (optind >= argc) {
153 log_error("Missing unit name.");
160 static void install_info_free(InstallInfo *i) {
165 strv_free(i->aliases);
166 strv_free(i->wanted_by);
170 static void install_info_hashmap_free(Hashmap *m) {
173 while ((i = hashmap_steal_first(m)))
174 install_info_free(i);
179 static bool unit_name_valid(const char *name) {
181 /* This is a minimal version of unit_name_valid() from
184 if (strchr(name, '/'))
190 if (ignore_file(name))
196 static int install_info_add(const char *name) {
200 if (!unit_name_valid(name))
203 if (hashmap_get(have_installed, name) ||
204 hashmap_get(will_install, name))
207 if (!(i = new0(InstallInfo, 1))) {
212 if (!(i->name = strdup(name))) {
217 if ((r = hashmap_put(will_install, i->name, i)) < 0)
224 install_info_free(i);
229 static int config_parse_also(
230 const char *filename,
246 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
250 if (!(n = strndup(w, l)))
253 r = install_info_add(n);
263 static int create_symlink(const char *old_path, const char *new_path) {
269 if (arg_action == ACTION_ENABLE) {
272 mkdir_parents(new_path, 0755);
274 if (symlink(old_path, new_path) >= 0)
277 if (errno != EEXIST) {
278 log_error("Cannot link %s to %s: %m", old_path, new_path);
282 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
284 if (errno == EINVAL) {
285 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path, new_path);
289 log_error("readlink() failed: %s", strerror(-r));
293 if (streq(dest, old_path)) {
299 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path, new_path, dest);
307 if (symlink(old_path, new_path) >= 0)
310 log_error("Cannot link %s to %s: %m", old_path, new_path);
313 } else if (arg_action == ACTION_DISABLE) {
316 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
320 if (errno == EINVAL) {
321 log_warning("File %s not a symlink, ignoring.", old_path);
325 log_error("readlink() failed: %s", strerror(-r));
329 if (!streq(dest, old_path)) {
330 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path, old_path, dest);
336 if (unlink(new_path) >= 0)
339 log_error("Cannot unlink %s: %m", new_path);
342 } else if (arg_action == ACTION_TEST) {
345 if ((r = readlink_and_make_absolute(new_path, &dest)) < 0) {
347 if (errno == ENOENT || errno == EINVAL)
350 log_error("readlink() failed: %s", strerror(-r));
354 if (streq(dest, old_path)) {
362 assert_not_reached("Unknown action.");
365 static int install_info_symlink_alias(InstallInfo *i, const char *config_path) {
367 char *alias_path = NULL;
372 STRV_FOREACH(s, i->aliases) {
374 if (!unit_name_valid(*s)) {
375 log_error("Invalid name %s.", *s);
381 if (!(alias_path = path_make_absolute(*s, config_path))) {
382 log_error("Out of memory");
387 if ((r = create_symlink(i->path, alias_path)) != 0)
399 static int install_info_symlink_wants(InstallInfo *i, const char *config_path) {
401 char *alias_path = NULL;
406 STRV_FOREACH(s, i->wanted_by) {
407 if (!unit_name_valid(*s)) {
408 log_error("Invalid name %s.", *s);
416 if (asprintf(&alias_path, "%s/%s.wants/%s", config_path, *s, i->name) < 0) {
417 log_error("Out of memory");
422 if ((r = create_symlink(i->path, alias_path)) != 0)
425 if (arg_action == ACTION_DISABLE) {
428 /* Try to remove .wants dir if we don't need it anymore */
429 if (asprintf(&t, "%s/%s.wants", config_path, *s) >= 0) {
444 static int install_info_apply(LookupPaths *paths, InstallInfo *i, const char *config_path) {
446 const ConfigItem items[] = {
447 { "Alias", config_parse_strv, &i->aliases, "Install" },
448 { "WantedBy", config_parse_strv, &i->wanted_by, "Install" },
449 { "Also", config_parse_also, NULL, "Install" },
451 { NULL, NULL, NULL, NULL }
455 char *filename = NULL;
462 STRV_FOREACH(p, paths->unit_path) {
464 if (!(filename = path_make_absolute(i->name, *p))) {
465 log_error("Out of memory");
469 if ((f = fopen(filename, "re")))
475 if (errno != ENOENT) {
476 log_error("Failed to open %s: %m", filename);
482 log_error("Couldn't find %s.", i->name);
488 if ((r = config_parse(filename, f, NULL, items, true, i)) < 0) {
495 if ((r = install_info_symlink_alias(i, config_path)) != 0)
498 if ((r = install_info_symlink_wants(i, config_path)) != 0)
504 static char *get_config_path(void) {
509 return strdup(SYSTEM_CONFIG_UNIT_PATH);
512 return strdup(SESSION_CONFIG_UNIT_PATH);
514 case WHERE_SESSION: {
517 if (session_config_home(&p) < 0)
524 assert_not_reached("Unknown config path.");
528 int main(int argc, char *argv[]) {
529 int r, retval = 1, j;
532 char *config_path = NULL;
536 log_parse_environment();
538 if ((r = parse_argv(argc, argv)) < 0)
545 if ((r = lookup_paths_init(&paths, arg_where == WHERE_SYSTEM ? MANAGER_INIT : MANAGER_SESSION)) < 0) {
546 log_error("Failed to determine lookup paths: %s", strerror(-r));
550 if (!(config_path = get_config_path())) {
551 log_error("Failed to determine config path");
555 will_install = hashmap_new(string_hash_func, string_compare_func);
556 have_installed = hashmap_new(string_hash_func, string_compare_func);
558 if (!will_install || !have_installed) {
559 log_error("Failed to allocate unit sets.");
563 for (j = optind; j < argc; j++)
564 if ((r = install_info_add(argv[j])) < 0)
567 while ((i = hashmap_first(will_install))) {
568 assert_se(hashmap_move_one(have_installed, will_install, i->name) == 0);
570 if ((r = install_info_apply(&paths, i, config_path)) != 0) {
575 /* In test mode and found something */
581 retval = arg_action == ACTION_TEST ? 1 : 0;
584 install_info_hashmap_free(will_install);
585 install_info_hashmap_free(have_installed);
587 lookup_paths_free(&paths);