]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/rc-local-generator/rc-local-generator.c
core: Record ExecMainStartTimestamp before forking
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
15673083
MS
2
3#include <errno.h>
4#include <stdio.h>
5#include <unistd.h>
6
afe44c8f 7#include "generator.h"
3ed075cf 8#include "initrd-util.h"
15673083 9#include "log.h"
35cd0ba5 10#include "mkdir-label.h"
07630cea 11#include "string-util.h"
15673083 12
7a44c7e3 13static 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 21static 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
835cf75a 27 from = strjoina(SYSTEM_DATA_UNIT_DIR "/", service);
449f4fc2 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
42static 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 57static 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
3ed075cf
LP
62 if (in_initrd()) {
63 log_debug("Skipping generator, running in the initrd.");
64 return EXIT_SUCCESS;
65 }
66
452d2dfd 67 if (check_executable(RC_LOCAL_PATH) >= 0) {
15673083
MS
68 log_debug("Automatically adding rc-local.service.");
69
1332ecb8 70 r = add_symlink("rc-local.service", "multi-user.target");
04b6f7c1 71 }
15673083 72
1332ecb8 73 return r < 0 ? r : k;
15673083 74}
1332ecb8 75
7a44c7e3 76DEFINE_MAIN_GENERATOR_FUNCTION(run);