]>
Commit | Line | Data |
---|---|---|
326bb68c LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include "util.h" | |
23 | #include "strv.h" | |
24 | #include "unit-name.h" | |
25 | #include "mkdir.h" | |
26 | ||
27 | static const char *arg_dest = "/tmp"; | |
28 | static char **arg_mask = NULL; | |
3c5a87a8 | 29 | static char **arg_wants = NULL; |
326bb68c LP |
30 | static bool arg_debug_shell = false; |
31 | ||
32 | static int parse_proc_cmdline_item(const char *key, const char *value) { | |
33 | int r; | |
34 | ||
3c5a87a8 LP |
35 | assert(key); |
36 | ||
326bb68c LP |
37 | if (streq(key, "systemd.mask")) { |
38 | ||
39 | if (!value) | |
40 | log_error("Missing argument for systemd.mask= kernel command line parameter."); | |
41 | else { | |
42 | char *n; | |
43 | ||
7410616c LP |
44 | r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n); |
45 | if (r < 0) | |
46 | return log_error_errno(r, "Failed to glob unit name: %m"); | |
326bb68c LP |
47 | |
48 | r = strv_consume(&arg_mask, n); | |
49 | if (r < 0) | |
50 | return log_oom(); | |
51 | } | |
3c5a87a8 LP |
52 | |
53 | } else if (streq(key, "systemd.wants")) { | |
54 | ||
55 | if (!value) | |
56 | log_error("Missing argument for systemd.want= kernel command line parameter."); | |
57 | else { | |
58 | char *n; | |
59 | ||
7410616c LP |
60 | r = unit_name_mangle(value, UNIT_NAME_NOGLOB, &n); |
61 | if (r < 0) | |
62 | return log_error_errno(r, "Failed to glob unit name: %m"); | |
3c5a87a8 LP |
63 | |
64 | r = strv_consume(&arg_wants, n); | |
65 | if (r < 0) | |
66 | return log_oom(); | |
67 | } | |
68 | ||
326bb68c LP |
69 | } else if (streq(key, "systemd.debug-shell")) { |
70 | ||
71 | if (value) { | |
72 | r = parse_boolean(value); | |
73 | if (r < 0) | |
74 | log_error("Failed to parse systemd.debug-shell= argument '%s', ignoring.", value); | |
75 | else | |
76 | arg_debug_shell = r; | |
77 | } else | |
78 | arg_debug_shell = true; | |
79 | } | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | static int generate_mask_symlinks(void) { | |
85 | char **u; | |
86 | int r = 0; | |
87 | ||
88 | if (strv_isempty(arg_mask)) | |
89 | return 0; | |
90 | ||
91 | STRV_FOREACH(u, arg_mask) { | |
3c5a87a8 | 92 | _cleanup_free_ char *p = NULL; |
326bb68c | 93 | |
3c5a87a8 | 94 | p = strjoin(arg_dest, "/", *u, NULL); |
326bb68c LP |
95 | if (!p) |
96 | return log_oom(); | |
97 | ||
1f6b4113 | 98 | if (symlink("/dev/null", p) < 0) |
94c156cd LP |
99 | r = log_error_errno(errno, |
100 | "Failed to create mask symlink %s: %m", | |
101 | p); | |
326bb68c LP |
102 | } |
103 | ||
104 | return r; | |
105 | } | |
106 | ||
3c5a87a8 LP |
107 | static int generate_wants_symlinks(void) { |
108 | char **u; | |
109 | int r = 0; | |
326bb68c | 110 | |
3c5a87a8 | 111 | if (strv_isempty(arg_wants)) |
326bb68c LP |
112 | return 0; |
113 | ||
3c5a87a8 LP |
114 | STRV_FOREACH(u, arg_wants) { |
115 | _cleanup_free_ char *p = NULL, *f = NULL; | |
116 | ||
117 | p = strjoin(arg_dest, "/default.target.wants/", *u, NULL); | |
118 | if (!p) | |
119 | return log_oom(); | |
120 | ||
121 | f = strappend(SYSTEM_DATA_UNIT_PATH "/", *u); | |
122 | if (!f) | |
123 | return log_oom(); | |
326bb68c | 124 | |
3c5a87a8 | 125 | mkdir_parents_label(p, 0755); |
326bb68c | 126 | |
1f6b4113 | 127 | if (symlink(f, p) < 0) |
94c156cd LP |
128 | r = log_error_errno(errno, |
129 | "Failed to create wants symlink %s: %m", | |
130 | p); | |
326bb68c LP |
131 | } |
132 | ||
3c5a87a8 | 133 | return r; |
326bb68c LP |
134 | } |
135 | ||
136 | int main(int argc, char *argv[]) { | |
137 | int r, q; | |
138 | ||
139 | if (argc > 1 && argc != 4) { | |
140 | log_error("This program takes three or no arguments."); | |
141 | return EXIT_FAILURE; | |
142 | } | |
143 | ||
144 | if (argc > 1) | |
145 | arg_dest = argv[2]; | |
146 | ||
147 | log_set_target(LOG_TARGET_SAFE); | |
148 | log_parse_environment(); | |
149 | log_open(); | |
150 | ||
151 | umask(0022); | |
152 | ||
b5884878 LP |
153 | r = parse_proc_cmdline(parse_proc_cmdline_item); |
154 | if (r < 0) | |
da927ba9 | 155 | log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
326bb68c | 156 | |
3c5a87a8 LP |
157 | if (arg_debug_shell) { |
158 | r = strv_extend(&arg_wants, "debug-shell.service"); | |
159 | if (r < 0) { | |
160 | r = log_oom(); | |
161 | goto finish; | |
162 | } | |
163 | } | |
164 | ||
326bb68c LP |
165 | r = generate_mask_symlinks(); |
166 | ||
3c5a87a8 | 167 | q = generate_wants_symlinks(); |
326bb68c LP |
168 | if (q < 0) |
169 | r = q; | |
170 | ||
3c5a87a8 | 171 | finish: |
326bb68c LP |
172 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
173 | ||
174 | } |