]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/getty-generator.c
relicense to LGPLv2.1 (with exceptions)
[thirdparty/systemd.git] / src / getty-generator.c
CommitLineData
2a796654
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
2a796654
LP
11 (at your option) any later version.
12
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
5430f7f2 16 Lesser General Public License for more details.
2a796654 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
2a796654
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <string.h>
23#include <errno.h>
24#include <unistd.h>
25
26#include "log.h"
27#include "util.h"
49e942b2 28#include "mkdir.h"
2a796654 29#include "unit-name.h"
5dc4c17f 30#include "virt.h"
2a796654 31
6a39419f 32static const char *arg_dest = "/tmp";
2a796654
LP
33
34static int add_symlink(const char *fservice, const char *tservice) {
35 char *from = NULL, *to = NULL;
36 int r;
37
4dc380d1
LP
38 assert(fservice);
39 assert(tservice);
40
2a796654
LP
41 asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", fservice);
42 asprintf(&to, "%s/getty.target.wants/%s", arg_dest, tservice);
43
44 if (!from || !to) {
45 log_error("Out of memory");
46 r = -ENOMEM;
47 goto finish;
48 }
49
50 mkdir_parents(to, 0755);
51
a17b785b
LP
52 r = symlink(from, to);
53 if (r < 0) {
54 if (errno == EEXIST)
3c20189a 55 /* In case console=hvc0 is passed this will very likely result in EEXIST */
a17b785b
LP
56 r = 0;
57 else {
58 log_error("Failed to create symlink from %s to %s: %m", from, to);
59 r = -errno;
60 }
2a796654
LP
61 }
62
63finish:
64
65 free(from);
66 free(to);
67
68 return r;
69}
70
4dc380d1
LP
71static int add_serial_getty(const char *tty) {
72 char *n;
73 int r;
74
75 assert(tty);
76
77 log_debug("Automatically adding serial getty for /dev/%s.", tty);
78
79 n = unit_name_replace_instance("serial-getty@.service", tty);
80 if (!n) {
81 log_error("Out of memory");
82 return -ENOMEM;
83 }
84
85 r = add_symlink("serial-getty@.service", n);
86 free(n);
87
88 return r;
89}
90
2a796654 91int main(int argc, char *argv[]) {
3c20189a
LP
92
93 static const char virtualization_consoles[] =
94 "hvc0\0"
95 "xvc0\0"
96 "hvsi0\0";
97
2a796654
LP
98 int r = EXIT_SUCCESS;
99 char *active;
3c20189a 100 const char *j;
2a796654
LP
101
102 if (argc > 2) {
103 log_error("This program takes one or no arguments.");
104 return EXIT_FAILURE;
105 }
106
4cfa2c99 107 log_set_target(LOG_TARGET_AUTO);
2a796654
LP
108 log_parse_environment();
109 log_open();
110
4c12626c
LP
111 umask(0022);
112
3c20189a
LP
113 if (argc > 1)
114 arg_dest = argv[1];
115
2a796654 116 if (detect_container(NULL) > 0) {
6705c2df 117 log_debug("Automatically adding console shell.");
2a796654
LP
118
119 if (add_symlink("console-shell.service", "console-shell.service") < 0)
120 r = EXIT_FAILURE;
121
122 /* Don't add any further magic if we are in a container */
123 goto finish;
124 }
125
126 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
127 const char *tty;
128
a17b785b
LP
129 tty = strrchr(active, ' ');
130 if (tty)
2a796654
LP
131 tty ++;
132 else
133 tty = active;
134
135 /* Automatically add in a serial getty on the kernel
136 * console */
3c20189a
LP
137 if (tty_is_vc(tty))
138 free(active);
139 else {
3c20189a 140 int k;
2a796654
LP
141
142 /* We assume that gettys on virtual terminals are
143 * started via manual configuration and do this magic
144 * only for non-VC terminals. */
145
4dc380d1 146 k = add_serial_getty(tty);
3c20189a
LP
147 free(active);
148
3c20189a
LP
149 if (k < 0) {
150 r = EXIT_FAILURE;
151 goto finish;
152 }
153 }
2a796654
LP
154 }
155
156 /* Automatically add in a serial getty on the first
157 * virtualizer console */
3c20189a 158 NULSTR_FOREACH(j, virtualization_consoles) {
4dc380d1 159 char *p;
3c20189a 160 int k;
2a796654 161
3c20189a
LP
162 if (asprintf(&p, "/sys/class/tty/%s", j) < 0) {
163 log_error("Out of memory");
2a796654 164 r = EXIT_FAILURE;
3c20189a
LP
165 goto finish;
166 }
980fc73d 167
3c20189a
LP
168 k = access(p, F_OK);
169 free(p);
170
171 if (k < 0)
172 continue;
980fc73d 173
4dc380d1 174 k = add_serial_getty(j);
3c20189a
LP
175 if (k < 0) {
176 r = EXIT_FAILURE;
177 goto finish;
178 }
2a796654
LP
179 }
180
181finish:
182 return r;
183}