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