chiark / gitweb /
util: fix misuse of memcmp
[elogind.git] / src / getty-generator / getty-generator.c
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 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.
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   Lesser General Public License for more details.
17
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/>.
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 "mkdir.h"
29 #include "unit-name.h"
30 #include "virt.h"
31 #include "fileio.h"
32
33 static const char *arg_dest = "/tmp";
34
35 static int add_symlink(const char *fservice, const char *tservice) {
36         _cleanup_free_ char *from = NULL, *to = NULL;
37         int r;
38
39         assert(fservice);
40         assert(tservice);
41
42         from = strappend(SYSTEM_DATA_UNIT_PATH "/", fservice);
43         if (!from)
44                 return log_oom();
45
46         to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL);
47         if (!to)
48                 return log_oom();
49
50         mkdir_parents_label(to, 0755);
51
52         r = symlink(from, to);
53         if (r < 0) {
54                 if (errno == EEXIST)
55                         /* In case console=hvc0 is passed this will very likely result in EEXIST */
56                         return 0;
57                 else {
58                         log_error("Failed to create symlink %s: %m", to);
59                         return -errno;
60                 }
61         }
62
63         return 0;
64 }
65
66 static int add_serial_getty(const char *tty) {
67         _cleanup_free_ char *n = NULL;
68
69         assert(tty);
70
71         log_debug("Automatically adding serial getty for /dev/%s.", tty);
72
73         n = unit_name_replace_instance("serial-getty@.service", tty);
74         if (!n)
75                 return log_oom();
76
77         return add_symlink("serial-getty@.service", n);
78 }
79
80 int main(int argc, char *argv[]) {
81
82         static const char virtualization_consoles[] =
83                 "hvc0\0"
84                 "xvc0\0"
85                 "hvsi0\0";
86
87         _cleanup_free_ char *active = NULL;
88         const char *j;
89
90         if (argc > 1 && argc != 4) {
91                 log_error("This program takes three or no arguments.");
92                 return EXIT_FAILURE;
93         }
94
95         if (argc > 1)
96                 arg_dest = argv[1];
97
98         log_set_target(LOG_TARGET_SAFE);
99         log_parse_environment();
100         log_open();
101
102         umask(0022);
103
104         if (detect_container(NULL) > 0) {
105                 log_debug("Automatically adding console shell.");
106
107                 if (add_symlink("console-getty.service", "console-getty.service") < 0)
108                         return EXIT_FAILURE;
109
110                 /* Don't add any further magic if we are in a container */
111                 return EXIT_SUCCESS;
112         }
113
114         if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
115                 char *w, *state;
116                 size_t l;
117
118                 /* Automatically add in a serial getty on all active
119                  * kernel consoles */
120                 FOREACH_WORD(w, l, active, state) {
121                         _cleanup_free_ char *tty = NULL;
122
123                         tty = strndup(w, l);
124                         if (!tty) {
125                                 log_oom();
126                                 return EXIT_FAILURE;
127                         }
128
129                         if (isempty(tty) || tty_is_vc(tty))
130                                 continue;
131
132                         /* We assume that gettys on virtual terminals are
133                          * started via manual configuration and do this magic
134                          * only for non-VC terminals. */
135
136                         if (add_serial_getty(tty) < 0)
137                                 return EXIT_FAILURE;
138                 }
139         }
140
141         /* Automatically add in a serial getty on the first
142          * virtualizer console */
143         NULSTR_FOREACH(j, virtualization_consoles) {
144                 _cleanup_free_ char *p = NULL;
145
146                 p = strappend("/sys/class/tty/", j);
147                 if (!p) {
148                         log_oom();
149                         return EXIT_FAILURE;
150                 }
151
152                 if (access(p, F_OK) < 0)
153                         continue;
154
155                 if (add_serial_getty(j) < 0)
156                         return EXIT_FAILURE;
157         }
158
159         return EXIT_SUCCESS;
160 }