]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rc-local-generator/rc-local-generator.c
e6122d415e7e86706e1496fddf5ff26ed170f09b
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 Copyright 2011 Michal Schmidt
7 ***/
8
9 #include <errno.h>
10 #include <stdio.h>
11 #include <unistd.h>
12
13 #include "log.h"
14 #include "mkdir.h"
15 #include "string-util.h"
16 #include "util.h"
17
18 static const char *arg_dest = "/tmp";
19
20 static int add_symlink(const char *service, const char *where) {
21 const char *from, *to;
22 int r;
23
24 assert(service);
25 assert(where);
26
27 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", service);
28 to = strjoina(arg_dest, "/", where, ".wants/", service);
29
30 (void) mkdir_parents_label(to, 0755);
31
32 r = symlink(from, to);
33 if (r < 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 int main(int argc, char *argv[]) {
44 int ret = EXIT_SUCCESS;
45
46 if (argc > 1 && argc != 4) {
47 log_error("This program takes three or no arguments.");
48 return EXIT_FAILURE;
49 }
50
51 if (argc > 1)
52 arg_dest = argv[1];
53
54 log_set_prohibit_ipc(true);
55 log_set_target(LOG_TARGET_AUTO);
56 log_parse_environment();
57 log_open();
58
59 umask(0022);
60
61 if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) >= 0) {
62 log_debug("Automatically adding rc-local.service.");
63
64 if (add_symlink("rc-local.service", "multi-user.target") < 0)
65 ret = EXIT_FAILURE;
66 }
67
68 if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) >= 0) {
69 log_debug("Automatically adding halt-local.service.");
70
71 if (add_symlink("halt-local.service", "final.target") < 0)
72 ret = EXIT_FAILURE;
73 }
74
75 return ret;
76 }