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