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