chiark / gitweb /
42d7ae41edd5ad97f2b0ccef898fc9e04e6553ec
[elogind.git] / src / 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 "/etc/rc.d/rc.local"
33 #elif defined(TARGET_SUSE)
34 #define SCRIPT_PATH "/etc/init.d/boot.local"
35 #endif
36
37 const char *arg_dest = "/tmp";
38
39 static int add_symlink(const char *service) {
40         char *from = NULL, *to = NULL;
41         int r;
42
43         assert(service);
44
45         asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", service);
46         asprintf(&to, "%s/multi-user.target.wants/%s", arg_dest, service);
47
48         if (!from || !to) {
49                 log_error("Out of memory");
50                 r = -ENOMEM;
51                 goto finish;
52         }
53
54         mkdir_parents(to, 0755);
55
56         r = symlink(from, to);
57         if (r < 0) {
58                 if (errno == EEXIST)
59                         r = 0;
60                 else {
61                         log_error("Failed to create symlink from %s to %s: %m", from, to);
62                         r = -errno;
63                 }
64         }
65
66 finish:
67
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
85         int r = EXIT_SUCCESS;
86
87         if (argc > 2) {
88                 log_error("This program takes one or no arguments.");
89                 return EXIT_FAILURE;
90         }
91
92         log_set_target(LOG_TARGET_AUTO);
93         log_parse_environment();
94         log_open();
95
96         if (argc > 1)
97                 arg_dest = argv[1];
98
99         if (file_is_executable(SCRIPT_PATH)) {
100                 log_debug("Automatically adding rc-local.service.");
101
102                 if (add_symlink("rc-local.service") < 0)
103                         r = EXIT_FAILURE;
104
105         }
106
107         return r;
108 }