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