]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/rc-local-generator/rc-local-generator.c
util: split out escaping code into escape.[ch]
[thirdparty/systemd.git] / src / rc-local-generator / rc-local-generator.c
CommitLineData
15673083
MS
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
5430f7f2
LP
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
15673083
MS
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
5430f7f2 17 Lesser General Public License for more details.
15673083 18
5430f7f2 19 You should have received a copy of the GNU Lesser General Public License
15673083
MS
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"
49e942b2 29#include "mkdir.h"
15673083 30
b1c4ca25
LP
31#ifndef RC_LOCAL_SCRIPT_PATH_START
32#define RC_LOCAL_SCRIPT_PATH_START "/etc/rc.d/rc.local"
15673083
MS
33#endif
34
b1c4ca25
LP
35#ifndef RC_LOCAL_SCRIPT_PATH_STOP
36#define RC_LOCAL_SCRIPT_PATH_STOP "/sbin/halt.local"
37#endif
04b6f7c1 38
15673083
MS
39const char *arg_dest = "/tmp";
40
04b6f7c1 41static int add_symlink(const char *service, const char *where) {
7b1132f6 42 _cleanup_free_ char *from = NULL, *to = NULL;
15673083
MS
43 int r;
44
45 assert(service);
7b1132f6 46 assert(where);
15673083 47
7b1132f6
LP
48 from = strjoin(SYSTEM_DATA_UNIT_PATH, "/", service, NULL);
49 if (!from)
50 return log_oom();
15673083 51
7b1132f6
LP
52 to = strjoin(arg_dest, "/", where, ".wants/", service, NULL);
53 if (!to)
54 return log_oom();
15673083 55
d2e54fae 56 mkdir_parents_label(to, 0755);
15673083
MS
57
58 r = symlink(from, to);
59 if (r < 0) {
60 if (errno == EEXIST)
7b1132f6 61 return 0;
15673083 62
56f64d95 63 log_error_errno(errno, "Failed to create symlink %s: %m", to);
7b1132f6
LP
64 return -errno;
65 }
15673083 66
7b1132f6 67 return 1;
15673083
MS
68}
69
70int main(int argc, char *argv[]) {
15673083
MS
71 int r = EXIT_SUCCESS;
72
07719a21
LP
73 if (argc > 1 && argc != 4) {
74 log_error("This program takes three or no arguments.");
15673083
MS
75 return EXIT_FAILURE;
76 }
77
07719a21
LP
78 if (argc > 1)
79 arg_dest = argv[1];
80
a6903061 81 log_set_target(LOG_TARGET_SAFE);
15673083
MS
82 log_parse_environment();
83 log_open();
84
07719a21 85 umask(0022);
15673083 86
7b1132f6 87 if (access(RC_LOCAL_SCRIPT_PATH_START, X_OK) >= 0) {
15673083
MS
88 log_debug("Automatically adding rc-local.service.");
89
04b6f7c1 90 if (add_symlink("rc-local.service", "multi-user.target") < 0)
15673083 91 r = EXIT_FAILURE;
04b6f7c1 92 }
15673083 93
7b1132f6 94 if (access(RC_LOCAL_SCRIPT_PATH_STOP, X_OK) >= 0) {
04b6f7c1
LP
95 log_debug("Automatically adding halt-local.service.");
96
97 if (add_symlink("halt-local.service", "final.target") < 0)
98 r = EXIT_FAILURE;
15673083
MS
99 }
100
101 return r;
102}