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