]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/rc-local-generator/rc-local-generator.c
rc-local-generator: small shortification
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
CommitLineData
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"
1332ecb8 9#include "main-func.h"
49e942b2 10#include "mkdir.h"
07630cea
LP
11#include "string-util.h"
12#include "util.h"
15673083 13
dd422d1e 14static const char *arg_dest = "/tmp";
15673083 15
bca89fe8
LP
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
04b6f7c1 22static int add_symlink(const char *service, const char *where) {
449f4fc2 23 const char *from, *to;
15673083
MS
24
25 assert(service);
7b1132f6 26 assert(where);
15673083 27
449f4fc2
LP
28 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", service);
29 to = strjoina(arg_dest, "/", where, ".wants/", service);
15673083 30
449f4fc2 31 (void) mkdir_parents_label(to, 0755);
15673083 32
c5df80a0 33 if (symlink(from, to) < 0) {
15673083 34 if (errno == EEXIST)
7b1132f6 35 return 0;
15673083 36
e1427b13 37 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
7b1132f6 38 }
15673083 39
7b1132f6 40 return 1;
15673083
MS
41}
42
1332ecb8
ZJS
43static int run(int argc, char *argv[]) {
44 int r = 0, k = 0;
15673083 45
1332ecb8
ZJS
46 log_setup_generator();
47
48 if (argc > 1 && argc != 4)
49 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
15673083 50
07719a21
LP
51 if (argc > 1)
52 arg_dest = argv[1];
53
19069ed8
ZJS
54 if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) < 0)
55 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
56 RC_LOCAL_SCRIPT_PATH_START " is not executable: %m");
57 else {
15673083
MS
58 log_debug("Automatically adding rc-local.service.");
59
1332ecb8 60 r = add_symlink("rc-local.service", "multi-user.target");
04b6f7c1 61 }
15673083 62
19069ed8
ZJS
63 if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) < 0)
64 log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, errno,
65 RC_LOCAL_SCRIPT_PATH_STOP " is not executable: %m");
66 else {
04b6f7c1
LP
67 log_debug("Automatically adding halt-local.service.");
68
1332ecb8 69 k = add_symlink("halt-local.service", "final.target");
15673083
MS
70 }
71
1332ecb8 72 return r < 0 ? r : k;
15673083 73}
1332ecb8
ZJS
74
75DEFINE_MAIN_FUNCTION(run);