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...] {COMMAND} ...\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\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
187 if (ignore_file(name
))
193 static int install_info_add(const char *name
) {
197 if (!unit_name_valid(name
))
200 if (hashmap_get(have_installed
, name
) ||
201 hashmap_get(will_install
, name
))
204 if (!(i
= new0(InstallInfo
, 1))) {
209 if (!(i
->name
= strdup(name
))) {
214 if ((r
= hashmap_put(will_install
, i
->name
, i
)) < 0)
221 install_info_free(i
);
226 static int config_parse_also(
227 const char *filename
,
243 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
) {
247 if (!(n
= strndup(w
, l
)))
250 r
= install_info_add(n
);
260 static int create_symlink(const char *old_path
, const char *new_path
) {
266 if (arg_action
== ACTION_ENABLE
) {
269 mkdir_parents(new_path
, 0755);
271 if (symlink(old_path
, new_path
) >= 0)
274 if (errno
!= EEXIST
) {
275 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
279 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
281 if (errno
== EINVAL
) {
282 log_error("Cannot link %s to %s, file exists already and is not a symlink.", old_path
, new_path
);
286 log_error("readlink() failed: %s", strerror(-r
));
290 if (streq(dest
, old_path
)) {
296 log_error("Cannot link %s to %s, symlink exists already and points to %s.", old_path
, new_path
, dest
);
304 if (symlink(old_path
, new_path
) >= 0)
307 log_error("Cannot link %s to %s: %m", old_path
, new_path
);
310 } else if (arg_action
== ACTION_DISABLE
) {
313 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
317 if (errno
== EINVAL
) {
318 log_warning("File %s not a symlink, ignoring.", old_path
);
322 log_error("readlink() failed: %s", strerror(-r
));
326 if (!streq(dest
, old_path
)) {
327 log_warning("File %s not a symlink to %s but points to %s, ignoring.", new_path
, old_path
, dest
);
333 if (unlink(new_path
) >= 0)
336 log_error("Cannot unlink %s: %m", new_path
);
339 } else if (arg_action
== ACTION_TEST
) {
342 if ((r
= readlink_and_make_absolute(new_path
, &dest
)) < 0) {
344 if (errno
== ENOENT
|| errno
== EINVAL
)
347 log_error("readlink() failed: %s", strerror(-r
));
351 if (streq(dest
, old_path
)) {
359 assert_not_reached("Unknown action.");
362 static int install_info_symlink_alias(InstallInfo
*i
, const char *config_path
) {
364 char *alias_path
= NULL
;
369 STRV_FOREACH(s
, i
->aliases
) {
371 if (!unit_name_valid(*s
)) {
372 log_error("Invalid name %s.", *s
);
378 if (!(alias_path
= path_make_absolute(*s
, config_path
))) {
379 log_error("Out of memory");
384 if ((r
= create_symlink(i
->path
, alias_path
)) != 0)
387 if (arg_action
== ACTION_DISABLE
)
388 rmdir_parents(alias_path
, config_path
);
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
)
426 rmdir_parents(alias_path
, config_path
);
437 static int install_info_apply(LookupPaths
*paths
, InstallInfo
*i
, const char *config_path
) {
439 const ConfigItem items
[] = {
440 { "Alias", config_parse_strv
, &i
->aliases
, "Install" },
441 { "WantedBy", config_parse_strv
, &i
->wanted_by
, "Install" },
442 { "Also", config_parse_also
, NULL
, "Install" },
444 { NULL
, NULL
, NULL
, NULL
}
448 char *filename
= NULL
;
455 STRV_FOREACH(p
, paths
->unit_path
) {
457 if (!(filename
= path_make_absolute(i
->name
, *p
))) {
458 log_error("Out of memory");
462 if ((f
= fopen(filename
, "re")))
468 if (errno
!= ENOENT
) {
469 log_error("Failed to open %s: %m", filename
);
475 log_error("Couldn't find %s.", i
->name
);
481 if ((r
= config_parse(filename
, f
, NULL
, items
, true, i
)) < 0) {
488 if ((r
= install_info_symlink_alias(i
, config_path
)) != 0)
491 if ((r
= install_info_symlink_wants(i
, config_path
)) != 0)
497 static char *get_config_path(void) {
502 return strdup(SYSTEM_CONFIG_UNIT_PATH
);
505 return strdup(SESSION_CONFIG_UNIT_PATH
);
507 case WHERE_SESSION
: {
510 if (session_config_home(&p
) < 0)
517 assert_not_reached("Unknown config path.");
521 int main(int argc
, char *argv
[]) {
522 int r
, retval
= 1, j
;
525 char *config_path
= NULL
;
529 log_parse_environment();
531 if ((r
= parse_argv(argc
, argv
)) < 0)
538 if ((r
= lookup_paths_init(&paths
, arg_where
== WHERE_SYSTEM
? MANAGER_SYSTEM
: MANAGER_SESSION
)) < 0) {
539 log_error("Failed to determine lookup paths: %s", strerror(-r
));
543 if (!(config_path
= get_config_path())) {
544 log_error("Failed to determine config path");
548 will_install
= hashmap_new(string_hash_func
, string_compare_func
);
549 have_installed
= hashmap_new(string_hash_func
, string_compare_func
);
551 if (!will_install
|| !have_installed
) {
552 log_error("Failed to allocate unit sets.");
556 for (j
= optind
; j
< argc
; j
++)
557 if ((r
= install_info_add(argv
[j
])) < 0)
560 while ((i
= hashmap_first(will_install
))) {
561 assert_se(hashmap_move_one(have_installed
, will_install
, i
->name
) == 0);
563 if ((r
= install_info_apply(&paths
, i
, config_path
)) != 0) {
568 /* In test mode and found something */
574 retval
= arg_action
== ACTION_TEST
? 1 : 0;
577 install_info_hashmap_free(will_install
);
578 install_info_hashmap_free(have_installed
);
580 lookup_paths_free(&paths
);