chiark / gitweb /
dbus: add data argument to BusPropertySetCallback
[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 General Public License as published by
11   the Free Software Foundation; either version 2 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   General Public License for more details.
18
19   You should have received a copy of the GNU 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
30 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
31 #define SCRIPT_PATH "/etc/rc.d/rc.local"
32 #elif defined(TARGET_SUSE)
33 #define SCRIPT_PATH "/etc/init.d/boot.local"
34 #endif
35
36 const char *arg_dest = "/tmp";
37
38 static int add_symlink(const char *service) {
39         char *from = NULL, *to = NULL;
40         int r;
41
42         assert(service);
43
44         asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", service);
45         asprintf(&to, "%s/multi-user.target.wants/%s", arg_dest, service);
46
47         if (!from || !to) {
48                 log_error("Out of memory");
49                 r = -ENOMEM;
50                 goto finish;
51         }
52
53         mkdir_parents(to, 0755);
54
55         r = symlink(from, to);
56         if (r < 0) {
57                 if (errno == EEXIST)
58                         r = 0;
59                 else {
60                         log_error("Failed to create symlink from %s to %s: %m", from, to);
61                         r = -errno;
62                 }
63         }
64
65 finish:
66
67         free(from);
68         free(to);
69
70         return r;
71 }
72
73 static bool file_is_executable(const char *f) {
74         struct stat st;
75
76         if (stat(f, &st) < 0)
77                 return false;
78
79         return S_ISREG(st.st_mode) && (st.st_mode & 0111);
80 }
81
82 int main(int argc, char *argv[]) {
83
84         int r = EXIT_SUCCESS;
85
86         if (argc > 2) {
87                 log_error("This program takes one or no arguments.");
88                 return EXIT_FAILURE;
89         }
90
91         log_set_target(LOG_TARGET_AUTO);
92         log_parse_environment();
93         log_open();
94
95         if (argc > 1)
96                 arg_dest = argv[1];
97
98         if (file_is_executable(SCRIPT_PATH)) {
99                 log_debug("Automatically adding rc-local.service.");
100
101                 if (add_symlink("rc-local.service") < 0)
102                         r = EXIT_FAILURE;
103
104         }
105
106         return r;
107 }