1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include "unit-name.h"
33 static const char *arg_dest = "/tmp";
35 static int add_symlink(const char *fservice, const char *tservice) {
36 char *from = NULL, *to = NULL;
42 from = strappend(SYSTEM_DATA_UNIT_PATH "/", fservice);
43 to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL);
50 mkdir_parents_label(to, 0755);
52 r = symlink(from, to);
55 /* In case console=hvc0 is passed this will very likely result in EEXIST */
58 log_error("Failed to create symlink %s: %m", to);
71 static int add_serial_getty(const char *tty) {
77 log_debug("Automatically adding serial getty for /dev/%s.", tty);
79 n = unit_name_replace_instance("serial-getty@.service", tty);
83 r = add_symlink("serial-getty@.service", n);
89 int main(int argc, char *argv[]) {
91 static const char virtualization_consoles[] =
100 if (argc > 1 && argc != 4) {
101 log_error("This program takes three or no arguments.");
108 log_set_target(LOG_TARGET_SAFE);
109 log_parse_environment();
114 if (detect_container(NULL) > 0) {
115 log_debug("Automatically adding console shell.");
117 if (add_symlink("console-getty.service", "console-getty.service") < 0)
120 /* Don't add any further magic if we are in a container */
124 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
127 tty = strrchr(active, ' ');
133 /* Automatically add in a serial getty on the kernel
135 if (isempty(tty) || tty_is_vc(tty))
140 /* We assume that gettys on virtual terminals are
141 * started via manual configuration and do this magic
142 * only for non-VC terminals. */
144 k = add_serial_getty(tty);
154 /* Automatically add in a serial getty on the first
155 * virtualizer console */
156 NULSTR_FOREACH(j, virtualization_consoles) {
160 if (asprintf(&p, "/sys/class/tty/%s", j) < 0) {
172 k = add_serial_getty(j);