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