]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rc-local-generator/rc-local-generator.c
8b1b75f569262cb91b971ffe62b47385e7d07fa4
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "generator.h"
6 #include "initrd-util.h"
7 #include "log.h"
8 #include "mkdir-label.h"
9 #include "string-util.h"
10
11 static const char *arg_dest = NULL;
12
13 /* So you are reading this, and might wonder: why is this implemented as a generator rather than as a plain, statically
14 * enabled service that carries appropriate ConditionFileIsExecutable= lines? The answer is this: conditions bypass
15 * execution of a service's binary, but they have no influence on unit dependencies. Thus, a service that is
16 * conditioned out will still act as synchronization point in the dependency tree, and we'd rather not have that for
17 * these two legacy scripts. */
18
19 static int add_symlink(const char *service, const char *where) {
20 const char *from, *to;
21
22 assert(service);
23 assert(where);
24
25 from = strjoina(SYSTEM_DATA_UNIT_DIR "/", service);
26 to = strjoina(arg_dest, "/", where, ".wants/", service);
27
28 (void) mkdir_parents_label(to, 0755);
29
30 if (symlink(from, to) < 0) {
31 if (errno == EEXIST)
32 return 0;
33
34 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
35 }
36
37 return 1;
38 }
39
40 static int check_executable(const char *path) {
41 assert(path);
42
43 if (access(path, X_OK) < 0) {
44 if (errno == ENOENT)
45 return log_debug_errno(errno, "%s does not exist, skipping.", path);
46 if (errno == EACCES)
47 return log_info_errno(errno, "%s is not marked executable, skipping.", path);
48
49 return log_warning_errno(errno, "Couldn't determine if %s exists and is executable, skipping: %m", path);
50 }
51
52 return 0;
53 }
54
55 static int run(const char *dest, const char *dest_early, const char *dest_late) {
56 int r = 0, k = 0;
57
58 assert_se(arg_dest = dest);
59
60 if (in_initrd()) {
61 log_debug("Skipping generator, running in the initrd.");
62 return EXIT_SUCCESS;
63 }
64
65 if (check_executable(SYSTEM_SYSVRCLOCAL_PATH) >= 0) {
66 log_debug("Automatically adding rc-local.service.");
67
68 r = add_symlink("rc-local.service", "multi-user.target");
69 }
70
71 return r < 0 ? r : k;
72 }
73
74 DEFINE_MAIN_GENERATOR_FUNCTION(run);