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.
7 * Ported to Linux by Peter Orbaek <poe@daimi.aau.dk>
8 * Adopt the mingetty features for a better support
9 * of virtual consoles by Werner Fink <werner@suse.de>
11 * This program is freely distributable.
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <arpa/inet.h>
38 #include <sys/utsname.h>
43 #include "pathnames.h"
48 #include "color-names.h"
51 #ifdef USE_PLYMOUTH_SUPPORT
52 # include "plymouth-ctrl.h"
55 #ifdef HAVE_SYS_PARAM_H
56 # include <sys/param.h>
59 #if defined(__FreeBSD_kernel__)
64 # ifdef HAVE_LIBUTIL_H
72 # ifndef DEFAULT_VCTERM
73 # define DEFAULT_VCTERM "linux"
75 # if defined (__s390__) || defined (__s390x__)
76 # define DEFAULT_TTYS0 "dumb"
77 # define DEFAULT_TTY32 "ibm327x"
78 # define DEFAULT_TTYS1 "vt220"
80 # ifndef DEFAULT_STERM
81 # define DEFAULT_STERM "vt102"
83 #elif defined(__GNU__)
85 # ifndef DEFAULT_VCTERM
86 # define DEFAULT_VCTERM "hurd"
88 # ifndef DEFAULT_STERM
89 # define DEFAULT_STERM "vt102"
92 # ifndef DEFAULT_VCTERM
93 # define DEFAULT_VCTERM "vt100"
95 # ifndef DEFAULT_STERM
96 # define DEFAULT_STERM "vt100"
100 #ifdef __FreeBSD_kernel__
104 /* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */
110 * Some heuristics to find out what environment we are in: if it is not
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.
119 * Things you may want to modify.
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.
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.
130 /* Displayed before the login prompt. */
132 # define ISSUE_SUPPORT
133 # if defined(HAVE_SCANDIRAT) && defined(HAVE_OPENAT)
135 # include "fileutils.h"
136 # define ISSUEDIR_SUPPORT
137 # define ISSUEDIR_EXT ".issue"
138 # define ISSUEDIR_EXTSIZ (sizeof(ISSUEDIR_EXT) - 1)
143 #define LOGIN "login: "
144 #define LOGIN_ARGV_MAX 16 /* Numbers of args for login */
150 # include <sys/inotify.h>
151 # include <linux/netlink.h>
152 # include <linux/rtnetlink.h>
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
;
156 static int netlink_fd
= AGETTY_RELOAD_FDNONE
;
157 static uint32_t netlink_groups
;
168 unsigned int do_tcsetattr
: 1,
173 * When multiple baud rates are specified on the command line, the first one
174 * we will try is the first one specified.
176 #define FIRST_SPEED 0
178 /* Storage for command-line options. */
179 #define MAX_SPEED 10 /* max. nr. of baud rates */
182 int flags
; /* toggle switches, see below */
183 unsigned int timeout
; /* time-out period */
184 char *autolog
; /* login the user automatically */
185 char *chdir
; /* Chdir before the login */
186 char *chroot
; /* Chroot before the login */
187 char *login
; /* login program */
188 char *logopt
; /* options for login program */
189 char *tty
; /* name of tty */
190 char *vcline
; /* line of virtual console */
191 char *term
; /* terminal type */
192 char *initstring
; /* modem init string */
193 char *issue
; /* alternative issue file or directory */
194 char *erasechars
; /* string with erase chars */
195 char *killchars
; /* string with kill chars */
196 char *osrelease
; /* /etc/os-release data */
197 unsigned int delay
; /* Sleep seconds before prompt */
198 int nice
; /* Run login with this priority */
199 int numspeed
; /* number of baud rates to try */
200 int clocal
; /* CLOCAL_MODE_* */
201 int kbmode
; /* Keyboard mode if virtual console */
202 speed_t speeds
[MAX_SPEED
]; /* baud rates to be tried */
206 CLOCAL_MODE_AUTO
= 0,
211 #define F_PARSE (1<<0) /* process modem status messages */
212 #define F_ISSUE (1<<1) /* display /etc/issue or /etc/issue.d */
213 #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
215 #define F_INITSTRING (1<<4) /* initstring is set */
216 #define F_WAITCRLF (1<<5) /* wait for CR or LF */
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 */
222 #define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */
223 #define F_VCONSOLE (1<<12) /* This is a virtual console */
224 #define F_HANGUP (1<<13) /* Do call vhangup(2) */
225 #define F_UTF8 (1<<14) /* We can do UTF8 */
226 #define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */
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 */
231 #define F_NOHINTS (1<<20) /* Don't print hints */
232 #define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */
234 #define serial_tty_option(opt, flag) \
235 (((opt)->flags & (F_VCONSOLE|(flag))) == (flag))
242 static const struct Speedtab speedtab
[] = {
314 static void init_special_char(char* arg
, struct options
*op
);
315 static void parse_args(int argc
, char **argv
, struct options
*op
);
316 static void parse_speeds(struct options
*op
, char *arg
);
317 static void update_utmp(struct options
*op
);
318 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
);
319 static void termio_init(struct options
*op
, struct termios
*tp
);
320 static void reset_vc(const struct options
*op
, struct termios
*tp
, int canon
);
321 static void auto_baud(struct termios
*tp
);
322 static void list_speeds(void);
323 static void output_special_char (struct issue
*ie
, unsigned char c
, struct options
*op
,
324 struct termios
*tp
, FILE *fp
);
325 static void do_prompt(struct issue
*ie
, struct options
*op
, struct termios
*tp
);
326 static void next_speed(struct options
*op
, struct termios
*tp
);
327 static char *get_logname(struct issue
*ie
, struct options
*op
,
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
);
332 static speed_t
bcode(char *s
);
333 static void usage(void) __attribute__((__noreturn__
));
334 static void exit_slowly(int code
) __attribute__((__noreturn__
));
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)));
339 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
);
340 static void check_username (const char* nm
);
341 static void login_options_to_argv(char *argv
[], int *argc
, char *str
, char *username
);
342 static void reload_agettys(void);
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
);
346 /* Fake hostname for ut_host specified on command line. */
347 static char *fakehost
;
350 # include "closestream.h"
351 # ifndef DEBUG_OUTPUT
352 # define DEBUG_OUTPUT "/dev/tty10"
354 # define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
357 # define debug(s) do { ; } while (0)
360 int main(int argc
, char **argv
)
362 char *username
= NULL
; /* login name, given to /bin/login */
363 struct chardata chardata
; /* will be set by get_logname() */
364 struct termios termios
; /* terminal mode bits */
365 struct options options
= {
366 .flags
= F_ISSUE
, /* show /etc/issue (SYSV_STYLE) */
367 .login
= _PATH_LOGIN
, /* default login program */
368 .tty
= "tty1" /* default tty line */
370 struct issue issue
= {
373 char *login_argv
[LOGIN_ARGV_MAX
+ 1];
375 struct sigaction sa
, sa_hup
, sa_quit
, sa_int
;
378 setlocale(LC_ALL
, "");
379 bindtextdomain(PACKAGE
, LOCALEDIR
);
382 /* In case vhangup(2) has to called */
383 sa
.sa_handler
= SIG_IGN
;
384 sa
.sa_flags
= SA_RESTART
;
385 sigemptyset (&sa
.sa_mask
);
386 sigaction(SIGHUP
, &sa
, &sa_hup
);
387 sigaction(SIGQUIT
, &sa
, &sa_quit
);
388 sigaction(SIGINT
, &sa
, &sa_int
);
391 dbf
= fopen(DEBUG_OUTPUT
, "w");
392 for (int i
= 1; i
< argc
; i
++) {
398 #endif /* DEBUGGING */
400 /* Parse command-line arguments. */
401 parse_args(argc
, argv
, &options
);
403 login_argv
[login_argc
++] = options
.login
; /* set login program name */
405 /* Update the utmp file. */
407 update_utmp(&options
);
410 sleep(options
.delay
);
412 debug("calling open_tty\n");
414 /* Open the tty as standard { input, output, error }. */
415 open_tty(options
.tty
, &termios
, &options
);
417 /* Unmask SIGHUP if inherited */
419 sigaddset(&set
, SIGHUP
);
420 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
421 sigaction(SIGHUP
, &sa_hup
, NULL
);
423 tcsetpgrp(STDIN_FILENO
, getpid());
425 /* Default is to follow the current line speed and then default to 9600 */
426 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
== 0) {
427 options
.speeds
[options
.numspeed
++] = bcode("9600");
428 options
.flags
|= F_KEEPSPEED
;
431 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
432 debug("calling termio_init\n");
433 termio_init(&options
, &termios
);
435 /* Write the modem init string and DO NOT flush the buffers. */
436 if (serial_tty_option(&options
, F_INITSTRING
) &&
437 options
.initstring
&& *options
.initstring
!= '\0') {
438 debug("writing init string\n");
439 write_all(STDOUT_FILENO
, options
.initstring
,
440 strlen(options
.initstring
));
443 if (options
.flags
& F_VCONSOLE
|| options
.clocal
!= CLOCAL_MODE_ALWAYS
)
444 /* Go to blocking mode unless -L is specified, this change
445 * affects stdout, stdin and stderr as all the file descriptors
446 * are created by dup(). */
447 fcntl(STDOUT_FILENO
, F_SETFL
,
448 fcntl(STDOUT_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
450 /* Optionally detect the baud rate from the modem status message. */
451 debug("before autobaud\n");
452 if (serial_tty_option(&options
, F_PARSE
))
455 /* Set the optional timer. */
457 alarm(options
.timeout
);
459 /* Optionally wait for CR or LF before writing /etc/issue */
460 if (serial_tty_option(&options
, F_WAITCRLF
)) {
463 debug("waiting for cr-lf\n");
464 while (read(STDIN_FILENO
, &ch
, 1) == 1) {
465 /* Strip "parity bit". */
468 fprintf(dbf
, "read %c\n", ch
);
470 if (ch
== '\n' || ch
== '\r')
475 INIT_CHARDATA(&chardata
);
477 if (options
.autolog
) {
478 debug("doing auto login\n");
479 username
= options
.autolog
;
482 if (options
.flags
& F_NOPROMPT
) { /* --skip-login */
483 eval_issue_file(&issue
, &options
, &termios
);
484 print_issue_file(&issue
, &options
, &termios
);
485 } else { /* regular (auto)login */
486 if (options
.autolog
) {
487 /* Autologin prompt */
488 eval_issue_file(&issue
, &options
, &termios
);
489 do_prompt(&issue
, &options
, &termios
);
490 printf(_("%s%s (automatic login)\n"), LOGIN
, options
.autolog
);
492 /* Read the login name. */
493 debug("reading login name\n");
495 get_logname(&issue
, &options
, &termios
, &chardata
)) == NULL
)
496 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
)
497 next_speed(&options
, &termios
);
505 /* Finalize the termios settings. */
506 if ((options
.flags
& F_VCONSOLE
) == 0)
507 termio_final(&options
, &termios
, &chardata
);
509 reset_vc(&options
, &termios
, 1);
511 /* Now the newline character should be properly written. */
512 write_all(STDOUT_FILENO
, "\r\n", 2);
514 sigaction(SIGQUIT
, &sa_quit
, NULL
);
515 sigaction(SIGINT
, &sa_int
, NULL
);
518 check_username(username
);
520 if (options
.logopt
) {
522 * The --login-options completely overwrites the default
523 * way how agetty composes login(1) command line.
525 login_options_to_argv(login_argv
, &login_argc
,
526 options
.logopt
, username
);
528 if (options
.flags
& F_REMOTE
) {
530 login_argv
[login_argc
++] = "-h";
531 login_argv
[login_argc
++] = fakehost
;
532 } else if (options
.flags
& F_NOHOSTNAME
)
533 login_argv
[login_argc
++] = "-H";
537 login_argv
[login_argc
++] = "-f";
539 login_argv
[login_argc
++] = "--";
540 login_argv
[login_argc
++] = username
;
544 login_argv
[login_argc
] = NULL
; /* last login argv */
546 if (options
.chroot
&& chroot(options
.chroot
) < 0)
547 log_err(_("%s: can't change root directory %s: %m"),
548 options
.tty
, options
.chroot
);
549 if (options
.chdir
&& chdir(options
.chdir
) < 0)
550 log_err(_("%s: can't change working directory %s: %m"),
551 options
.tty
, options
.chdir
);
552 if (options
.nice
&& nice(options
.nice
) < 0)
553 log_warn(_("%s: can't change process priority: %m"),
556 free(options
.osrelease
);
558 if (close_stream(dbf
) != 0)
559 log_err("write failed: %s", DEBUG_OUTPUT
);
562 /* Let the login program take care of password validation. */
563 execv(options
.login
, login_argv
);
564 log_err(_("%s: can't exec %s: %m"), options
.tty
, login_argv
[0]);
568 * Returns : @str if \u not found
569 * : @username if @str equal to "\u"
570 * : newly allocated string if \u mixed with something other
572 static char *replace_u(char *str
, char *username
)
574 char *entry
= NULL
, *p
= str
;
575 size_t usz
= username
? strlen(username
) : 0;
579 char *tp
, *old
= entry
;
581 if (memcmp(p
, "\\u", 2)) {
583 continue; /* no \u */
587 if (p
== str
&& sz
== 2) {
588 /* 'str' contains only '\u' */
593 tp
= entry
= malloc(sz
+ usz
);
595 log_err(_("failed to allocate memory: %m"));
598 /* copy chars before \u */
599 memcpy(tp
, str
, p
- str
);
604 memcpy(tp
, username
, usz
);
608 /* copy chars after \u + \0 */
609 memcpy(tp
, p
+ 2, sz
- (p
- str
) - 1);
618 return entry
? entry
: str
;
621 static void login_options_to_argv(char *argv
[], int *argc
,
622 char *str
, char *username
)
627 while (str
&& isspace(*str
))
631 while (p
&& *p
&& i
< LOGIN_ARGV_MAX
) {
634 while (isspace(*++p
))
637 argv
[i
++] = replace_u(str
, username
);
643 if (str
&& *str
&& i
< LOGIN_ARGV_MAX
)
644 argv
[i
++] = replace_u(str
, username
);
648 static void output_version(void)
650 static const char *features
[] = {
663 #ifdef ISSUEDIR_SUPPORT
669 #ifdef USE_PLYMOUTH_SUPPORT
685 printf( _("%s from %s"), program_invocation_short_name
, PACKAGE_STRING
);
687 for (i
= 0; features
[i
]; i
++) {
690 printf("%s", features
[i
]);
692 fputs(")\n", stdout
);
695 #define is_speed(str) (strlen((str)) == strspn((str), "0123456789,"))
697 /* Parse command-line arguments. */
698 static void parse_args(int argc
, char **argv
, struct options
*op
)
703 VERSION_OPTION
= CHAR_MAX
+ 1,
713 const struct option longopts
[] = {
714 { "8bits", no_argument
, NULL
, '8' },
715 { "autologin", required_argument
, NULL
, 'a' },
716 { "noreset", no_argument
, NULL
, 'c' },
717 { "chdir", required_argument
, NULL
, 'C' },
718 { "delay", required_argument
, NULL
, 'd' },
719 { "remote", no_argument
, NULL
, 'E' },
720 { "issue-file", required_argument
, NULL
, 'f' },
721 { "flow-control", no_argument
, NULL
, 'h' },
722 { "host", required_argument
, NULL
, 'H' },
723 { "noissue", no_argument
, NULL
, 'i' },
724 { "init-string", required_argument
, NULL
, 'I' },
725 { "noclear", no_argument
, NULL
, 'J' },
726 { "login-program", required_argument
, NULL
, 'l' },
727 { "local-line", optional_argument
, NULL
, 'L' },
728 { "extract-baud", no_argument
, NULL
, 'm' },
729 { "list-speeds", no_argument
, NULL
, LIST_SPEEDS_OPTION
},
730 { "skip-login", no_argument
, NULL
, 'n' },
731 { "nonewline", no_argument
, NULL
, 'N' },
732 { "login-options", required_argument
, NULL
, 'o' },
733 { "login-pause", no_argument
, NULL
, 'p' },
734 { "nice", required_argument
, NULL
, 'P' },
735 { "chroot", required_argument
, NULL
, 'r' },
736 { "hangup", no_argument
, NULL
, 'R' },
737 { "keep-baud", no_argument
, NULL
, 's' },
738 { "timeout", required_argument
, NULL
, 't' },
739 { "detect-case", no_argument
, NULL
, 'U' },
740 { "wait-cr", no_argument
, NULL
, 'w' },
741 { "nohints", no_argument
, NULL
, NOHINTS_OPTION
},
742 { "nohostname", no_argument
, NULL
, NOHOSTNAME_OPTION
},
743 { "long-hostname", no_argument
, NULL
, LONGHOSTNAME_OPTION
},
744 { "reload", no_argument
, NULL
, RELOAD_OPTION
},
745 { "version", no_argument
, NULL
, VERSION_OPTION
},
746 { "help", no_argument
, NULL
, HELP_OPTION
},
747 { "erase-chars", required_argument
, NULL
, ERASE_CHARS_OPTION
},
748 { "kill-chars", required_argument
, NULL
, KILL_CHARS_OPTION
},
752 while ((c
= getopt_long(argc
, argv
,
753 "8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts
,
757 op
->flags
|= F_EIGHTBITS
;
760 op
->autolog
= optarg
;
763 op
->flags
|= F_KEEPCFLAGS
;
769 op
->delay
= strtou32_or_err(optarg
, _("invalid delay argument"));
772 op
->flags
|= F_REMOTE
;
778 op
->flags
|= F_RTSCTS
;
784 op
->flags
&= ~F_ISSUE
;
787 init_special_char(optarg
, op
);
788 op
->flags
|= F_INITSTRING
;
791 op
->flags
|= F_NOCLEAR
;
797 /* -L and -L=always have the same meaning */
798 op
->clocal
= CLOCAL_MODE_ALWAYS
;
800 if (strcmp(optarg
, "=always") == 0)
801 op
->clocal
= CLOCAL_MODE_ALWAYS
;
802 else if (strcmp(optarg
, "=never") == 0)
803 op
->clocal
= CLOCAL_MODE_NEVER
;
804 else if (strcmp(optarg
, "=auto") == 0)
805 op
->clocal
= CLOCAL_MODE_AUTO
;
807 log_err(_("invalid argument of --local-line"));
811 op
->flags
|= F_PARSE
;
814 op
->flags
|= F_NOPROMPT
;
823 op
->flags
|= F_LOGINPAUSE
;
826 op
->nice
= strtos32_or_err(optarg
, _("invalid nice argument"));
832 op
->flags
|= F_HANGUP
;
835 op
->flags
|= F_KEEPSPEED
;
838 op
->timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
844 op
->flags
|= F_WAITCRLF
;
847 op
->flags
|= F_NOHINTS
;
849 case NOHOSTNAME_OPTION
:
850 op
->flags
|= F_NOHOSTNAME
;
852 case LONGHOSTNAME_OPTION
:
853 op
->flags
|= F_LONGHNAME
;
855 case ERASE_CHARS_OPTION
:
856 op
->erasechars
= optarg
;
858 case KILL_CHARS_OPTION
:
859 op
->killchars
= optarg
;
864 case LIST_SPEEDS_OPTION
:
873 errtryhelp(EXIT_FAILURE
);
877 debug("after getopt loop\n");
879 if (argc
< optind
+ 1) {
880 log_warn(_("not enough arguments"));
881 errx(EXIT_FAILURE
, _("not enough arguments"));
884 /* Accept "tty", "baudrate tty", and "tty baudrate". */
885 if (is_speed(argv
[optind
])) {
886 /* Assume BSD style speed. */
887 parse_speeds(op
, argv
[optind
++]);
888 if (argc
< optind
+ 1) {
889 log_warn(_("not enough arguments"));
890 errx(EXIT_FAILURE
, _("not enough arguments"));
892 op
->tty
= argv
[optind
++];
894 op
->tty
= argv
[optind
++];
896 char *v
= argv
[optind
];
904 /* On virtual console remember the line which is used for */
905 if (strncmp(op
->tty
, "tty", 3) == 0 &&
906 strspn(op
->tty
+ 3, "0123456789") == strlen(op
->tty
+3))
907 op
->vcline
= op
->tty
+3;
909 if (argc
> optind
&& argv
[optind
])
910 op
->term
= argv
[optind
];
912 debug("exiting parseargs\n");
915 /* Parse alternate baud rates. */
916 static void parse_speeds(struct options
*op
, char *arg
)
919 char *str
= strdup(arg
);
922 log_err(_("failed to allocate memory: %m"));
924 debug("entered parse_speeds:\n");
925 for (cp
= strtok(str
, ","); cp
!= NULL
; cp
= strtok((char *)0, ",")) {
926 if ((op
->speeds
[op
->numspeed
++] = bcode(cp
)) <= 0)
927 log_err(_("bad speed: %s"), cp
);
928 if (op
->numspeed
>= MAX_SPEED
)
929 log_err(_("too many alternate speeds"));
931 debug("exiting parsespeeds\n");
937 /* Update our utmp entry. */
938 static void update_utmp(struct options
*op
)
942 pid_t pid
= getpid();
943 pid_t sid
= getsid(0);
944 char *vcline
= op
->vcline
;
945 char *line
= op
->tty
;
949 * The utmp file holds miscellaneous information about things started by
950 * /sbin/init and other system-related events. Our purpose is to update
951 * the utmp entry for the current process, in particular the process type
952 * and the tty line we are listening to. Return successfully only if the
953 * utmp file can be opened for update, and if we are able to find our
954 * entry in the utmp file.
956 utmpxname(_PATH_UTMP
);
960 * Find my pid in utmp.
962 * FIXME: Earlier (when was that?) code here tested only utp->ut_type !=
963 * INIT_PROCESS, so maybe the >= here should be >.
965 * FIXME: The present code is taken from login.c, so if this is changed,
966 * maybe login has to be changed as well (is this true?).
968 while ((utp
= getutxent()))
969 if (utp
->ut_pid
== pid
970 && utp
->ut_type
>= INIT_PROCESS
971 && utp
->ut_type
<= DEAD_PROCESS
)
975 memcpy(&ut
, utp
, sizeof(ut
));
977 /* Some inits do not initialize utmp. */
978 memset(&ut
, 0, sizeof(ut
));
979 if (vcline
&& *vcline
)
980 /* Standard virtual console devices */
981 str2memcpy(ut
.ut_id
, vcline
, sizeof(ut
.ut_id
));
983 size_t len
= strlen(line
);
985 if (len
>= sizeof(ut
.ut_id
))
986 ptr
= line
+ len
- sizeof(ut
.ut_id
);
989 str2memcpy(ut
.ut_id
, ptr
, sizeof(ut
.ut_id
));
993 str2memcpy(ut
.ut_user
, "LOGIN", sizeof(ut
.ut_user
));
994 str2memcpy(ut
.ut_line
, line
, sizeof(ut
.ut_line
));
996 str2memcpy(ut
.ut_host
, fakehost
, sizeof(ut
.ut_host
));
999 ut
.ut_type
= LOGIN_PROCESS
;
1001 ut
.ut_session
= sid
;
1006 updwtmpx(_PATH_WTMP
, &ut
);
1009 #endif /* SYSV_STYLE */
1011 /* Set up tty as stdin, stdout & stderr. */
1012 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
)
1014 const pid_t pid
= getpid();
1020 /* Set up new standard input, unless we are given an already opened port. */
1022 if (strcmp(tty
, "-") != 0) {
1023 char buf
[PATH_MAX
+1];
1024 struct group
*gr
= NULL
;
1030 /* Use tty group if available */
1031 if ((gr
= getgrnam("tty")))
1034 len
= snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
1035 if (len
< 0 || (size_t)len
>= sizeof(buf
))
1036 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1038 /* Open the tty as standard input. */
1039 if ((fd
= open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0)) < 0)
1040 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1043 * There is always a race between this reset and the call to
1044 * vhangup() that s.o. can use to get access to your tty.
1045 * Linux login(1) will change tty permissions. Use root owner and group
1046 * with permission -rw------- for the period between getty and login.
1048 if (fchown(fd
, 0, gid
) || fchmod(fd
, (gid
? 0620 : 0600))) {
1050 log_warn("%s: %m", buf
);
1052 log_err("%s: %m", buf
);
1055 /* Sanity checks... */
1056 if (fstat(fd
, &st
) < 0)
1057 log_err("%s: %m", buf
);
1058 if ((st
.st_mode
& S_IFMT
) != S_IFCHR
)
1059 log_err(_("/dev/%s: not a character device"), tty
);
1061 log_err(_("/dev/%s: not a tty"), tty
);
1063 if (((tid
= tcgetsid(fd
)) < 0) || (pid
!= tid
)) {
1064 if (ioctl(fd
, TIOCSCTTY
, 1) == -1)
1065 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1068 close(STDIN_FILENO
);
1071 if (op
->flags
& F_HANGUP
) {
1073 if (ioctl(fd
, TIOCNOTTY
))
1074 debug("TIOCNOTTY ioctl failed\n");
1077 * Let's close all file descriptors before vhangup
1078 * https://lkml.org/lkml/2012/6/5/145
1081 close(STDOUT_FILENO
);
1082 close(STDERR_FILENO
);
1087 log_err(_("/dev/%s: vhangup() failed: %m"), tty
);
1092 if (open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0) != 0)
1093 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1095 if (((tid
= tcgetsid(STDIN_FILENO
)) < 0) || (pid
!= tid
)) {
1096 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 1) == -1)
1097 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1103 * Standard input should already be connected to an open port. Make
1104 * sure it is open for read/write.
1107 if ((fcntl(STDIN_FILENO
, F_GETFL
, 0) & O_RDWR
) != O_RDWR
)
1108 log_err(_("%s: not open for read/write"), tty
);
1112 if (tcsetpgrp(STDIN_FILENO
, pid
))
1113 log_warn(_("/dev/%s: cannot set process group: %m"), tty
);
1115 /* Get rid of the present outputs. */
1117 close(STDOUT_FILENO
);
1118 close(STDERR_FILENO
);
1122 /* Set up standard output and standard error file descriptors. */
1125 /* set up stdout and stderr */
1126 if (dup(STDIN_FILENO
) != 1 || dup(STDIN_FILENO
) != 2)
1127 log_err(_("%s: dup problem: %m"), tty
);
1129 /* make stdio unbuffered for slow modem lines */
1130 setvbuf(stdout
, NULL
, _IONBF
, 0);
1133 * The following ioctl will fail if stdin is not a tty, but also when
1134 * there is noise on the modem control lines. In the latter case, the
1135 * common course of action is (1) fix your cables (2) give the modem
1136 * more time to properly reset after hanging up.
1138 * SunOS users can achieve (2) by patching the SunOS kernel variable
1139 * "zsadtrlow" to a larger value; 5 seconds seems to be a good value.
1140 * http://www.sunmanagers.org/archives/1993/0574.html
1142 memset(tp
, 0, sizeof(struct termios
));
1143 if (tcgetattr(STDIN_FILENO
, tp
) < 0)
1144 log_err(_("%s: failed to get terminal attributes: %m"), tty
);
1146 #if defined (__s390__) || defined (__s390x__)
1149 * Special terminal on first serial line on a S/390(x) which
1150 * is due legacy reasons a block terminal of type 3270 or
1151 * higher. Whereas the second serial line on a S/390(x) is
1152 * a real character terminal which is compatible with VT220.
1154 if (strcmp(op
->tty
, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */
1155 op
->term
= DEFAULT_TTYS0
;
1156 else if (strncmp(op
->tty
, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */
1157 op
->term
= DEFAULT_TTY32
;
1158 else if (strcmp(op
->tty
, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */
1159 op
->term
= DEFAULT_TTYS1
;
1163 #if defined(__FreeBSD_kernel__)
1168 * Detect if this is a virtual console or serial/modem line.
1169 * In case of a virtual console the ioctl KDGKBMODE succeeds
1170 * whereas on other lines it will fails.
1173 if (ioctl(STDIN_FILENO
, KDGKBMODE
, &op
->kbmode
) == 0)
1175 if (ioctl(STDIN_FILENO
, TIOCMGET
, &serial
) < 0 && (errno
== EINVAL
))
1178 op
->flags
|= F_VCONSOLE
;
1180 op
->term
= DEFAULT_VCTERM
;
1186 op
->term
= DEFAULT_STERM
;
1189 if (setenv("TERM", op
->term
, 1) != 0)
1190 log_err(_("failed to set the %s environment variable"), "TERM");
1193 /* Initialize termios settings. */
1194 static void termio_clear(int fd
)
1197 * Do not write a full reset (ESC c) because this destroys
1198 * the unicode mode again if the terminal was in unicode
1199 * mode. Also it clears the CONSOLE_MAGIC features which
1200 * are required for some languages/console-fonts.
1201 * Just put the cursor to the home position (ESC [ H),
1202 * erase everything below the cursor (ESC [ J), and set the
1203 * scrolling region to the full window (ESC [ r)
1205 write_all(fd
, "\033[r\033[H\033[J", 9);
1208 /* Initialize termios settings. */
1209 static void termio_init(struct options
*op
, struct termios
*tp
)
1211 speed_t ispeed
, ospeed
;
1213 #ifdef USE_PLYMOUTH_SUPPORT
1214 struct termios lock
;
1215 int i
= (plymouth_command(MAGIC_PING
) == 0) ? PLYMOUTH_TERMIOS_FLAGS_DELAY
: 0;
1217 plymouth_command(MAGIC_QUIT
);
1220 * Even with TTYReset=no it seems with systemd or plymouth
1221 * the termios flags become changed from under the first
1222 * agetty on a serial system console as the flags are locked.
1224 memset(&lock
, 0, sizeof(struct termios
));
1225 if (ioctl(STDIN_FILENO
, TIOCGLCKTRMIOS
, &lock
) < 0)
1227 if (!lock
.c_iflag
&& !lock
.c_oflag
&& !lock
.c_cflag
&& !lock
.c_lflag
)
1229 debug("termios locked\n");
1232 memset(&lock
, 0, sizeof(struct termios
));
1233 ioctl(STDIN_FILENO
, TIOCSLCKTRMIOS
, &lock
);
1236 if (op
->flags
& F_VCONSOLE
) {
1237 #if defined(IUTF8) && defined(KDGKBMODE)
1238 switch(op
->kbmode
) {
1240 setlocale(LC_CTYPE
, "C.UTF-8");
1241 op
->flags
|= F_UTF8
;
1247 setlocale(LC_CTYPE
, "POSIX");
1248 op
->flags
&= ~F_UTF8
;
1252 setlocale(LC_CTYPE
, "POSIX");
1253 op
->flags
&= ~F_UTF8
;
1255 reset_vc(op
, tp
, 0);
1257 if ((tp
->c_cflag
& (CS8
|PARODD
|PARENB
)) == CS8
)
1258 op
->flags
|= F_EIGHTBITS
;
1260 if ((op
->flags
& F_NOCLEAR
) == 0)
1261 termio_clear(STDOUT_FILENO
);
1269 if (op
->flags
& F_KEEPSPEED
|| !op
->numspeed
) {
1270 /* Save the original setting. */
1271 ispeed
= cfgetispeed(tp
);
1272 ospeed
= cfgetospeed(tp
);
1274 if (!ispeed
) ispeed
= TTYDEF_SPEED
;
1275 if (!ospeed
) ospeed
= TTYDEF_SPEED
;
1278 ospeed
= ispeed
= op
->speeds
[FIRST_SPEED
];
1282 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
1283 * Special characters are set after we have read the login name; all
1284 * reads will be done in raw mode anyway. Errors will be dealt with
1288 /* The default is set c_iflag in termio_final() according to chardata.
1289 * Unfortunately, the chardata are not set according to the serial line
1290 * if --autolog is enabled. In this case we do not read from the line
1291 * at all. The best what we can do in this case is to keep c_iflag
1292 * unmodified for --autolog.
1296 tp
->c_iflag
= tp
->c_iflag
& IUTF8
;
1297 if (tp
->c_iflag
& IUTF8
)
1298 op
->flags
|= F_UTF8
;
1305 tp
->c_oflag
&= OPOST
| ONLCR
;
1307 if ((op
->flags
& F_KEEPCFLAGS
) == 0)
1308 tp
->c_cflag
= CS8
| HUPCL
| CREAD
| (tp
->c_cflag
& CLOCAL
);
1311 * Note that the speed is stored in the c_cflag termios field, so we have
1312 * set the speed always when the cflag is reset.
1314 cfsetispeed(tp
, ispeed
);
1315 cfsetospeed(tp
, ospeed
);
1317 /* The default is to follow setting from kernel, but it's possible
1318 * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/
1319 switch (op
->clocal
) {
1320 case CLOCAL_MODE_ALWAYS
:
1321 tp
->c_cflag
|= CLOCAL
; /* -L or -L=always */
1323 case CLOCAL_MODE_NEVER
:
1324 tp
->c_cflag
&= ~CLOCAL
; /* -L=never */
1326 case CLOCAL_MODE_AUTO
: /* -L=auto */
1330 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
1334 tp
->c_cc
[VTIME
] = 0;
1336 /* Check for terminal size and if not found set default */
1337 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0) {
1342 if (ioctl(STDIN_FILENO
, TIOCSWINSZ
, &ws
))
1343 debug("TIOCSWINSZ ioctl failed\n");
1346 /* Optionally enable hardware flow control. */
1348 if (op
->flags
& F_RTSCTS
)
1349 tp
->c_cflag
|= CRTSCTS
;
1351 /* Flush input and output queues, important for modems! */
1352 tcflush(STDIN_FILENO
, TCIOFLUSH
);
1354 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
))
1355 log_warn(_("setting terminal attributes failed: %m"));
1357 /* Go to blocking input even in local mode. */
1358 fcntl(STDIN_FILENO
, F_SETFL
,
1359 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1361 debug("term_io 2\n");
1364 /* Reset virtual console on stdin to its defaults */
1365 static void reset_vc(const struct options
*op
, struct termios
*tp
, int canon
)
1369 fl
|= (op
->flags
& F_KEEPCFLAGS
) == 0 ? 0 : UL_TTY_KEEPCFLAGS
;
1370 fl
|= (op
->flags
& F_UTF8
) == 0 ? 0 : UL_TTY_UTF8
;
1372 reset_virtual_console(tp
, fl
);
1374 #ifdef AGETTY_RELOAD
1376 * Discard all the flags that makes the line go canonical with echoing.
1377 * We need to know when the user starts typing.
1383 if (tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
))
1384 log_warn(_("setting terminal attributes failed: %m"));
1386 /* Go to blocking input even in local mode. */
1387 fcntl(STDIN_FILENO
, F_SETFL
,
1388 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1391 /* Extract baud rate from modem status message. */
1392 static void auto_baud(struct termios
*tp
)
1402 * This works only if the modem produces its status code AFTER raising
1403 * the DCD line, and if the computer is fast enough to set the proper
1404 * baud rate before the message has gone by. We expect a message of the
1407 * <junk><number><junk>
1409 * The number is interpreted as the baud rate of the incoming call. If the
1410 * modem does not tell us the baud rate within one second, we will keep
1411 * using the current baud rate. It is advisable to enable BREAK
1412 * processing (comma-separated list of baud rates) if the processing of
1413 * modem status messages is enabled.
1417 * Use 7-bit characters, don't block if input queue is empty. Errors will
1418 * be dealt with later on.
1420 iflag
= tp
->c_iflag
;
1421 /* Enable 8th-bit stripping. */
1422 tp
->c_iflag
|= ISTRIP
;
1423 vmin
= tp
->c_cc
[VMIN
];
1424 /* Do not block when queue is empty. */
1426 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1429 * Wait for a while, then read everything the modem has said so far and
1430 * try to extract the speed of the dial-in call.
1433 if ((nread
= read(STDIN_FILENO
, buf
, sizeof(buf
) - 1)) > 0) {
1435 for (bp
= buf
; bp
< buf
+ nread
; bp
++)
1436 if (c_isascii(*bp
) && isdigit(*bp
)) {
1437 if ((speed
= bcode(bp
))) {
1438 cfsetispeed(tp
, speed
);
1439 cfsetospeed(tp
, speed
);
1445 /* Restore terminal settings. Errors will be dealt with later on. */
1446 tp
->c_iflag
= iflag
;
1447 tp
->c_cc
[VMIN
] = vmin
;
1448 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1451 static char *xgethostname(void)
1454 size_t sz
= get_hostname_max() + 1;
1456 name
= malloc(sizeof(char) * sz
);
1458 log_err(_("failed to allocate memory: %m"));
1460 if (gethostname(name
, sz
) != 0) {
1464 name
[sz
- 1] = '\0';
1468 static char *xgetdomainname(void)
1470 #ifdef HAVE_GETDOMAINNAME
1472 const size_t sz
= get_hostname_max() + 1;
1474 name
= malloc(sizeof(char) * sz
);
1476 log_err(_("failed to allocate memory: %m"));
1478 if (getdomainname(name
, sz
) != 0) {
1482 name
[sz
- 1] = '\0';
1490 static char *read_os_release(struct options
*op
, const char *varname
)
1494 size_t varsz
= strlen(varname
);
1495 char *p
, *buf
= NULL
, *ret
= NULL
;
1497 /* read the file only once */
1498 if (!op
->osrelease
) {
1499 fd
= open(_PATH_OS_RELEASE_ETC
, O_RDONLY
);
1501 fd
= open(_PATH_OS_RELEASE_USR
, O_RDONLY
);
1503 log_warn(_("cannot open os-release file"));
1508 if (fstat(fd
, &st
) < 0 || st
.st_size
> 4 * 1024 * 1024)
1511 op
->osrelease
= malloc(st
.st_size
+ 1);
1513 log_err(_("failed to allocate memory: %m"));
1514 if (read_all(fd
, op
->osrelease
, st
.st_size
) != (ssize_t
) st
.st_size
) {
1515 free(op
->osrelease
);
1516 op
->osrelease
= NULL
;
1519 op
->osrelease
[st
.st_size
] = 0;
1521 buf
= strdup(op
->osrelease
);
1523 log_err(_("failed to allocate memory: %m"));
1529 p
+= strspn(p
, "\n\r");
1530 p
+= strspn(p
, " \t\n\r");
1533 if (strspn(p
, "#;\n") != 0) {
1534 p
+= strcspn(p
, "\n\r");
1537 if (strncmp(p
, varname
, varsz
) != 0) {
1538 p
+= strcspn(p
, "\n\r");
1542 p
+= strspn(p
, " \t\n\r");
1547 p
+= strspn(p
, " \t\n\r=\"");
1548 eol
= p
+ strcspn(p
, "\n\r");
1552 if (*eon
== '\t' || *eon
== ' ') {
1565 log_err(_("failed to allocate memory: %m"));
1575 #ifdef AGETTY_RELOAD
1576 static void open_netlink(void)
1578 struct sockaddr_nl addr
= { 0, };
1581 if (netlink_fd
!= AGETTY_RELOAD_FDNONE
)
1584 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1586 addr
.nl_family
= AF_NETLINK
;
1587 addr
.nl_pid
= getpid();
1588 addr
.nl_groups
= netlink_groups
;
1589 if (bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
1596 static int process_netlink_msg(int *triggered
)
1599 struct sockaddr_nl snl
;
1603 struct iovec iov
= {
1605 .iov_len
= sizeof(buf
)
1607 struct msghdr msg
= {
1609 .msg_namelen
= sizeof(snl
),
1612 .msg_control
= NULL
,
1613 .msg_controllen
= 0,
1617 rc
= recvmsg(netlink_fd
, &msg
, MSG_DONTWAIT
);
1619 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1622 /* Failure, just stop listening for changes */
1624 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1628 for (h
= (struct nlmsghdr
*)buf
; NLMSG_OK(h
, (unsigned int)rc
); h
= NLMSG_NEXT(h
, rc
)) {
1629 if (h
->nlmsg_type
== NLMSG_DONE
||
1630 h
->nlmsg_type
== NLMSG_ERROR
) {
1632 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1643 static int process_netlink(void)
1646 while (process_netlink_msg(&triggered
));
1650 static int wait_for_term_input(int fd
)
1652 char buffer
[sizeof(struct inotify_event
) + NAME_MAX
+ 1];
1655 if (inotify_fd
== AGETTY_RELOAD_FDNONE
) {
1656 /* make sure the reload trigger file exists */
1657 int reload_fd
= open(AGETTY_RELOAD_FILENAME
,
1658 O_CREAT
|O_CLOEXEC
|O_RDONLY
,
1661 /* initialize reload trigger inotify stuff */
1662 if (reload_fd
>= 0) {
1663 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1665 inotify_add_watch(inotify_fd
, AGETTY_RELOAD_FILENAME
,
1666 IN_ATTRIB
| IN_MODIFY
);
1670 log_warn(_("failed to create reload file: %s: %m"),
1671 AGETTY_RELOAD_FILENAME
);
1680 if (inotify_fd
>= 0) {
1681 FD_SET(inotify_fd
, &rfds
);
1682 nfds
= max(nfds
, inotify_fd
);
1684 if (netlink_fd
>= 0) {
1685 FD_SET(netlink_fd
, &rfds
);
1686 nfds
= max(nfds
, netlink_fd
);
1689 /* If waiting fails, just fall through, presumably reading input will fail */
1690 if (select(nfds
+ 1, &rfds
, NULL
, NULL
, NULL
) < 0)
1693 if (FD_ISSET(fd
, &rfds
)) {
1696 } else if (netlink_fd
>= 0 && FD_ISSET(netlink_fd
, &rfds
)) {
1697 if (!process_netlink())
1700 /* Just drain the inotify buffer */
1701 } else if (inotify_fd
>= 0 && FD_ISSET(inotify_fd
, &rfds
)) {
1702 while (read(inotify_fd
, buffer
, sizeof (buffer
)) > 0);
1708 #endif /* AGETTY_RELOAD */
1710 #ifdef ISSUEDIR_SUPPORT
1711 static int issuedir_filter(const struct dirent
*d
)
1715 #ifdef _DIRENT_HAVE_D_TYPE
1716 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
&&
1717 d
->d_type
!= DT_LNK
)
1720 if (*d
->d_name
== '.')
1723 namesz
= strlen(d
->d_name
);
1724 if (!namesz
|| namesz
< ISSUEDIR_EXTSIZ
+ 1 ||
1725 strcmp(d
->d_name
+ (namesz
- ISSUEDIR_EXTSIZ
), ISSUEDIR_EXT
))
1732 static FILE *issuedir_next_file(int dd
, struct dirent
**namelist
, int nfiles
, int *n
)
1734 while (*n
< nfiles
) {
1735 struct dirent
*d
= namelist
[*n
];
1741 if (fstatat(dd
, d
->d_name
, &st
, 0) ||
1742 !S_ISREG(st
.st_mode
))
1745 f
= fopen_at(dd
, d
->d_name
, O_RDONLY
|O_CLOEXEC
, "r" UL_CLOEXECSTR
);
1752 #endif /* ISSUEDIR_SUPPORT */
1754 #ifndef ISSUE_SUPPORT
1755 static void print_issue_file(struct issue
*ie
__attribute__((__unused__
)),
1757 struct termios
*tp
__attribute__((__unused__
)))
1759 if ((op
->flags
& F_NONL
) == 0) {
1760 /* Issue not in use, start with a new line. */
1761 write_all(STDOUT_FILENO
, "\r\n", 2);
1765 static void eval_issue_file(struct issue
*ie
__attribute__((__unused__
)),
1766 struct options
*op
__attribute__((__unused__
)),
1767 struct termios
*tp
__attribute__((__unused__
)))
1770 #else /* ISSUE_SUPPORT */
1772 #ifdef AGETTY_RELOAD
1773 static int issue_is_changed(struct issue
*ie
)
1775 if (ie
->mem_old
&& ie
->mem
1776 && strcmp(ie
->mem_old
, ie
->mem
) == 0) {
1778 ie
->mem_old
= ie
->mem
;
1787 static void print_issue_file(struct issue
*ie
,
1791 int oflag
= tp
->c_oflag
; /* Save current setting. */
1793 if ((op
->flags
& F_NONL
) == 0) {
1794 /* Issue not in use, start with a new line. */
1795 write_all(STDOUT_FILENO
, "\r\n", 2);
1798 if (ie
->do_tcsetattr
) {
1799 if ((op
->flags
& F_VCONSOLE
) == 0) {
1800 /* Map new line in output to carriage return & new line. */
1801 tp
->c_oflag
|= (ONLCR
| OPOST
);
1802 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1807 write_all(STDOUT_FILENO
, ie
->mem
, ie
->mem_sz
);
1809 if (ie
->do_tcrestore
) {
1810 /* Restore settings. */
1811 tp
->c_oflag
= oflag
;
1812 /* Wait till output is gone. */
1813 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1816 #ifdef AGETTY_RELOAD
1818 ie
->mem_old
= ie
->mem
;
1828 static void eval_issue_file(struct issue
*ie
,
1832 const char *filename
, *dirname
= NULL
;
1834 #ifdef ISSUEDIR_SUPPORT
1835 int dd
= -1, nfiles
= 0, i
;
1836 struct dirent
**namelist
= NULL
;
1838 #ifdef AGETTY_RELOAD
1842 if (!(op
->flags
& F_ISSUE
))
1846 * The custom issue file or directory specified by: agetty -f <path>.
1847 * Note that nothing is printed if the file/dir does not exist.
1849 filename
= op
->issue
;
1853 if (stat(filename
, &st
) < 0)
1855 if (S_ISDIR(st
.st_mode
)) {
1860 /* The default /etc/issue and optional /etc/issue.d directory
1861 * as extension to the file. The /etc/issue.d directory is
1862 * ignored if there is no /etc/issue file. The file may be
1865 if (access(_PATH_ISSUE
, F_OK
|R_OK
) != 0)
1867 filename
= _PATH_ISSUE
;
1868 dirname
= _PATH_ISSUEDIR
;
1871 ie
->output
= open_memstream(&ie
->mem
, &ie
->mem_sz
);
1872 #ifdef ISSUEDIR_SUPPORT
1874 dd
= open(dirname
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1876 nfiles
= scandirat(dd
, ".", &namelist
, issuedir_filter
, versionsort
);
1883 f
= fopen(filename
, "r");
1888 ie
->do_tcsetattr
= 1;
1891 #ifdef ISSUEDIR_SUPPORT
1892 if (!f
&& i
< nfiles
)
1893 f
= issuedir_next_file(dd
, namelist
, nfiles
, &i
);
1897 while ((c
= getc(f
)) != EOF
) {
1899 output_special_char(ie
, getc(f
), op
, tp
, f
);
1901 putc(c
, ie
->output
);
1907 if ((op
->flags
& F_VCONSOLE
) == 0)
1908 ie
->do_tcrestore
= 1;
1911 #ifdef ISSUEDIR_SUPPORT
1912 for (i
= 0; i
< nfiles
; i
++)
1918 #ifdef AGETTY_RELOAD
1919 if (netlink_groups
!= 0)
1924 #endif /* ISSUE_SUPPORT */
1926 /* Show login prompt, optionally preceded by /etc/issue contents. */
1927 static void do_prompt(struct issue
*ie
, struct options
*op
, struct termios
*tp
)
1929 #ifdef AGETTY_RELOAD
1932 print_issue_file(ie
, op
, tp
);
1934 if (op
->flags
& F_LOGINPAUSE
) {
1935 puts(_("[press ENTER to login]"));
1936 #ifdef AGETTY_RELOAD
1938 if (!wait_for_term_input(STDIN_FILENO
)) {
1939 eval_issue_file(ie
, op
, tp
);
1940 if (issue_is_changed(ie
)) {
1941 if (op
->flags
& F_VCONSOLE
)
1942 termio_clear(STDOUT_FILENO
);
1950 if (!(op
->flags
& F_NOHINTS
) && !op
->autolog
&&
1951 (op
->flags
& F_VCONSOLE
)) {
1954 if (ioctl(STDIN_FILENO
, KDGKBLED
, &kb
) == 0) {
1955 char hint
[256] = { '\0' };
1958 if (access(_PATH_NUMLOCK_ON
, F_OK
) == 0)
1961 if (nl
&& (kb
& 0x02) == 0)
1962 append(hint
, sizeof(hint
), NULL
, _("Num Lock off"));
1964 else if (nl
== 0 && (kb
& 2) && (kb
& 0x20) == 0)
1965 append(hint
, sizeof(hint
), NULL
, _("Num Lock on"));
1967 if ((kb
& 0x04) && (kb
& 0x40) == 0)
1968 append(hint
, sizeof(hint
), ", ", _("Caps Lock on"));
1970 if ((kb
& 0x01) && (kb
& 0x10) == 0)
1971 append(hint
, sizeof(hint
), ", ", _("Scroll Lock on"));
1974 printf(_("Hint: %s\n\n"), hint
);
1977 #endif /* KDGKBLED */
1978 if ((op
->flags
& F_NOHOSTNAME
) == 0) {
1979 char *hn
= xgethostname();
1982 char *dot
= strchr(hn
, '.');
1984 struct addrinfo
*res
= NULL
;
1986 if ((op
->flags
& F_LONGHNAME
) == 0) {
1990 } else if (dot
== NULL
) {
1991 struct addrinfo hints
;
1993 memset(&hints
, 0, sizeof(hints
));
1994 hints
.ai_flags
= AI_CANONNAME
;
1996 if (!getaddrinfo(hn
, NULL
, &hints
, &res
)
1997 && res
&& res
->ai_canonname
)
1998 cn
= res
->ai_canonname
;
2001 write_all(STDOUT_FILENO
, cn
, strlen(cn
));
2002 write_all(STDOUT_FILENO
, " ", 1);
2010 /* Always show login prompt. */
2011 write_all(STDOUT_FILENO
, LOGIN
, sizeof(LOGIN
) - 1);
2015 /* Select next baud rate. */
2016 static void next_speed(struct options
*op
, struct termios
*tp
)
2018 static int baud_index
= -1;
2020 if (baud_index
== -1)
2022 * If the F_KEEPSPEED flags is set then the FIRST_SPEED is not
2023 * tested yet (see termio_init()).
2026 (op
->flags
& F_KEEPSPEED
) ? FIRST_SPEED
: 1 % op
->numspeed
;
2028 baud_index
= (baud_index
+ 1) % op
->numspeed
;
2030 cfsetispeed(tp
, op
->speeds
[baud_index
]);
2031 cfsetospeed(tp
, op
->speeds
[baud_index
]);
2032 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
2035 /* Get user name, establish parity, speed, erase, kill & eol. */
2036 static char *get_logname(struct issue
*ie
, struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
2038 static char logname
[BUFSIZ
];
2040 char c
; /* input character, full eight bits */
2041 char ascval
; /* low 7 bits of input character */
2043 static char *erase
[] = { /* backspace-space-backspace */
2044 "\010\040\010", /* space parity */
2045 "\010\040\010", /* odd parity */
2046 "\210\240\210", /* even parity */
2047 "\210\240\210", /* no parity */
2050 /* Initialize kill, erase, parity etc. (also after switching speeds). */
2054 * Flush pending input (especially important after parsing or switching
2057 if ((op
->flags
& F_VCONSOLE
) == 0)
2059 tcflush(STDIN_FILENO
, TCIFLUSH
);
2061 eightbit
= (op
->flags
& (F_EIGHTBITS
|F_UTF8
));
2065 eval_issue_file(ie
, op
, tp
);
2066 while (*logname
== '\0') {
2067 /* Write issue file and prompt */
2068 do_prompt(ie
, op
, tp
);
2071 #ifdef AGETTY_RELOAD
2072 if (!wait_for_term_input(STDIN_FILENO
)) {
2073 /* refresh prompt -- discard input data, clear terminal
2074 * and call do_prompt() again
2076 if ((op
->flags
& F_VCONSOLE
) == 0)
2078 eval_issue_file(ie
, op
, tp
);
2079 if (!issue_is_changed(ie
))
2081 tcflush(STDIN_FILENO
, TCIFLUSH
);
2082 if (op
->flags
& F_VCONSOLE
)
2083 termio_clear(STDOUT_FILENO
);
2091 /* Read name, watch for break and end-of-line. */
2092 while (cp
->eol
== '\0') {
2097 debug("read from FD\n");
2098 readres
= read(STDIN_FILENO
, &c
, 1);
2100 debug("read failed\n");
2102 /* The terminal could be open with O_NONBLOCK when
2103 * -L (force CLOCAL) is specified... */
2104 if (errno
== EINTR
|| errno
== EAGAIN
) {
2114 exit_slowly(EXIT_SUCCESS
);
2116 log_err(_("%s: read: %m"), op
->tty
);
2123 /* Do parity bit handling. */
2126 else if (c
!= (ascval
= (c
& 0177))) {
2127 uint32_t bits
; /* # of "1" bits per character */
2128 uint32_t mask
; /* mask with 1 bit up */
2129 for (bits
= 1, mask
= 1; mask
& 0177; mask
<<= 1) {
2133 cp
->parity
|= ((bits
& 1) ? 1 : 2);
2136 if (op
->killchars
&& strchr(op
->killchars
, ascval
))
2138 else if (op
->erasechars
&& strchr(op
->erasechars
, ascval
))
2143 /* Do erase, kill and end-of-line processing. */
2147 if (op
->numspeed
> 1 && !(op
->flags
& F_VCONSOLE
))
2150 exit_slowly(EXIT_SUCCESS
);
2154 *bp
= 0; /* terminate logname */
2155 cp
->eol
= ascval
; /* set end-of-line char */
2159 cp
->erase
= ascval
; /* set erase character */
2161 if ((tp
->c_lflag
& ECHO
) == 0)
2162 write_all(1, erase
[cp
->parity
], 3);
2167 cp
->kill
= ascval
; /* set kill character */
2168 while (bp
> logname
) {
2169 if ((tp
->c_lflag
& ECHO
) == 0)
2170 write_all(1, erase
[cp
->parity
], 3);
2177 if ((size_t)(bp
- logname
) >= sizeof(logname
) - 1)
2178 log_err(_("%s: input overrun"), op
->tty
);
2179 if ((tp
->c_lflag
& ECHO
) == 0)
2180 write_all(1, &c
, 1); /* echo the character */
2181 *bp
++ = ascval
; /* and store it */
2184 /* Everything was erased. */
2185 if (bp
== logname
&& cp
->eol
== '\0')
2190 #ifdef HAVE_WIDECHAR
2191 if ((op
->flags
& (F_EIGHTBITS
|F_UTF8
)) == (F_EIGHTBITS
|F_UTF8
)) {
2192 /* Check out UTF-8 multibyte characters */
2196 len
= mbstowcs((wchar_t *)0, logname
, 0);
2198 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
2200 wcs
= malloc((len
+ 1) * sizeof(wchar_t));
2202 log_err(_("failed to allocate memory: %m"));
2204 len
= mbstowcs(wcs
, logname
, len
+ 1);
2206 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
2210 const wint_t wc
= *wcp
++;
2212 log_err(_("%s: invalid character 0x%x in login name"), op
->tty
, wc
);
2217 if ((op
->flags
& F_LCUC
) && (cp
->capslock
= caps_lock(logname
))) {
2219 /* Handle names with upper case and no lower case. */
2220 for (bp
= logname
; *bp
; bp
++)
2222 *bp
= tolower(*bp
); /* map name to lower case */
2228 /* Set the final tty mode bits. */
2229 static void termio_final(struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
2231 /* General terminal-independent stuff. */
2233 /* 2-way flow control */
2234 tp
->c_iflag
|= IXON
| IXOFF
;
2235 tp
->c_lflag
|= ICANON
| ISIG
| ECHO
| ECHOE
| ECHOK
| ECHOKE
;
2236 /* no longer| ECHOCTL | ECHOPRT */
2237 tp
->c_oflag
|= OPOST
;
2238 /* tp->c_cflag = 0; */
2239 tp
->c_cc
[VINTR
] = DEF_INTR
;
2240 tp
->c_cc
[VQUIT
] = DEF_QUIT
;
2241 tp
->c_cc
[VEOF
] = DEF_EOF
;
2242 tp
->c_cc
[VEOL
] = DEF_EOL
;
2244 tp
->c_cc
[VSWTC
] = DEF_SWITCH
;
2245 #elif defined(VSWTCH)
2246 tp
->c_cc
[VSWTCH
] = DEF_SWITCH
;
2247 #endif /* __linux__ */
2249 /* Account for special characters seen in input. */
2250 if (cp
->eol
== CR
) {
2251 tp
->c_iflag
|= ICRNL
;
2252 tp
->c_oflag
|= ONLCR
;
2254 tp
->c_cc
[VERASE
] = cp
->erase
;
2255 tp
->c_cc
[VKILL
] = cp
->kill
;
2257 /* Account for the presence or absence of parity bits in input. */
2258 switch (cp
->parity
) {
2260 /* space (always 0) parity */
2264 tp
->c_cflag
|= PARODD
;
2268 tp
->c_cflag
|= PARENB
;
2269 tp
->c_iflag
|= INPCK
| ISTRIP
;
2273 tp
->c_cflag
&= ~CSIZE
;
2277 /* Account for upper case without lower case. */
2280 tp
->c_iflag
|= IUCLC
;
2283 tp
->c_lflag
|= XCASE
;
2286 tp
->c_oflag
|= OLCUC
;
2289 /* Optionally enable hardware flow control. */
2291 if (op
->flags
& F_RTSCTS
)
2292 tp
->c_cflag
|= CRTSCTS
;
2295 /* Finally, make the new settings effective. */
2296 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
) < 0)
2297 log_err(_("%s: failed to set terminal attributes: %m"), op
->tty
);
2301 * String contains upper case without lower case.
2302 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940
2303 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242
2305 static int caps_lock(char *s
)
2309 for (capslock
= 0; *s
; s
++) {
2311 return EXIT_SUCCESS
;
2313 capslock
= isupper(*s
);
2318 /* Convert speed string to speed code; return 0 on failure. */
2319 static speed_t
bcode(char *s
)
2321 const struct Speedtab
*sp
;
2322 long speed
= atol(s
);
2324 for (sp
= speedtab
; sp
->speed
; sp
++)
2325 if (sp
->speed
== speed
)
2330 static void __attribute__((__noreturn__
)) usage(void)
2334 fputs(USAGE_HEADER
, out
);
2335 fprintf(out
, _(" %1$s [options] <line> [<baud_rate>,...] [<termtype>]\n"
2336 " %1$s [options] <baud_rate>,... <line> [<termtype>]\n"), program_invocation_short_name
);
2338 fputs(USAGE_SEPARATOR
, out
);
2339 fputs(_("Open a terminal and set its mode.\n"), out
);
2341 fputs(USAGE_OPTIONS
, out
);
2342 fputs(_(" -8, --8bits assume 8-bit tty\n"), out
);
2343 fputs(_(" -a, --autologin <user> login the specified user automatically\n"), out
);
2344 fputs(_(" -c, --noreset do not reset control mode\n"), out
);
2345 fputs(_(" -E, --remote use -r <hostname> for login(1)\n"), out
);
2346 fputs(_(" -f, --issue-file <file> display issue file\n"), out
);
2347 fputs(_(" -h, --flow-control enable hardware flow control\n"), out
);
2348 fputs(_(" -H, --host <hostname> specify login host\n"), out
);
2349 fputs(_(" -i, --noissue do not display issue file\n"), out
);
2350 fputs(_(" -I, --init-string <string> set init string\n"), out
);
2351 fputs(_(" -J --noclear do not clear the screen before prompt\n"), out
);
2352 fputs(_(" -l, --login-program <file> specify login program\n"), out
);
2353 fputs(_(" -L, --local-line[=<mode>] control the local line flag\n"), out
);
2354 fputs(_(" -m, --extract-baud extract baud rate during connect\n"), out
);
2355 fputs(_(" -n, --skip-login do not prompt for login\n"), out
);
2356 fputs(_(" -N --nonewline do not print a newline before issue\n"), out
);
2357 fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out
);
2358 fputs(_(" -p, --login-pause wait for any key before the login\n"), out
);
2359 fputs(_(" -r, --chroot <dir> change root to the directory\n"), out
);
2360 fputs(_(" -R, --hangup do virtually hangup on the tty\n"), out
);
2361 fputs(_(" -s, --keep-baud try to keep baud rate after break\n"), out
);
2362 fputs(_(" -t, --timeout <number> login process timeout\n"), out
);
2363 fputs(_(" -U, --detect-case detect uppercase terminal\n"), out
);
2364 fputs(_(" -w, --wait-cr wait carriage-return\n"), out
);
2365 fputs(_(" --nohints do not print hints\n"), out
);
2366 fputs(_(" --nohostname no hostname at all will be shown\n"), out
);
2367 fputs(_(" --long-hostname show full qualified hostname\n"), out
);
2368 fputs(_(" --erase-chars <string> additional backspace chars\n"), out
);
2369 fputs(_(" --kill-chars <string> additional kill chars\n"), out
);
2370 fputs(_(" --chdir <directory> chdir before the login\n"), out
);
2371 fputs(_(" --delay <number> sleep seconds before prompt\n"), out
);
2372 fputs(_(" --nice <number> run login with this priority\n"), out
);
2373 fputs(_(" --reload reload prompts on running agetty instances\n"), out
);
2374 fputs(_(" --list-speeds display supported baud rates\n"), out
);
2375 printf( " --help %s\n", USAGE_OPTSTR_HELP
);
2376 printf( " --version %s\n", USAGE_OPTSTR_VERSION
);
2377 printf(USAGE_MAN_TAIL("agetty(8)"));
2382 static void list_speeds(void)
2384 const struct Speedtab
*sp
;
2386 for (sp
= speedtab
; sp
->speed
; sp
++)
2387 printf("%10ld\n", sp
->speed
);
2391 * Helper function reports errors to console or syslog.
2392 * Will be used by log_err() and log_warn() therefore
2393 * it takes a format as well as va_list.
2395 static void dolog(int priority
2397 __attribute__((__unused__
))
2399 , const char *fmt
, va_list ap
)
2403 * If the diagnostic is reported via syslog(3), the process name is
2404 * automatically prepended to the message. If we write directly to
2405 * /dev/console, we must prepend the process name ourselves.
2407 openlog(program_invocation_short_name
, LOG_PID
, LOG_AUTHPRIV
);
2408 vsyslog(priority
, fmt
, ap
);
2412 * Write the diagnostic directly to /dev/console if we do not use
2413 * the syslog(3) facility.
2416 char new_fmt
[BUFSIZ
];
2419 snprintf(new_fmt
, sizeof(new_fmt
), "%s: %s\r\n",
2420 program_invocation_short_name
, fmt
);
2421 /* Terminate with CR-LF since the console mode is unknown. */
2422 vsnprintf(buf
, sizeof(buf
), new_fmt
, ap
);
2424 if ((fd
= open("/dev/console", 1)) >= 0) {
2425 write_all(fd
, buf
, strlen(buf
));
2428 #endif /* USE_SYSLOG */
2431 static void exit_slowly(int code
)
2433 /* Be kind to init(8). */
2438 static void log_err(const char *fmt
, ...)
2443 dolog(LOG_ERR
, fmt
, ap
);
2446 exit_slowly(EXIT_FAILURE
);
2449 static void log_warn(const char *fmt
, ...)
2454 dolog(LOG_WARNING
, fmt
, ap
);
2458 static void print_addr(struct issue
*ie
, sa_family_t family
, void *addr
)
2460 char buff
[INET6_ADDRSTRLEN
+ 1];
2462 inet_ntop(family
, addr
, buff
, sizeof(buff
));
2463 fprintf(ie
->output
, "%s", buff
);
2467 * Prints IP for the specified interface (@iface), if the interface is not
2468 * specified then prints the "best" one (UP, RUNNING, non-LOOPBACK). If not
2469 * found the "best" interface then prints at least host IP.
2471 static void output_iface_ip(struct issue
*ie
,
2472 struct ifaddrs
*addrs
,
2477 struct addrinfo hints
, *info
= NULL
;
2484 for (p
= addrs
; p
; p
= p
->ifa_next
) {
2488 p
->ifa_addr
->sa_family
!= family
)
2492 /* Filter out by interface name */
2493 if (strcmp(p
->ifa_name
, iface
) != 0)
2496 /* Select the "best" interface */
2497 if ((p
->ifa_flags
& IFF_LOOPBACK
) ||
2498 !(p
->ifa_flags
& IFF_UP
) ||
2499 !(p
->ifa_flags
& IFF_RUNNING
))
2504 switch (p
->ifa_addr
->sa_family
) {
2506 addr
= &((struct sockaddr_in
*) p
->ifa_addr
)->sin_addr
;
2509 addr
= &((struct sockaddr_in6
*) p
->ifa_addr
)->sin6_addr
;
2514 print_addr(ie
, family
, addr
);
2522 /* Hmm.. not found the best interface, print host IP at least */
2523 memset(&hints
, 0, sizeof(hints
));
2524 hints
.ai_family
= family
;
2525 if (family
== AF_INET6
)
2526 hints
.ai_flags
= AI_V4MAPPED
;
2528 host
= xgethostname();
2529 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2530 switch (info
->ai_family
) {
2532 addr
= &((struct sockaddr_in
*) info
->ai_addr
)->sin_addr
;
2535 addr
= &((struct sockaddr_in6
*) info
->ai_addr
)->sin6_addr
;
2539 print_addr(ie
, family
, addr
);
2547 * parses \x{argument}, if not argument specified then returns NULL, the @fd
2548 * has to point to one char after the sequence (it means '{').
2550 static char *get_escape_argument(FILE *fd
, char *buf
, size_t bufsz
)
2555 if (c
== EOF
|| (unsigned char) c
!= '{') {
2564 if ((unsigned char) c
!= '}' && i
< bufsz
- 1)
2565 buf
[i
++] = (unsigned char) c
;
2567 } while ((unsigned char) c
!= '}');
2573 static void output_special_char(struct issue
*ie
,
2584 char escname
[UL_COLORNAME_MAXSZ
];
2586 if (get_escape_argument(fp
, escname
, sizeof(escname
))) {
2587 const char *esc
= color_sequence_from_colorname(escname
);
2589 fputs(esc
, ie
->output
);
2591 fputs("\033", ie
->output
);
2596 fprintf(ie
->output
, "%s", uts
.sysname
);
2600 fprintf(ie
->output
, "%s", uts
.nodename
);
2604 fprintf(ie
->output
, "%s", uts
.release
);
2608 fprintf(ie
->output
, "%s", uts
.version
);
2612 fprintf(ie
->output
, "%s", uts
.machine
);
2616 char *dom
= xgetdomainname();
2618 fputs(dom
? dom
: "unknown_domain", ie
->output
);
2625 char *host
= xgethostname();
2626 struct addrinfo hints
, *info
= NULL
;
2628 memset(&hints
, 0, sizeof(hints
));
2629 hints
.ai_flags
= AI_CANONNAME
;
2631 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2634 if (info
->ai_canonname
&&
2635 (canon
= strchr(info
->ai_canonname
, '.')))
2638 fputs(dom
? dom
: "unknown_domain", ie
->output
);
2651 tm
= localtime(&now
);
2656 if (c
== 'd') /* ISO 8601 */
2657 fprintf(ie
->output
, "%s %s %d %d",
2658 nl_langinfo(ABDAY_1
+ tm
->tm_wday
),
2659 nl_langinfo(ABMON_1
+ tm
->tm_mon
),
2661 tm
->tm_year
< 70 ? tm
->tm_year
+ 2000 :
2662 tm
->tm_year
+ 1900);
2664 fprintf(ie
->output
, "%02d:%02d:%02d",
2665 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2669 fprintf (ie
->output
, "%s", op
->tty
);
2673 const speed_t speed
= cfgetispeed(tp
);
2676 for (i
= 0; speedtab
[i
].speed
; i
++) {
2677 if (speedtab
[i
].code
== speed
) {
2678 fprintf(ie
->output
, "%ld", speedtab
[i
].speed
);
2686 char *var
= NULL
, varname
[64];
2689 if (get_escape_argument(fp
, varname
, sizeof(varname
))) {
2690 var
= read_os_release(op
, varname
);
2692 if (strcmp(varname
, "ANSI_COLOR") == 0)
2693 fprintf(ie
->output
, "\033[%sm", var
);
2695 fputs(var
, ie
->output
);
2698 } else if ((var
= read_os_release(op
, "PRETTY_NAME"))) {
2699 fputs(var
, ie
->output
);
2701 /* \S and PRETTY_NAME not found */
2704 fputs(uts
.sysname
, ie
->output
);
2717 while ((ut
= getutxent()))
2718 if (ut
->ut_type
== USER_PROCESS
)
2722 fprintf(ie
->output
, P_("%d user", "%d users", users
), users
);
2724 fprintf (ie
->output
, "%d ", users
);
2727 #if defined(RTMGRP_IPV4_IFADDR) && defined(RTMGRP_IPV6_IFADDR)
2731 sa_family_t family
= c
== '4' ? AF_INET
: AF_INET6
;
2732 struct ifaddrs
*addrs
= NULL
;
2735 if (getifaddrs(&addrs
))
2738 if (get_escape_argument(fp
, iface
, sizeof(iface
)))
2739 output_iface_ip(ie
, addrs
, iface
, family
);
2741 output_iface_ip(ie
, addrs
, NULL
, family
);
2746 netlink_groups
|= RTMGRP_IPV4_IFADDR
;
2748 netlink_groups
|= RTMGRP_IPV6_IFADDR
;
2753 putc(c
, ie
->output
);
2758 static void init_special_char(char* arg
, struct options
*op
)
2763 op
->initstring
= malloc(strlen(arg
) + 1);
2764 if (!op
->initstring
)
2765 log_err(_("failed to allocate memory: %m"));
2768 * Copy optarg into op->initstring decoding \ddd octal
2774 /* The \\ is converted to \ */
2781 /* Handle \000 - \177. */
2783 for (i
= 1; i
<= 3; i
++) {
2784 if (*p
>= '0' && *p
<= '7') {
2801 * Appends @str to @dest and if @dest is not empty then use @sep as a
2802 * separator. The maximal final length of the @dest is @len.
2804 * Returns the final @dest length or -1 in case of error.
2806 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
)
2808 size_t dsz
= 0, ssz
= 0, sz
;
2811 if (!dest
|| !len
|| !src
)
2820 if (dsz
+ ssz
+ sz
+ 1 > len
)
2825 memcpy(p
, sep
, ssz
);
2831 return dsz
+ ssz
+ sz
;
2835 * Do not allow the user to pass an option as a user name
2836 * To be more safe: Use `--' to make sure the rest is
2837 * interpreted as non-options by the program, if it supports it.
2839 static void check_username(const char* nm
)
2844 if (strlen(nm
) > 42)
2853 log_err(_("checkname failed: %m"));
2856 static void reload_agettys(void)
2858 #ifdef AGETTY_RELOAD
2859 int fd
= open(AGETTY_RELOAD_FILENAME
, O_CREAT
|O_CLOEXEC
|O_WRONLY
,
2862 err(EXIT_FAILURE
, _("cannot open %s"), AGETTY_RELOAD_FILENAME
);
2864 if (futimens(fd
, NULL
) < 0 || close(fd
) < 0)
2865 err(EXIT_FAILURE
, _("cannot touch file %s"),
2866 AGETTY_RELOAD_FILENAME
);
2869 errx(EXIT_FAILURE
, _("--reload is unsupported on your system"));