]>
Commit | Line | Data |
---|---|---|
b75c8134 | 1 | /* |
53d55042 SK |
2 | * Alternate Getty (agetty) 'agetty' is a versatile, portable, easy to use |
3 | * replacement for getty on SunOS 4.1.x or the SAC ttymon/ttyadm/sacadm/pmadm | |
4 | * suite on Solaris and other SVR4 systems. 'agetty' was written by Wietse | |
5 | * Venema, enhanced by John DiMarco, and further enhanced by Dennis Cronin. | |
b75c8134 KZ |
6 | * |
7 | * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk> | |
1683f6bf DWF |
8 | * Adopt the mingetty features for a better support |
9 | * of virtual consoles by Werner Fink <werner@suse.de> | |
b75c8134 | 10 | * |
53d55042 | 11 | * This program is freely distributable. |
b75c8134 | 12 | */ |
c32270d4 | 13 | |
6dbe3af9 KZ |
14 | #include <stdio.h> |
15 | #include <unistd.h> | |
16 | #include <stdlib.h> | |
2b6fc908 | 17 | #include <string.h> |
1961482a | 18 | #include <termios.h> |
6dbe3af9 KZ |
19 | #include <signal.h> |
20 | #include <errno.h> | |
3aa6b68f | 21 | #include <sys/ioctl.h> |
6dbe3af9 KZ |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
bb280f79 | 24 | #include <sys/wait.h> |
6dbe3af9 | 25 | #include <fcntl.h> |
22853e4a | 26 | #include <stdarg.h> |
6dbe3af9 KZ |
27 | #include <ctype.h> |
28 | #include <utmp.h> | |
29 | #include <getopt.h> | |
22853e4a | 30 | #include <time.h> |
6dbe3af9 | 31 | #include <sys/file.h> |
e61e66bd | 32 | #include <sys/socket.h> |
729def03 | 33 | #include <langinfo.h> |
3aa6b68f | 34 | #include <grp.h> |
2b945eda KZ |
35 | #include <arpa/inet.h> |
36 | #include <netdb.h> | |
37 | #include <ifaddrs.h> | |
0f283438 | 38 | #include <net/if.h> |
df73ad46 | 39 | |
8abcf290 | 40 | #include "strutils.h" |
e12c9866 | 41 | #include "all-io.h" |
7eda085c | 42 | #include "nls.h" |
e6590f06 | 43 | #include "pathnames.h" |
e5b17b31 | 44 | #include "c.h" |
1683f6bf | 45 | #include "widechar.h" |
879a7ab4 | 46 | #include "ttyutils.h" |
6dbe3af9 | 47 | |
4ebac79f SK |
48 | #ifdef HAVE_SYS_PARAM_H |
49 | # include <sys/param.h> | |
50 | #endif | |
51 | ||
c32270d4 CE |
52 | #if defined(__FreeBSD_kernel__) |
53 | #include <pty.h> | |
c32270d4 CE |
54 | #endif |
55 | ||
fd6b7a7f | 56 | #ifdef __linux__ |
0da025da | 57 | # include <sys/kd.h> |
53d55042 | 58 | # define USE_SYSLOG |
729def03 WF |
59 | # ifndef DEFAULT_VCTERM |
60 | # define DEFAULT_VCTERM "linux" | |
61 | # endif | |
fba8a535 | 62 | # if defined (__s390__) || defined (__s390x__) |
f2bcda51 WF |
63 | # define DEFAULT_TTYS0 "dumb" |
64 | # define DEFAULT_TTY32 "ibm327x" | |
fba8a535 WF |
65 | # define DEFAULT_TTYS1 "vt220" |
66 | # endif | |
729def03 WF |
67 | # ifndef DEFAULT_STERM |
68 | # define DEFAULT_STERM "vt102" | |
69 | # endif | |
4585ec0b ST |
70 | #elif defined(__GNU__) |
71 | # define USE_SYSLOG | |
72 | # ifndef DEFAULT_VCTERM | |
73 | # define DEFAULT_VCTERM "hurd" | |
74 | # endif | |
75 | # ifndef DEFAULT_STERM | |
76 | # define DEFAULT_STERM "vt102" | |
77 | # endif | |
729def03 WF |
78 | #else |
79 | # ifndef DEFAULT_VCTERM | |
80 | # define DEFAULT_VCTERM "vt100" | |
81 | # endif | |
82 | # ifndef DEFAULT_STERM | |
83 | # define DEFAULT_STERM "vt100" | |
84 | # endif | |
6dbe3af9 KZ |
85 | #endif |
86 | ||
c32270d4 CE |
87 | #ifdef __FreeBSD_kernel__ |
88 | #define USE_SYSLOG | |
89 | #endif | |
90 | ||
53d55042 | 91 | /* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */ |
6dbe3af9 | 92 | #ifdef USE_SYSLOG |
53d55042 | 93 | # include <syslog.h> |
6dbe3af9 KZ |
94 | #endif |
95 | ||
b75c8134 KZ |
96 | /* |
97 | * Some heuristics to find out what environment we are in: if it is not | |
53d55042 SK |
98 | * System V, assume it is SunOS 4. The LOGIN_PROCESS is defined in System V |
99 | * utmp.h, which will select System V style getty. | |
b75c8134 | 100 | */ |
53d55042 SK |
101 | #ifdef LOGIN_PROCESS |
102 | # define SYSV_STYLE | |
6dbe3af9 KZ |
103 | #endif |
104 | ||
b75c8134 KZ |
105 | /* |
106 | * Things you may want to modify. | |
107 | * | |
108 | * If ISSUE is not defined, agetty will never display the contents of the | |
109 | * /etc/issue file. You will not want to spit out large "issue" files at the | |
110 | * wrong baud rate. Relevant for System V only. | |
111 | * | |
112 | * You may disagree with the default line-editing etc. characters defined | |
113 | * below. Note, however, that DEL cannot be used for interrupt generation | |
114 | * and for line editing at the same time. | |
115 | */ | |
6dbe3af9 | 116 | |
53d55042 | 117 | /* Displayed before the login prompt. */ |
6dbe3af9 | 118 | #ifdef SYSV_STYLE |
96cc7b0b | 119 | # define ISSUE _PATH_ISSUE |
53d55042 | 120 | # include <sys/utsname.h> |
6dbe3af9 KZ |
121 | #endif |
122 | ||
53d55042 | 123 | /* Login prompt. */ |
e85281a8 | 124 | #define LOGIN "login: " |
9aeb66dc | 125 | #define LOGIN_ARGV_MAX 16 /* Numbers of args for login */ |
6dbe3af9 | 126 | |
6443dd43 SW |
127 | /* |
128 | * agetty --reload | |
129 | */ | |
6443dd43 SW |
130 | #ifdef AGETTY_RELOAD |
131 | # include <sys/inotify.h> | |
e36deb64 SW |
132 | # include <linux/netlink.h> |
133 | # include <linux/rtnetlink.h> | |
6443dd43 SW |
134 | # define AGETTY_RELOAD_FILENAME "/run/agetty.reload" /* trigger file */ |
135 | # define AGETTY_RELOAD_FDNONE -2 /* uninitialized fd */ | |
136 | static int inotify_fd = AGETTY_RELOAD_FDNONE; | |
e36deb64 | 137 | static int netlink_fd = AGETTY_RELOAD_FDNONE; |
6443dd43 SW |
138 | #endif |
139 | ||
a359830c KZ |
140 | #define AGETTY_PLYMOUTH "/usr/bin/plymouth" |
141 | #define AGETTY_PLYMOUTH_FDFILE "/dev/null" | |
142 | ||
b75c8134 KZ |
143 | /* |
144 | * When multiple baud rates are specified on the command line, the first one | |
145 | * we will try is the first one specified. | |
146 | */ | |
6dbe3af9 KZ |
147 | #define FIRST_SPEED 0 |
148 | ||
149 | /* Storage for command-line options. */ | |
53d55042 | 150 | #define MAX_SPEED 10 /* max. nr. of baud rates */ |
6dbe3af9 KZ |
151 | |
152 | struct options { | |
bde20e1b WF |
153 | int flags; /* toggle switches, see below */ |
154 | int timeout; /* time-out period */ | |
eb8e1f9f | 155 | char *autolog; /* login the user automatically */ |
e85281a8 DWF |
156 | char *chdir; /* Chdir before the login */ |
157 | char *chroot; /* Chroot before the login */ | |
bde20e1b | 158 | char *login; /* login program */ |
eb8e1f9f | 159 | char *logopt; /* options for login program */ |
bde20e1b | 160 | char *tty; /* name of tty */ |
729def03 WF |
161 | char *vcline; /* line of virtual console */ |
162 | char *term; /* terminal type */ | |
bde20e1b WF |
163 | char *initstring; /* modem init string */ |
164 | char *issue; /* alternative issue file */ | |
cb872ac9 KZ |
165 | char *erasechars; /* string with erase chars */ |
166 | char *killchars; /* string with kill chars */ | |
b34f097e | 167 | char *osrelease; /* /etc/os-release data */ |
e85281a8 DWF |
168 | int delay; /* Sleep seconds before prompt */ |
169 | int nice; /* Run login with this priority */ | |
bde20e1b | 170 | int numspeed; /* number of baud rates to try */ |
ef264c83 | 171 | int clocal; /* CLOCAL_MODE_* */ |
b9c73909 | 172 | int kbmode; /* Keyboard mode if virtual console */ |
bde20e1b | 173 | speed_t speeds[MAX_SPEED]; /* baud rates to be tried */ |
6dbe3af9 KZ |
174 | }; |
175 | ||
ef264c83 KZ |
176 | enum { |
177 | CLOCAL_MODE_AUTO = 0, | |
178 | CLOCAL_MODE_ALWAYS, | |
179 | CLOCAL_MODE_NEVER | |
180 | }; | |
181 | ||
53d55042 SK |
182 | #define F_PARSE (1<<0) /* process modem status messages */ |
183 | #define F_ISSUE (1<<1) /* display /etc/issue */ | |
184 | #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */ | |
ef264c83 | 185 | |
53d55042 SK |
186 | #define F_INITSTRING (1<<4) /* initstring is set */ |
187 | #define F_WAITCRLF (1<<5) /* wait for CR or LF */ | |
188 | #define F_CUSTISSUE (1<<6) /* give alternative issue file */ | |
189 | #define F_NOPROMPT (1<<7) /* do not ask for login name! */ | |
190 | #define F_LCUC (1<<8) /* support for *LCUC stty modes */ | |
191 | #define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */ | |
192 | #define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */ | |
bde20e1b | 193 | #define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */ |
3aa6b68f WF |
194 | #define F_VCONSOLE (1<<12) /* This is a virtual console */ |
195 | #define F_HANGUP (1<<13) /* Do call vhangup(2) */ | |
0da025da | 196 | #define F_UTF8 (1<<14) /* We can do UTF8 */ |
eb8e1f9f | 197 | #define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */ |
e85281a8 DWF |
198 | #define F_NOCLEAR (1<<16) /* Do not clear the screen before prompting */ |
199 | #define F_NONL (1<<17) /* No newline before issue */ | |
200 | #define F_NOHOSTNAME (1<<18) /* Do not show the hostname */ | |
201 | #define F_LONGHNAME (1<<19) /* Show Full qualified hostname */ | |
36601b23 | 202 | #define F_NOHINTS (1<<20) /* Don't print hints */ |
01095ae3 | 203 | #define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */ |
0da025da WF |
204 | |
205 | #define serial_tty_option(opt, flag) \ | |
206 | (((opt)->flags & (F_VCONSOLE|(flag))) == (flag)) | |
6dbe3af9 | 207 | |
fd6b7a7f | 208 | struct Speedtab { |
53d55042 | 209 | long speed; |
bde20e1b | 210 | speed_t code; |
fd6b7a7f KZ |
211 | }; |
212 | ||
1a204cd2 | 213 | static const struct Speedtab speedtab[] = { |
53d55042 SK |
214 | {50, B50}, |
215 | {75, B75}, | |
216 | {110, B110}, | |
217 | {134, B134}, | |
218 | {150, B150}, | |
219 | {200, B200}, | |
220 | {300, B300}, | |
221 | {600, B600}, | |
222 | {1200, B1200}, | |
223 | {1800, B1800}, | |
224 | {2400, B2400}, | |
225 | {4800, B4800}, | |
226 | {9600, B9600}, | |
09625c2e | 227 | #ifdef B19200 |
53d55042 | 228 | {19200, B19200}, |
09625c2e | 229 | #elif defined(EXTA) |
53d55042 | 230 | {19200, EXTA}, |
fd6b7a7f | 231 | #endif |
09625c2e JB |
232 | #ifdef B38400 |
233 | {38400, B38400}, | |
234 | #elif defined(EXTB) | |
53d55042 | 235 | {38400, EXTB}, |
fd6b7a7f KZ |
236 | #endif |
237 | #ifdef B57600 | |
53d55042 | 238 | {57600, B57600}, |
fd6b7a7f KZ |
239 | #endif |
240 | #ifdef B115200 | |
53d55042 | 241 | {115200, B115200}, |
fd6b7a7f KZ |
242 | #endif |
243 | #ifdef B230400 | |
53d55042 | 244 | {230400, B230400}, |
f28dde8e JB |
245 | #endif |
246 | #ifdef B460800 | |
247 | {460800, B460800}, | |
248 | #endif | |
249 | #ifdef B500000 | |
250 | {500000, B500000}, | |
251 | #endif | |
252 | #ifdef B576000 | |
253 | {576000, B576000}, | |
254 | #endif | |
255 | #ifdef B921600 | |
256 | {921600, B921600}, | |
257 | #endif | |
258 | #ifdef B1000000 | |
259 | {1000000, B1000000}, | |
260 | #endif | |
261 | #ifdef B1152000 | |
262 | {1152000, B1152000}, | |
263 | #endif | |
264 | #ifdef B1500000 | |
265 | {1500000, B1500000}, | |
266 | #endif | |
267 | #ifdef B2000000 | |
268 | {2000000, B2000000}, | |
269 | #endif | |
270 | #ifdef B2500000 | |
271 | {2500000, B2500000}, | |
272 | #endif | |
273 | #ifdef B3000000 | |
274 | {3000000, B3000000}, | |
275 | #endif | |
276 | #ifdef B3500000 | |
277 | {3500000, B3500000}, | |
278 | #endif | |
279 | #ifdef B4000000 | |
280 | {4000000, B4000000}, | |
fd6b7a7f | 281 | #endif |
53d55042 | 282 | {0, 0}, |
6dbe3af9 KZ |
283 | }; |
284 | ||
729def03 | 285 | static void init_special_char(char* arg, struct options *op); |
53d55042 SK |
286 | static void parse_args(int argc, char **argv, struct options *op); |
287 | static void parse_speeds(struct options *op, char *arg); | |
729def03 WF |
288 | static void update_utmp(struct options *op); |
289 | static void open_tty(char *tty, struct termios *tp, struct options *op); | |
53d55042 | 290 | static void termio_init(struct options *op, struct termios *tp); |
0da025da | 291 | static void reset_vc (const struct options *op, struct termios *tp); |
53d55042 | 292 | static void auto_baud(struct termios *tp); |
2b945eda KZ |
293 | static void output_special_char (unsigned char c, struct options *op, |
294 | struct termios *tp, FILE *fp); | |
53d55042 SK |
295 | static void do_prompt(struct options *op, struct termios *tp); |
296 | static void next_speed(struct options *op, struct termios *tp); | |
297 | static char *get_logname(struct options *op, | |
298 | struct termios *tp, struct chardata *cp); | |
299 | static void termio_final(struct options *op, | |
300 | struct termios *tp, struct chardata *cp); | |
301 | static int caps_lock(char *s); | |
bde20e1b WF |
302 | static speed_t bcode(char *s); |
303 | static void usage(FILE * out) __attribute__((__noreturn__)); | |
763d7a87 KZ |
304 | static void log_err(const char *, ...) __attribute__((__noreturn__)) |
305 | __attribute__((__format__(printf, 1, 2))); | |
306 | static void log_warn (const char *, ...) | |
307 | __attribute__((__format__(printf, 1, 2))); | |
a694957a | 308 | static ssize_t append(char *dest, size_t len, const char *sep, const char *src); |
9aeb66dc KZ |
309 | static void check_username (const char* nm); |
310 | static void login_options_to_argv(char *argv[], int *argc, char *str, char *username); | |
bb280f79 | 311 | static int plymouth_command(const char* arg); |
6443dd43 | 312 | static void reload_agettys(void); |
6dbe3af9 | 313 | |
eb63b9b8 | 314 | /* Fake hostname for ut_host specified on command line. */ |
bde20e1b | 315 | static char *fakehost; |
eb63b9b8 | 316 | |
6dbe3af9 | 317 | #ifdef DEBUGGING |
bb280f79 | 318 | # include "closestream.h" |
46568b00 | 319 | # ifndef DEBUG_OUTPUT |
bb280f79 | 320 | # define DEBUG_OUTPUT "/dev/tty10" |
46568b00 KZ |
321 | # endif |
322 | # define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0) | |
6dbe3af9 KZ |
323 | FILE *dbf; |
324 | #else | |
46568b00 | 325 | # define debug(s) do { ; } while (0) |
efcf26f4 | 326 | #endif |
6dbe3af9 | 327 | |
53d55042 | 328 | int main(int argc, char **argv) |
6dbe3af9 | 329 | { |
9aeb66dc | 330 | char *username = NULL; /* login name, given to /bin/login */ |
eb8e1f9f WF |
331 | struct chardata chardata; /* will be set by get_logname() */ |
332 | struct termios termios; /* terminal mode bits */ | |
1a204cd2 | 333 | struct options options = { |
eb8e1f9f WF |
334 | .flags = F_ISSUE, /* show /etc/issue (SYSV_STYLE) */ |
335 | .login = _PATH_LOGIN, /* default login program */ | |
eb8e1f9f | 336 | .tty = "tty1", /* default tty line */ |
5676f365 | 337 | .issue = ISSUE /* default issue file */ |
70bedfa1 | 338 | }; |
9aeb66dc KZ |
339 | char *login_argv[LOGIN_ARGV_MAX + 1]; |
340 | int login_argc = 0; | |
3aa6b68f WF |
341 | struct sigaction sa, sa_hup, sa_quit, sa_int; |
342 | sigset_t set; | |
70bedfa1 KZ |
343 | |
344 | setlocale(LC_ALL, ""); | |
345 | bindtextdomain(PACKAGE, LOCALEDIR); | |
346 | textdomain(PACKAGE); | |
b75c8134 | 347 | |
3aa6b68f WF |
348 | /* In case vhangup(2) has to called */ |
349 | sa.sa_handler = SIG_IGN; | |
350 | sa.sa_flags = SA_RESTART; | |
351 | sigemptyset (&sa.sa_mask); | |
352 | sigaction(SIGHUP, &sa, &sa_hup); | |
353 | sigaction(SIGQUIT, &sa, &sa_quit); | |
354 | sigaction(SIGINT, &sa, &sa_int); | |
355 | ||
6dbe3af9 | 356 | #ifdef DEBUGGING |
1aba8336 | 357 | dbf = fopen(DEBUG_OUTPUT, "w"); |
bb280f79 WF |
358 | for (int i = 1; i < argc; i++) { |
359 | if (i > 1) | |
360 | debug(" "); | |
53d55042 | 361 | debug(argv[i]); |
bb280f79 WF |
362 | } |
363 | debug("\n"); | |
53d55042 | 364 | #endif /* DEBUGGING */ |
6dbe3af9 | 365 | |
70bedfa1 | 366 | /* Parse command-line arguments. */ |
70bedfa1 | 367 | parse_args(argc, argv, &options); |
6dbe3af9 | 368 | |
9aeb66dc KZ |
369 | login_argv[login_argc++] = options.login; /* set login program name */ |
370 | ||
70bedfa1 | 371 | /* Update the utmp file. */ |
6dbe3af9 | 372 | #ifdef SYSV_STYLE |
729def03 | 373 | update_utmp(&options); |
6dbe3af9 | 374 | #endif |
e85281a8 DWF |
375 | if (options.delay) |
376 | sleep(options.delay); | |
377 | ||
70bedfa1 | 378 | debug("calling open_tty\n"); |
53d55042 | 379 | |
70bedfa1 | 380 | /* Open the tty as standard { input, output, error }. */ |
729def03 | 381 | open_tty(options.tty, &termios, &options); |
70bedfa1 | 382 | |
3aa6b68f WF |
383 | /* Unmask SIGHUP if inherited */ |
384 | sigemptyset(&set); | |
385 | sigaddset(&set, SIGHUP); | |
386 | sigprocmask(SIG_UNBLOCK, &set, NULL); | |
387 | sigaction(SIGHUP, &sa_hup, NULL); | |
388 | ||
70bedfa1 | 389 | tcsetpgrp(STDIN_FILENO, getpid()); |
8410cdd3 | 390 | |
914047b4 KZ |
391 | /* Default is to follow the current line speend and then default to 9600 */ |
392 | if ((options.flags & F_VCONSOLE) == 0 && options.numspeed == 0) { | |
8410cdd3 | 393 | options.speeds[options.numspeed++] = bcode("9600"); |
3c5ee57c | 394 | options.flags |= F_KEEPSPEED; |
914047b4 | 395 | } |
8410cdd3 | 396 | |
70bedfa1 KZ |
397 | /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */ |
398 | debug("calling termio_init\n"); | |
53d55042 | 399 | termio_init(&options, &termios); |
70bedfa1 | 400 | |
53d55042 | 401 | /* Write the modem init string and DO NOT flush the buffers. */ |
eb8e1f9f WF |
402 | if (serial_tty_option(&options, F_INITSTRING) && |
403 | options.initstring && *options.initstring != '\0') { | |
70bedfa1 | 404 | debug("writing init string\n"); |
729def03 | 405 | write_all(STDOUT_FILENO, options.initstring, |
3aa6b68f | 406 | strlen(options.initstring)); |
70bedfa1 KZ |
407 | } |
408 | ||
714cff30 KZ |
409 | if (options.flags & F_VCONSOLE || options.clocal != CLOCAL_MODE_ALWAYS) |
410 | /* Go to blocking mode unless -L is specified, this change | |
411 | * affects stdout, stdin and stderr as all the file descriptors | |
412 | * are created by dup(). */ | |
70bedfa1 | 413 | fcntl(STDOUT_FILENO, F_SETFL, |
53d55042 | 414 | fcntl(STDOUT_FILENO, F_GETFL, 0) & ~O_NONBLOCK); |
70bedfa1 KZ |
415 | |
416 | /* Optionally detect the baud rate from the modem status message. */ | |
417 | debug("before autobaud\n"); | |
0da025da | 418 | if (serial_tty_option(&options, F_PARSE)) |
70bedfa1 KZ |
419 | auto_baud(&termios); |
420 | ||
421 | /* Set the optional timer. */ | |
422 | if (options.timeout) | |
763d7a87 | 423 | alarm((unsigned) options.timeout); |
70bedfa1 | 424 | |
53d55042 | 425 | /* Optionally wait for CR or LF before writing /etc/issue */ |
0da025da | 426 | if (serial_tty_option(&options, F_WAITCRLF)) { |
70bedfa1 KZ |
427 | char ch; |
428 | ||
429 | debug("waiting for cr-lf\n"); | |
53d55042 SK |
430 | while (read(STDIN_FILENO, &ch, 1) == 1) { |
431 | /* Strip "parity bit". */ | |
432 | ch &= 0x7f; | |
fd6b7a7f | 433 | #ifdef DEBUGGING |
70bedfa1 | 434 | fprintf(dbf, "read %c\n", ch); |
fd6b7a7f | 435 | #endif |
53d55042 SK |
436 | if (ch == '\n' || ch == '\r') |
437 | break; | |
70bedfa1 | 438 | } |
726f69e2 | 439 | } |
726f69e2 | 440 | |
f5664477 | 441 | INIT_CHARDATA(&chardata); |
933956cb KZ |
442 | |
443 | if (options.autolog) { | |
444 | debug("doing auto login\n"); | |
445 | username = options.autolog; | |
446 | } | |
447 | ||
1683f6bf | 448 | if ((options.flags & F_NOPROMPT) == 0) { |
eb8e1f9f | 449 | if (options.autolog) { |
933956cb | 450 | /* Autologin prompt */ |
eb8e1f9f | 451 | do_prompt(&options, &termios); |
8c219bf4 | 452 | printf(_("%s%s (automatic login)\n"), LOGIN, options.autolog); |
eb8e1f9f WF |
453 | } else { |
454 | /* Read the login name. */ | |
455 | debug("reading login name\n"); | |
9aeb66dc | 456 | while ((username = |
f8bd089b | 457 | get_logname(&options, &termios, &chardata)) == NULL) |
8410cdd3 | 458 | if ((options.flags & F_VCONSOLE) == 0 && options.numspeed) |
eb8e1f9f WF |
459 | next_speed(&options, &termios); |
460 | } | |
70bedfa1 | 461 | } |
6dbe3af9 | 462 | |
70bedfa1 | 463 | /* Disable timer. */ |
70bedfa1 | 464 | if (options.timeout) |
53d55042 | 465 | alarm(0); |
6dbe3af9 | 466 | |
0da025da WF |
467 | if ((options.flags & F_VCONSOLE) == 0) { |
468 | /* Finalize the termios settings. */ | |
469 | termio_final(&options, &termios, &chardata); | |
6dbe3af9 | 470 | |
0da025da WF |
471 | /* Now the newline character should be properly written. */ |
472 | write_all(STDOUT_FILENO, "\r\n", 2); | |
473 | } | |
6dbe3af9 | 474 | |
763d7a87 KZ |
475 | sigaction(SIGQUIT, &sa_quit, NULL); |
476 | sigaction(SIGINT, &sa_int, NULL); | |
3aa6b68f | 477 | |
9aeb66dc KZ |
478 | if (username) |
479 | check_username(username); | |
480 | ||
481 | if (options.logopt) { | |
482 | /* | |
483 | * The --login-options completely overwrites the default | |
484 | * way how agetty composes login(1) command line. | |
485 | */ | |
486 | login_options_to_argv(login_argv, &login_argc, | |
487 | options.logopt, username); | |
01095ae3 KZ |
488 | } else { |
489 | if (fakehost && (options.flags & F_REMOTE)) { | |
490 | login_argv[login_argc++] = "-h"; | |
491 | login_argv[login_argc++] = fakehost; | |
492 | } | |
493 | if (username) { | |
494 | if (options.autolog) | |
495 | login_argv[login_argc++] = "-f"; | |
496 | else | |
497 | login_argv[login_argc++] = "--"; | |
498 | login_argv[login_argc++] = username; | |
499 | } | |
9aeb66dc | 500 | } |
eb8e1f9f | 501 | |
9aeb66dc | 502 | login_argv[login_argc] = NULL; /* last login argv */ |
eb8e1f9f | 503 | |
e85281a8 DWF |
504 | if (options.chroot) { |
505 | if (chroot(options.chroot) < 0) | |
506 | log_err(_("%s: can't change root directory %s: %m"), | |
507 | options.tty, options.chroot); | |
508 | } | |
509 | if (options.chdir) { | |
510 | if (chdir(options.chdir) < 0) | |
511 | log_err(_("%s: can't change working directory %s: %m"), | |
512 | options.tty, options.chdir); | |
513 | } | |
514 | if (options.nice) { | |
515 | if (nice(options.nice) < 0) | |
516 | log_warn(_("%s: can't change process priority: %m"), | |
517 | options.tty); | |
518 | } | |
f614b73c | 519 | free(options.osrelease); |
1aba8336 | 520 | #ifdef DEBUGGING |
1aba8336 SK |
521 | if (close_stream(dbf) != 0) |
522 | log_err("write failed: %s", DEBUG_OUTPUT); | |
523 | #endif | |
524 | ||
70bedfa1 | 525 | /* Let the login program take care of password validation. */ |
9aeb66dc KZ |
526 | execv(options.login, login_argv); |
527 | log_err(_("%s: can't exec %s: %m"), options.tty, login_argv[0]); | |
528 | } | |
529 | ||
530 | /* | |
531 | * Returns : @str if \u not found | |
532 | * : @username if @str equal to "\u" | |
533 | * : newly allocated string if \u mixed with something other | |
534 | */ | |
535 | static char *replace_u(char *str, char *username) | |
536 | { | |
537 | char *entry = NULL, *p = str; | |
538 | size_t usz = username ? strlen(username) : 0; | |
539 | ||
540 | while (*p) { | |
541 | size_t sz; | |
542 | char *tp, *old = entry; | |
543 | ||
544 | if (memcmp(p, "\\u", 2)) { | |
545 | p++; | |
546 | continue; /* no \u */ | |
547 | } | |
548 | sz = strlen(str); | |
549 | ||
3797cf25 | 550 | if (p == str && sz == 2) { |
9aeb66dc | 551 | /* 'str' contains only '\u' */ |
3797cf25 | 552 | free(old); |
9aeb66dc | 553 | return username; |
3797cf25 | 554 | } |
9aeb66dc KZ |
555 | |
556 | tp = entry = malloc(sz + usz); | |
557 | if (!tp) | |
558 | log_err(_("failed to allocate memory: %m")); | |
559 | ||
560 | if (p != str) { | |
561 | /* copy chars befor \u */ | |
562 | memcpy(tp, str, p - str); | |
563 | tp += p - str; | |
564 | } | |
565 | if (usz) { | |
566 | /* copy username */ | |
567 | memcpy(tp, username, usz); | |
568 | tp += usz; | |
569 | } | |
570 | if (*(p + 2)) | |
571 | /* copy chars after \u + \0 */ | |
572 | memcpy(tp, p + 2, sz - (p - str) - 1); | |
573 | else | |
574 | *tp = '\0'; | |
575 | ||
576 | p = tp; | |
577 | str = entry; | |
578 | free(old); | |
579 | } | |
580 | ||
581 | return entry ? entry : str; | |
582 | } | |
583 | ||
584 | static void login_options_to_argv(char *argv[], int *argc, | |
585 | char *str, char *username) | |
586 | { | |
587 | char *p; | |
588 | int i = *argc; | |
589 | ||
590 | while (str && isspace(*str)) | |
591 | str++; | |
592 | p = str; | |
593 | ||
594 | while (p && *p && i < LOGIN_ARGV_MAX) { | |
595 | if (isspace(*p)) { | |
596 | *p = '\0'; | |
597 | while (isspace(*++p)) | |
598 | ; | |
599 | if (*p) { | |
600 | argv[i++] = replace_u(str, username); | |
601 | str = p; | |
602 | } | |
603 | } else | |
604 | p++; | |
605 | } | |
606 | if (str && *str && i < LOGIN_ARGV_MAX) | |
607 | argv[i++] = replace_u(str, username); | |
608 | *argc = i; | |
6dbe3af9 KZ |
609 | } |
610 | ||
f2bcda51 WF |
611 | #define is_speed(str) (strlen((str)) == strspn((str), "0123456789,")) |
612 | ||
53d55042 | 613 | /* Parse command-line arguments. */ |
bde20e1b | 614 | static void parse_args(int argc, char **argv, struct options *op) |
6dbe3af9 | 615 | { |
53d55042 | 616 | int c; |
70bedfa1 KZ |
617 | |
618 | enum { | |
619 | VERSION_OPTION = CHAR_MAX + 1, | |
36601b23 | 620 | NOHINTS_OPTION, |
e85281a8 DWF |
621 | NOHOSTNAME_OPTION, |
622 | LONGHOSTNAME_OPTION, | |
cb872ac9 KZ |
623 | HELP_OPTION, |
624 | ERASE_CHARS_OPTION, | |
625 | KILL_CHARS_OPTION, | |
6443dd43 | 626 | RELOAD_OPTION, |
70bedfa1 | 627 | }; |
3aa6b68f | 628 | const struct option longopts[] = { |
70bedfa1 | 629 | { "8bits", no_argument, 0, '8' }, |
eb8e1f9f | 630 | { "autologin", required_argument, 0, 'a' }, |
70bedfa1 | 631 | { "noreset", no_argument, 0, 'c' }, |
e85281a8 DWF |
632 | { "chdir", required_argument, 0, 'C' }, |
633 | { "delay", required_argument, 0, 'd' }, | |
01095ae3 | 634 | { "remote", no_argument, 0, 'E' }, |
70bedfa1 KZ |
635 | { "issue-file", required_argument, 0, 'f' }, |
636 | { "flow-control", no_argument, 0, 'h' }, | |
637 | { "host", required_argument, 0, 'H' }, | |
638 | { "noissue", no_argument, 0, 'i' }, | |
639 | { "init-string", required_argument, 0, 'I' }, | |
e85281a8 | 640 | { "noclear", no_argument, 0, 'J' }, |
70bedfa1 | 641 | { "login-program", required_argument, 0, 'l' }, |
ef264c83 | 642 | { "local-line", optional_argument, 0, 'L' }, |
70bedfa1 KZ |
643 | { "extract-baud", no_argument, 0, 'm' }, |
644 | { "skip-login", no_argument, 0, 'n' }, | |
e85281a8 | 645 | { "nonewline", no_argument, 0, 'N' }, |
eb8e1f9f | 646 | { "login-options", required_argument, 0, 'o' }, |
3fc62fd3 | 647 | { "login-pause", no_argument, 0, 'p' }, |
e85281a8 DWF |
648 | { "nice", required_argument, 0, 'P' }, |
649 | { "chroot", required_argument, 0, 'r' }, | |
3aa6b68f | 650 | { "hangup", no_argument, 0, 'R' }, |
70bedfa1 KZ |
651 | { "keep-baud", no_argument, 0, 's' }, |
652 | { "timeout", required_argument, 0, 't' }, | |
653 | { "detect-case", no_argument, 0, 'U' }, | |
654 | { "wait-cr", no_argument, 0, 'w' }, | |
36601b23 | 655 | { "nohints", no_argument, 0, NOHINTS_OPTION }, |
3fc62fd3 | 656 | { "nohostname", no_argument, 0, NOHOSTNAME_OPTION }, |
e85281a8 | 657 | { "long-hostname", no_argument, 0, LONGHOSTNAME_OPTION }, |
6443dd43 | 658 | { "reload", no_argument, 0, RELOAD_OPTION }, |
70bedfa1 KZ |
659 | { "version", no_argument, 0, VERSION_OPTION }, |
660 | { "help", no_argument, 0, HELP_OPTION }, | |
cb872ac9 KZ |
661 | { "erase-chars", required_argument, 0, ERASE_CHARS_OPTION }, |
662 | { "kill-chars", required_argument, 0, KILL_CHARS_OPTION }, | |
70bedfa1 KZ |
663 | { NULL, 0, 0, 0 } |
664 | }; | |
665 | ||
763d7a87 | 666 | while ((c = getopt_long(argc, argv, |
6f964206 | 667 | "8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts, |
53d55042 | 668 | NULL)) != -1) { |
70bedfa1 KZ |
669 | switch (c) { |
670 | case '8': | |
bde20e1b | 671 | op->flags |= F_EIGHTBITS; |
70bedfa1 | 672 | break; |
eb8e1f9f WF |
673 | case 'a': |
674 | op->autolog = optarg; | |
675 | break; | |
70bedfa1 KZ |
676 | case 'c': |
677 | op->flags |= F_KEEPCFLAGS; | |
678 | break; | |
e85281a8 DWF |
679 | case 'C': |
680 | op->chdir = optarg; | |
681 | break; | |
682 | case 'd': | |
683 | op->delay = atoi(optarg); | |
684 | break; | |
01095ae3 KZ |
685 | case 'E': |
686 | op->flags |= F_REMOTE; | |
687 | break; | |
53d55042 | 688 | case 'f': |
70bedfa1 KZ |
689 | op->flags |= F_CUSTISSUE; |
690 | op->issue = optarg; | |
691 | break; | |
53d55042 | 692 | case 'h': |
70bedfa1 KZ |
693 | op->flags |= F_RTSCTS; |
694 | break; | |
53d55042 | 695 | case 'H': |
70bedfa1 KZ |
696 | fakehost = optarg; |
697 | break; | |
53d55042 | 698 | case 'i': |
70bedfa1 KZ |
699 | op->flags &= ~F_ISSUE; |
700 | break; | |
701 | case 'I': | |
729def03 WF |
702 | init_special_char(optarg, op); |
703 | op->flags |= F_INITSTRING; | |
704 | break; | |
e85281a8 DWF |
705 | case 'J': |
706 | op->flags |= F_NOCLEAR; | |
707 | break; | |
70bedfa1 | 708 | case 'l': |
53d55042 | 709 | op->login = optarg; |
70bedfa1 | 710 | break; |
53d55042 | 711 | case 'L': |
ef264c83 KZ |
712 | /* -L and -L=always have the same meaning */ |
713 | op->clocal = CLOCAL_MODE_ALWAYS; | |
714 | if (optarg) { | |
715 | if (strcmp(optarg, "=always") == 0) | |
716 | op->clocal = CLOCAL_MODE_ALWAYS; | |
717 | else if (strcmp(optarg, "=never") == 0) | |
718 | op->clocal = CLOCAL_MODE_NEVER; | |
719 | else if (strcmp(optarg, "=auto") == 0) | |
720 | op->clocal = CLOCAL_MODE_AUTO; | |
721 | else | |
38ae77d7 | 722 | log_err(_("invalid argument of --local-line")); |
ef264c83 | 723 | } |
70bedfa1 | 724 | break; |
53d55042 | 725 | case 'm': |
70bedfa1 KZ |
726 | op->flags |= F_PARSE; |
727 | break; | |
728 | case 'n': | |
729 | op->flags |= F_NOPROMPT; | |
730 | break; | |
6c77c7fa BR |
731 | case 'N': |
732 | op->flags |= F_NONL; | |
733 | break; | |
eb8e1f9f WF |
734 | case 'o': |
735 | op->logopt = optarg; | |
736 | break; | |
737 | case 'p': | |
738 | op->flags |= F_LOGINPAUSE; | |
739 | break; | |
e85281a8 DWF |
740 | case 'P': |
741 | op->nice = atoi(optarg); | |
742 | break; | |
743 | case 'r': | |
744 | op->chroot = optarg; | |
745 | break; | |
3aa6b68f WF |
746 | case 'R': |
747 | op->flags |= F_HANGUP; | |
748 | break; | |
70bedfa1 | 749 | case 's': |
53d55042 | 750 | op->flags |= F_KEEPSPEED; |
70bedfa1 | 751 | break; |
53d55042 | 752 | case 't': |
70bedfa1 | 753 | if ((op->timeout = atoi(optarg)) <= 0) |
bde20e1b | 754 | log_err(_("bad timeout value: %s"), optarg); |
70bedfa1 KZ |
755 | break; |
756 | case 'U': | |
757 | op->flags |= F_LCUC; | |
758 | break; | |
759 | case 'w': | |
760 | op->flags |= F_WAITCRLF; | |
761 | break; | |
36601b23 KZ |
762 | case NOHINTS_OPTION: |
763 | op->flags |= F_NOHINTS; | |
764 | break; | |
e85281a8 DWF |
765 | case NOHOSTNAME_OPTION: |
766 | op->flags |= F_NOHOSTNAME; | |
767 | break; | |
768 | case LONGHOSTNAME_OPTION: | |
769 | op->flags |= F_LONGHNAME; | |
770 | break; | |
cb872ac9 KZ |
771 | case ERASE_CHARS_OPTION: |
772 | op->erasechars = optarg; | |
773 | break; | |
774 | case KILL_CHARS_OPTION: | |
775 | op->killchars = optarg; | |
776 | break; | |
6443dd43 SW |
777 | case RELOAD_OPTION: |
778 | reload_agettys(); | |
779 | exit(EXIT_SUCCESS); | |
70bedfa1 | 780 | case VERSION_OPTION: |
f6277500 | 781 | printf(UTIL_LINUX_VERSION); |
70bedfa1 KZ |
782 | exit(EXIT_SUCCESS); |
783 | case HELP_OPTION: | |
784 | usage(stdout); | |
785 | default: | |
786 | usage(stderr); | |
726f69e2 | 787 | } |
6dbe3af9 | 788 | } |
70bedfa1 KZ |
789 | |
790 | debug("after getopt loop\n"); | |
791 | ||
729def03 | 792 | if (argc < optind + 1) { |
bde20e1b | 793 | log_warn(_("not enough arguments")); |
70bedfa1 KZ |
794 | usage(stderr); |
795 | } | |
796 | ||
729def03 | 797 | /* Accept "tty", "baudrate tty", and "tty baudrate". */ |
f2bcda51 | 798 | if (is_speed(argv[optind])) { |
53d55042 SK |
799 | /* Assume BSD style speed. */ |
800 | parse_speeds(op, argv[optind++]); | |
729def03 WF |
801 | if (argc < optind + 1) { |
802 | warn(_("not enough arguments")); | |
803 | usage(stderr); | |
804 | } | |
805 | op->tty = argv[optind++]; | |
70bedfa1 | 806 | } else { |
53d55042 | 807 | op->tty = argv[optind++]; |
729def03 | 808 | if (argc > optind) { |
14da9b1f KZ |
809 | char *v = argv[optind]; |
810 | if (is_speed(v)) { | |
729def03 | 811 | parse_speeds(op, v); |
14da9b1f KZ |
812 | optind++; |
813 | } | |
729def03 | 814 | } |
70bedfa1 KZ |
815 | } |
816 | ||
729def03 WF |
817 | /* On virtual console remember the line which is used for */ |
818 | if (strncmp(op->tty, "tty", 3) == 0 && | |
819 | strspn(op->tty + 3, "0123456789") == strlen(op->tty+3)) | |
820 | op->vcline = op->tty+3; | |
821 | ||
70bedfa1 | 822 | if (argc > optind && argv[optind]) |
729def03 | 823 | op->term = argv[optind]; |
6dbe3af9 | 824 | |
66ee8158 | 825 | #ifdef DO_DEVFS_FIDDLING |
70bedfa1 | 826 | /* |
53d55042 | 827 | * Some devfs junk, following Goswin Brederlow: |
70bedfa1 KZ |
828 | * turn ttyS<n> into tts/<n> |
829 | * turn tty<n> into vc/<n> | |
53d55042 | 830 | * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=72241 |
70bedfa1 KZ |
831 | */ |
832 | if (op->tty && strlen(op->tty) < 90) { | |
833 | char dev_name[100]; | |
834 | struct stat st; | |
835 | ||
836 | if (strncmp(op->tty, "ttyS", 4) == 0) { | |
837 | strcpy(dev_name, "/dev/"); | |
838 | strcat(dev_name, op->tty); | |
839 | if (stat(dev_name, &st) < 0) { | |
840 | strcpy(dev_name, "/dev/tts/"); | |
841 | strcat(dev_name, op->tty + 4); | |
ca8e91a4 KZ |
842 | if (stat(dev_name, &st) == 0) { |
843 | op->tty = strdup(dev_name + 5); | |
844 | if (!op->tty) | |
845 | log_err(_("failed to allocate memory: %m")); | |
846 | } | |
70bedfa1 KZ |
847 | } |
848 | } else if (strncmp(op->tty, "tty", 3) == 0) { | |
849 | strcpy(dev_name, "/dev/"); | |
850 | strncat(dev_name, op->tty, 90); | |
851 | if (stat(dev_name, &st) < 0) { | |
852 | strcpy(dev_name, "/dev/vc/"); | |
853 | strcat(dev_name, op->tty + 3); | |
ca8e91a4 KZ |
854 | if (stat(dev_name, &st) == 0) { |
855 | op->tty = strdup(dev_name + 5); | |
856 | if (!op->tty) | |
857 | log_err(_("failed to allocate memory: %m")); | |
858 | } | |
70bedfa1 KZ |
859 | } |
860 | } | |
861 | } | |
53d55042 | 862 | #endif /* DO_DEVFS_FIDDLING */ |
66ee8158 | 863 | |
70bedfa1 | 864 | debug("exiting parseargs\n"); |
6dbe3af9 KZ |
865 | } |
866 | ||
53d55042 | 867 | /* Parse alternate baud rates. */ |
bde20e1b | 868 | static void parse_speeds(struct options *op, char *arg) |
6dbe3af9 | 869 | { |
53d55042 | 870 | char *cp; |
6dbe3af9 | 871 | |
f021834e | 872 | debug("entered parse_speeds\n"); |
f8bd089b | 873 | for (cp = strtok(arg, ","); cp != NULL; cp = strtok((char *)0, ",")) { |
70bedfa1 | 874 | if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0) |
bde20e1b | 875 | log_err(_("bad speed: %s"), cp); |
70bedfa1 | 876 | if (op->numspeed >= MAX_SPEED) |
bde20e1b | 877 | log_err(_("too many alternate speeds")); |
70bedfa1 KZ |
878 | } |
879 | debug("exiting parsespeeds\n"); | |
6dbe3af9 KZ |
880 | } |
881 | ||
882 | #ifdef SYSV_STYLE | |
883 | ||
53d55042 | 884 | /* Update our utmp entry. */ |
729def03 | 885 | static void update_utmp(struct options *op) |
6dbe3af9 | 886 | { |
53d55042 SK |
887 | struct utmp ut; |
888 | time_t t; | |
729def03 WF |
889 | pid_t pid = getpid(); |
890 | pid_t sid = getsid(0); | |
891 | char *vcline = op->vcline; | |
892 | char *line = op->tty; | |
53d55042 | 893 | struct utmp *utp; |
70bedfa1 KZ |
894 | |
895 | /* | |
896 | * The utmp file holds miscellaneous information about things started by | |
897 | * /sbin/init and other system-related events. Our purpose is to update | |
898 | * the utmp entry for the current process, in particular the process type | |
899 | * and the tty line we are listening to. Return successfully only if the | |
900 | * utmp file can be opened for update, and if we are able to find our | |
901 | * entry in the utmp file. | |
902 | */ | |
70bedfa1 KZ |
903 | utmpname(_PATH_UTMP); |
904 | setutent(); | |
905 | ||
53d55042 | 906 | /* |
729def03 | 907 | * Find my pid in utmp. |
53d55042 SK |
908 | * |
909 | * FIXME: Earlier (when was that?) code here tested only utp->ut_type != | |
910 | * INIT_PROCESS, so maybe the >= here should be >. | |
911 | * | |
912 | * FIXME: The present code is taken from login.c, so if this is changed, | |
913 | * maybe login has to be changed as well (is this true?). | |
914 | */ | |
70bedfa1 | 915 | while ((utp = getutent())) |
729def03 | 916 | if (utp->ut_pid == pid |
70bedfa1 KZ |
917 | && utp->ut_type >= INIT_PROCESS |
918 | && utp->ut_type <= DEAD_PROCESS) | |
919 | break; | |
920 | ||
921 | if (utp) { | |
922 | memcpy(&ut, utp, sizeof(ut)); | |
923 | } else { | |
53d55042 | 924 | /* Some inits do not initialize utmp. */ |
70bedfa1 | 925 | memset(&ut, 0, sizeof(ut)); |
729def03 WF |
926 | if (vcline && *vcline) |
927 | /* Standard virtual console devices */ | |
763d7a87 | 928 | strncpy(ut.ut_id, vcline, sizeof(ut.ut_id)); |
729def03 WF |
929 | else { |
930 | size_t len = strlen(line); | |
931 | char * ptr; | |
763d7a87 KZ |
932 | if (len >= sizeof(ut.ut_id)) |
933 | ptr = line + len - sizeof(ut.ut_id); | |
729def03 WF |
934 | else |
935 | ptr = line; | |
763d7a87 | 936 | strncpy(ut.ut_id, ptr, sizeof(ut.ut_id)); |
729def03 | 937 | } |
70bedfa1 KZ |
938 | } |
939 | ||
940 | strncpy(ut.ut_user, "LOGIN", sizeof(ut.ut_user)); | |
941 | strncpy(ut.ut_line, line, sizeof(ut.ut_line)); | |
942 | if (fakehost) | |
943 | strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host)); | |
944 | time(&t); | |
cfa7fe89 SK |
945 | #if defined(_HAVE_UT_TV) |
946 | ut.ut_tv.tv_sec = t; | |
947 | #else | |
70bedfa1 | 948 | ut.ut_time = t; |
cfa7fe89 | 949 | #endif |
70bedfa1 | 950 | ut.ut_type = LOGIN_PROCESS; |
729def03 WF |
951 | ut.ut_pid = pid; |
952 | ut.ut_session = sid; | |
70bedfa1 KZ |
953 | |
954 | pututline(&ut); | |
955 | endutent(); | |
956 | ||
957 | { | |
48d7b13a | 958 | #ifdef HAVE_UPDWTMP |
70bedfa1 | 959 | updwtmp(_PATH_WTMP, &ut); |
5c36a0eb | 960 | #else |
70bedfa1 KZ |
961 | int ut_fd; |
962 | int lf; | |
963 | ||
53d55042 | 964 | if ((lf = open(_PATH_WTMPLOCK, O_CREAT | O_WRONLY, 0660)) >= 0) { |
70bedfa1 | 965 | flock(lf, LOCK_EX); |
53d55042 SK |
966 | if ((ut_fd = |
967 | open(_PATH_WTMP, O_APPEND | O_WRONLY)) >= 0) { | |
729def03 | 968 | write_all(ut_fd, &ut, sizeof(ut)); |
70bedfa1 KZ |
969 | close(ut_fd); |
970 | } | |
971 | flock(lf, LOCK_UN); | |
972 | close(lf); | |
973 | } | |
53d55042 | 974 | #endif /* HAVE_UPDWTMP */ |
70bedfa1 | 975 | } |
6dbe3af9 KZ |
976 | } |
977 | ||
53d55042 | 978 | #endif /* SYSV_STYLE */ |
6dbe3af9 | 979 | |
53d55042 | 980 | /* Set up tty as stdin, stdout & stderr. */ |
729def03 | 981 | static void open_tty(char *tty, struct termios *tp, struct options *op) |
6dbe3af9 | 982 | { |
3aa6b68f | 983 | const pid_t pid = getpid(); |
b9c73909 | 984 | int closed = 0; |
88e0f3df ST |
985 | #ifndef KDGKBMODE |
986 | int serial; | |
987 | #endif | |
6dbe3af9 | 988 | |
3aa6b68f WF |
989 | /* Set up new standard input, unless we are given an already opened port. */ |
990 | ||
991 | if (strcmp(tty, "-") != 0) { | |
992 | char buf[PATH_MAX+1]; | |
993 | struct group *gr = NULL; | |
70bedfa1 | 994 | struct stat st; |
3aa6b68f WF |
995 | int fd, len; |
996 | pid_t tid; | |
997 | gid_t gid = 0; | |
998 | ||
999 | /* Use tty group if available */ | |
1000 | if ((gr = getgrnam("tty"))) | |
1001 | gid = gr->gr_gid; | |
1002 | ||
763d7a87 KZ |
1003 | if (((len = snprintf(buf, sizeof(buf), "/dev/%s", tty)) >= |
1004 | (int)sizeof(buf)) || (len < 0)) | |
3aa6b68f WF |
1005 | log_err(_("/dev/%s: cannot open as standard input: %m"), tty); |
1006 | ||
c3a9f86f SK |
1007 | /* Open the tty as standard input. */ |
1008 | if ((fd = open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0)) < 0) | |
1009 | log_err(_("/dev/%s: cannot open as standard input: %m"), tty); | |
1010 | ||
3aa6b68f WF |
1011 | /* |
1012 | * There is always a race between this reset and the call to | |
1013 | * vhangup() that s.o. can use to get access to your tty. | |
1014 | * Linux login(1) will change tty permissions. Use root owner and group | |
1015 | * with permission -rw------- for the period between getty and login. | |
1016 | */ | |
c3a9f86f | 1017 | if (fchown(fd, 0, gid) || fchmod(fd, (gid ? 0620 : 0600))) { |
3aa6b68f WF |
1018 | if (errno == EROFS) |
1019 | log_warn("%s: %m", buf); | |
1020 | else | |
1021 | log_err("%s: %m", buf); | |
1022 | } | |
6dbe3af9 | 1023 | |
3aa6b68f | 1024 | /* Sanity checks... */ |
3aa6b68f | 1025 | if (fstat(fd, &st) < 0) |
763d7a87 | 1026 | log_err("%s: %m", buf); |
70bedfa1 | 1027 | if ((st.st_mode & S_IFMT) != S_IFCHR) |
bde20e1b | 1028 | log_err(_("/dev/%s: not a character device"), tty); |
c6b35139 KZ |
1029 | if (!isatty(fd)) |
1030 | log_err(_("/dev/%s: not a tty"), tty); | |
6dbe3af9 | 1031 | |
3aa6b68f | 1032 | if (((tid = tcgetsid(fd)) < 0) || (pid != tid)) { |
763d7a87 | 1033 | if (ioctl(fd, TIOCSCTTY, 1) == -1) |
8c219bf4 | 1034 | log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty); |
3aa6b68f WF |
1035 | } |
1036 | ||
783b08fc KZ |
1037 | close(STDIN_FILENO); |
1038 | errno = 0; | |
1039 | ||
3aa6b68f | 1040 | if (op->flags & F_HANGUP) { |
783b08fc KZ |
1041 | |
1042 | if (ioctl(fd, TIOCNOTTY)) | |
1043 | debug("TIOCNOTTY ioctl failed\n"); | |
1044 | ||
3aa6b68f | 1045 | /* |
783b08fc KZ |
1046 | * Let's close all file decriptors before vhangup |
1047 | * https://lkml.org/lkml/2012/6/5/145 | |
3aa6b68f | 1048 | */ |
783b08fc KZ |
1049 | close(fd); |
1050 | close(STDOUT_FILENO); | |
1051 | close(STDERR_FILENO); | |
1052 | errno = 0; | |
1053 | closed = 1; | |
1054 | ||
3aa6b68f | 1055 | if (vhangup()) |
8c219bf4 | 1056 | log_err(_("/dev/%s: vhangup() failed: %m"), tty); |
783b08fc KZ |
1057 | } else |
1058 | close(fd); | |
6dbe3af9 | 1059 | |
70bedfa1 | 1060 | debug("open(2)\n"); |
3aa6b68f WF |
1061 | if (open(buf, O_RDWR|O_NOCTTY|O_NONBLOCK, 0) != 0) |
1062 | log_err(_("/dev/%s: cannot open as standard input: %m"), tty); | |
783b08fc | 1063 | |
3aa6b68f | 1064 | if (((tid = tcgetsid(STDIN_FILENO)) < 0) || (pid != tid)) { |
763d7a87 | 1065 | if (ioctl(STDIN_FILENO, TIOCSCTTY, 1) == -1) |
8c219bf4 | 1066 | log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty); |
3aa6b68f WF |
1067 | } |
1068 | ||
70bedfa1 | 1069 | } else { |
3aa6b68f | 1070 | |
70bedfa1 KZ |
1071 | /* |
1072 | * Standard input should already be connected to an open port. Make | |
1073 | * sure it is open for read/write. | |
1074 | */ | |
3aa6b68f | 1075 | |
70bedfa1 | 1076 | if ((fcntl(STDIN_FILENO, F_GETFL, 0) & O_RDWR) != O_RDWR) |
bde20e1b | 1077 | log_err(_("%s: not open for read/write"), tty); |
3aa6b68f | 1078 | |
70bedfa1 KZ |
1079 | } |
1080 | ||
3aa6b68f | 1081 | if (tcsetpgrp(STDIN_FILENO, pid)) |
8c219bf4 | 1082 | log_warn(_("/dev/%s: cannot set process group: %m"), tty); |
3aa6b68f WF |
1083 | |
1084 | /* Get rid of the present outputs. */ | |
783b08fc KZ |
1085 | if (!closed) { |
1086 | close(STDOUT_FILENO); | |
1087 | close(STDERR_FILENO); | |
1088 | errno = 0; | |
1089 | } | |
3aa6b68f | 1090 | |
70bedfa1 KZ |
1091 | /* Set up standard output and standard error file descriptors. */ |
1092 | debug("duping\n"); | |
53d55042 | 1093 | |
70bedfa1 KZ |
1094 | /* set up stdout and stderr */ |
1095 | if (dup(STDIN_FILENO) != 1 || dup(STDIN_FILENO) != 2) | |
bde20e1b | 1096 | log_err(_("%s: dup problem: %m"), tty); |
6dbe3af9 | 1097 | |
3aa6b68f WF |
1098 | /* make stdio unbuffered for slow modem lines */ |
1099 | setvbuf(stdout, NULL, _IONBF, 0); | |
1100 | ||
6dbe3af9 | 1101 | /* |
70bedfa1 KZ |
1102 | * The following ioctl will fail if stdin is not a tty, but also when |
1103 | * there is noise on the modem control lines. In the latter case, the | |
53d55042 SK |
1104 | * common course of action is (1) fix your cables (2) give the modem |
1105 | * more time to properly reset after hanging up. | |
1106 | * | |
1107 | * SunOS users can achieve (2) by patching the SunOS kernel variable | |
1108 | * "zsadtrlow" to a larger value; 5 seconds seems to be a good value. | |
1109 | * http://www.sunmanagers.org/archives/1993/0574.html | |
6dbe3af9 | 1110 | */ |
bde20e1b | 1111 | memset(tp, 0, sizeof(struct termios)); |
70bedfa1 | 1112 | if (tcgetattr(STDIN_FILENO, tp) < 0) |
8c219bf4 | 1113 | log_err(_("%s: failed to get terminal attributes: %m"), tty); |
70bedfa1 | 1114 | |
fba8a535 WF |
1115 | #if defined (__s390__) || defined (__s390x__) |
1116 | if (!op->term) { | |
1117 | /* | |
1118 | * Special terminal on first serial line on a S/390(x) which | |
1119 | * is due legacy reasons a block terminal of type 3270 or | |
1120 | * higher. Whereas the second serial line on a S/390(x) is | |
1121 | * a real character terminal which is compatible with VT220. | |
1122 | */ | |
f2bcda51 | 1123 | if (strcmp(op->tty, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */ |
fba8a535 | 1124 | op->term = DEFAULT_TTYS0; |
f2bcda51 WF |
1125 | else if (strncmp(op->tty, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */ |
1126 | op->term = DEFAULT_TTY32; | |
1127 | else if (strcmp(op->tty, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */ | |
fba8a535 WF |
1128 | op->term = DEFAULT_TTYS1; |
1129 | } | |
1130 | #endif | |
c32270d4 CE |
1131 | |
1132 | #if defined(__FreeBSD_kernel__) | |
1133 | login_tty (0); | |
1134 | #endif | |
1135 | ||
70bedfa1 | 1136 | /* |
3aa6b68f | 1137 | * Detect if this is a virtual console or serial/modem line. |
b9c73909 WF |
1138 | * In case of a virtual console the ioctl KDGKBMODE succeeds |
1139 | * whereas on other lines it will fails. | |
70bedfa1 | 1140 | */ |
88e0f3df ST |
1141 | #ifdef KDGKBMODE |
1142 | if (ioctl(STDIN_FILENO, KDGKBMODE, &op->kbmode) == 0) | |
1143 | #else | |
1144 | if (ioctl(STDIN_FILENO, TIOCMGET, &serial) < 0 && (errno == EINVAL)) | |
1145 | #endif | |
1146 | { | |
3aa6b68f WF |
1147 | op->flags |= F_VCONSOLE; |
1148 | if (!op->term) | |
1149 | op->term = DEFAULT_VCTERM; | |
b9c73909 | 1150 | } else { |
88e0f3df | 1151 | #ifdef K_RAW |
b9c73909 | 1152 | op->kbmode = K_RAW; |
88e0f3df | 1153 | #endif |
b9c73909 WF |
1154 | if (!op->term) |
1155 | op->term = DEFAULT_STERM; | |
1156 | } | |
729def03 WF |
1157 | |
1158 | setenv("TERM", op->term, 1); | |
6dbe3af9 KZ |
1159 | } |
1160 | ||
6443dd43 SW |
1161 | /* Initialize termios settings. */ |
1162 | static void termio_clear(int fd) | |
1163 | { | |
1164 | /* | |
1165 | * Do not write a full reset (ESC c) because this destroys | |
1166 | * the unicode mode again if the terminal was in unicode | |
1167 | * mode. Also it clears the CONSOLE_MAGIC features which | |
1168 | * are required for some languages/console-fonts. | |
1169 | * Just put the cursor to the home position (ESC [ H), | |
1170 | * erase everything below the cursor (ESC [ J), and set the | |
1171 | * scrolling region to the full window (ESC [ r) | |
1172 | */ | |
1173 | write_all(fd, "\033[r\033[H\033[J", 9); | |
1174 | } | |
1175 | ||
53d55042 | 1176 | /* Initialize termios settings. */ |
bde20e1b | 1177 | static void termio_init(struct options *op, struct termios *tp) |
6dbe3af9 | 1178 | { |
70bedfa1 | 1179 | speed_t ispeed, ospeed; |
1683f6bf | 1180 | struct winsize ws; |
bb280f79 | 1181 | struct termios lock; |
88e0f3df | 1182 | #ifdef TIOCGLCKTRMIOS |
bb280f79 WF |
1183 | int i = (plymouth_command("--ping") == 0) ? 30 : 0; |
1184 | ||
1185 | while (i-- > 0) { | |
1186 | /* | |
1187 | * Even with TTYReset=no it seems with systemd or plymouth | |
1188 | * the termios flags become changed from under the first | |
1189 | * agetty on a serial system console as the flags are locked. | |
1190 | */ | |
1191 | memset(&lock, 0, sizeof(struct termios)); | |
1192 | if (ioctl(STDIN_FILENO, TIOCGLCKTRMIOS, &lock) < 0) | |
1193 | break; | |
1194 | if (!lock.c_iflag && !lock.c_oflag && !lock.c_cflag && !lock.c_lflag) | |
1195 | break; | |
1196 | debug("termios locked\n"); | |
1197 | if (i == 15 && plymouth_command("quit") != 0) | |
1198 | break; | |
1199 | sleep(1); | |
1200 | } | |
1201 | memset(&lock, 0, sizeof(struct termios)); | |
1202 | ioctl(STDIN_FILENO, TIOCSLCKTRMIOS, &lock); | |
88e0f3df | 1203 | #endif |
70bedfa1 | 1204 | |
0da025da WF |
1205 | if (op->flags & F_VCONSOLE) { |
1206 | #if defined(IUTF8) && defined(KDGKBMODE) | |
b9c73909 | 1207 | switch(op->kbmode) { |
0da025da | 1208 | case K_UNICODE: |
b0198a11 | 1209 | setlocale(LC_CTYPE, "C.UTF-8"); |
0da025da WF |
1210 | op->flags |= F_UTF8; |
1211 | break; | |
1212 | case K_RAW: | |
1213 | case K_MEDIUMRAW: | |
1214 | case K_XLATE: | |
1215 | default: | |
1216 | setlocale(LC_CTYPE, "POSIX"); | |
1217 | op->flags &= ~F_UTF8; | |
1218 | break; | |
1219 | } | |
1220 | #else | |
1221 | setlocale(LC_CTYPE, "POSIX"); | |
1222 | op->flags &= ~F_UTF8; | |
1223 | #endif | |
1224 | reset_vc(op, tp); | |
1225 | ||
1226 | if ((tp->c_cflag & (CS8|PARODD|PARENB)) == CS8) | |
1227 | op->flags |= F_EIGHTBITS; | |
1228 | ||
6443dd43 SW |
1229 | if ((op->flags & F_NOCLEAR) == 0) |
1230 | termio_clear(STDOUT_FILENO); | |
0da025da WF |
1231 | return; |
1232 | } | |
1233 | ||
8410cdd3 KZ |
1234 | /* |
1235 | * Serial line | |
1236 | */ | |
1237 | ||
1238 | if (op->flags & F_KEEPSPEED || !op->numspeed) { | |
53d55042 SK |
1239 | /* Save the original setting. */ |
1240 | ispeed = cfgetispeed(tp); | |
70bedfa1 | 1241 | ospeed = cfgetospeed(tp); |
1683f6bf DWF |
1242 | |
1243 | if (!ispeed) ispeed = TTYDEF_SPEED; | |
1244 | if (!ospeed) ospeed = TTYDEF_SPEED; | |
1245 | ||
53d55042 | 1246 | } else { |
70bedfa1 | 1247 | ospeed = ispeed = op->speeds[FIRST_SPEED]; |
53d55042 | 1248 | } |
70bedfa1 KZ |
1249 | |
1250 | /* | |
1251 | * Initial termios settings: 8-bit characters, raw-mode, blocking i/o. | |
1252 | * Special characters are set after we have read the login name; all | |
1253 | * reads will be done in raw mode anyway. Errors will be dealt with | |
53d55042 | 1254 | * later on. |
70bedfa1 | 1255 | */ |
53d55042 | 1256 | |
8408647d WF |
1257 | #ifdef IUTF8 |
1258 | tp->c_iflag = tp->c_iflag & IUTF8; | |
7478fce0 ST |
1259 | if (tp->c_iflag & IUTF8) |
1260 | op->flags |= F_UTF8; | |
8408647d WF |
1261 | #else |
1262 | tp->c_iflag = 0; | |
1263 | #endif | |
9c62a232 DJ |
1264 | tp->c_lflag = 0; |
1265 | tp->c_oflag &= OPOST | ONLCR; | |
70bedfa1 | 1266 | |
1683f6bf | 1267 | if ((op->flags & F_KEEPCFLAGS) == 0) |
70bedfa1 KZ |
1268 | tp->c_cflag = CS8 | HUPCL | CREAD | (tp->c_cflag & CLOCAL); |
1269 | ||
53d55042 SK |
1270 | /* |
1271 | * Note that the speed is stored in the c_cflag termios field, so we have | |
70bedfa1 KZ |
1272 | * set the speed always when the cflag se reseted. |
1273 | */ | |
1274 | cfsetispeed(tp, ispeed); | |
1275 | cfsetospeed(tp, ospeed); | |
1276 | ||
ef264c83 KZ |
1277 | /* The default is to follow setting from kernel, but it's possible |
1278 | * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/ | |
1279 | switch (op->clocal) { | |
1280 | case CLOCAL_MODE_ALWAYS: | |
1281 | tp->c_cflag |= CLOCAL; /* -L or -L=always */ | |
1282 | break; | |
1283 | case CLOCAL_MODE_NEVER: | |
1284 | tp->c_cflag &= ~CLOCAL; /* -L=never */ | |
1285 | break; | |
1286 | case CLOCAL_MODE_AUTO: /* -L=auto */ | |
1287 | break; | |
1288 | } | |
1289 | ||
30e8b186 | 1290 | #ifdef HAVE_STRUCT_TERMIOS_C_LINE |
70bedfa1 | 1291 | tp->c_line = 0; |
1961482a | 1292 | #endif |
70bedfa1 KZ |
1293 | tp->c_cc[VMIN] = 1; |
1294 | tp->c_cc[VTIME] = 0; | |
7eda085c | 1295 | |
1683f6bf DWF |
1296 | /* Check for terminal size and if not found set default */ |
1297 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == 0) { | |
1298 | int set = 0; | |
1299 | if (ws.ws_row == 0) { | |
1300 | ws.ws_row = 24; | |
1301 | set++; | |
1302 | } | |
1303 | if (ws.ws_col == 0) { | |
1304 | ws.ws_col = 80; | |
1305 | set++; | |
1306 | } | |
efcf26f4 KZ |
1307 | if (ioctl(STDIN_FILENO, TIOCSWINSZ, &ws)) |
1308 | debug("TIOCSWINSZ ioctl failed\n"); | |
1683f6bf DWF |
1309 | } |
1310 | ||
53d55042 | 1311 | /* Optionally enable hardware flow control. */ |
7eda085c | 1312 | #ifdef CRTSCTS |
70bedfa1 KZ |
1313 | if (op->flags & F_RTSCTS) |
1314 | tp->c_cflag |= CRTSCTS; | |
7eda085c | 1315 | #endif |
bb280f79 WF |
1316 | /* Flush input and output queues, important for modems! */ |
1317 | tcflush(STDIN_FILENO, TCIOFLUSH); | |
7eda085c | 1318 | |
bb280f79 WF |
1319 | if (tcsetattr(STDIN_FILENO, TCSANOW, tp)) |
1320 | log_warn(_("setting terminal attributes failed: %m")); | |
fd6b7a7f | 1321 | |
53d55042 SK |
1322 | /* Go to blocking input even in local mode. */ |
1323 | fcntl(STDIN_FILENO, F_SETFL, | |
1324 | fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK); | |
fd6b7a7f | 1325 | |
70bedfa1 | 1326 | debug("term_io 2\n"); |
6dbe3af9 KZ |
1327 | } |
1328 | ||
0da025da WF |
1329 | /* Reset virtual console on stdin to its defaults */ |
1330 | static void reset_vc(const struct options *op, struct termios *tp) | |
1331 | { | |
879a7ab4 KZ |
1332 | int fl = 0; |
1333 | ||
1334 | fl |= (op->flags & F_KEEPCFLAGS) == 0 ? 0 : UL_TTY_KEEPCFLAGS; | |
1335 | fl |= (op->flags & F_UTF8) == 0 ? 0 : UL_TTY_UTF8; | |
1336 | ||
1337 | reset_virtual_console(tp, fl); | |
0da025da | 1338 | |
0da025da | 1339 | if (tcsetattr(STDIN_FILENO, TCSADRAIN, tp)) |
8c219bf4 | 1340 | log_warn(_("setting terminal attributes failed: %m")); |
bb280f79 WF |
1341 | |
1342 | /* Go to blocking input even in local mode. */ | |
1343 | fcntl(STDIN_FILENO, F_SETFL, | |
1344 | fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK); | |
0da025da WF |
1345 | } |
1346 | ||
53d55042 | 1347 | /* Extract baud rate from modem status message. */ |
bde20e1b | 1348 | static void auto_baud(struct termios *tp) |
6dbe3af9 | 1349 | { |
bde20e1b | 1350 | speed_t speed; |
53d55042 | 1351 | int vmin; |
70bedfa1 | 1352 | unsigned iflag; |
53d55042 SK |
1353 | char buf[BUFSIZ]; |
1354 | char *bp; | |
1355 | int nread; | |
70bedfa1 KZ |
1356 | |
1357 | /* | |
1358 | * This works only if the modem produces its status code AFTER raising | |
1359 | * the DCD line, and if the computer is fast enough to set the proper | |
1360 | * baud rate before the message has gone by. We expect a message of the | |
1361 | * following format: | |
1362 | * | |
1363 | * <junk><number><junk> | |
1364 | * | |
1365 | * The number is interpreted as the baud rate of the incoming call. If the | |
1366 | * modem does not tell us the baud rate within one second, we will keep | |
1367 | * using the current baud rate. It is advisable to enable BREAK | |
1368 | * processing (comma-separated list of baud rates) if the processing of | |
1369 | * modem status messages is enabled. | |
1370 | */ | |
1371 | ||
1372 | /* | |
1373 | * Use 7-bit characters, don't block if input queue is empty. Errors will | |
53d55042 | 1374 | * be dealt with later on. |
70bedfa1 | 1375 | */ |
70bedfa1 | 1376 | iflag = tp->c_iflag; |
53d55042 SK |
1377 | /* Enable 8th-bit stripping. */ |
1378 | tp->c_iflag |= ISTRIP; | |
70bedfa1 | 1379 | vmin = tp->c_cc[VMIN]; |
53d55042 SK |
1380 | /* Do not block when queue is empty. */ |
1381 | tp->c_cc[VMIN] = 0; | |
70bedfa1 KZ |
1382 | tcsetattr(STDIN_FILENO, TCSANOW, tp); |
1383 | ||
1384 | /* | |
1385 | * Wait for a while, then read everything the modem has said so far and | |
1386 | * try to extract the speed of the dial-in call. | |
1387 | */ | |
53d55042 | 1388 | sleep(1); |
70bedfa1 KZ |
1389 | if ((nread = read(STDIN_FILENO, buf, sizeof(buf) - 1)) > 0) { |
1390 | buf[nread] = '\0'; | |
53d55042 | 1391 | for (bp = buf; bp < buf + nread; bp++) |
70bedfa1 KZ |
1392 | if (isascii(*bp) && isdigit(*bp)) { |
1393 | if ((speed = bcode(bp))) { | |
1394 | cfsetispeed(tp, speed); | |
1395 | cfsetospeed(tp, speed); | |
1396 | } | |
1397 | break; | |
1398 | } | |
6dbe3af9 | 1399 | } |
6dbe3af9 | 1400 | |
53d55042 | 1401 | /* Restore terminal settings. Errors will be dealt with later on. */ |
70bedfa1 KZ |
1402 | tp->c_iflag = iflag; |
1403 | tp->c_cc[VMIN] = vmin; | |
53d55042 | 1404 | tcsetattr(STDIN_FILENO, TCSANOW, tp); |
6dbe3af9 KZ |
1405 | } |
1406 | ||
2448f336 KZ |
1407 | static char *xgethostname(void) |
1408 | { | |
1409 | char *name; | |
1410 | size_t sz = get_hostname_max() + 1; | |
1411 | ||
1412 | name = malloc(sizeof(char) * sz); | |
1413 | if (!name) | |
1414 | log_err(_("failed to allocate memory: %m")); | |
1415 | ||
8362545b KZ |
1416 | if (gethostname(name, sz) != 0) { |
1417 | free(name); | |
2448f336 | 1418 | return NULL; |
8362545b | 1419 | } |
2448f336 KZ |
1420 | name[sz - 1] = '\0'; |
1421 | return name; | |
1422 | } | |
1423 | ||
1424 | static char *xgetdomainname(void) | |
1425 | { | |
1426 | #ifdef HAVE_GETDOMAINNAME | |
1427 | char *name; | |
1428 | size_t sz = get_hostname_max() + 1; | |
1429 | ||
1430 | name = malloc(sizeof(char) * sz); | |
1431 | if (!name) | |
1432 | log_err(_("failed to allocate memory: %m")); | |
1433 | ||
8362545b KZ |
1434 | if (getdomainname(name, sz) != 0) { |
1435 | free(name); | |
2448f336 | 1436 | return NULL; |
8362545b | 1437 | } |
2448f336 KZ |
1438 | name[sz - 1] = '\0'; |
1439 | return name; | |
1440 | #endif | |
1441 | return NULL; | |
1442 | } | |
1443 | ||
b34f097e KZ |
1444 | static char *read_os_release(struct options *op, const char *varname) |
1445 | { | |
1446 | int fd = -1; | |
1447 | struct stat st; | |
1448 | size_t varsz = strlen(varname); | |
1449 | char *p, *buf = NULL, *ret = NULL; | |
1450 | ||
1451 | /* read the file only once */ | |
1452 | if (!op->osrelease) { | |
b28842ae | 1453 | fd = open(_PATH_OS_RELEASE_ETC, O_RDONLY); |
b34f097e | 1454 | if (fd == -1) { |
b28842ae KZ |
1455 | fd = open(_PATH_OS_RELEASE_USR, O_RDONLY); |
1456 | if (fd == -1) { | |
1457 | log_warn(_("cannot open os-release file")); | |
1458 | return NULL; | |
1459 | } | |
b34f097e KZ |
1460 | } |
1461 | ||
1462 | if (fstat(fd, &st) < 0 || st.st_size > 4 * 1024 * 1024) | |
1463 | goto done; | |
1464 | ||
1465 | op->osrelease = malloc(st.st_size + 1); | |
1466 | if (!op->osrelease) | |
1467 | log_err(_("failed to allocate memory: %m")); | |
1468 | if (read_all(fd, op->osrelease, st.st_size) != (ssize_t) st.st_size) { | |
1469 | free(op->osrelease); | |
1470 | op->osrelease = NULL; | |
1471 | goto done; | |
1472 | } | |
1473 | op->osrelease[st.st_size] = 0; | |
1474 | } | |
1475 | buf = strdup(op->osrelease); | |
1476 | if (!buf) | |
1477 | log_err(_("failed to allocate memory: %m")); | |
1478 | p = buf; | |
1479 | ||
1480 | for (;;) { | |
1481 | char *eol, *eon; | |
1482 | ||
1483 | p += strspn(p, "\n\r"); | |
1484 | p += strspn(p, " \t\n\r"); | |
1485 | if (!*p) | |
1486 | break; | |
1487 | if (strspn(p, "#;\n") != 0) { | |
1488 | p += strcspn(p, "\n\r"); | |
1489 | continue; | |
1490 | } | |
1491 | if (strncmp(p, varname, varsz) != 0) { | |
1492 | p += strcspn(p, "\n\r"); | |
1493 | continue; | |
1494 | } | |
1495 | p += varsz; | |
1496 | p += strspn(p, " \t\n\r=\""); | |
1497 | eol = p + strcspn(p, "\n\r"); | |
1498 | *eol = '\0'; | |
1499 | eon = eol-1; | |
1500 | while (eon > p) { | |
1501 | if (*eon == '\t' || *eon == ' ') { | |
1502 | eon--; | |
1503 | continue; | |
1504 | } | |
1505 | if (*eon == '"') { | |
1506 | *eon = '\0'; | |
1507 | break; | |
1508 | } | |
1509 | break; | |
1510 | } | |
f614b73c | 1511 | free(ret); |
b34f097e KZ |
1512 | ret = strdup(p); |
1513 | if (!ret) | |
1514 | log_err(_("failed to allocate memory: %m")); | |
1515 | p = eol + 1; | |
1516 | } | |
1517 | done: | |
1518 | free(buf); | |
1519 | if (fd >= 0) | |
1520 | close(fd); | |
1521 | return ret; | |
1522 | } | |
1523 | ||
c2ef308b | 1524 | #ifdef AGETTY_RELOAD |
e36deb64 SW |
1525 | static void open_netlink(void) |
1526 | { | |
1527 | struct sockaddr_nl addr = { 0, }; | |
1528 | int sock; | |
1529 | ||
1530 | if (netlink_fd != AGETTY_RELOAD_FDNONE) | |
1531 | return; | |
1532 | ||
1533 | sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | |
1534 | if (sock >= 0) { | |
1535 | addr.nl_family = AF_NETLINK; | |
1536 | addr.nl_pid = getpid(); | |
1537 | addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR; | |
1538 | if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) | |
1539 | close(sock); | |
1540 | else | |
1541 | netlink_fd = sock; | |
1542 | } | |
1543 | } | |
1544 | ||
1545 | static int process_netlink_msg(int *changed) | |
1546 | { | |
1547 | char buf[4096]; | |
1548 | struct sockaddr_nl snl; | |
1549 | struct nlmsghdr *h; | |
1550 | int rc; | |
1551 | ||
1552 | struct iovec iov = { | |
1553 | .iov_base = buf, | |
1554 | .iov_len = sizeof(buf) | |
1555 | }; | |
1556 | struct msghdr msg = { | |
1557 | .msg_name = &snl, | |
1558 | .msg_namelen = sizeof(snl), | |
1559 | .msg_iov = &iov, | |
1560 | .msg_iovlen = 1, | |
1561 | .msg_control = NULL, | |
1562 | .msg_controllen = 0, | |
1563 | .msg_flags = 0 | |
1564 | }; | |
1565 | ||
1566 | rc = recvmsg(netlink_fd, &msg, MSG_DONTWAIT); | |
1567 | if (rc < 0) { | |
1568 | if (errno == EWOULDBLOCK || errno == EAGAIN) | |
1569 | return 0; | |
1570 | ||
1571 | /* Failure, just stop listening for changes */ | |
1572 | close(netlink_fd); | |
1573 | netlink_fd = AGETTY_RELOAD_FDNONE; | |
1574 | return 0; | |
1575 | } | |
1576 | ||
1577 | for (h = (struct nlmsghdr *)buf; NLMSG_OK(h, (unsigned int)rc); h = NLMSG_NEXT(h, rc)) { | |
1578 | if (h->nlmsg_type == NLMSG_DONE || | |
1579 | h->nlmsg_type == NLMSG_ERROR) { | |
1580 | close(netlink_fd); | |
1581 | netlink_fd = AGETTY_RELOAD_FDNONE; | |
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | *changed = 1; | |
1586 | break; | |
1587 | } | |
1588 | ||
1589 | return 1; | |
1590 | } | |
1591 | ||
1592 | static int process_netlink(void) | |
1593 | { | |
1594 | int changed = 0; | |
1595 | while (process_netlink_msg(&changed)); | |
1596 | return changed; | |
1597 | } | |
1598 | ||
c2ef308b KZ |
1599 | static int wait_for_term_input(int fd) |
1600 | { | |
1601 | char buffer[sizeof(struct inotify_event) + NAME_MAX + 1]; | |
1602 | struct termios orig, nonc; | |
1603 | fd_set rfds; | |
1604 | int count, i; | |
1605 | ||
1606 | /* Our aim here is to fall through if something fails | |
1607 | * and not be stuck waiting. On failure assume we have input */ | |
1608 | ||
1609 | if (tcgetattr(fd, &orig) != 0) | |
1610 | return 1; | |
1611 | ||
1612 | memcpy(&nonc, &orig, sizeof (nonc)); | |
1613 | nonc.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHOKE); | |
1614 | nonc.c_cc[VMIN] = 1; | |
1615 | nonc.c_cc[VTIME] = 0; | |
1616 | ||
1617 | if (tcsetattr(fd, TCSANOW, &nonc) != 0) | |
1618 | return 1; | |
1619 | ||
1620 | if (inotify_fd == AGETTY_RELOAD_FDNONE) { | |
1621 | /* make sure the reload trigger file exists */ | |
1622 | int reload_fd = open(AGETTY_RELOAD_FILENAME, | |
1623 | O_CREAT|O_CLOEXEC|O_RDONLY, | |
1624 | S_IRUSR|S_IWUSR); | |
1625 | ||
1626 | /* initialize reload trigger inotify stuff */ | |
1627 | if (reload_fd >= 0) { | |
1628 | inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); | |
1629 | if (inotify_fd > 0) | |
1630 | inotify_add_watch(inotify_fd, AGETTY_RELOAD_FILENAME, | |
1631 | IN_ATTRIB | IN_MODIFY); | |
1632 | ||
1633 | close(reload_fd); | |
1634 | } else | |
1635 | log_warn(_("failed to create reload file: %s: %m"), | |
1636 | AGETTY_RELOAD_FILENAME); | |
1637 | } | |
1638 | ||
e36deb64 | 1639 | while (1) { |
5184d936 KZ |
1640 | int nfds = fd; |
1641 | ||
e36deb64 SW |
1642 | FD_ZERO(&rfds); |
1643 | FD_SET(fd, &rfds); | |
c2ef308b | 1644 | |
5184d936 | 1645 | if (inotify_fd >= 0) { |
e36deb64 | 1646 | FD_SET(inotify_fd, &rfds); |
5184d936 KZ |
1647 | nfds = max(nfds, inotify_fd); |
1648 | } | |
1649 | if (netlink_fd >= 0) { | |
e36deb64 | 1650 | FD_SET(netlink_fd, &rfds); |
5184d936 KZ |
1651 | nfds = max(nfds, netlink_fd); |
1652 | } | |
c2ef308b | 1653 | |
e36deb64 | 1654 | /* If waiting fails, just fall through, presumably reading input will fail */ |
5184d936 | 1655 | if (select(nfds + 1, &rfds, NULL, NULL, NULL) < 0) |
e36deb64 | 1656 | return 1; |
c2ef308b | 1657 | |
e36deb64 SW |
1658 | if (FD_ISSET(fd, &rfds)) { |
1659 | count = read(fd, buffer, sizeof (buffer)); | |
c2ef308b | 1660 | |
e36deb64 | 1661 | tcsetattr(fd, TCSANOW, &orig); |
c2ef308b | 1662 | |
e36deb64 SW |
1663 | /* Reinject the bytes we read back into the buffer, usually just one byte */ |
1664 | for (i = 0; i < count; i++) | |
1665 | ioctl(fd, TIOCSTI, buffer + i); | |
c2ef308b | 1666 | |
e36deb64 SW |
1667 | /* Have terminal input */ |
1668 | return 1; | |
c2ef308b | 1669 | |
e36deb64 SW |
1670 | } else if (netlink_fd >= 0 && FD_ISSET(netlink_fd, &rfds)) { |
1671 | if (!process_netlink()) | |
1672 | continue; | |
c2ef308b KZ |
1673 | |
1674 | /* Just drain the inotify buffer */ | |
e36deb64 SW |
1675 | } else if (inotify_fd >= 0 && FD_ISSET(inotify_fd, &rfds)) { |
1676 | while (read(inotify_fd, buffer, sizeof (buffer)) > 0); | |
1677 | } | |
1678 | ||
1679 | tcsetattr(fd, TCSANOW, &orig); | |
c2ef308b KZ |
1680 | |
1681 | /* Need to reprompt */ | |
1682 | return 0; | |
1683 | } | |
1684 | } | |
1685 | #endif /* AGETTY_RELOAD */ | |
1686 | static void print_issue_file(struct options *op, struct termios *tp) | |
6dbe3af9 KZ |
1687 | { |
1688 | #ifdef ISSUE | |
53d55042 | 1689 | FILE *fd; |
c2ef308b | 1690 | #endif |
e85281a8 DWF |
1691 | if ((op->flags & F_NONL) == 0) { |
1692 | /* Issue not in use, start with a new line. */ | |
1693 | write_all(STDOUT_FILENO, "\r\n", 2); | |
1694 | } | |
3aa6b68f | 1695 | |
53d55042 | 1696 | #ifdef ISSUE |
70bedfa1 | 1697 | if ((op->flags & F_ISSUE) && (fd = fopen(op->issue, "r"))) { |
1683f6bf | 1698 | int c, oflag = tp->c_oflag; /* Save current setting. */ |
3aa6b68f | 1699 | |
1683f6bf DWF |
1700 | if ((op->flags & F_VCONSOLE) == 0) { |
1701 | /* Map new line in output to carriage return & new line. */ | |
1702 | tp->c_oflag |= (ONLCR | OPOST); | |
1703 | tcsetattr(STDIN_FILENO, TCSADRAIN, tp); | |
1704 | } | |
70bedfa1 KZ |
1705 | |
1706 | while ((c = getc(fd)) != EOF) { | |
729def03 | 1707 | if (c == '\\') |
2b945eda | 1708 | output_special_char(getc(fd), op, tp, fd); |
729def03 | 1709 | else |
763d7a87 | 1710 | putchar(c); |
70bedfa1 KZ |
1711 | } |
1712 | fflush(stdout); | |
6dbe3af9 | 1713 | |
1683f6bf DWF |
1714 | if ((op->flags & F_VCONSOLE) == 0) { |
1715 | /* Restore settings. */ | |
1716 | tp->c_oflag = oflag; | |
1717 | /* Wait till output is gone. */ | |
1718 | tcsetattr(STDIN_FILENO, TCSADRAIN, tp); | |
1719 | } | |
53d55042 | 1720 | fclose(fd); |
6dbe3af9 | 1721 | } |
eb8e1f9f | 1722 | #endif /* ISSUE */ |
c2ef308b KZ |
1723 | } |
1724 | ||
1725 | /* Show login prompt, optionally preceded by /etc/issue contents. */ | |
1726 | static void do_prompt(struct options *op, struct termios *tp) | |
1727 | { | |
1728 | again: | |
1729 | print_issue_file(op, tp); | |
1730 | ||
eb8e1f9f | 1731 | if (op->flags & F_LOGINPAUSE) { |
8c219bf4 | 1732 | puts(_("[press ENTER to login]")); |
c2ef308b KZ |
1733 | #ifdef AGETTY_RELOAD |
1734 | if (!wait_for_term_input(STDIN_FILENO)) { | |
1735 | /* reload issue */ | |
1736 | if (op->flags & F_VCONSOLE) | |
1737 | termio_clear(STDOUT_FILENO); | |
1738 | goto again; | |
1739 | } | |
1740 | #endif | |
eb8e1f9f WF |
1741 | getc(stdin); |
1742 | } | |
1743 | #ifdef KDGKBLED | |
36601b23 KZ |
1744 | if (!(op->flags & F_NOHINTS) && !op->autolog && |
1745 | (op->flags & F_VCONSOLE)) { | |
a694957a KZ |
1746 | int kb = 0; |
1747 | ||
eb8e1f9f | 1748 | if (ioctl(STDIN_FILENO, KDGKBLED, &kb) == 0) { |
a694957a KZ |
1749 | char hint[256] = { '\0' }; |
1750 | int nl = 0; | |
a694957a | 1751 | |
01c5b787 | 1752 | if (access(_PATH_NUMLOCK_ON, F_OK) == 0) |
a694957a KZ |
1753 | nl = 1; |
1754 | ||
1755 | if (nl && (kb & 0x02) == 0) | |
1756 | append(hint, sizeof(hint), NULL, _("Num Lock off")); | |
1757 | ||
1758 | else if (nl == 0 && (kb & 2) && (kb & 0x20) == 0) | |
1759 | append(hint, sizeof(hint), NULL, _("Num Lock on")); | |
1760 | ||
1761 | if ((kb & 0x04) && (kb & 0x40) == 0) | |
1762 | append(hint, sizeof(hint), ", ", _("Caps Lock on")); | |
1763 | ||
1764 | if ((kb & 0x01) && (kb & 0x10) == 0) | |
1765 | append(hint, sizeof(hint), ", ", _("Scroll Lock on")); | |
1766 | ||
1767 | if (*hint) | |
1768 | printf(_("Hint: %s\n\n"), hint); | |
eb8e1f9f WF |
1769 | } |
1770 | } | |
1771 | #endif /* KDGKBLED */ | |
e85281a8 | 1772 | if ((op->flags & F_NOHOSTNAME) == 0) { |
2448f336 KZ |
1773 | char *hn = xgethostname(); |
1774 | ||
1775 | if (hn) { | |
e85281a8 | 1776 | char *dot = strchr(hn, '.'); |
74b3df85 SK |
1777 | char *cn = hn; |
1778 | struct addrinfo *res = NULL; | |
e85281a8 | 1779 | |
e85281a8 DWF |
1780 | if ((op->flags & F_LONGHNAME) == 0) { |
1781 | if (dot) | |
1782 | *dot = '\0'; | |
74b3df85 SK |
1783 | |
1784 | } else if (dot == NULL) { | |
1785 | struct addrinfo hints; | |
1786 | ||
1787 | memset(&hints, 0, sizeof(hints)); | |
1788 | hints.ai_flags = AI_CANONNAME; | |
1789 | ||
1790 | if (!getaddrinfo(hn, NULL, &hints, &res) | |
1791 | && res && res->ai_canonname) | |
1792 | cn = res->ai_canonname; | |
1793 | } | |
1794 | ||
1795 | write_all(STDOUT_FILENO, cn, strlen(cn)); | |
e85281a8 | 1796 | write_all(STDOUT_FILENO, " ", 1); |
74b3df85 SK |
1797 | |
1798 | if (res) | |
1799 | freeaddrinfo(res); | |
2448f336 | 1800 | free(hn); |
e85281a8 | 1801 | } |
70bedfa1 | 1802 | } |
933956cb | 1803 | if (!op->autolog) { |
eb8e1f9f WF |
1804 | /* Always show login prompt. */ |
1805 | write_all(STDOUT_FILENO, LOGIN, sizeof(LOGIN) - 1); | |
1806 | } | |
6dbe3af9 KZ |
1807 | } |
1808 | ||
53d55042 | 1809 | /* Select next baud rate. */ |
bde20e1b | 1810 | static void next_speed(struct options *op, struct termios *tp) |
6dbe3af9 | 1811 | { |
70bedfa1 KZ |
1812 | static int baud_index = -1; |
1813 | ||
1814 | if (baud_index == -1) | |
1815 | /* | |
53d55042 | 1816 | * If the F_KEEPSPEED flags is set then the FIRST_SPEED is not |
70bedfa1 KZ |
1817 | * tested yet (see termio_init()). |
1818 | */ | |
53d55042 SK |
1819 | baud_index = |
1820 | (op->flags & F_KEEPSPEED) ? FIRST_SPEED : 1 % op->numspeed; | |
70bedfa1 KZ |
1821 | else |
1822 | baud_index = (baud_index + 1) % op->numspeed; | |
1823 | ||
1824 | cfsetispeed(tp, op->speeds[baud_index]); | |
1825 | cfsetospeed(tp, op->speeds[baud_index]); | |
53d55042 | 1826 | tcsetattr(STDIN_FILENO, TCSANOW, tp); |
6dbe3af9 KZ |
1827 | } |
1828 | ||
53d55042 | 1829 | /* Get user name, establish parity, speed, erase, kill & eol. */ |
bde20e1b | 1830 | static char *get_logname(struct options *op, struct termios *tp, struct chardata *cp) |
6dbe3af9 | 1831 | { |
70bedfa1 | 1832 | static char logname[BUFSIZ]; |
53d55042 SK |
1833 | char *bp; |
1834 | char c; /* input character, full eight bits */ | |
1835 | char ascval; /* low 7 bits of input character */ | |
1683f6bf | 1836 | int eightbit; |
53d55042 SK |
1837 | static char *erase[] = { /* backspace-space-backspace */ |
1838 | "\010\040\010", /* space parity */ | |
1839 | "\010\040\010", /* odd parity */ | |
1840 | "\210\240\210", /* even parity */ | |
1841 | "\210\240\210", /* no parity */ | |
70bedfa1 KZ |
1842 | }; |
1843 | ||
1844 | /* Initialize kill, erase, parity etc. (also after switching speeds). */ | |
f5664477 | 1845 | INIT_CHARDATA(cp); |
70bedfa1 | 1846 | |
53d55042 SK |
1847 | /* |
1848 | * Flush pending input (especially important after parsing or switching | |
1849 | * the baud rate). | |
1850 | */ | |
1683f6bf DWF |
1851 | if ((op->flags & F_VCONSOLE) == 0) |
1852 | sleep(1); | |
53d55042 | 1853 | tcflush(STDIN_FILENO, TCIFLUSH); |
70bedfa1 | 1854 | |
1683f6bf DWF |
1855 | eightbit = (op->flags & F_EIGHTBITS); |
1856 | bp = logname; | |
1857 | *bp = '\0'; | |
1858 | ||
1859 | while (*logname == '\0') { | |
1860 | ||
1861 | /* Write issue file and prompt */ | |
70bedfa1 KZ |
1862 | do_prompt(op, tp); |
1863 | ||
6443dd43 SW |
1864 | #ifdef AGETTY_RELOAD |
1865 | /* If asked to reprompt *before* terminal input arrives, then do so */ | |
1866 | if (!wait_for_term_input(STDIN_FILENO)) { | |
1867 | if (op->flags & F_VCONSOLE) | |
1868 | termio_clear(STDOUT_FILENO); | |
1869 | continue; | |
1870 | } | |
1871 | #endif | |
1683f6bf DWF |
1872 | cp->eol = '\0'; |
1873 | ||
1874 | /* Read name, watch for break and end-of-line. */ | |
1875 | while (cp->eol == '\0') { | |
1876 | ||
cb872ac9 KZ |
1877 | char key; |
1878 | ||
763d7a87 | 1879 | if (read(STDIN_FILENO, &c, 1) < 1) { |
1683f6bf | 1880 | |
714cff30 KZ |
1881 | /* The terminal could be open with O_NONBLOCK when |
1882 | * -L (force CLOCAL) is specified... */ | |
1683f6bf | 1883 | if (errno == EINTR || errno == EAGAIN) { |
a5bd7939 | 1884 | xusleep(250000); |
1683f6bf DWF |
1885 | continue; |
1886 | } | |
1887 | switch (errno) { | |
1888 | case 0: | |
1889 | case EIO: | |
1890 | case ESRCH: | |
1891 | case EINVAL: | |
1892 | case ENOENT: | |
1893 | break; | |
1894 | default: | |
1895 | log_err(_("%s: read: %m"), op->tty); | |
1896 | } | |
70bedfa1 | 1897 | } |
1683f6bf | 1898 | |
70bedfa1 | 1899 | /* Do parity bit handling. */ |
1683f6bf | 1900 | if (eightbit) |
70bedfa1 | 1901 | ascval = c; |
1683f6bf DWF |
1902 | else if (c != (ascval = (c & 0177))) { |
1903 | uint32_t bits; /* # of "1" bits per character */ | |
1904 | uint32_t mask; /* mask with 1 bit up */ | |
1905 | for (bits = 1, mask = 1; mask & 0177; mask <<= 1) { | |
70bedfa1 | 1906 | if (mask & ascval) |
53d55042 | 1907 | bits++; |
1683f6bf | 1908 | } |
70bedfa1 KZ |
1909 | cp->parity |= ((bits & 1) ? 1 : 2); |
1910 | } | |
1683f6bf | 1911 | |
cb872ac9 KZ |
1912 | if (op->killchars && strchr(op->killchars, ascval)) |
1913 | key = CTL('U'); | |
1914 | else if (op->erasechars && strchr(op->erasechars, ascval)) | |
1915 | key = DEL; | |
1916 | else | |
1917 | key = ascval; | |
1918 | ||
70bedfa1 | 1919 | /* Do erase, kill and end-of-line processing. */ |
cb872ac9 | 1920 | switch (key) { |
1683f6bf DWF |
1921 | case 0: |
1922 | *bp = 0; | |
1923 | if (op->numspeed > 1) | |
4a9b7543 | 1924 | return NULL; |
1683f6bf | 1925 | break; |
70bedfa1 KZ |
1926 | case CR: |
1927 | case NL: | |
1683f6bf DWF |
1928 | *bp = 0; /* terminate logname */ |
1929 | cp->eol = ascval; /* set end-of-line char */ | |
70bedfa1 KZ |
1930 | break; |
1931 | case BS: | |
1932 | case DEL: | |
1683f6bf | 1933 | cp->erase = ascval; /* set erase character */ |
70bedfa1 | 1934 | if (bp > logname) { |
b9261127 | 1935 | if ((tp->c_lflag & ECHO) == 0) |
1683f6bf | 1936 | write_all(1, erase[cp->parity], 3); |
70bedfa1 KZ |
1937 | bp--; |
1938 | } | |
1939 | break; | |
1940 | case CTL('U'): | |
1683f6bf | 1941 | cp->kill = ascval; /* set kill character */ |
70bedfa1 | 1942 | while (bp > logname) { |
b9261127 | 1943 | if ((tp->c_lflag & ECHO) == 0) |
1683f6bf | 1944 | write_all(1, erase[cp->parity], 3); |
70bedfa1 KZ |
1945 | bp--; |
1946 | } | |
1947 | break; | |
1948 | case CTL('D'): | |
1949 | exit(EXIT_SUCCESS); | |
1950 | default: | |
1683f6bf DWF |
1951 | if (!isascii(ascval) || !isprint(ascval)) |
1952 | break; | |
1953 | if ((size_t)(bp - logname) >= sizeof(logname) - 1) | |
bde20e1b | 1954 | log_err(_("%s: input overrun"), op->tty); |
b9261127 | 1955 | if ((tp->c_lflag & ECHO) == 0) |
1683f6bf DWF |
1956 | write_all(1, &c, 1); /* echo the character */ |
1957 | *bp++ = ascval; /* and store it */ | |
70bedfa1 KZ |
1958 | break; |
1959 | } | |
6dbe3af9 | 1960 | } |
6dbe3af9 | 1961 | } |
1683f6bf DWF |
1962 | #ifdef HAVE_WIDECHAR |
1963 | if ((op->flags & (F_EIGHTBITS|F_UTF8)) == (F_EIGHTBITS|F_UTF8)) { | |
1964 | /* Check out UTF-8 multibyte characters */ | |
1965 | ssize_t len; | |
1966 | wchar_t *wcs, *wcp; | |
1967 | ||
1968 | len = mbstowcs((wchar_t *)0, logname, 0); | |
1969 | if (len < 0) | |
8c219bf4 | 1970 | log_err(_("%s: invalid character conversion for login name"), op->tty); |
1683f6bf | 1971 | |
ca8e91a4 KZ |
1972 | wcs = (wchar_t *) malloc((len + 1) * sizeof(wchar_t)); |
1973 | if (!wcs) | |
1974 | log_err(_("failed to allocate memory: %m")); | |
1683f6bf DWF |
1975 | |
1976 | len = mbstowcs(wcs, logname, len + 1); | |
1977 | if (len < 0) | |
8c219bf4 | 1978 | log_err(_("%s: invalid character conversion for login name"), op->tty); |
1683f6bf DWF |
1979 | |
1980 | wcp = wcs; | |
1981 | while (*wcp) { | |
1982 | const wint_t wc = *wcp++; | |
1983 | if (!iswprint(wc)) | |
8c219bf4 | 1984 | log_err(_("%s: invalid character 0x%x in login name"), op->tty, wc); |
1683f6bf DWF |
1985 | } |
1986 | free(wcs); | |
1987 | } else | |
1988 | #endif | |
1989 | if ((op->flags & F_LCUC) && (cp->capslock = caps_lock(logname))) { | |
1990 | ||
1991 | /* Handle names with upper case and no lower case. */ | |
70bedfa1 KZ |
1992 | for (bp = logname; *bp; bp++) |
1993 | if (isupper(*bp)) | |
1683f6bf DWF |
1994 | *bp = tolower(*bp); /* map name to lower case */ |
1995 | } | |
1996 | ||
70bedfa1 | 1997 | return logname; |
6dbe3af9 KZ |
1998 | } |
1999 | ||
53d55042 | 2000 | /* Set the final tty mode bits. */ |
bde20e1b | 2001 | static void termio_final(struct options *op, struct termios *tp, struct chardata *cp) |
6dbe3af9 | 2002 | { |
70bedfa1 | 2003 | /* General terminal-independent stuff. */ |
53d55042 SK |
2004 | |
2005 | /* 2-way flow control */ | |
2006 | tp->c_iflag |= IXON | IXOFF; | |
2007 | tp->c_lflag |= ICANON | ISIG | ECHO | ECHOE | ECHOK | ECHOKE; | |
2008 | /* no longer| ECHOCTL | ECHOPRT */ | |
70bedfa1 KZ |
2009 | tp->c_oflag |= OPOST; |
2010 | /* tp->c_cflag = 0; */ | |
53d55042 SK |
2011 | tp->c_cc[VINTR] = DEF_INTR; |
2012 | tp->c_cc[VQUIT] = DEF_QUIT; | |
2013 | tp->c_cc[VEOF] = DEF_EOF; | |
70bedfa1 | 2014 | tp->c_cc[VEOL] = DEF_EOL; |
fd6b7a7f | 2015 | #ifdef __linux__ |
53d55042 | 2016 | tp->c_cc[VSWTC] = DEF_SWITCH; |
1961482a | 2017 | #elif defined(VSWTCH) |
53d55042 SK |
2018 | tp->c_cc[VSWTCH] = DEF_SWITCH; |
2019 | #endif /* __linux__ */ | |
6dbe3af9 | 2020 | |
70bedfa1 KZ |
2021 | /* Account for special characters seen in input. */ |
2022 | if (cp->eol == CR) { | |
53d55042 SK |
2023 | tp->c_iflag |= ICRNL; |
2024 | tp->c_oflag |= ONLCR; | |
70bedfa1 | 2025 | } |
53d55042 SK |
2026 | tp->c_cc[VERASE] = cp->erase; |
2027 | tp->c_cc[VKILL] = cp->kill; | |
70bedfa1 KZ |
2028 | |
2029 | /* Account for the presence or absence of parity bits in input. */ | |
2030 | switch (cp->parity) { | |
53d55042 SK |
2031 | case 0: |
2032 | /* space (always 0) parity */ | |
2033 | break; | |
2034 | case 1: | |
2035 | /* odd parity */ | |
2036 | tp->c_cflag |= PARODD; | |
2037 | /* do not break */ | |
2038 | case 2: | |
2039 | /* even parity */ | |
2040 | tp->c_cflag |= PARENB; | |
2041 | tp->c_iflag |= INPCK | ISTRIP; | |
2042 | /* do not break */ | |
2043 | case (1 | 2): | |
2044 | /* no parity bit */ | |
2045 | tp->c_cflag &= ~CSIZE; | |
2046 | tp->c_cflag |= CS7; | |
2047 | break; | |
70bedfa1 KZ |
2048 | } |
2049 | /* Account for upper case without lower case. */ | |
2050 | if (cp->capslock) { | |
ee519041 | 2051 | #ifdef IUCLC |
70bedfa1 | 2052 | tp->c_iflag |= IUCLC; |
ee519041 | 2053 | #endif |
b75c8134 | 2054 | #ifdef XCASE |
70bedfa1 | 2055 | tp->c_lflag |= XCASE; |
1961482a | 2056 | #endif |
ee519041 | 2057 | #ifdef OLCUC |
70bedfa1 | 2058 | tp->c_oflag |= OLCUC; |
ee519041 | 2059 | #endif |
70bedfa1 | 2060 | } |
53d55042 | 2061 | /* Optionally enable hardware flow control. */ |
6dbe3af9 | 2062 | #ifdef CRTSCTS |
70bedfa1 KZ |
2063 | if (op->flags & F_RTSCTS) |
2064 | tp->c_cflag |= CRTSCTS; | |
6dbe3af9 KZ |
2065 | #endif |
2066 | ||
53d55042 | 2067 | /* Finally, make the new settings effective. */ |
70bedfa1 | 2068 | if (tcsetattr(STDIN_FILENO, TCSANOW, tp) < 0) |
8c219bf4 | 2069 | log_err(_("%s: failed to set terminal attributes: %m"), op->tty); |
6dbe3af9 KZ |
2070 | } |
2071 | ||
53d55042 SK |
2072 | /* |
2073 | * String contains upper case without lower case. | |
2074 | * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940 | |
2075 | * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242 | |
2076 | */ | |
bde20e1b | 2077 | static int caps_lock(char *s) |
6dbe3af9 | 2078 | { |
53d55042 | 2079 | int capslock; |
70bedfa1 KZ |
2080 | |
2081 | for (capslock = 0; *s; s++) { | |
2082 | if (islower(*s)) | |
2083 | return EXIT_SUCCESS; | |
2084 | if (capslock == 0) | |
2085 | capslock = isupper(*s); | |
2086 | } | |
2087 | return capslock; | |
6dbe3af9 KZ |
2088 | } |
2089 | ||
53d55042 | 2090 | /* Convert speed string to speed code; return 0 on failure. */ |
bde20e1b | 2091 | static speed_t bcode(char *s) |
6dbe3af9 | 2092 | { |
1a204cd2 | 2093 | const struct Speedtab *sp; |
53d55042 | 2094 | long speed = atol(s); |
6dbe3af9 | 2095 | |
70bedfa1 KZ |
2096 | for (sp = speedtab; sp->speed; sp++) |
2097 | if (sp->speed == speed) | |
2098 | return sp->code; | |
2099 | return 0; | |
6dbe3af9 KZ |
2100 | } |
2101 | ||
7d368556 | 2102 | static void __attribute__ ((__noreturn__)) usage(FILE *out) |
6dbe3af9 | 2103 | { |
7d368556 | 2104 | fputs(USAGE_HEADER, out); |
3755d48b BS |
2105 | fprintf(out, _(" %1$s [options] <line> [<baud_rate>,...] [<termtype>]\n" |
2106 | " %1$s [options] <baud_rate>,... <line> [<termtype>]\n"), program_invocation_short_name); | |
451dbcfa BS |
2107 | |
2108 | fputs(USAGE_SEPARATOR, out); | |
2109 | fputs(_("Open a terminal and set its mode.\n"), out); | |
2110 | ||
7d368556 SK |
2111 | fputs(USAGE_OPTIONS, out); |
2112 | fputs(_(" -8, --8bits assume 8-bit tty\n"), out); | |
2113 | fputs(_(" -a, --autologin <user> login the specified user automatically\n"), out); | |
2114 | fputs(_(" -c, --noreset do not reset control mode\n"), out); | |
1505a0b2 | 2115 | fputs(_(" -E, --remote use -r <hostname> for login(1)\n"), out); |
7d368556 SK |
2116 | fputs(_(" -f, --issue-file <file> display issue file\n"), out); |
2117 | fputs(_(" -h, --flow-control enable hardware flow control\n"), out); | |
2118 | fputs(_(" -H, --host <hostname> specify login host\n"), out); | |
2119 | fputs(_(" -i, --noissue do not display issue file\n"), out); | |
2120 | fputs(_(" -I, --init-string <string> set init string\n"), out); | |
584c0744 | 2121 | fputs(_(" -J --noclear do not clear the screen before prompt\n"), out); |
7d368556 | 2122 | fputs(_(" -l, --login-program <file> specify login program\n"), out); |
38ae77d7 | 2123 | fputs(_(" -L, --local-line[=<mode>] control the local line flag\n"), out); |
7d368556 SK |
2124 | fputs(_(" -m, --extract-baud extract baud rate during connect\n"), out); |
2125 | fputs(_(" -n, --skip-login do not prompt for login\n"), out); | |
584c0744 | 2126 | fputs(_(" -N --nonewline do not print a newline before issue\n"), out); |
7d368556 | 2127 | fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out); |
a056cfe2 | 2128 | fputs(_(" -p, --login-pause wait for any key before the login\n"), out); |
4b856ee4 | 2129 | fputs(_(" -r, --chroot <dir> change root to the directory\n"), out); |
7d368556 SK |
2130 | fputs(_(" -R, --hangup do virtually hangup on the tty\n"), out); |
2131 | fputs(_(" -s, --keep-baud try to keep baud rate after break\n"), out); | |
2132 | fputs(_(" -t, --timeout <number> login process timeout\n"), out); | |
2133 | fputs(_(" -U, --detect-case detect uppercase terminal\n"), out); | |
2134 | fputs(_(" -w, --wait-cr wait carriage-return\n"), out); | |
7d368556 | 2135 | fputs(_(" --nohints do not print hints\n"), out); |
a056cfe2 | 2136 | fputs(_(" --nohostname no hostname at all will be shown\n"), out); |
7d368556 SK |
2137 | fputs(_(" --long-hostname show full qualified hostname\n"), out); |
2138 | fputs(_(" --erase-chars <string> additional backspace chars\n"), out); | |
2139 | fputs(_(" --kill-chars <string> additional kill chars\n"), out); | |
584c0744 BR |
2140 | fputs(_(" --chdir <directory> chdir before the login\n"), out); |
2141 | fputs(_(" --delay <number> sleep seconds before prompt\n"), out); | |
2142 | fputs(_(" --nice <number> run login with this priority\n"), out); | |
6443dd43 | 2143 | fputs(_(" --reload reload prompts on running agetty instances\n"), out); |
86ad1432 SK |
2144 | fputs(_(" --help display this help and exit\n"), out); |
2145 | fputs(_(" --version output version information and exit\n"), out); | |
7d368556 | 2146 | fprintf(out, USAGE_MAN_TAIL("agetty(8)")); |
70bedfa1 KZ |
2147 | |
2148 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
6dbe3af9 KZ |
2149 | } |
2150 | ||
bde20e1b WF |
2151 | /* |
2152 | * Helper function reports errors to console or syslog. | |
2153 | * Will be used by log_err() and log_warn() therefore | |
2154 | * it takes a format as well as va_list. | |
2155 | */ | |
6dbe3af9 KZ |
2156 | #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2) |
2157 | ||
bde20e1b | 2158 | static void dolog(int priority, const char *fmt, va_list ap) |
53d55042 | 2159 | { |
6dbe3af9 | 2160 | #ifndef USE_SYSLOG |
53d55042 | 2161 | int fd; |
6dbe3af9 | 2162 | #endif |
53d55042 SK |
2163 | char buf[BUFSIZ]; |
2164 | char *bp; | |
6dbe3af9 | 2165 | |
70bedfa1 KZ |
2166 | /* |
2167 | * If the diagnostic is reported via syslog(3), the process name is | |
2168 | * automatically prepended to the message. If we write directly to | |
2169 | * /dev/console, we must prepend the process name ourselves. | |
2170 | */ | |
6dbe3af9 | 2171 | #ifdef USE_SYSLOG |
70bedfa1 KZ |
2172 | buf[0] = '\0'; |
2173 | bp = buf; | |
6dbe3af9 | 2174 | #else |
53d55042 | 2175 | str2cpy(buf, program_invocation_short_name, ": "); |
70bedfa1 | 2176 | bp = buf + strlen(buf); |
53d55042 | 2177 | #endif /* USE_SYSLOG */ |
763d7a87 | 2178 | vsnprintf(bp, sizeof(buf)-strlen(buf), fmt, ap); |
6dbe3af9 | 2179 | |
70bedfa1 KZ |
2180 | /* |
2181 | * Write the diagnostic directly to /dev/console if we do not use the | |
2182 | * syslog(3) facility. | |
2183 | */ | |
6dbe3af9 | 2184 | #ifdef USE_SYSLOG |
53d55042 | 2185 | openlog(program_invocation_short_name, LOG_PID, LOG_AUTHPRIV); |
bde20e1b | 2186 | syslog(priority, "%s", buf); |
70bedfa1 | 2187 | closelog(); |
6dbe3af9 | 2188 | #else |
70bedfa1 | 2189 | /* Terminate with CR-LF since the console mode is unknown. */ |
53d55042 | 2190 | strcat(bp, "\r\n"); |
70bedfa1 | 2191 | if ((fd = open("/dev/console", 1)) >= 0) { |
729def03 | 2192 | write_all(fd, buf, strlen(buf)); |
53d55042 | 2193 | close(fd); |
70bedfa1 | 2194 | } |
53d55042 | 2195 | #endif /* USE_SYSLOG */ |
bde20e1b WF |
2196 | } |
2197 | ||
2198 | static void log_err(const char *fmt, ...) | |
2199 | { | |
2200 | va_list ap; | |
2201 | ||
2202 | va_start(ap, fmt); | |
2203 | dolog(LOG_ERR, fmt, ap); | |
2204 | va_end(ap); | |
2205 | ||
53d55042 SK |
2206 | /* Be kind to init(8). */ |
2207 | sleep(10); | |
70bedfa1 | 2208 | exit(EXIT_FAILURE); |
6dbe3af9 | 2209 | } |
bde20e1b WF |
2210 | |
2211 | static void log_warn(const char *fmt, ...) | |
2212 | { | |
2213 | va_list ap; | |
2214 | ||
2215 | va_start(ap, fmt); | |
2216 | dolog(LOG_WARNING, fmt, ap); | |
2217 | va_end(ap); | |
2218 | } | |
729def03 | 2219 | |
0f283438 | 2220 | static void print_addr(sa_family_t family, void *addr) |
2b945eda | 2221 | { |
0f283438 KZ |
2222 | char buff[INET6_ADDRSTRLEN + 1]; |
2223 | ||
2224 | inet_ntop(family, addr, buff, sizeof(buff)); | |
2225 | printf("%s", buff); | |
2226 | } | |
2227 | ||
2228 | /* | |
2229 | * Prints IP for the specified interface (@iface), if the interface is not | |
2230 | * specified then prints the "best" one (UP, RUNNING, non-LOOPBACK). If not | |
2231 | * found the "best" interface then prints at least host IP. | |
2232 | */ | |
2233 | static void output_iface_ip(struct ifaddrs *addrs, | |
2234 | const char *iface, | |
2235 | sa_family_t family) | |
2236 | { | |
2237 | struct ifaddrs *p; | |
2238 | struct addrinfo hints, *info = NULL; | |
2239 | char *host = NULL; | |
2240 | void *addr = NULL; | |
2241 | ||
2242 | if (!addrs) | |
2b945eda KZ |
2243 | return; |
2244 | ||
0f283438 | 2245 | for (p = addrs; p; p = p->ifa_next) { |
2b945eda | 2246 | |
0f283438 KZ |
2247 | if (!p->ifa_name || |
2248 | !p->ifa_addr || | |
2249 | p->ifa_addr->sa_family != family) | |
2250 | continue; | |
2251 | ||
2252 | if (iface) { | |
2253 | /* Filter out by interface name */ | |
2254 | if (strcmp(p->ifa_name, iface) != 0) | |
2255 | continue; | |
2256 | } else { | |
2257 | /* Select the "best" interface */ | |
2258 | if ((p->ifa_flags & IFF_LOOPBACK) || | |
2259 | !(p->ifa_flags & IFF_UP) || | |
2260 | !(p->ifa_flags & IFF_RUNNING)) | |
2261 | continue; | |
2262 | } | |
2b945eda | 2263 | |
0f283438 KZ |
2264 | addr = NULL; |
2265 | switch (p->ifa_addr->sa_family) { | |
2b945eda | 2266 | case AF_INET: |
0f283438 | 2267 | addr = &((struct sockaddr_in *) p->ifa_addr)->sin_addr; |
2b945eda KZ |
2268 | break; |
2269 | case AF_INET6: | |
0f283438 | 2270 | addr = &((struct sockaddr_in6 *) p->ifa_addr)->sin6_addr; |
2b945eda KZ |
2271 | break; |
2272 | } | |
0f283438 | 2273 | |
2b945eda | 2274 | if (addr) { |
0f283438 KZ |
2275 | print_addr(family, addr); |
2276 | return; | |
2b945eda | 2277 | } |
0f283438 | 2278 | } |
2b945eda | 2279 | |
0f283438 KZ |
2280 | if (iface) |
2281 | return; | |
2b945eda | 2282 | |
0f283438 | 2283 | /* Hmm.. not found the best interface, print host IP at least */ |
2b945eda KZ |
2284 | memset(&hints, 0, sizeof(hints)); |
2285 | hints.ai_family = family; | |
2286 | if (family == AF_INET6) | |
2287 | hints.ai_flags = AI_V4MAPPED; | |
2288 | ||
2448f336 KZ |
2289 | host = xgethostname(); |
2290 | if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) { | |
2b945eda KZ |
2291 | switch (info->ai_family) { |
2292 | case AF_INET: | |
2293 | addr = &((struct sockaddr_in *) info->ai_addr)->sin_addr; | |
2294 | break; | |
2295 | case AF_INET6: | |
2296 | addr = &((struct sockaddr_in6 *) info->ai_addr)->sin6_addr; | |
2297 | break; | |
2298 | } | |
0f283438 KZ |
2299 | if (addr) |
2300 | print_addr(family, addr); | |
2b945eda KZ |
2301 | |
2302 | freeaddrinfo(info); | |
2303 | } | |
2448f336 | 2304 | free(host); |
2b945eda KZ |
2305 | } |
2306 | ||
2307 | /* | |
2308 | * parses \x{argument}, if not argument specified then returns NULL, the @fd | |
2309 | * has to point to one char after the sequence (it means '{'). | |
2310 | */ | |
2311 | static char *get_escape_argument(FILE *fd, char *buf, size_t bufsz) | |
2312 | { | |
2313 | size_t i = 0; | |
2314 | int c = fgetc(fd); | |
2315 | ||
2316 | if (c == EOF || (unsigned char) c != '{') { | |
2317 | ungetc(c, fd); | |
2318 | return NULL; | |
2319 | } | |
2320 | ||
2321 | do { | |
2322 | c = fgetc(fd); | |
2323 | if (c == EOF) | |
2324 | return NULL; | |
2325 | if ((unsigned char) c != '}' && i < bufsz - 1) | |
2326 | buf[i++] = (unsigned char) c; | |
2327 | ||
2328 | } while ((unsigned char) c != '}'); | |
2329 | ||
2330 | buf[i] = '\0'; | |
2331 | return buf; | |
2332 | } | |
2333 | ||
729def03 | 2334 | static void output_special_char(unsigned char c, struct options *op, |
2b945eda | 2335 | struct termios *tp, FILE *fp) |
729def03 WF |
2336 | { |
2337 | struct utsname uts; | |
763d7a87 KZ |
2338 | |
2339 | uname(&uts); | |
3aa6b68f | 2340 | |
729def03 | 2341 | switch (c) { |
583627ef KZ |
2342 | case 'e': |
2343 | fputs("\033", stdout); | |
2344 | break; | |
729def03 | 2345 | case 's': |
763d7a87 | 2346 | printf("%s", uts.sysname); |
729def03 WF |
2347 | break; |
2348 | case 'n': | |
763d7a87 | 2349 | printf("%s", uts.nodename); |
729def03 WF |
2350 | break; |
2351 | case 'r': | |
763d7a87 | 2352 | printf("%s", uts.release); |
729def03 WF |
2353 | break; |
2354 | case 'v': | |
763d7a87 | 2355 | printf("%s", uts.version); |
729def03 WF |
2356 | break; |
2357 | case 'm': | |
763d7a87 | 2358 | printf("%s", uts.machine); |
729def03 WF |
2359 | break; |
2360 | case 'o': | |
2361 | { | |
2448f336 KZ |
2362 | char *dom = xgetdomainname(); |
2363 | ||
2364 | fputs(dom ? dom : "unknown_domain", stdout); | |
2365 | free(dom); | |
729def03 WF |
2366 | break; |
2367 | } | |
2368 | case 'O': | |
2369 | { | |
2448f336 KZ |
2370 | char *dom = NULL; |
2371 | char *host = xgethostname(); | |
729def03 WF |
2372 | struct addrinfo hints, *info = NULL; |
2373 | ||
2374 | memset(&hints, 0, sizeof(hints)); | |
2375 | hints.ai_flags = AI_CANONNAME; | |
2376 | ||
2448f336 | 2377 | if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) { |
729def03 | 2378 | char *canon; |
2448f336 | 2379 | |
729def03 WF |
2380 | if (info->ai_canonname && |
2381 | (canon = strchr(info->ai_canonname, '.'))) | |
2382 | dom = canon + 1; | |
729def03 | 2383 | } |
2448f336 KZ |
2384 | fputs(dom ? dom : "unknown_domain", stdout); |
2385 | if (info) | |
2386 | freeaddrinfo(info); | |
2387 | free(host); | |
729def03 WF |
2388 | break; |
2389 | } | |
2390 | case 'd': | |
2391 | case 't': | |
2392 | { | |
2393 | time_t now; | |
2394 | struct tm *tm; | |
2395 | ||
763d7a87 | 2396 | time(&now); |
729def03 WF |
2397 | tm = localtime(&now); |
2398 | ||
d9201203 KZ |
2399 | if (!tm) |
2400 | break; | |
2401 | ||
729def03 | 2402 | if (c == 'd') /* ISO 8601 */ |
763d7a87 | 2403 | printf("%s %s %d %d", |
729def03 WF |
2404 | nl_langinfo(ABDAY_1 + tm->tm_wday), |
2405 | nl_langinfo(ABMON_1 + tm->tm_mon), | |
2406 | tm->tm_mday, | |
2407 | tm->tm_year < 70 ? tm->tm_year + 2000 : | |
2408 | tm->tm_year + 1900); | |
2409 | else | |
763d7a87 | 2410 | printf("%02d:%02d:%02d", |
729def03 WF |
2411 | tm->tm_hour, tm->tm_min, tm->tm_sec); |
2412 | break; | |
2413 | } | |
2414 | case 'l': | |
763d7a87 | 2415 | printf ("%s", op->tty); |
729def03 WF |
2416 | break; |
2417 | case 'b': | |
2418 | { | |
2419 | const speed_t speed = cfgetispeed(tp); | |
2420 | int i; | |
2421 | ||
2422 | for (i = 0; speedtab[i].speed; i++) { | |
2423 | if (speedtab[i].code == speed) { | |
2424 | printf("%ld", speedtab[i].speed); | |
2425 | break; | |
2426 | } | |
2427 | } | |
2428 | break; | |
2429 | } | |
b34f097e KZ |
2430 | case 'S': |
2431 | { | |
2432 | char *var = NULL, varname[64]; | |
2433 | ||
2434 | if (get_escape_argument(fp, varname, sizeof(varname))) | |
2435 | var = read_os_release(op, varname); | |
2436 | else if (!(var = read_os_release(op, "PRETTY_NAME"))) | |
2437 | var = uts.sysname; | |
2438 | if (var) { | |
2439 | if (strcmp(varname, "ANSI_COLOR") == 0) | |
2440 | printf("\033[%sm", var); | |
2441 | else | |
2442 | printf("%s", var); | |
2443 | if (var != uts.sysname) | |
2444 | free(var); | |
2445 | } | |
2446 | break; | |
2447 | } | |
729def03 WF |
2448 | case 'u': |
2449 | case 'U': | |
2450 | { | |
2451 | int users = 0; | |
2452 | struct utmp *ut; | |
2453 | setutent(); | |
2454 | while ((ut = getutent())) | |
2455 | if (ut->ut_type == USER_PROCESS) | |
2456 | users++; | |
2457 | endutent(); | |
729def03 | 2458 | if (c == 'U') |
9e531400 BS |
2459 | printf(P_("%d user", "%d users", users), users); |
2460 | else | |
2461 | printf ("%d ", users); | |
729def03 WF |
2462 | break; |
2463 | } | |
2b945eda KZ |
2464 | case '4': |
2465 | case '6': | |
2466 | { | |
2467 | sa_family_t family = c == '4' ? AF_INET : AF_INET6; | |
0f283438 | 2468 | struct ifaddrs *addrs = NULL; |
2b945eda KZ |
2469 | char iface[128]; |
2470 | ||
e36deb64 SW |
2471 | #ifdef AGETTY_RELOAD |
2472 | open_netlink(); | |
2473 | #endif | |
2474 | ||
0f283438 KZ |
2475 | if (getifaddrs(&addrs)) |
2476 | break; | |
2477 | ||
2478 | if (get_escape_argument(fp, iface, sizeof(iface))) | |
2b945eda | 2479 | output_iface_ip(addrs, iface, family); |
0f283438 KZ |
2480 | else |
2481 | output_iface_ip(addrs, NULL, family); | |
2482 | ||
2483 | freeifaddrs(addrs); | |
2b945eda KZ |
2484 | break; |
2485 | } | |
729def03 | 2486 | default: |
763d7a87 | 2487 | putchar(c); |
729def03 WF |
2488 | break; |
2489 | } | |
2490 | } | |
2491 | ||
2492 | static void init_special_char(char* arg, struct options *op) | |
2493 | { | |
2494 | char ch, *p, *q; | |
2495 | int i; | |
2496 | ||
ca8e91a4 KZ |
2497 | op->initstring = malloc(strlen(arg) + 1); |
2498 | if (!op->initstring) | |
2499 | log_err(_("failed to allocate memory: %m")); | |
729def03 WF |
2500 | |
2501 | /* | |
2502 | * Copy optarg into op->initstring decoding \ddd octal | |
2503 | * codes into chars. | |
2504 | */ | |
2505 | q = op->initstring; | |
2506 | p = arg; | |
2507 | while (*p) { | |
2508 | /* The \\ is converted to \ */ | |
2509 | if (*p == '\\') { | |
2510 | p++; | |
2511 | if (*p == '\\') { | |
2512 | ch = '\\'; | |
2513 | p++; | |
2514 | } else { | |
2515 | /* Handle \000 - \177. */ | |
2516 | ch = 0; | |
2517 | for (i = 1; i <= 3; i++) { | |
2518 | if (*p >= '0' && *p <= '7') { | |
2519 | ch <<= 3; | |
2520 | ch += *p - '0'; | |
2521 | p++; | |
2522 | } else { | |
2523 | break; | |
2524 | } | |
2525 | } | |
2526 | } | |
2527 | *q++ = ch; | |
2528 | } else | |
2529 | *q++ = *p++; | |
2530 | } | |
2531 | *q = '\0'; | |
2532 | } | |
eb8e1f9f | 2533 | |
a694957a KZ |
2534 | /* |
2535 | * Appends @str to @dest and if @dest is not empty then use use @sep as a | |
2536 | * separator. The maximal final length of the @dest is @len. | |
2537 | * | |
2538 | * Returns the final @dest length or -1 in case of error. | |
2539 | */ | |
2540 | static ssize_t append(char *dest, size_t len, const char *sep, const char *src) | |
2541 | { | |
2542 | size_t dsz = 0, ssz = 0, sz; | |
2543 | char *p; | |
2544 | ||
2545 | if (!dest || !len || !src) | |
2546 | return -1; | |
2547 | ||
2548 | if (*dest) | |
2549 | dsz = strlen(dest); | |
2550 | if (dsz && sep) | |
2551 | ssz = strlen(sep); | |
2552 | sz = strlen(src); | |
2553 | ||
2554 | if (dsz + ssz + sz + 1 > len) | |
2555 | return -1; | |
2556 | ||
2557 | p = dest + dsz; | |
2558 | if (ssz) { | |
2559 | memcpy(p, sep, ssz); | |
2560 | p += ssz; | |
2561 | } | |
2562 | memcpy(p, src, sz); | |
2563 | *(p + sz) = '\0'; | |
2564 | ||
2565 | return dsz + ssz + sz; | |
2566 | } | |
9aeb66dc | 2567 | |
eb8e1f9f WF |
2568 | /* |
2569 | * Do not allow the user to pass an option as a user name | |
2570 | * To be more safe: Use `--' to make sure the rest is | |
2571 | * interpreted as non-options by the program, if it supports it. | |
2572 | */ | |
9aeb66dc | 2573 | static void check_username(const char* nm) |
eb8e1f9f WF |
2574 | { |
2575 | const char *p = nm; | |
2576 | if (!nm) | |
2577 | goto err; | |
763d7a87 | 2578 | if (strlen(nm) > 42) |
eb8e1f9f | 2579 | goto err; |
763d7a87 | 2580 | while (isspace(*p)) |
eb8e1f9f WF |
2581 | p++; |
2582 | if (*p == '-') | |
2583 | goto err; | |
2584 | return; | |
2585 | err: | |
2586 | errno = EPERM; | |
8c219bf4 | 2587 | log_err(_("checkname failed: %m")); |
eb8e1f9f WF |
2588 | } |
2589 | ||
bb280f79 WF |
2590 | /* |
2591 | * For the case plymouth is found on this system | |
2592 | */ | |
2593 | static int plymouth_command(const char* arg) | |
2594 | { | |
bb280f79 WF |
2595 | static int has_plymouth = 1; |
2596 | pid_t pid; | |
2597 | ||
2598 | if (!has_plymouth) | |
2599 | return 127; | |
2600 | ||
2601 | pid = fork(); | |
2602 | if (!pid) { | |
a359830c KZ |
2603 | int fd = open(AGETTY_PLYMOUTH_FDFILE, O_RDWR); |
2604 | ||
2605 | if (fd < 0) | |
2606 | err(EXIT_FAILURE,_("cannot open %s"), | |
2607 | AGETTY_PLYMOUTH_FDFILE); | |
bb280f79 WF |
2608 | dup2(fd, 0); |
2609 | dup2(fd, 1); | |
2610 | dup2(fd, 2); | |
2611 | close(fd); | |
a359830c | 2612 | execl(AGETTY_PLYMOUTH, AGETTY_PLYMOUTH, arg, (char *) NULL); |
bb280f79 WF |
2613 | exit(127); |
2614 | } else if (pid > 0) { | |
2615 | int status; | |
2616 | waitpid(pid, &status, 0); | |
2617 | if (status == 127) | |
2618 | has_plymouth = 0; | |
2619 | return status; | |
2620 | } | |
2621 | return 1; | |
2622 | } | |
6443dd43 SW |
2623 | |
2624 | static void reload_agettys(void) | |
2625 | { | |
2626 | #ifdef AGETTY_RELOAD | |
c9f5ec0f KZ |
2627 | int fd = open(AGETTY_RELOAD_FILENAME, O_CREAT|O_CLOEXEC|O_WRONLY, |
2628 | S_IRUSR|S_IWUSR); | |
6443dd43 | 2629 | if (fd < 0) |
54fefa07 | 2630 | err(EXIT_FAILURE, _("cannot open %s"), AGETTY_RELOAD_FILENAME); |
6443dd43 | 2631 | |
90d5285d | 2632 | if (futimens(fd, NULL) < 0 || close(fd) < 0) |
54fefa07 | 2633 | err(EXIT_FAILURE, _("cannot touch file %s"), |
6443dd43 SW |
2634 | AGETTY_RELOAD_FILENAME); |
2635 | #else | |
2636 | /* very unusual */ | |
1d231190 | 2637 | errx(EXIT_FAILURE, _("--reload is unsupported on your system")); |
6443dd43 SW |
2638 | #endif |
2639 | } |