]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rc-local-generator/rc-local-generator.c
log.h: new log_oom() -> int -ENOMEM, use it
[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 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
32 #define SCRIPT_PATH_START "/etc/rc.d/rc.local"
33 #elif defined(TARGET_SUSE)
34 #define SCRIPT_PATH_START "/etc/init.d/boot.local"
35 #endif
36
37 #define SCRIPT_PATH_STOP "/sbin/halt.local"
38
39 const char *arg_dest = "/tmp";
40
41 static int add_symlink(const char *service, const char *where) {
42 char *from = NULL, *to = NULL;
43 int r;
44
45 assert(service);
46
47 asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", service);
48 asprintf(&to, "%s/%s.wants/%s", arg_dest, where, service);
49
50 if (!from || !to) {
51 r = log_oom();
52 goto finish;
53 }
54
55 mkdir_parents_label(to, 0755);
56
57 r = symlink(from, to);
58 if (r < 0) {
59 if (errno == EEXIST)
60 r = 0;
61 else {
62 log_error("Failed to create symlink from %s to %s: %m", from, to);
63 r = -errno;
64 }
65 }
66
67 finish:
68 free(from);
69 free(to);
70
71 return r;
72 }
73
74 static bool file_is_executable(const char *f) {
75 struct stat st;
76
77 if (stat(f, &st) < 0)
78 return false;
79
80 return S_ISREG(st.st_mode) && (st.st_mode & 0111);
81 }
82
83 int main(int argc, char *argv[]) {
84 int r = EXIT_SUCCESS;
85
86 if (argc > 1 && argc != 4) {
87 log_error("This program takes three or no arguments.");
88 return EXIT_FAILURE;
89 }
90
91 if (argc > 1)
92 arg_dest = argv[1];
93
94 log_set_target(LOG_TARGET_SAFE);
95 log_parse_environment();
96 log_open();
97
98 umask(0022);
99
100 if (file_is_executable(SCRIPT_PATH_START)) {
101 log_debug("Automatically adding rc-local.service.");
102
103 if (add_symlink("rc-local.service", "multi-user.target") < 0)
104 r = EXIT_FAILURE;
105 }
106
107 if (file_is_executable(SCRIPT_PATH_STOP)) {
108 log_debug("Automatically adding halt-local.service.");
109
110 if (add_symlink("halt-local.service", "final.target") < 0)
111 r = EXIT_FAILURE;
112 }
113
114 return r;
115 }