diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-06-19 16:10:55 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-06-19 16:33:01 +0200 |
commit | 326bb68c40ff2a7119c344b9ab7a7473f9fcdf3c (patch) | |
tree | 3b5e75cb22ad73f045f72d76176566de0c7ee8ed | |
parent | c806ffb9592fa9a2b13a1f9f9be4c77cd5b211aa (diff) |
debug-generator: add new generator
debug-generator can mask specific units if they are specified on the
kernel command line with systemd.mask=.
debug-generator can pull in debug-shell.service is systemd.debug-shell
is passed on the kernel command line.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 11 | ||||
l--------- | src/debug-generator/Makefile | 1 | ||||
-rw-r--r-- | src/debug-generator/debug-generator.c | 137 |
4 files changed, 149 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index 66fa09dc52..fb4c209ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -60,6 +60,7 @@ /systemd-cryptsetup /systemd-cryptsetup-generator /systemd-dbus1-generator +/systemd-debug-generator /systemd-delta /systemd-detect-virt /systemd-efi-boot-generator diff --git a/Makefile.am b/Makefile.am index bafb80e4db..06105bde36 100644 --- a/Makefile.am +++ b/Makefile.am @@ -371,7 +371,8 @@ rootlibexec_PROGRAMS = \ systemgenerator_PROGRAMS = \ systemd-getty-generator \ systemd-fstab-generator \ - systemd-system-update-generator + systemd-system-update-generator \ + systemd-debug-generator dist_bashcompletion_DATA = \ shell-completion/bash/busctl \ @@ -1892,6 +1893,14 @@ systemd_getty_generator_LDADD = \ libsystemd-shared.la # ------------------------------------------------------------------------------ +systemd_debug_generator_SOURCES = \ + src/debug-generator/debug-generator.c + +systemd_debug_generator_LDADD = \ + libsystemd-label.la \ + libsystemd-shared.la + +# ------------------------------------------------------------------------------ systemd_fstab_generator_SOURCES = \ src/fstab-generator/fstab-generator.c \ src/core/mount-setup.c diff --git a/src/debug-generator/Makefile b/src/debug-generator/Makefile new file mode 120000 index 0000000000..d0b0e8e008 --- /dev/null +++ b/src/debug-generator/Makefile @@ -0,0 +1 @@ +../Makefile
\ No newline at end of file diff --git a/src/debug-generator/debug-generator.c b/src/debug-generator/debug-generator.c new file mode 100644 index 0000000000..cefac89d11 --- /dev/null +++ b/src/debug-generator/debug-generator.c @@ -0,0 +1,137 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include "util.h" +#include "strv.h" +#include "unit-name.h" +#include "mkdir.h" + +static const char *arg_dest = "/tmp"; +static char **arg_mask = NULL; +static bool arg_debug_shell = false; + +static int parse_proc_cmdline_item(const char *key, const char *value) { + int r; + + if (streq(key, "systemd.mask")) { + + if (!value) + log_error("Missing argument for systemd.mask= kernel command line parameter."); + else { + char *n; + + n = strdup(value); + if (!n) + return log_oom(); + + r = strv_consume(&arg_mask, n); + if (r < 0) + return log_oom(); + } + } else if (streq(key, "systemd.debug-shell")) { + + if (value) { + r = parse_boolean(value); + if (r < 0) + log_error("Failed to parse systemd.debug-shell= argument '%s', ignoring.", value); + else + arg_debug_shell = r; + } else + arg_debug_shell = true; + } + + return 0; +} + +static int generate_mask_symlinks(void) { + char **u; + int r = 0; + + if (strv_isempty(arg_mask)) + return 0; + + STRV_FOREACH(u, arg_mask) { + _cleanup_free_ char *m = NULL, *p = NULL; + + m = unit_name_mangle(*u, MANGLE_NOGLOB); + if (!m) + return log_oom(); + + p = strjoin(arg_dest, "/", m, NULL); + if (!p) + return log_oom(); + + if (symlink("/dev/null", p) < 0) { + log_error("Failed to create mask symlink %s: %m", p); + r = -errno; + } + } + + return r; +} + +static int generate_debug_shell_symlink(void) { + const char *p; + + if (!arg_debug_shell) + return 0; + + p = strappenda(arg_dest, "/default.target.wants/debug-shell.service"); + + mkdir_parents_label(p, 0755); + + if (symlink(SYSTEM_DATA_UNIT_PATH "/debug-shell.service", p) < 0) { + log_error("Failed to create %s symlink: %m", p); + return -errno; + } + + return 0; +} + +int main(int argc, char *argv[]) { + int r, q; + + if (argc > 1 && argc != 4) { + log_error("This program takes three or no arguments."); + return EXIT_FAILURE; + } + + if (argc > 1) + arg_dest = argv[2]; + + log_set_target(LOG_TARGET_SAFE); + log_parse_environment(); + log_open(); + + umask(0022); + + if (parse_proc_cmdline(parse_proc_cmdline_item) < 0) + return EXIT_FAILURE; + + r = generate_mask_symlinks(); + + q = generate_debug_shell_symlink(); + if (q < 0) + r = q; + + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; + +} |