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