]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rc-local-generator/rc-local-generator.c
treewide: use log_*_errno whenever %m is in the format 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 "util.h"
29 #include "mkdir.h"
30
31 #ifndef RC_LOCAL_SCRIPT_PATH_START
32 #define RC_LOCAL_SCRIPT_PATH_START "/etc/rc.d/rc.local"
33 #endif
34
35 #ifndef RC_LOCAL_SCRIPT_PATH_STOP
36 #define RC_LOCAL_SCRIPT_PATH_STOP "/sbin/halt.local"
37 #endif
38
39 const char *arg_dest = "/tmp";
40
41 static int add_symlink(const char *service, const char *where) {
42 _cleanup_free_ char *from = NULL, *to = NULL;
43 int r;
44
45 assert(service);
46 assert(where);
47
48 from = strjoin(SYSTEM_DATA_UNIT_PATH, "/", service, NULL);
49 if (!from)
50 return log_oom();
51
52 to = strjoin(arg_dest, "/", where, ".wants/", service, NULL);
53 if (!to)
54 return log_oom();
55
56 mkdir_parents_label(to, 0755);
57
58 r = symlink(from, to);
59 if (r < 0) {
60 if (errno == EEXIST)
61 return 0;
62
63 log_error_errno(errno, "Failed to create symlink %s: %m", to);
64 return -errno;
65 }
66
67 return 1;
68 }
69
70 int main(int argc, char *argv[]) {
71 int r = EXIT_SUCCESS;
72
73 if (argc > 1 && argc != 4) {
74 log_error("This program takes three or no arguments.");
75 return EXIT_FAILURE;
76 }
77
78 if (argc > 1)
79 arg_dest = argv[1];
80
81 log_set_target(LOG_TARGET_SAFE);
82 log_parse_environment();
83 log_open();
84
85 umask(0022);
86
87 if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) >= 0) {
88 log_debug("Automatically adding rc-local.service.");
89
90 if (add_symlink("rc-local.service", "multi-user.target") < 0)
91 r = EXIT_FAILURE;
92 }
93
94 if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) >= 0) {
95 log_debug("Automatically adding halt-local.service.");
96
97 if (add_symlink("halt-local.service", "final.target") < 0)
98 r = EXIT_FAILURE;
99 }
100
101 return r;
102 }