]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/rc-local-generator/rc-local-generator.c
analyze: fix typo
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
15673083 2
15673083
MS
3#include <unistd.h>
4
afe44c8f 5#include "generator.h"
3ed075cf 6#include "initrd-util.h"
15673083 7#include "log.h"
35cd0ba5 8#include "mkdir-label.h"
07630cea 9#include "string-util.h"
15673083 10
7a44c7e3 11static const char *arg_dest = NULL;
15673083 12
bca89fe8
LP
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
04b6f7c1 19static int add_symlink(const char *service, const char *where) {
449f4fc2 20 const char *from, *to;
15673083
MS
21
22 assert(service);
7b1132f6 23 assert(where);
15673083 24
835cf75a 25 from = strjoina(SYSTEM_DATA_UNIT_DIR "/", service);
449f4fc2 26 to = strjoina(arg_dest, "/", where, ".wants/", service);
15673083 27
449f4fc2 28 (void) mkdir_parents_label(to, 0755);
15673083 29
c5df80a0 30 if (symlink(from, to) < 0) {
15673083 31 if (errno == EEXIST)
7b1132f6 32 return 0;
15673083 33
e1427b13 34 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
7b1132f6 35 }
15673083 36
7b1132f6 37 return 1;
15673083
MS
38}
39
7f6b1a21
LP
40static 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
7a44c7e3 55static int run(const char *dest, const char *dest_early, const char *dest_late) {
1332ecb8 56 int r = 0, k = 0;
15673083 57
7a44c7e3 58 assert_se(arg_dest = dest);
07719a21 59
3ed075cf
LP
60 if (in_initrd()) {
61 log_debug("Skipping generator, running in the initrd.");
62 return EXIT_SUCCESS;
63 }
64
011360ee 65 if (check_executable(SYSTEM_SYSVRCLOCAL_PATH) >= 0) {
15673083
MS
66 log_debug("Automatically adding rc-local.service.");
67
1332ecb8 68 r = add_symlink("rc-local.service", "multi-user.target");
04b6f7c1 69 }
15673083 70
1332ecb8 71 return r < 0 ? r : k;
15673083 72}
1332ecb8 73
7a44c7e3 74DEFINE_MAIN_GENERATOR_FUNCTION(run);