]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rc-local-generator/rc-local-generator.c
2762fa7e89f5ad830e56f9da2265dddfd83c1f2a
[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 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #include "log.h"
27 #include "mkdir.h"
28 #include "string-util.h"
29 #include "util.h"
30
31 static const char *arg_dest = "/tmp";
32
33 static int add_symlink(const char *service, const char *where) {
34 const char *from, *to;
35 int r;
36
37 assert(service);
38 assert(where);
39
40 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", service);
41 to = strjoina(arg_dest, "/", where, ".wants/", service);
42
43 (void) mkdir_parents_label(to, 0755);
44
45 r = symlink(from, to);
46 if (r < 0) {
47 if (errno == EEXIST)
48 return 0;
49
50 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
51 }
52
53 return 1;
54 }
55
56 int main(int argc, char *argv[]) {
57 int ret = EXIT_SUCCESS;
58
59 if (argc > 1 && argc != 4) {
60 log_error("This program takes three or no arguments.");
61 return EXIT_FAILURE;
62 }
63
64 if (argc > 1)
65 arg_dest = argv[1];
66
67 log_set_prohibit_ipc(true);
68 log_set_target(LOG_TARGET_AUTO);
69 log_parse_environment();
70 log_open();
71
72 umask(0022);
73
74 if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) >= 0) {
75 log_debug("Automatically adding rc-local.service.");
76
77 if (add_symlink("rc-local.service", "multi-user.target") < 0)
78 ret = EXIT_FAILURE;
79 }
80
81 if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) >= 0) {
82 log_debug("Automatically adding halt-local.service.");
83
84 if (add_symlink("halt-local.service", "final.target") < 0)
85 ret = EXIT_FAILURE;
86 }
87
88 return ret;
89 }