]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/debug-generator/debug-generator.c
debug-generator: add new generator
[thirdparty/systemd.git] / src / debug-generator / debug-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 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;
29 static bool arg_debug_shell = false;
30
31 static int parse_proc_cmdline_item(const char *key, const char *value) {
32 int r;
33
34 if (streq(key, "systemd.mask")) {
35
36 if (!value)
37 log_error("Missing argument for systemd.mask= kernel command line parameter.");
38 else {
39 char *n;
40
41 n = strdup(value);
42 if (!n)
43 return log_oom();
44
45 r = strv_consume(&arg_mask, n);
46 if (r < 0)
47 return log_oom();
48 }
49 } else if (streq(key, "systemd.debug-shell")) {
50
51 if (value) {
52 r = parse_boolean(value);
53 if (r < 0)
54 log_error("Failed to parse systemd.debug-shell= argument '%s', ignoring.", value);
55 else
56 arg_debug_shell = r;
57 } else
58 arg_debug_shell = true;
59 }
60
61 return 0;
62 }
63
64 static int generate_mask_symlinks(void) {
65 char **u;
66 int r = 0;
67
68 if (strv_isempty(arg_mask))
69 return 0;
70
71 STRV_FOREACH(u, arg_mask) {
72 _cleanup_free_ char *m = NULL, *p = NULL;
73
74 m = unit_name_mangle(*u, MANGLE_NOGLOB);
75 if (!m)
76 return log_oom();
77
78 p = strjoin(arg_dest, "/", m, NULL);
79 if (!p)
80 return log_oom();
81
82 if (symlink("/dev/null", p) < 0) {
83 log_error("Failed to create mask symlink %s: %m", p);
84 r = -errno;
85 }
86 }
87
88 return r;
89 }
90
91 static int generate_debug_shell_symlink(void) {
92 const char *p;
93
94 if (!arg_debug_shell)
95 return 0;
96
97 p = strappenda(arg_dest, "/default.target.wants/debug-shell.service");
98
99 mkdir_parents_label(p, 0755);
100
101 if (symlink(SYSTEM_DATA_UNIT_PATH "/debug-shell.service", p) < 0) {
102 log_error("Failed to create %s symlink: %m", p);
103 return -errno;
104 }
105
106 return 0;
107 }
108
109 int main(int argc, char *argv[]) {
110 int r, q;
111
112 if (argc > 1 && argc != 4) {
113 log_error("This program takes three or no arguments.");
114 return EXIT_FAILURE;
115 }
116
117 if (argc > 1)
118 arg_dest = argv[2];
119
120 log_set_target(LOG_TARGET_SAFE);
121 log_parse_environment();
122 log_open();
123
124 umask(0022);
125
126 if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
127 return EXIT_FAILURE;
128
129 r = generate_mask_symlinks();
130
131 q = generate_debug_shell_symlink();
132 if (q < 0)
133 r = q;
134
135 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
136
137 }