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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "unit-name.h"
30 const char *arg_dest = "/tmp";
32 static int add_symlink(const char *fservice, const char *tservice) {
33 char *from = NULL, *to = NULL;
36 asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", fservice);
37 asprintf(&to, "%s/getty.target.wants/%s", arg_dest, tservice);
40 log_error("Out of memory");
45 mkdir_parents(to, 0755);
47 r = symlink(from, to);
50 /* In case console=hvc is passed this will very likely result in EEXIST */
53 log_error("Failed to create symlink from %s to %s: %m", from, to);
66 int main(int argc, char *argv[]) {
71 log_error("This program takes one or no arguments.");
78 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
79 log_parse_environment();
84 if (detect_container(NULL) > 0) {
85 log_debug("Automatically adding console shell.");
87 if (add_symlink("console-shell.service", "console-shell.service") < 0)
90 /* Don't add any further magic if we are in a container */
94 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
97 tty = strrchr(active, ' ');
103 /* Automatically add in a serial getty on the kernel
105 if (!tty_is_vc(tty)) {
108 /* We assume that gettys on virtual terminals are
109 * started via manual configuration and do this magic
110 * only for non-VC terminals. */
112 log_debug("Automatically adding serial getty for /dev/%s.", tty);
114 n = unit_name_replace_instance("serial-getty@.service", tty);
115 if (!n || add_symlink("serial-getty@.service", n) < 0)
124 /* Automatically add in a serial getty on the first
125 * virtualizer console */
126 if (access("/sys/class/tty/hvc0", F_OK) == 0) {
127 log_debug("Automatically adding serial getty for hvc0.");
129 if (add_symlink("serial-getty@.service", "serial-getty@hvc0.service") < 0)
134 if (access("/sys/class/tty/xvc0", F_OK) == 0) {
135 log_debug("Automatically adding serial getty for xvc0.");
137 if (add_symlink("serial-getty@.service", "serial-getty@xvc0.service") < 0)