]>
git.proxmox.com Git - systemd.git/blob - src/getty-generator/getty-generator.c
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/>.
30 #include "unit-name.h"
33 #include "path-util.h"
35 static const char *arg_dest
= "/tmp";
37 static int add_symlink(const char *fservice
, const char *tservice
) {
44 from
= strappenda(SYSTEM_DATA_UNIT_PATH
"/", fservice
);
45 to
= strappenda(arg_dest
, "/getty.target.wants/", tservice
);
47 mkdir_parents_label(to
, 0755);
49 r
= symlink(from
, to
);
52 /* In case console=hvc0 is passed this will very likely result in EEXIST */
55 log_error_errno(errno
, "Failed to create symlink %s: %m", to
);
63 static int add_serial_getty(const char *tty
) {
64 _cleanup_free_
char *n
= NULL
;
68 log_debug("Automatically adding serial getty for /dev/%s.", tty
);
70 n
= unit_name_from_path_instance("serial-getty", tty
, ".service");
74 return add_symlink("serial-getty@.service", n
);
77 static int add_container_getty(const char *tty
) {
78 _cleanup_free_
char *n
= NULL
;
82 log_debug("Automatically adding container getty for /dev/pts/%s.", tty
);
84 n
= unit_name_from_path_instance("container-getty", tty
, ".service");
88 return add_symlink("container-getty@.service", n
);
91 static int verify_tty(const char *name
) {
92 _cleanup_close_
int fd
= -1;
95 /* Some TTYs are weird and have been enumerated but don't work
96 * when you try to use them, such as classic ttyS0 and
97 * friends. Let's check that and open the device and run
100 p
= strappenda("/dev/", name
);
102 /* O_NONBLOCK is essential here, to make sure we don't wait
104 fd
= open(p
, O_RDWR
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
|O_NOFOLLOW
);
110 return errno
? -errno
: -EIO
;
115 int main(int argc
, char *argv
[]) {
117 static const char virtualization_consoles
[] =
125 _cleanup_free_
char *active
= NULL
;
129 if (argc
> 1 && argc
!= 4) {
130 log_error("This program takes three or no arguments.");
137 log_set_target(LOG_TARGET_SAFE
);
138 log_parse_environment();
143 if (detect_container(NULL
) > 0) {
144 _cleanup_free_
char *container_ttys
= NULL
;
146 log_debug("Automatically adding console shell.");
148 if (add_symlink("console-getty.service", "console-getty.service") < 0)
151 /* When $container_ttys is set for PID 1, spawn
152 * gettys on all ptys named therein. Note that despite
153 * the variable name we only support ptys here. */
155 r
= getenv_for_pid(1, "container_ttys", &container_ttys
);
157 const char *word
, *state
;
160 FOREACH_WORD(word
, l
, container_ttys
, state
) {
164 memcpy(tty
, word
, l
);
167 /* First strip off /dev/ if it is specified */
168 t
= path_startswith(tty
, "/dev/");
172 /* Then, make sure it's actually a pty */
173 t
= path_startswith(t
, "pts/");
177 if (add_container_getty(t
) < 0)
182 /* Don't add any further magic if we are in a container */
186 if (read_one_line_file("/sys/class/tty/console/active", &active
) >= 0) {
187 const char *word
, *state
;
190 /* Automatically add in a serial getty on all active
192 FOREACH_WORD(word
, l
, active
, state
) {
193 _cleanup_free_
char *tty
= NULL
;
195 tty
= strndup(word
, l
);
201 if (isempty(tty
) || tty_is_vc(tty
))
204 if (verify_tty(tty
) < 0)
207 /* We assume that gettys on virtual terminals are
208 * started via manual configuration and do this magic
209 * only for non-VC terminals. */
211 if (add_serial_getty(tty
) < 0)
216 /* Automatically add in a serial getty on the first
217 * virtualizer console */
218 NULSTR_FOREACH(j
, virtualization_consoles
) {
221 p
= strappenda("/sys/class/tty/", j
);
222 if (access(p
, F_OK
) < 0)
225 if (add_serial_getty(j
) < 0)