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"
32 #include "path-util.h"
34 static const char *arg_dest = "/tmp";
36 static int add_symlink(const char *fservice, const char *tservice) {
37 _cleanup_free_ char *from = NULL, *to = NULL;
43 from = strappend(SYSTEM_DATA_UNIT_PATH "/", fservice);
47 to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL);
51 mkdir_parents_label(to, 0755);
53 r = symlink(from, to);
56 /* In case console=hvc0 is passed this will very likely result in EEXIST */
59 log_error("Failed to create symlink %s: %m", to);
67 static int add_serial_getty(const char *tty) {
68 _cleanup_free_ char *n = NULL;
72 log_debug("Automatically adding serial getty for /dev/%s.", tty);
74 n = unit_name_replace_instance("serial-getty@.service", tty);
78 return add_symlink("serial-getty@.service", n);
81 static int add_container_getty(const char *tty) {
82 _cleanup_free_ char *n = NULL;
86 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
88 n = unit_name_replace_instance("container-getty@.service", tty);
92 return add_symlink("container-getty@.service", n);
95 int main(int argc, char *argv[]) {
97 static const char virtualization_consoles[] =
102 _cleanup_free_ char *active = NULL;
106 if (argc > 1 && argc != 4) {
107 log_error("This program takes three or no arguments.");
114 log_set_target(LOG_TARGET_SAFE);
115 log_parse_environment();
120 if (detect_container(NULL) > 0) {
121 _cleanup_free_ char *container_ttys = NULL;
123 log_debug("Automatically adding console shell.");
125 if (add_symlink("console-getty.service", "console-getty.service") < 0)
128 /* When $container_ttys is set for PID 1, spawn
129 * gettys on all ptys named therein. Note that despite
130 * the variable name we only support ptys here. */
132 r = getenv_for_pid(1, "container_ttys", &container_ttys);
137 FOREACH_WORD(w, l, container_ttys, state) {
144 /* First strip off /dev/ if it is specified */
145 t = path_startswith(tty, "/dev/");
149 /* Then, make sure it's actually a pty */
150 t = path_startswith(t, "pts/");
154 if (add_container_getty(t) < 0)
159 /* Don't add any further magic if we are in a container */
163 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
167 /* Automatically add in a serial getty on all active
169 FOREACH_WORD(w, l, active, state) {
170 _cleanup_free_ char *tty = NULL;
178 if (isempty(tty) || tty_is_vc(tty))
181 /* We assume that gettys on virtual terminals are
182 * started via manual configuration and do this magic
183 * only for non-VC terminals. */
185 if (add_serial_getty(tty) < 0)
190 /* Automatically add in a serial getty on the first
191 * virtualizer console */
192 NULSTR_FOREACH(j, virtualization_consoles) {
193 _cleanup_free_ char *p = NULL;
195 p = strappend("/sys/class/tty/", j);
201 if (access(p, F_OK) < 0)
204 if (add_serial_getty(j) < 0)