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