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>
42 #include "pathnames.h"
46 #include "color-names.h"
49 #ifdef USE_PLYMOUTH_SUPPORT
50 # include "plymouth-ctrl.h"
53 #ifdef HAVE_SYS_PARAM_H
54 # include <sys/param.h>
57 #if defined(__FreeBSD_kernel__)
62 # ifdef HAVE_LIBUTIL_H
70 # ifndef DEFAULT_VCTERM
71 # define DEFAULT_VCTERM "linux"
73 # if defined (__s390__) || defined (__s390x__)
74 # define DEFAULT_TTYS0 "dumb"
75 # define DEFAULT_TTY32 "ibm327x"
76 # define DEFAULT_TTYS1 "vt220"
78 # ifndef DEFAULT_STERM
79 # define DEFAULT_STERM "vt102"
81 #elif defined(__GNU__)
83 # ifndef DEFAULT_VCTERM
84 # define DEFAULT_VCTERM "hurd"
86 # ifndef DEFAULT_STERM
87 # define DEFAULT_STERM "vt102"
90 # ifndef DEFAULT_VCTERM
91 # define DEFAULT_VCTERM "vt100"
93 # ifndef DEFAULT_STERM
94 # define DEFAULT_STERM "vt100"
98 #ifdef __FreeBSD_kernel__
102 /* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */
108 * Some heuristics to find out what environment we are in: if it is not
109 * System V, assume it is SunOS 4. The LOGIN_PROCESS is defined in System V
110 * utmp.h, which will select System V style getty.
117 * Things you may want to modify.
119 * If ISSUE is not defined, agetty will never display the contents of the
120 * /etc/issue file. You will not want to spit out large "issue" files at the
121 * wrong baud rate. Relevant for System V only.
123 * You may disagree with the default line-editing etc. characters defined
124 * below. Note, however, that DEL cannot be used for interrupt generation
125 * and for line editing at the same time.
128 /* Displayed before the login prompt. */
130 # define ISSUE _PATH_ISSUE
131 # include <sys/utsname.h>
135 #define LOGIN "login: "
136 #define LOGIN_ARGV_MAX 16 /* Numbers of args for login */
142 # include <sys/inotify.h>
143 # include <linux/netlink.h>
144 # include <linux/rtnetlink.h>
145 # define AGETTY_RELOAD_FILENAME "/run/agetty.reload" /* trigger file */
146 # define AGETTY_RELOAD_FDNONE -2 /* uninitialized fd */
147 static int inotify_fd
= AGETTY_RELOAD_FDNONE
;
148 static int netlink_fd
= AGETTY_RELOAD_FDNONE
;
152 * When multiple baud rates are specified on the command line, the first one
153 * we will try is the first one specified.
155 #define FIRST_SPEED 0
157 /* Storage for command-line options. */
158 #define MAX_SPEED 10 /* max. nr. of baud rates */
161 int flags
; /* toggle switches, see below */
162 unsigned int timeout
; /* time-out period */
163 char *autolog
; /* login the user automatically */
164 char *chdir
; /* Chdir before the login */
165 char *chroot
; /* Chroot before the login */
166 char *login
; /* login program */
167 char *logopt
; /* options for login program */
168 char *tty
; /* name of tty */
169 char *vcline
; /* line of virtual console */
170 char *term
; /* terminal type */
171 char *initstring
; /* modem init string */
172 char *issue
; /* alternative issue file */
173 char *erasechars
; /* string with erase chars */
174 char *killchars
; /* string with kill chars */
175 char *osrelease
; /* /etc/os-release data */
176 unsigned int delay
; /* Sleep seconds before prompt */
177 int nice
; /* Run login with this priority */
178 int numspeed
; /* number of baud rates to try */
179 int clocal
; /* CLOCAL_MODE_* */
180 int kbmode
; /* Keyboard mode if virtual console */
181 speed_t speeds
[MAX_SPEED
]; /* baud rates to be tried */
185 CLOCAL_MODE_AUTO
= 0,
190 #define F_PARSE (1<<0) /* process modem status messages */
191 #define F_ISSUE (1<<1) /* display /etc/issue */
192 #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
194 #define F_INITSTRING (1<<4) /* initstring is set */
195 #define F_WAITCRLF (1<<5) /* wait for CR or LF */
196 #define F_CUSTISSUE (1<<6) /* give alternative issue file */
197 #define F_NOPROMPT (1<<7) /* do not ask for login name! */
198 #define F_LCUC (1<<8) /* support for *LCUC stty modes */
199 #define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */
200 #define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */
201 #define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */
202 #define F_VCONSOLE (1<<12) /* This is a virtual console */
203 #define F_HANGUP (1<<13) /* Do call vhangup(2) */
204 #define F_UTF8 (1<<14) /* We can do UTF8 */
205 #define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */
206 #define F_NOCLEAR (1<<16) /* Do not clear the screen before prompting */
207 #define F_NONL (1<<17) /* No newline before issue */
208 #define F_NOHOSTNAME (1<<18) /* Do not show the hostname */
209 #define F_LONGHNAME (1<<19) /* Show Full qualified hostname */
210 #define F_NOHINTS (1<<20) /* Don't print hints */
211 #define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */
213 #define serial_tty_option(opt, flag) \
214 (((opt)->flags & (F_VCONSOLE|(flag))) == (flag))
221 static const struct Speedtab speedtab
[] = {
293 static void init_special_char(char* arg
, struct options
*op
);
294 static void parse_args(int argc
, char **argv
, struct options
*op
);
295 static void parse_speeds(struct options
*op
, char *arg
);
296 static void update_utmp(struct options
*op
);
297 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
);
298 static void termio_init(struct options
*op
, struct termios
*tp
);
299 static void reset_vc (const struct options
*op
, struct termios
*tp
);
300 static void auto_baud(struct termios
*tp
);
301 static void output_special_char (unsigned char c
, struct options
*op
,
302 struct termios
*tp
, FILE *fp
);
303 static void do_prompt(struct options
*op
, struct termios
*tp
);
304 static void next_speed(struct options
*op
, struct termios
*tp
);
305 static char *get_logname(struct options
*op
,
306 struct termios
*tp
, struct chardata
*cp
);
307 static void termio_final(struct options
*op
,
308 struct termios
*tp
, struct chardata
*cp
);
309 static int caps_lock(char *s
);
310 static speed_t
bcode(char *s
);
311 static void usage(FILE * out
) __attribute__((__noreturn__
));
312 static void log_err(const char *, ...) __attribute__((__noreturn__
))
313 __attribute__((__format__(printf
, 1, 2)));
314 static void log_warn (const char *, ...)
315 __attribute__((__format__(printf
, 1, 2)));
316 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
);
317 static void check_username (const char* nm
);
318 static void login_options_to_argv(char *argv
[], int *argc
, char *str
, char *username
);
319 static void reload_agettys(void);
321 /* Fake hostname for ut_host specified on command line. */
322 static char *fakehost
;
325 # include "closestream.h"
326 # ifndef DEBUG_OUTPUT
327 # define DEBUG_OUTPUT "/dev/tty10"
329 # define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
332 # define debug(s) do { ; } while (0)
335 int main(int argc
, char **argv
)
337 char *username
= NULL
; /* login name, given to /bin/login */
338 struct chardata chardata
; /* will be set by get_logname() */
339 struct termios termios
; /* terminal mode bits */
340 struct options options
= {
341 .flags
= F_ISSUE
, /* show /etc/issue (SYSV_STYLE) */
342 .login
= _PATH_LOGIN
, /* default login program */
343 .tty
= "tty1", /* default tty line */
344 .issue
= ISSUE
/* default issue file */
346 char *login_argv
[LOGIN_ARGV_MAX
+ 1];
348 struct sigaction sa
, sa_hup
, sa_quit
, sa_int
;
351 setlocale(LC_ALL
, "");
352 bindtextdomain(PACKAGE
, LOCALEDIR
);
355 /* In case vhangup(2) has to called */
356 sa
.sa_handler
= SIG_IGN
;
357 sa
.sa_flags
= SA_RESTART
;
358 sigemptyset (&sa
.sa_mask
);
359 sigaction(SIGHUP
, &sa
, &sa_hup
);
360 sigaction(SIGQUIT
, &sa
, &sa_quit
);
361 sigaction(SIGINT
, &sa
, &sa_int
);
364 dbf
= fopen(DEBUG_OUTPUT
, "w");
365 for (int i
= 1; i
< argc
; i
++) {
371 #endif /* DEBUGGING */
373 /* Parse command-line arguments. */
374 parse_args(argc
, argv
, &options
);
376 login_argv
[login_argc
++] = options
.login
; /* set login program name */
378 /* Update the utmp file. */
380 update_utmp(&options
);
383 sleep(options
.delay
);
385 debug("calling open_tty\n");
387 /* Open the tty as standard { input, output, error }. */
388 open_tty(options
.tty
, &termios
, &options
);
390 /* Unmask SIGHUP if inherited */
392 sigaddset(&set
, SIGHUP
);
393 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
394 sigaction(SIGHUP
, &sa_hup
, NULL
);
396 tcsetpgrp(STDIN_FILENO
, getpid());
398 /* Default is to follow the current line speed and then default to 9600 */
399 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
== 0) {
400 options
.speeds
[options
.numspeed
++] = bcode("9600");
401 options
.flags
|= F_KEEPSPEED
;
404 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
405 debug("calling termio_init\n");
406 termio_init(&options
, &termios
);
408 /* Write the modem init string and DO NOT flush the buffers. */
409 if (serial_tty_option(&options
, F_INITSTRING
) &&
410 options
.initstring
&& *options
.initstring
!= '\0') {
411 debug("writing init string\n");
412 write_all(STDOUT_FILENO
, options
.initstring
,
413 strlen(options
.initstring
));
416 if (options
.flags
& F_VCONSOLE
|| options
.clocal
!= CLOCAL_MODE_ALWAYS
)
417 /* Go to blocking mode unless -L is specified, this change
418 * affects stdout, stdin and stderr as all the file descriptors
419 * are created by dup(). */
420 fcntl(STDOUT_FILENO
, F_SETFL
,
421 fcntl(STDOUT_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
423 /* Optionally detect the baud rate from the modem status message. */
424 debug("before autobaud\n");
425 if (serial_tty_option(&options
, F_PARSE
))
428 /* Set the optional timer. */
430 alarm(options
.timeout
);
432 /* Optionally wait for CR or LF before writing /etc/issue */
433 if (serial_tty_option(&options
, F_WAITCRLF
)) {
436 debug("waiting for cr-lf\n");
437 while (read(STDIN_FILENO
, &ch
, 1) == 1) {
438 /* Strip "parity bit". */
441 fprintf(dbf
, "read %c\n", ch
);
443 if (ch
== '\n' || ch
== '\r')
448 INIT_CHARDATA(&chardata
);
450 if (options
.autolog
) {
451 debug("doing auto login\n");
452 username
= options
.autolog
;
455 if ((options
.flags
& F_NOPROMPT
) == 0) {
456 if (options
.autolog
) {
457 /* Autologin prompt */
458 do_prompt(&options
, &termios
);
459 printf(_("%s%s (automatic login)\n"), LOGIN
, options
.autolog
);
461 /* Read the login name. */
462 debug("reading login name\n");
464 get_logname(&options
, &termios
, &chardata
)) == NULL
)
465 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
)
466 next_speed(&options
, &termios
);
474 if ((options
.flags
& F_VCONSOLE
) == 0) {
475 /* Finalize the termios settings. */
476 termio_final(&options
, &termios
, &chardata
);
478 /* Now the newline character should be properly written. */
479 write_all(STDOUT_FILENO
, "\r\n", 2);
482 sigaction(SIGQUIT
, &sa_quit
, NULL
);
483 sigaction(SIGINT
, &sa_int
, NULL
);
486 check_username(username
);
488 if (options
.logopt
) {
490 * The --login-options completely overwrites the default
491 * way how agetty composes login(1) command line.
493 login_options_to_argv(login_argv
, &login_argc
,
494 options
.logopt
, username
);
496 if (fakehost
&& (options
.flags
& F_REMOTE
)) {
497 login_argv
[login_argc
++] = "-h";
498 login_argv
[login_argc
++] = fakehost
;
502 login_argv
[login_argc
++] = "-f";
504 login_argv
[login_argc
++] = "--";
505 login_argv
[login_argc
++] = username
;
509 login_argv
[login_argc
] = NULL
; /* last login argv */
511 if (options
.chroot
&& chroot(options
.chroot
) < 0)
512 log_err(_("%s: can't change root directory %s: %m"),
513 options
.tty
, options
.chroot
);
514 if (options
.chdir
&& chdir(options
.chdir
) < 0)
515 log_err(_("%s: can't change working directory %s: %m"),
516 options
.tty
, options
.chdir
);
517 if (options
.nice
&& nice(options
.nice
) < 0)
518 log_warn(_("%s: can't change process priority: %m"),
521 free(options
.osrelease
);
523 if (close_stream(dbf
) != 0)
524 log_err("write failed: %s", DEBUG_OUTPUT
);
527 /* Let the login program take care of password validation. */
528 execv(options
.login
, login_argv
);
529 log_err(_("%s: can't exec %s: %m"), options
.tty
, login_argv
[0]);
533 * Returns : @str if \u not found
534 * : @username if @str equal to "\u"
535 * : newly allocated string if \u mixed with something other
537 static char *replace_u(char *str
, char *username
)
539 char *entry
= NULL
, *p
= str
;
540 size_t usz
= username
? strlen(username
) : 0;
544 char *tp
, *old
= entry
;
546 if (memcmp(p
, "\\u", 2)) {
548 continue; /* no \u */
552 if (p
== str
&& sz
== 2) {
553 /* 'str' contains only '\u' */
558 tp
= entry
= malloc(sz
+ usz
);
560 log_err(_("failed to allocate memory: %m"));
563 /* copy chars before \u */
564 memcpy(tp
, str
, p
- str
);
569 memcpy(tp
, username
, usz
);
573 /* copy chars after \u + \0 */
574 memcpy(tp
, p
+ 2, sz
- (p
- str
) - 1);
583 return entry
? entry
: str
;
586 static void login_options_to_argv(char *argv
[], int *argc
,
587 char *str
, char *username
)
592 while (str
&& isspace(*str
))
596 while (p
&& *p
&& i
< LOGIN_ARGV_MAX
) {
599 while (isspace(*++p
))
602 argv
[i
++] = replace_u(str
, username
);
608 if (str
&& *str
&& i
< LOGIN_ARGV_MAX
)
609 argv
[i
++] = replace_u(str
, username
);
613 #define is_speed(str) (strlen((str)) == strspn((str), "0123456789,"))
615 /* Parse command-line arguments. */
616 static void parse_args(int argc
, char **argv
, struct options
*op
)
621 VERSION_OPTION
= CHAR_MAX
+ 1,
630 const struct option longopts
[] = {
631 { "8bits", no_argument
, 0, '8' },
632 { "autologin", required_argument
, 0, 'a' },
633 { "noreset", no_argument
, 0, 'c' },
634 { "chdir", required_argument
, 0, 'C' },
635 { "delay", required_argument
, 0, 'd' },
636 { "remote", no_argument
, 0, 'E' },
637 { "issue-file", required_argument
, 0, 'f' },
638 { "flow-control", no_argument
, 0, 'h' },
639 { "host", required_argument
, 0, 'H' },
640 { "noissue", no_argument
, 0, 'i' },
641 { "init-string", required_argument
, 0, 'I' },
642 { "noclear", no_argument
, 0, 'J' },
643 { "login-program", required_argument
, 0, 'l' },
644 { "local-line", optional_argument
, 0, 'L' },
645 { "extract-baud", no_argument
, 0, 'm' },
646 { "skip-login", no_argument
, 0, 'n' },
647 { "nonewline", no_argument
, 0, 'N' },
648 { "login-options", required_argument
, 0, 'o' },
649 { "login-pause", no_argument
, 0, 'p' },
650 { "nice", required_argument
, 0, 'P' },
651 { "chroot", required_argument
, 0, 'r' },
652 { "hangup", no_argument
, 0, 'R' },
653 { "keep-baud", no_argument
, 0, 's' },
654 { "timeout", required_argument
, 0, 't' },
655 { "detect-case", no_argument
, 0, 'U' },
656 { "wait-cr", no_argument
, 0, 'w' },
657 { "nohints", no_argument
, 0, NOHINTS_OPTION
},
658 { "nohostname", no_argument
, 0, NOHOSTNAME_OPTION
},
659 { "long-hostname", no_argument
, 0, LONGHOSTNAME_OPTION
},
660 { "reload", no_argument
, 0, RELOAD_OPTION
},
661 { "version", no_argument
, 0, VERSION_OPTION
},
662 { "help", no_argument
, 0, HELP_OPTION
},
663 { "erase-chars", required_argument
, 0, ERASE_CHARS_OPTION
},
664 { "kill-chars", required_argument
, 0, KILL_CHARS_OPTION
},
668 while ((c
= getopt_long(argc
, argv
,
669 "8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts
,
673 op
->flags
|= F_EIGHTBITS
;
676 op
->autolog
= optarg
;
679 op
->flags
|= F_KEEPCFLAGS
;
685 op
->delay
= strtou32_or_err(optarg
, _("invalid delay argument"));
688 op
->flags
|= F_REMOTE
;
691 op
->flags
|= F_CUSTISSUE
;
695 op
->flags
|= F_RTSCTS
;
701 op
->flags
&= ~F_ISSUE
;
704 init_special_char(optarg
, op
);
705 op
->flags
|= F_INITSTRING
;
708 op
->flags
|= F_NOCLEAR
;
714 /* -L and -L=always have the same meaning */
715 op
->clocal
= CLOCAL_MODE_ALWAYS
;
717 if (strcmp(optarg
, "=always") == 0)
718 op
->clocal
= CLOCAL_MODE_ALWAYS
;
719 else if (strcmp(optarg
, "=never") == 0)
720 op
->clocal
= CLOCAL_MODE_NEVER
;
721 else if (strcmp(optarg
, "=auto") == 0)
722 op
->clocal
= CLOCAL_MODE_AUTO
;
724 log_err(_("invalid argument of --local-line"));
728 op
->flags
|= F_PARSE
;
731 op
->flags
|= F_NOPROMPT
;
740 op
->flags
|= F_LOGINPAUSE
;
743 op
->nice
= strtos32_or_err(optarg
, _("invalid nice argument"));
749 op
->flags
|= F_HANGUP
;
752 op
->flags
|= F_KEEPSPEED
;
755 op
->timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
761 op
->flags
|= F_WAITCRLF
;
764 op
->flags
|= F_NOHINTS
;
766 case NOHOSTNAME_OPTION
:
767 op
->flags
|= F_NOHOSTNAME
;
769 case LONGHOSTNAME_OPTION
:
770 op
->flags
|= F_LONGHNAME
;
772 case ERASE_CHARS_OPTION
:
773 op
->erasechars
= optarg
;
775 case KILL_CHARS_OPTION
:
776 op
->killchars
= optarg
;
782 printf(UTIL_LINUX_VERSION
);
791 debug("after getopt loop\n");
793 if (argc
< optind
+ 1) {
794 log_warn(_("not enough arguments"));
798 /* Accept "tty", "baudrate tty", and "tty baudrate". */
799 if (is_speed(argv
[optind
])) {
800 /* Assume BSD style speed. */
801 parse_speeds(op
, argv
[optind
++]);
802 if (argc
< optind
+ 1) {
803 warn(_("not enough arguments"));
806 op
->tty
= argv
[optind
++];
808 op
->tty
= argv
[optind
++];
810 char *v
= argv
[optind
];
818 /* On virtual console remember the line which is used for */
819 if (strncmp(op
->tty
, "tty", 3) == 0 &&
820 strspn(op
->tty
+ 3, "0123456789") == strlen(op
->tty
+3))
821 op
->vcline
= op
->tty
+3;
823 if (argc
> optind
&& argv
[optind
])
824 op
->term
= argv
[optind
];
826 #ifdef DO_DEVFS_FIDDLING
828 * Some devfs junk, following Goswin Brederlow:
829 * turn ttyS<n> into tts/<n>
830 * turn tty<n> into vc/<n>
831 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=72241
833 if (op
->tty
&& strlen(op
->tty
) < 90) {
837 if (strncmp(op
->tty
, "ttyS", 4) == 0) {
838 strcpy(dev_name
, "/dev/");
839 strcat(dev_name
, op
->tty
);
840 if (stat(dev_name
, &st
) < 0) {
841 strcpy(dev_name
, "/dev/tts/");
842 strcat(dev_name
, op
->tty
+ 4);
843 if (stat(dev_name
, &st
) == 0) {
844 op
->tty
= strdup(dev_name
+ 5);
846 log_err(_("failed to allocate memory: %m"));
849 } else if (strncmp(op
->tty
, "tty", 3) == 0) {
850 strcpy(dev_name
, "/dev/");
851 strncat(dev_name
, op
->tty
, 90);
852 if (stat(dev_name
, &st
) < 0) {
853 strcpy(dev_name
, "/dev/vc/");
854 strcat(dev_name
, op
->tty
+ 3);
855 if (stat(dev_name
, &st
) == 0) {
856 op
->tty
= strdup(dev_name
+ 5);
858 log_err(_("failed to allocate memory: %m"));
863 #endif /* DO_DEVFS_FIDDLING */
865 debug("exiting parseargs\n");
868 /* Parse alternate baud rates. */
869 static void parse_speeds(struct options
*op
, char *arg
)
872 char *str
= strdup(arg
);
875 log_err(_("failed to allocate memory: %m"));
877 debug("entered parse_speeds:\n");
878 for (cp
= strtok(str
, ","); cp
!= NULL
; cp
= strtok((char *)0, ",")) {
879 if ((op
->speeds
[op
->numspeed
++] = bcode(cp
)) <= 0)
880 log_err(_("bad speed: %s"), cp
);
881 if (op
->numspeed
>= MAX_SPEED
)
882 log_err(_("too many alternate speeds"));
884 debug("exiting parsespeeds\n");
890 /* Update our utmp entry. */
891 static void update_utmp(struct options
*op
)
895 pid_t pid
= getpid();
896 pid_t sid
= getsid(0);
897 char *vcline
= op
->vcline
;
898 char *line
= op
->tty
;
902 * The utmp file holds miscellaneous information about things started by
903 * /sbin/init and other system-related events. Our purpose is to update
904 * the utmp entry for the current process, in particular the process type
905 * and the tty line we are listening to. Return successfully only if the
906 * utmp file can be opened for update, and if we are able to find our
907 * entry in the utmp file.
909 utmpxname(_PATH_UTMP
);
913 * Find my pid in utmp.
915 * FIXME: Earlier (when was that?) code here tested only utp->ut_type !=
916 * INIT_PROCESS, so maybe the >= here should be >.
918 * FIXME: The present code is taken from login.c, so if this is changed,
919 * maybe login has to be changed as well (is this true?).
921 while ((utp
= getutxent()))
922 if (utp
->ut_pid
== pid
923 && utp
->ut_type
>= INIT_PROCESS
924 && utp
->ut_type
<= DEAD_PROCESS
)
928 memcpy(&ut
, utp
, sizeof(ut
));
930 /* Some inits do not initialize utmp. */
931 memset(&ut
, 0, sizeof(ut
));
932 if (vcline
&& *vcline
)
933 /* Standard virtual console devices */
934 strncpy(ut
.ut_id
, vcline
, sizeof(ut
.ut_id
));
936 size_t len
= strlen(line
);
938 if (len
>= sizeof(ut
.ut_id
))
939 ptr
= line
+ len
- sizeof(ut
.ut_id
);
942 strncpy(ut
.ut_id
, ptr
, sizeof(ut
.ut_id
));
946 strncpy(ut
.ut_user
, "LOGIN", sizeof(ut
.ut_user
));
947 strncpy(ut
.ut_line
, line
, sizeof(ut
.ut_line
));
949 strncpy(ut
.ut_host
, fakehost
, sizeof(ut
.ut_host
));
952 ut
.ut_type
= LOGIN_PROCESS
;
959 updwtmpx(_PATH_WTMP
, &ut
);
962 #endif /* SYSV_STYLE */
964 /* Set up tty as stdin, stdout & stderr. */
965 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
)
967 const pid_t pid
= getpid();
973 /* Set up new standard input, unless we are given an already opened port. */
975 if (strcmp(tty
, "-") != 0) {
976 char buf
[PATH_MAX
+1];
977 struct group
*gr
= NULL
;
983 /* Use tty group if available */
984 if ((gr
= getgrnam("tty")))
987 len
= snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
988 if (len
< 0 || (size_t)len
>= sizeof(buf
))
989 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
991 /* Open the tty as standard input. */
992 if ((fd
= open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0)) < 0)
993 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
996 * There is always a race between this reset and the call to
997 * vhangup() that s.o. can use to get access to your tty.
998 * Linux login(1) will change tty permissions. Use root owner and group
999 * with permission -rw------- for the period between getty and login.
1001 if (fchown(fd
, 0, gid
) || fchmod(fd
, (gid
? 0620 : 0600))) {
1003 log_warn("%s: %m", buf
);
1005 log_err("%s: %m", buf
);
1008 /* Sanity checks... */
1009 if (fstat(fd
, &st
) < 0)
1010 log_err("%s: %m", buf
);
1011 if ((st
.st_mode
& S_IFMT
) != S_IFCHR
)
1012 log_err(_("/dev/%s: not a character device"), tty
);
1014 log_err(_("/dev/%s: not a tty"), tty
);
1016 if (((tid
= tcgetsid(fd
)) < 0) || (pid
!= tid
)) {
1017 if (ioctl(fd
, TIOCSCTTY
, 1) == -1)
1018 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1021 close(STDIN_FILENO
);
1024 if (op
->flags
& F_HANGUP
) {
1026 if (ioctl(fd
, TIOCNOTTY
))
1027 debug("TIOCNOTTY ioctl failed\n");
1030 * Let's close all file descriptors before vhangup
1031 * https://lkml.org/lkml/2012/6/5/145
1034 close(STDOUT_FILENO
);
1035 close(STDERR_FILENO
);
1040 log_err(_("/dev/%s: vhangup() failed: %m"), tty
);
1045 if (open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0) != 0)
1046 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1048 if (((tid
= tcgetsid(STDIN_FILENO
)) < 0) || (pid
!= tid
)) {
1049 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 1) == -1)
1050 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1056 * Standard input should already be connected to an open port. Make
1057 * sure it is open for read/write.
1060 if ((fcntl(STDIN_FILENO
, F_GETFL
, 0) & O_RDWR
) != O_RDWR
)
1061 log_err(_("%s: not open for read/write"), tty
);
1065 if (tcsetpgrp(STDIN_FILENO
, pid
))
1066 log_warn(_("/dev/%s: cannot set process group: %m"), tty
);
1068 /* Get rid of the present outputs. */
1070 close(STDOUT_FILENO
);
1071 close(STDERR_FILENO
);
1075 /* Set up standard output and standard error file descriptors. */
1078 /* set up stdout and stderr */
1079 if (dup(STDIN_FILENO
) != 1 || dup(STDIN_FILENO
) != 2)
1080 log_err(_("%s: dup problem: %m"), tty
);
1082 /* make stdio unbuffered for slow modem lines */
1083 setvbuf(stdout
, NULL
, _IONBF
, 0);
1086 * The following ioctl will fail if stdin is not a tty, but also when
1087 * there is noise on the modem control lines. In the latter case, the
1088 * common course of action is (1) fix your cables (2) give the modem
1089 * more time to properly reset after hanging up.
1091 * SunOS users can achieve (2) by patching the SunOS kernel variable
1092 * "zsadtrlow" to a larger value; 5 seconds seems to be a good value.
1093 * http://www.sunmanagers.org/archives/1993/0574.html
1095 memset(tp
, 0, sizeof(struct termios
));
1096 if (tcgetattr(STDIN_FILENO
, tp
) < 0)
1097 log_err(_("%s: failed to get terminal attributes: %m"), tty
);
1099 #if defined (__s390__) || defined (__s390x__)
1102 * Special terminal on first serial line on a S/390(x) which
1103 * is due legacy reasons a block terminal of type 3270 or
1104 * higher. Whereas the second serial line on a S/390(x) is
1105 * a real character terminal which is compatible with VT220.
1107 if (strcmp(op
->tty
, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */
1108 op
->term
= DEFAULT_TTYS0
;
1109 else if (strncmp(op
->tty
, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */
1110 op
->term
= DEFAULT_TTY32
;
1111 else if (strcmp(op
->tty
, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */
1112 op
->term
= DEFAULT_TTYS1
;
1116 #if defined(__FreeBSD_kernel__)
1121 * Detect if this is a virtual console or serial/modem line.
1122 * In case of a virtual console the ioctl KDGKBMODE succeeds
1123 * whereas on other lines it will fails.
1126 if (ioctl(STDIN_FILENO
, KDGKBMODE
, &op
->kbmode
) == 0)
1128 if (ioctl(STDIN_FILENO
, TIOCMGET
, &serial
) < 0 && (errno
== EINVAL
))
1131 op
->flags
|= F_VCONSOLE
;
1133 op
->term
= DEFAULT_VCTERM
;
1139 op
->term
= DEFAULT_STERM
;
1142 if (setenv("TERM", op
->term
, 1) != 0)
1143 log_err(_("failed to set the %s environment variable"), "TERM");
1146 /* Initialize termios settings. */
1147 static void termio_clear(int fd
)
1150 * Do not write a full reset (ESC c) because this destroys
1151 * the unicode mode again if the terminal was in unicode
1152 * mode. Also it clears the CONSOLE_MAGIC features which
1153 * are required for some languages/console-fonts.
1154 * Just put the cursor to the home position (ESC [ H),
1155 * erase everything below the cursor (ESC [ J), and set the
1156 * scrolling region to the full window (ESC [ r)
1158 write_all(fd
, "\033[r\033[H\033[J", 9);
1161 /* Initialize termios settings. */
1162 static void termio_init(struct options
*op
, struct termios
*tp
)
1164 speed_t ispeed
, ospeed
;
1166 #ifdef USE_PLYMOUTH_SUPPORT
1167 struct termios lock
;
1168 int i
= (plymouth_command(MAGIC_PING
) == 0) ? PLYMOUTH_TERMIOS_FLAGS_DELAY
: 0;
1170 plymouth_command(MAGIC_QUIT
);
1173 * Even with TTYReset=no it seems with systemd or plymouth
1174 * the termios flags become changed from under the first
1175 * agetty on a serial system console as the flags are locked.
1177 memset(&lock
, 0, sizeof(struct termios
));
1178 if (ioctl(STDIN_FILENO
, TIOCGLCKTRMIOS
, &lock
) < 0)
1180 if (!lock
.c_iflag
&& !lock
.c_oflag
&& !lock
.c_cflag
&& !lock
.c_lflag
)
1182 debug("termios locked\n");
1185 memset(&lock
, 0, sizeof(struct termios
));
1186 ioctl(STDIN_FILENO
, TIOCSLCKTRMIOS
, &lock
);
1189 if (op
->flags
& F_VCONSOLE
) {
1190 #if defined(IUTF8) && defined(KDGKBMODE)
1191 switch(op
->kbmode
) {
1193 setlocale(LC_CTYPE
, "C.UTF-8");
1194 op
->flags
|= F_UTF8
;
1200 setlocale(LC_CTYPE
, "POSIX");
1201 op
->flags
&= ~F_UTF8
;
1205 setlocale(LC_CTYPE
, "POSIX");
1206 op
->flags
&= ~F_UTF8
;
1210 if ((tp
->c_cflag
& (CS8
|PARODD
|PARENB
)) == CS8
)
1211 op
->flags
|= F_EIGHTBITS
;
1213 if ((op
->flags
& F_NOCLEAR
) == 0)
1214 termio_clear(STDOUT_FILENO
);
1222 if (op
->flags
& F_KEEPSPEED
|| !op
->numspeed
) {
1223 /* Save the original setting. */
1224 ispeed
= cfgetispeed(tp
);
1225 ospeed
= cfgetospeed(tp
);
1227 if (!ispeed
) ispeed
= TTYDEF_SPEED
;
1228 if (!ospeed
) ospeed
= TTYDEF_SPEED
;
1231 ospeed
= ispeed
= op
->speeds
[FIRST_SPEED
];
1235 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
1236 * Special characters are set after we have read the login name; all
1237 * reads will be done in raw mode anyway. Errors will be dealt with
1242 tp
->c_iflag
= tp
->c_iflag
& IUTF8
;
1243 if (tp
->c_iflag
& IUTF8
)
1244 op
->flags
|= F_UTF8
;
1249 tp
->c_oflag
&= OPOST
| ONLCR
;
1251 if ((op
->flags
& F_KEEPCFLAGS
) == 0)
1252 tp
->c_cflag
= CS8
| HUPCL
| CREAD
| (tp
->c_cflag
& CLOCAL
);
1255 * Note that the speed is stored in the c_cflag termios field, so we have
1256 * set the speed always when the cflag is reset.
1258 cfsetispeed(tp
, ispeed
);
1259 cfsetospeed(tp
, ospeed
);
1261 /* The default is to follow setting from kernel, but it's possible
1262 * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/
1263 switch (op
->clocal
) {
1264 case CLOCAL_MODE_ALWAYS
:
1265 tp
->c_cflag
|= CLOCAL
; /* -L or -L=always */
1267 case CLOCAL_MODE_NEVER
:
1268 tp
->c_cflag
&= ~CLOCAL
; /* -L=never */
1270 case CLOCAL_MODE_AUTO
: /* -L=auto */
1274 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
1278 tp
->c_cc
[VTIME
] = 0;
1280 /* Check for terminal size and if not found set default */
1281 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0) {
1283 if (ws
.ws_row
== 0) {
1287 if (ws
.ws_col
== 0) {
1291 if (ioctl(STDIN_FILENO
, TIOCSWINSZ
, &ws
))
1292 debug("TIOCSWINSZ ioctl failed\n");
1295 /* Optionally enable hardware flow control. */
1297 if (op
->flags
& F_RTSCTS
)
1298 tp
->c_cflag
|= CRTSCTS
;
1300 /* Flush input and output queues, important for modems! */
1301 tcflush(STDIN_FILENO
, TCIOFLUSH
);
1303 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
))
1304 log_warn(_("setting terminal attributes failed: %m"));
1306 /* Go to blocking input even in local mode. */
1307 fcntl(STDIN_FILENO
, F_SETFL
,
1308 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1310 debug("term_io 2\n");
1313 /* Reset virtual console on stdin to its defaults */
1314 static void reset_vc(const struct options
*op
, struct termios
*tp
)
1318 fl
|= (op
->flags
& F_KEEPCFLAGS
) == 0 ? 0 : UL_TTY_KEEPCFLAGS
;
1319 fl
|= (op
->flags
& F_UTF8
) == 0 ? 0 : UL_TTY_UTF8
;
1321 reset_virtual_console(tp
, fl
);
1323 if (tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
))
1324 log_warn(_("setting terminal attributes failed: %m"));
1326 /* Go to blocking input even in local mode. */
1327 fcntl(STDIN_FILENO
, F_SETFL
,
1328 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1331 /* Extract baud rate from modem status message. */
1332 static void auto_baud(struct termios
*tp
)
1342 * This works only if the modem produces its status code AFTER raising
1343 * the DCD line, and if the computer is fast enough to set the proper
1344 * baud rate before the message has gone by. We expect a message of the
1347 * <junk><number><junk>
1349 * The number is interpreted as the baud rate of the incoming call. If the
1350 * modem does not tell us the baud rate within one second, we will keep
1351 * using the current baud rate. It is advisable to enable BREAK
1352 * processing (comma-separated list of baud rates) if the processing of
1353 * modem status messages is enabled.
1357 * Use 7-bit characters, don't block if input queue is empty. Errors will
1358 * be dealt with later on.
1360 iflag
= tp
->c_iflag
;
1361 /* Enable 8th-bit stripping. */
1362 tp
->c_iflag
|= ISTRIP
;
1363 vmin
= tp
->c_cc
[VMIN
];
1364 /* Do not block when queue is empty. */
1366 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1369 * Wait for a while, then read everything the modem has said so far and
1370 * try to extract the speed of the dial-in call.
1373 if ((nread
= read(STDIN_FILENO
, buf
, sizeof(buf
) - 1)) > 0) {
1375 for (bp
= buf
; bp
< buf
+ nread
; bp
++)
1376 if (isascii(*bp
) && isdigit(*bp
)) {
1377 if ((speed
= bcode(bp
))) {
1378 cfsetispeed(tp
, speed
);
1379 cfsetospeed(tp
, speed
);
1385 /* Restore terminal settings. Errors will be dealt with later on. */
1386 tp
->c_iflag
= iflag
;
1387 tp
->c_cc
[VMIN
] = vmin
;
1388 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1391 static char *xgethostname(void)
1394 size_t sz
= get_hostname_max() + 1;
1396 name
= malloc(sizeof(char) * sz
);
1398 log_err(_("failed to allocate memory: %m"));
1400 if (gethostname(name
, sz
) != 0) {
1404 name
[sz
- 1] = '\0';
1408 static char *xgetdomainname(void)
1410 #ifdef HAVE_GETDOMAINNAME
1412 const size_t sz
= get_hostname_max() + 1;
1414 name
= malloc(sizeof(char) * sz
);
1416 log_err(_("failed to allocate memory: %m"));
1418 if (getdomainname(name
, sz
) != 0) {
1422 name
[sz
- 1] = '\0';
1429 static char *read_os_release(struct options
*op
, const char *varname
)
1433 size_t varsz
= strlen(varname
);
1434 char *p
, *buf
= NULL
, *ret
= NULL
;
1436 /* read the file only once */
1437 if (!op
->osrelease
) {
1438 fd
= open(_PATH_OS_RELEASE_ETC
, O_RDONLY
);
1440 fd
= open(_PATH_OS_RELEASE_USR
, O_RDONLY
);
1442 log_warn(_("cannot open os-release file"));
1447 if (fstat(fd
, &st
) < 0 || st
.st_size
> 4 * 1024 * 1024)
1450 op
->osrelease
= malloc(st
.st_size
+ 1);
1452 log_err(_("failed to allocate memory: %m"));
1453 if (read_all(fd
, op
->osrelease
, st
.st_size
) != (ssize_t
) st
.st_size
) {
1454 free(op
->osrelease
);
1455 op
->osrelease
= NULL
;
1458 op
->osrelease
[st
.st_size
] = 0;
1460 buf
= strdup(op
->osrelease
);
1462 log_err(_("failed to allocate memory: %m"));
1468 p
+= strspn(p
, "\n\r");
1469 p
+= strspn(p
, " \t\n\r");
1472 if (strspn(p
, "#;\n") != 0) {
1473 p
+= strcspn(p
, "\n\r");
1476 if (strncmp(p
, varname
, varsz
) != 0) {
1477 p
+= strcspn(p
, "\n\r");
1481 p
+= strspn(p
, " \t\n\r=\"");
1482 eol
= p
+ strcspn(p
, "\n\r");
1486 if (*eon
== '\t' || *eon
== ' ') {
1499 log_err(_("failed to allocate memory: %m"));
1509 #ifdef AGETTY_RELOAD
1510 static void open_netlink(void)
1512 struct sockaddr_nl addr
= { 0, };
1515 if (netlink_fd
!= AGETTY_RELOAD_FDNONE
)
1518 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1520 addr
.nl_family
= AF_NETLINK
;
1521 addr
.nl_pid
= getpid();
1522 addr
.nl_groups
= RTMGRP_IPV4_IFADDR
| RTMGRP_IPV6_IFADDR
;
1523 if (bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
1530 static int process_netlink_msg(int *changed
)
1533 struct sockaddr_nl snl
;
1537 struct iovec iov
= {
1539 .iov_len
= sizeof(buf
)
1541 struct msghdr msg
= {
1543 .msg_namelen
= sizeof(snl
),
1546 .msg_control
= NULL
,
1547 .msg_controllen
= 0,
1551 rc
= recvmsg(netlink_fd
, &msg
, MSG_DONTWAIT
);
1553 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1556 /* Failure, just stop listening for changes */
1558 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1562 for (h
= (struct nlmsghdr
*)buf
; NLMSG_OK(h
, (unsigned int)rc
); h
= NLMSG_NEXT(h
, rc
)) {
1563 if (h
->nlmsg_type
== NLMSG_DONE
||
1564 h
->nlmsg_type
== NLMSG_ERROR
) {
1566 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1577 static int process_netlink(void)
1580 while (process_netlink_msg(&changed
));
1584 static int wait_for_term_input(int fd
)
1586 char buffer
[sizeof(struct inotify_event
) + NAME_MAX
+ 1];
1587 struct termios orig
, nonc
;
1591 /* Our aim here is to fall through if something fails
1592 * and not be stuck waiting. On failure assume we have input */
1594 if (tcgetattr(fd
, &orig
) != 0)
1597 memcpy(&nonc
, &orig
, sizeof (nonc
));
1598 nonc
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
| ECHOK
| ECHOKE
);
1599 nonc
.c_cc
[VMIN
] = 1;
1600 nonc
.c_cc
[VTIME
] = 0;
1602 if (tcsetattr(fd
, TCSANOW
, &nonc
) != 0)
1605 if (inotify_fd
== AGETTY_RELOAD_FDNONE
) {
1606 /* make sure the reload trigger file exists */
1607 int reload_fd
= open(AGETTY_RELOAD_FILENAME
,
1608 O_CREAT
|O_CLOEXEC
|O_RDONLY
,
1611 /* initialize reload trigger inotify stuff */
1612 if (reload_fd
>= 0) {
1613 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1615 inotify_add_watch(inotify_fd
, AGETTY_RELOAD_FILENAME
,
1616 IN_ATTRIB
| IN_MODIFY
);
1620 log_warn(_("failed to create reload file: %s: %m"),
1621 AGETTY_RELOAD_FILENAME
);
1630 if (inotify_fd
>= 0) {
1631 FD_SET(inotify_fd
, &rfds
);
1632 nfds
= max(nfds
, inotify_fd
);
1634 if (netlink_fd
>= 0) {
1635 FD_SET(netlink_fd
, &rfds
);
1636 nfds
= max(nfds
, netlink_fd
);
1639 /* If waiting fails, just fall through, presumably reading input will fail */
1640 if (select(nfds
+ 1, &rfds
, NULL
, NULL
, NULL
) < 0)
1643 if (FD_ISSET(fd
, &rfds
)) {
1644 count
= read(fd
, buffer
, sizeof (buffer
));
1646 tcsetattr(fd
, TCSANOW
, &orig
);
1648 /* Reinject the bytes we read back into the buffer, usually just one byte */
1649 for (i
= 0; i
< count
; i
++)
1650 ioctl(fd
, TIOCSTI
, buffer
+ i
);
1652 /* Have terminal input */
1655 } else if (netlink_fd
>= 0 && FD_ISSET(netlink_fd
, &rfds
)) {
1656 if (!process_netlink())
1659 /* Just drain the inotify buffer */
1660 } else if (inotify_fd
>= 0 && FD_ISSET(inotify_fd
, &rfds
)) {
1661 while (read(inotify_fd
, buffer
, sizeof (buffer
)) > 0);
1664 tcsetattr(fd
, TCSANOW
, &orig
);
1666 /* Need to reprompt */
1670 #endif /* AGETTY_RELOAD */
1671 static void print_issue_file(struct options
*op
, struct termios
*tp
)
1676 if ((op
->flags
& F_NONL
) == 0) {
1677 /* Issue not in use, start with a new line. */
1678 write_all(STDOUT_FILENO
, "\r\n", 2);
1682 if ((op
->flags
& F_ISSUE
) && (fd
= fopen(op
->issue
, "r"))) {
1683 int c
, oflag
= tp
->c_oflag
; /* Save current setting. */
1685 if ((op
->flags
& F_VCONSOLE
) == 0) {
1686 /* Map new line in output to carriage return & new line. */
1687 tp
->c_oflag
|= (ONLCR
| OPOST
);
1688 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1691 while ((c
= getc(fd
)) != EOF
) {
1693 output_special_char(getc(fd
), op
, tp
, fd
);
1699 if ((op
->flags
& F_VCONSOLE
) == 0) {
1700 /* Restore settings. */
1701 tp
->c_oflag
= oflag
;
1702 /* Wait till output is gone. */
1703 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1710 /* Show login prompt, optionally preceded by /etc/issue contents. */
1711 static void do_prompt(struct options
*op
, struct termios
*tp
)
1713 #ifdef AGETTY_RELOAD
1716 print_issue_file(op
, tp
);
1718 if (op
->flags
& F_LOGINPAUSE
) {
1719 puts(_("[press ENTER to login]"));
1720 #ifdef AGETTY_RELOAD
1721 if (!wait_for_term_input(STDIN_FILENO
)) {
1723 if (op
->flags
& F_VCONSOLE
)
1724 termio_clear(STDOUT_FILENO
);
1731 if (!(op
->flags
& F_NOHINTS
) && !op
->autolog
&&
1732 (op
->flags
& F_VCONSOLE
)) {
1735 if (ioctl(STDIN_FILENO
, KDGKBLED
, &kb
) == 0) {
1736 char hint
[256] = { '\0' };
1739 if (access(_PATH_NUMLOCK_ON
, F_OK
) == 0)
1742 if (nl
&& (kb
& 0x02) == 0)
1743 append(hint
, sizeof(hint
), NULL
, _("Num Lock off"));
1745 else if (nl
== 0 && (kb
& 2) && (kb
& 0x20) == 0)
1746 append(hint
, sizeof(hint
), NULL
, _("Num Lock on"));
1748 if ((kb
& 0x04) && (kb
& 0x40) == 0)
1749 append(hint
, sizeof(hint
), ", ", _("Caps Lock on"));
1751 if ((kb
& 0x01) && (kb
& 0x10) == 0)
1752 append(hint
, sizeof(hint
), ", ", _("Scroll Lock on"));
1755 printf(_("Hint: %s\n\n"), hint
);
1758 #endif /* KDGKBLED */
1759 if ((op
->flags
& F_NOHOSTNAME
) == 0) {
1760 char *hn
= xgethostname();
1763 char *dot
= strchr(hn
, '.');
1765 struct addrinfo
*res
= NULL
;
1767 if ((op
->flags
& F_LONGHNAME
) == 0) {
1771 } else if (dot
== NULL
) {
1772 struct addrinfo hints
;
1774 memset(&hints
, 0, sizeof(hints
));
1775 hints
.ai_flags
= AI_CANONNAME
;
1777 if (!getaddrinfo(hn
, NULL
, &hints
, &res
)
1778 && res
&& res
->ai_canonname
)
1779 cn
= res
->ai_canonname
;
1782 write_all(STDOUT_FILENO
, cn
, strlen(cn
));
1783 write_all(STDOUT_FILENO
, " ", 1);
1791 /* Always show login prompt. */
1792 write_all(STDOUT_FILENO
, LOGIN
, sizeof(LOGIN
) - 1);
1796 /* Select next baud rate. */
1797 static void next_speed(struct options
*op
, struct termios
*tp
)
1799 static int baud_index
= -1;
1801 if (baud_index
== -1)
1803 * If the F_KEEPSPEED flags is set then the FIRST_SPEED is not
1804 * tested yet (see termio_init()).
1807 (op
->flags
& F_KEEPSPEED
) ? FIRST_SPEED
: 1 % op
->numspeed
;
1809 baud_index
= (baud_index
+ 1) % op
->numspeed
;
1811 cfsetispeed(tp
, op
->speeds
[baud_index
]);
1812 cfsetospeed(tp
, op
->speeds
[baud_index
]);
1813 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1816 /* Get user name, establish parity, speed, erase, kill & eol. */
1817 static char *get_logname(struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
1819 static char logname
[BUFSIZ
];
1821 char c
; /* input character, full eight bits */
1822 char ascval
; /* low 7 bits of input character */
1824 static char *erase
[] = { /* backspace-space-backspace */
1825 "\010\040\010", /* space parity */
1826 "\010\040\010", /* odd parity */
1827 "\210\240\210", /* even parity */
1828 "\210\240\210", /* no parity */
1831 /* Initialize kill, erase, parity etc. (also after switching speeds). */
1835 * Flush pending input (especially important after parsing or switching
1838 if ((op
->flags
& F_VCONSOLE
) == 0)
1840 tcflush(STDIN_FILENO
, TCIFLUSH
);
1842 eightbit
= (op
->flags
& F_EIGHTBITS
);
1846 while (*logname
== '\0') {
1848 /* Write issue file and prompt */
1851 #ifdef AGETTY_RELOAD
1852 /* If asked to reprompt *before* terminal input arrives, then do so */
1853 if (!wait_for_term_input(STDIN_FILENO
)) {
1854 if (op
->flags
& F_VCONSOLE
)
1855 termio_clear(STDOUT_FILENO
);
1861 /* Read name, watch for break and end-of-line. */
1862 while (cp
->eol
== '\0') {
1866 if (read(STDIN_FILENO
, &c
, 1) < 1) {
1868 /* The terminal could be open with O_NONBLOCK when
1869 * -L (force CLOCAL) is specified... */
1870 if (errno
== EINTR
|| errno
== EAGAIN
) {
1882 log_err(_("%s: read: %m"), op
->tty
);
1886 /* Do parity bit handling. */
1889 else if (c
!= (ascval
= (c
& 0177))) {
1890 uint32_t bits
; /* # of "1" bits per character */
1891 uint32_t mask
; /* mask with 1 bit up */
1892 for (bits
= 1, mask
= 1; mask
& 0177; mask
<<= 1) {
1896 cp
->parity
|= ((bits
& 1) ? 1 : 2);
1899 if (op
->killchars
&& strchr(op
->killchars
, ascval
))
1901 else if (op
->erasechars
&& strchr(op
->erasechars
, ascval
))
1906 /* Do erase, kill and end-of-line processing. */
1910 if (op
->numspeed
> 1)
1915 *bp
= 0; /* terminate logname */
1916 cp
->eol
= ascval
; /* set end-of-line char */
1920 cp
->erase
= ascval
; /* set erase character */
1922 if ((tp
->c_lflag
& ECHO
) == 0)
1923 write_all(1, erase
[cp
->parity
], 3);
1928 cp
->kill
= ascval
; /* set kill character */
1929 while (bp
> logname
) {
1930 if ((tp
->c_lflag
& ECHO
) == 0)
1931 write_all(1, erase
[cp
->parity
], 3);
1938 if (!isascii(ascval
) || !isprint(ascval
))
1940 if ((size_t)(bp
- logname
) >= sizeof(logname
) - 1)
1941 log_err(_("%s: input overrun"), op
->tty
);
1942 if ((tp
->c_lflag
& ECHO
) == 0)
1943 write_all(1, &c
, 1); /* echo the character */
1944 *bp
++ = ascval
; /* and store it */
1949 #ifdef HAVE_WIDECHAR
1950 if ((op
->flags
& (F_EIGHTBITS
|F_UTF8
)) == (F_EIGHTBITS
|F_UTF8
)) {
1951 /* Check out UTF-8 multibyte characters */
1955 len
= mbstowcs((wchar_t *)0, logname
, 0);
1957 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
1959 wcs
= malloc((len
+ 1) * sizeof(wchar_t));
1961 log_err(_("failed to allocate memory: %m"));
1963 len
= mbstowcs(wcs
, logname
, len
+ 1);
1965 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
1969 const wint_t wc
= *wcp
++;
1971 log_err(_("%s: invalid character 0x%x in login name"), op
->tty
, wc
);
1976 if ((op
->flags
& F_LCUC
) && (cp
->capslock
= caps_lock(logname
))) {
1978 /* Handle names with upper case and no lower case. */
1979 for (bp
= logname
; *bp
; bp
++)
1981 *bp
= tolower(*bp
); /* map name to lower case */
1987 /* Set the final tty mode bits. */
1988 static void termio_final(struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
1990 /* General terminal-independent stuff. */
1992 /* 2-way flow control */
1993 tp
->c_iflag
|= IXON
| IXOFF
;
1994 tp
->c_lflag
|= ICANON
| ISIG
| ECHO
| ECHOE
| ECHOK
| ECHOKE
;
1995 /* no longer| ECHOCTL | ECHOPRT */
1996 tp
->c_oflag
|= OPOST
;
1997 /* tp->c_cflag = 0; */
1998 tp
->c_cc
[VINTR
] = DEF_INTR
;
1999 tp
->c_cc
[VQUIT
] = DEF_QUIT
;
2000 tp
->c_cc
[VEOF
] = DEF_EOF
;
2001 tp
->c_cc
[VEOL
] = DEF_EOL
;
2003 tp
->c_cc
[VSWTC
] = DEF_SWITCH
;
2004 #elif defined(VSWTCH)
2005 tp
->c_cc
[VSWTCH
] = DEF_SWITCH
;
2006 #endif /* __linux__ */
2008 /* Account for special characters seen in input. */
2009 if (cp
->eol
== CR
) {
2010 tp
->c_iflag
|= ICRNL
;
2011 tp
->c_oflag
|= ONLCR
;
2013 tp
->c_cc
[VERASE
] = cp
->erase
;
2014 tp
->c_cc
[VKILL
] = cp
->kill
;
2016 /* Account for the presence or absence of parity bits in input. */
2017 switch (cp
->parity
) {
2019 /* space (always 0) parity */
2023 tp
->c_cflag
|= PARODD
;
2027 tp
->c_cflag
|= PARENB
;
2028 tp
->c_iflag
|= INPCK
| ISTRIP
;
2032 tp
->c_cflag
&= ~CSIZE
;
2036 /* Account for upper case without lower case. */
2039 tp
->c_iflag
|= IUCLC
;
2042 tp
->c_lflag
|= XCASE
;
2045 tp
->c_oflag
|= OLCUC
;
2048 /* Optionally enable hardware flow control. */
2050 if (op
->flags
& F_RTSCTS
)
2051 tp
->c_cflag
|= CRTSCTS
;
2054 /* Finally, make the new settings effective. */
2055 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
) < 0)
2056 log_err(_("%s: failed to set terminal attributes: %m"), op
->tty
);
2060 * String contains upper case without lower case.
2061 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940
2062 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242
2064 static int caps_lock(char *s
)
2068 for (capslock
= 0; *s
; s
++) {
2070 return EXIT_SUCCESS
;
2072 capslock
= isupper(*s
);
2077 /* Convert speed string to speed code; return 0 on failure. */
2078 static speed_t
bcode(char *s
)
2080 const struct Speedtab
*sp
;
2081 long speed
= atol(s
);
2083 for (sp
= speedtab
; sp
->speed
; sp
++)
2084 if (sp
->speed
== speed
)
2089 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
2091 fputs(USAGE_HEADER
, out
);
2092 fprintf(out
, _(" %1$s [options] <line> [<baud_rate>,...] [<termtype>]\n"
2093 " %1$s [options] <baud_rate>,... <line> [<termtype>]\n"), program_invocation_short_name
);
2095 fputs(USAGE_SEPARATOR
, out
);
2096 fputs(_("Open a terminal and set its mode.\n"), out
);
2098 fputs(USAGE_OPTIONS
, out
);
2099 fputs(_(" -8, --8bits assume 8-bit tty\n"), out
);
2100 fputs(_(" -a, --autologin <user> login the specified user automatically\n"), out
);
2101 fputs(_(" -c, --noreset do not reset control mode\n"), out
);
2102 fputs(_(" -E, --remote use -r <hostname> for login(1)\n"), out
);
2103 fputs(_(" -f, --issue-file <file> display issue file\n"), out
);
2104 fputs(_(" -h, --flow-control enable hardware flow control\n"), out
);
2105 fputs(_(" -H, --host <hostname> specify login host\n"), out
);
2106 fputs(_(" -i, --noissue do not display issue file\n"), out
);
2107 fputs(_(" -I, --init-string <string> set init string\n"), out
);
2108 fputs(_(" -J --noclear do not clear the screen before prompt\n"), out
);
2109 fputs(_(" -l, --login-program <file> specify login program\n"), out
);
2110 fputs(_(" -L, --local-line[=<mode>] control the local line flag\n"), out
);
2111 fputs(_(" -m, --extract-baud extract baud rate during connect\n"), out
);
2112 fputs(_(" -n, --skip-login do not prompt for login\n"), out
);
2113 fputs(_(" -N --nonewline do not print a newline before issue\n"), out
);
2114 fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out
);
2115 fputs(_(" -p, --login-pause wait for any key before the login\n"), out
);
2116 fputs(_(" -r, --chroot <dir> change root to the directory\n"), out
);
2117 fputs(_(" -R, --hangup do virtually hangup on the tty\n"), out
);
2118 fputs(_(" -s, --keep-baud try to keep baud rate after break\n"), out
);
2119 fputs(_(" -t, --timeout <number> login process timeout\n"), out
);
2120 fputs(_(" -U, --detect-case detect uppercase terminal\n"), out
);
2121 fputs(_(" -w, --wait-cr wait carriage-return\n"), out
);
2122 fputs(_(" --nohints do not print hints\n"), out
);
2123 fputs(_(" --nohostname no hostname at all will be shown\n"), out
);
2124 fputs(_(" --long-hostname show full qualified hostname\n"), out
);
2125 fputs(_(" --erase-chars <string> additional backspace chars\n"), out
);
2126 fputs(_(" --kill-chars <string> additional kill chars\n"), out
);
2127 fputs(_(" --chdir <directory> chdir before the login\n"), out
);
2128 fputs(_(" --delay <number> sleep seconds before prompt\n"), out
);
2129 fputs(_(" --nice <number> run login with this priority\n"), out
);
2130 fputs(_(" --reload reload prompts on running agetty instances\n"), out
);
2131 fputs(_(" --help display this help and exit\n"), out
);
2132 fputs(_(" --version output version information and exit\n"), out
);
2133 fprintf(out
, USAGE_MAN_TAIL("agetty(8)"));
2135 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
2139 * Helper function reports errors to console or syslog.
2140 * Will be used by log_err() and log_warn() therefore
2141 * it takes a format as well as va_list.
2143 #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
2145 static void dolog(int priority
, const char *fmt
, va_list ap
)
2154 * If the diagnostic is reported via syslog(3), the process name is
2155 * automatically prepended to the message. If we write directly to
2156 * /dev/console, we must prepend the process name ourselves.
2162 str2cpy(buf
, program_invocation_short_name
, ": ");
2163 bp
= buf
+ strlen(buf
);
2164 #endif /* USE_SYSLOG */
2165 vsnprintf(bp
, sizeof(buf
)-strlen(buf
), fmt
, ap
);
2168 * Write the diagnostic directly to /dev/console if we do not use the
2169 * syslog(3) facility.
2172 openlog(program_invocation_short_name
, LOG_PID
, LOG_AUTHPRIV
);
2173 syslog(priority
, "%s", buf
);
2176 /* Terminate with CR-LF since the console mode is unknown. */
2178 if ((fd
= open("/dev/console", 1)) >= 0) {
2179 write_all(fd
, buf
, strlen(buf
));
2182 #endif /* USE_SYSLOG */
2185 static void log_err(const char *fmt
, ...)
2190 dolog(LOG_ERR
, fmt
, ap
);
2193 /* Be kind to init(8). */
2198 static void log_warn(const char *fmt
, ...)
2203 dolog(LOG_WARNING
, fmt
, ap
);
2207 static void print_addr(sa_family_t family
, void *addr
)
2209 char buff
[INET6_ADDRSTRLEN
+ 1];
2211 inet_ntop(family
, addr
, buff
, sizeof(buff
));
2216 * Prints IP for the specified interface (@iface), if the interface is not
2217 * specified then prints the "best" one (UP, RUNNING, non-LOOPBACK). If not
2218 * found the "best" interface then prints at least host IP.
2220 static void output_iface_ip(struct ifaddrs
*addrs
,
2225 struct addrinfo hints
, *info
= NULL
;
2232 for (p
= addrs
; p
; p
= p
->ifa_next
) {
2236 p
->ifa_addr
->sa_family
!= family
)
2240 /* Filter out by interface name */
2241 if (strcmp(p
->ifa_name
, iface
) != 0)
2244 /* Select the "best" interface */
2245 if ((p
->ifa_flags
& IFF_LOOPBACK
) ||
2246 !(p
->ifa_flags
& IFF_UP
) ||
2247 !(p
->ifa_flags
& IFF_RUNNING
))
2252 switch (p
->ifa_addr
->sa_family
) {
2254 addr
= &((struct sockaddr_in
*) p
->ifa_addr
)->sin_addr
;
2257 addr
= &((struct sockaddr_in6
*) p
->ifa_addr
)->sin6_addr
;
2262 print_addr(family
, addr
);
2270 /* Hmm.. not found the best interface, print host IP at least */
2271 memset(&hints
, 0, sizeof(hints
));
2272 hints
.ai_family
= family
;
2273 if (family
== AF_INET6
)
2274 hints
.ai_flags
= AI_V4MAPPED
;
2276 host
= xgethostname();
2277 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2278 switch (info
->ai_family
) {
2280 addr
= &((struct sockaddr_in
*) info
->ai_addr
)->sin_addr
;
2283 addr
= &((struct sockaddr_in6
*) info
->ai_addr
)->sin6_addr
;
2287 print_addr(family
, addr
);
2295 * parses \x{argument}, if not argument specified then returns NULL, the @fd
2296 * has to point to one char after the sequence (it means '{').
2298 static char *get_escape_argument(FILE *fd
, char *buf
, size_t bufsz
)
2303 if (c
== EOF
|| (unsigned char) c
!= '{') {
2312 if ((unsigned char) c
!= '}' && i
< bufsz
- 1)
2313 buf
[i
++] = (unsigned char) c
;
2315 } while ((unsigned char) c
!= '}');
2321 static void output_special_char(unsigned char c
, struct options
*op
,
2322 struct termios
*tp
, FILE *fp
)
2329 char escname
[UL_COLORNAME_MAXSZ
];
2331 if (get_escape_argument(fp
, escname
, sizeof(escname
))) {
2332 const char *esc
= color_sequence_from_colorname(escname
);
2336 fputs("\033", stdout
);
2341 printf("%s", uts
.sysname
);
2345 printf("%s", uts
.nodename
);
2349 printf("%s", uts
.release
);
2353 printf("%s", uts
.version
);
2357 printf("%s", uts
.machine
);
2361 char *dom
= xgetdomainname();
2363 fputs(dom
? dom
: "unknown_domain", stdout
);
2370 char *host
= xgethostname();
2371 struct addrinfo hints
, *info
= NULL
;
2373 memset(&hints
, 0, sizeof(hints
));
2374 hints
.ai_flags
= AI_CANONNAME
;
2376 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2379 if (info
->ai_canonname
&&
2380 (canon
= strchr(info
->ai_canonname
, '.')))
2383 fputs(dom
? dom
: "unknown_domain", stdout
);
2396 tm
= localtime(&now
);
2401 if (c
== 'd') /* ISO 8601 */
2402 printf("%s %s %d %d",
2403 nl_langinfo(ABDAY_1
+ tm
->tm_wday
),
2404 nl_langinfo(ABMON_1
+ tm
->tm_mon
),
2406 tm
->tm_year
< 70 ? tm
->tm_year
+ 2000 :
2407 tm
->tm_year
+ 1900);
2409 printf("%02d:%02d:%02d",
2410 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2414 printf ("%s", op
->tty
);
2418 const speed_t speed
= cfgetispeed(tp
);
2421 for (i
= 0; speedtab
[i
].speed
; i
++) {
2422 if (speedtab
[i
].code
== speed
) {
2423 printf("%ld", speedtab
[i
].speed
);
2431 char *var
= NULL
, varname
[64];
2434 if (get_escape_argument(fp
, varname
, sizeof(varname
))) {
2435 var
= read_os_release(op
, varname
);
2437 if (strcmp(varname
, "ANSI_COLOR") == 0)
2438 printf("\033[%sm", var
);
2443 } else if ((var
= read_os_release(op
, "PRETTY_NAME"))) {
2446 /* \S and PRETTY_NAME not found */
2449 fputs(uts
.sysname
, stdout
);
2459 while ((ut
= getutxent()))
2460 if (ut
->ut_type
== USER_PROCESS
)
2464 printf(P_("%d user", "%d users", users
), users
);
2466 printf ("%d ", users
);
2472 sa_family_t family
= c
== '4' ? AF_INET
: AF_INET6
;
2473 struct ifaddrs
*addrs
= NULL
;
2476 #ifdef AGETTY_RELOAD
2480 if (getifaddrs(&addrs
))
2483 if (get_escape_argument(fp
, iface
, sizeof(iface
)))
2484 output_iface_ip(addrs
, iface
, family
);
2486 output_iface_ip(addrs
, NULL
, family
);
2497 static void init_special_char(char* arg
, struct options
*op
)
2502 op
->initstring
= malloc(strlen(arg
) + 1);
2503 if (!op
->initstring
)
2504 log_err(_("failed to allocate memory: %m"));
2507 * Copy optarg into op->initstring decoding \ddd octal
2513 /* The \\ is converted to \ */
2520 /* Handle \000 - \177. */
2522 for (i
= 1; i
<= 3; i
++) {
2523 if (*p
>= '0' && *p
<= '7') {
2540 * Appends @str to @dest and if @dest is not empty then use @sep as a
2541 * separator. The maximal final length of the @dest is @len.
2543 * Returns the final @dest length or -1 in case of error.
2545 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
)
2547 size_t dsz
= 0, ssz
= 0, sz
;
2550 if (!dest
|| !len
|| !src
)
2559 if (dsz
+ ssz
+ sz
+ 1 > len
)
2564 memcpy(p
, sep
, ssz
);
2570 return dsz
+ ssz
+ sz
;
2574 * Do not allow the user to pass an option as a user name
2575 * To be more safe: Use `--' to make sure the rest is
2576 * interpreted as non-options by the program, if it supports it.
2578 static void check_username(const char* nm
)
2583 if (strlen(nm
) > 42)
2592 log_err(_("checkname failed: %m"));
2595 static void reload_agettys(void)
2597 #ifdef AGETTY_RELOAD
2598 int fd
= open(AGETTY_RELOAD_FILENAME
, O_CREAT
|O_CLOEXEC
|O_WRONLY
,
2601 err(EXIT_FAILURE
, _("cannot open %s"), AGETTY_RELOAD_FILENAME
);
2603 if (futimens(fd
, NULL
) < 0 || close(fd
) < 0)
2604 err(EXIT_FAILURE
, _("cannot touch file %s"),
2605 AGETTY_RELOAD_FILENAME
);
2608 errx(EXIT_FAILURE
, _("--reload is unsupported on your system"));