chiark / gitweb /
main: fix reexec
[elogind.git] / src / 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 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"
29
30 const char *arg_dest = "/tmp";
31
32 static int add_symlink(const char *fservice, const char *tservice) {
33         char *from = NULL, *to = NULL;
34         int r;
35
36         asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", fservice);
37         asprintf(&to, "%s/getty.target.wants/%s", arg_dest, tservice);
38
39         if (!from || !to) {
40                 log_error("Out of memory");
41                 r = -ENOMEM;
42                 goto finish;
43         }
44
45         mkdir_parents(to, 0755);
46
47         if ((r = symlink(from, to)) < 0) {
48                 log_error("Failed to create symlink from %s to %s: %m", from, to);
49                 r = -errno;
50         }
51
52 finish:
53
54         free(from);
55         free(to);
56
57         return r;
58 }
59
60 int main(int argc, char *argv[]) {
61         int r = EXIT_SUCCESS;
62         char *active;
63
64         if (argc > 2) {
65                 log_error("This program takes one or no arguments.");
66                 return EXIT_FAILURE;
67         }
68
69         if (argc > 1)
70             arg_dest = argv[1];
71
72         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
73         log_parse_environment();
74         log_open();
75
76         umask(0022);
77
78         if (detect_container(NULL) > 0) {
79                 log_debug("Automatic adding console shell.");
80
81                 if (add_symlink("console-shell.service", "console-shell.service") < 0)
82                         r = EXIT_FAILURE;
83
84                 /* Don't add any further magic if we are in a container */
85                 goto finish;
86         }
87
88         if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
89                 const char *tty;
90
91                 if ((tty = strrchr(active, ' ')))
92                         tty ++;
93                 else
94                         tty = active;
95
96                 /* Automatically add in a serial getty on the kernel
97                  * console */
98                 if (!tty_is_vc(tty)) {
99                         char *n;
100
101                         /* We assume that gettys on virtual terminals are
102                          * started via manual configuration and do this magic
103                          * only for non-VC terminals. */
104
105                         log_debug("Automatically adding serial getty for /dev/%s.", tty);
106
107                         if (!(n = unit_name_replace_instance("serial-getty@.service", tty)) ||
108                             add_symlink("serial-getty@.service", n) < 0)
109                                 r = EXIT_FAILURE;
110
111                         free(n);
112                 }
113
114                 free(active);
115         }
116
117         /* Automatically add in a serial getty on the first
118          * virtualizer console */
119         if (access("/sys/class/tty/hvc0", F_OK) == 0) {
120                 log_debug("Automatically adding serial getty for hvc0.");
121
122                 if (add_symlink("serial-getty@.service", "serial-getty@hvc0.service") < 0)
123                         r = EXIT_FAILURE;
124
125         }
126
127         if (access("/sys/class/tty/xvc0", F_OK) == 0) {
128                 log_debug("Automatically adding serial getty for xvc0.");
129
130                 if (add_symlink("serial-getty@.service", "serial-getty@xvc0.service") < 0)
131                         r = EXIT_FAILURE;
132         }
133
134 finish:
135         return r;
136 }