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"
47 #include "color-names.h"
50 #ifdef USE_PLYMOUTH_SUPPORT
51 # include "plymouth-ctrl.h"
54 #ifdef HAVE_SYS_PARAM_H
55 # include <sys/param.h>
58 #if defined(__FreeBSD_kernel__)
63 # ifdef HAVE_LIBUTIL_H
71 # ifndef DEFAULT_VCTERM
72 # define DEFAULT_VCTERM "linux"
74 # if defined (__s390__) || defined (__s390x__)
75 # define DEFAULT_TTYS0 "dumb"
76 # define DEFAULT_TTY32 "ibm327x"
77 # define DEFAULT_TTYS1 "vt220"
79 # ifndef DEFAULT_STERM
80 # define DEFAULT_STERM "vt102"
82 #elif defined(__GNU__)
84 # ifndef DEFAULT_VCTERM
85 # define DEFAULT_VCTERM "hurd"
87 # ifndef DEFAULT_STERM
88 # define DEFAULT_STERM "vt102"
91 # ifndef DEFAULT_VCTERM
92 # define DEFAULT_VCTERM "vt100"
94 # ifndef DEFAULT_STERM
95 # define DEFAULT_STERM "vt100"
99 #ifdef __FreeBSD_kernel__
103 /* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */
109 * Some heuristics to find out what environment we are in: if it is not
110 * System V, assume it is SunOS 4. The LOGIN_PROCESS is defined in System V
111 * utmp.h, which will select System V style getty.
118 * Things you may want to modify.
120 * If ISSUE_SUPPORT is not defined, agetty will never display the contents of
121 * the /etc/issue file. You will not want to spit out large "issue" files at
122 * the wrong baud rate. Relevant for System V only.
124 * You may disagree with the default line-editing etc. characters defined
125 * below. Note, however, that DEL cannot be used for interrupt generation
126 * and for line editing at the same time.
129 /* Displayed before the login prompt. */
131 # define ISSUE_SUPPORT
132 # if defined(HAVE_SCANDIRAT) && defined(HAVE_OPENAT)
134 # include "fileutils.h"
135 # define ISSUEDIR_SUPPORT
136 # define ISSUEDIR_EXT ".issue"
137 # define ISSUEDIR_EXTSIZ (sizeof(ISSUEDIR_EXT) - 1)
142 #define LOGIN "login: "
143 #define LOGIN_ARGV_MAX 16 /* Numbers of args for login */
149 # include <sys/inotify.h>
150 # include <linux/netlink.h>
151 # include <linux/rtnetlink.h>
152 # define AGETTY_RELOAD_FILENAME "/run/agetty.reload" /* trigger file */
153 # define AGETTY_RELOAD_FDNONE -2 /* uninitialized fd */
154 static int inotify_fd
= AGETTY_RELOAD_FDNONE
;
155 static int netlink_fd
= AGETTY_RELOAD_FDNONE
;
156 static uint32_t netlink_groups
;
167 unsigned int do_tcsetattr
: 1,
172 * When multiple baud rates are specified on the command line, the first one
173 * we will try is the first one specified.
175 #define FIRST_SPEED 0
177 /* Storage for command-line options. */
178 #define MAX_SPEED 10 /* max. nr. of baud rates */
181 int flags
; /* toggle switches, see below */
182 unsigned int timeout
; /* time-out period */
183 char *autolog
; /* login the user automatically */
184 char *chdir
; /* Chdir before the login */
185 char *chroot
; /* Chroot before the login */
186 char *login
; /* login program */
187 char *logopt
; /* options for login program */
188 char *tty
; /* name of tty */
189 char *vcline
; /* line of virtual console */
190 char *term
; /* terminal type */
191 char *initstring
; /* modem init string */
192 char *issue
; /* alternative issue file or directory */
193 char *erasechars
; /* string with erase chars */
194 char *killchars
; /* string with kill chars */
195 char *osrelease
; /* /etc/os-release data */
196 unsigned int delay
; /* Sleep seconds before prompt */
197 int nice
; /* Run login with this priority */
198 int numspeed
; /* number of baud rates to try */
199 int clocal
; /* CLOCAL_MODE_* */
200 int kbmode
; /* Keyboard mode if virtual console */
201 speed_t speeds
[MAX_SPEED
]; /* baud rates to be tried */
205 CLOCAL_MODE_AUTO
= 0,
210 #define F_PARSE (1<<0) /* process modem status messages */
211 #define F_ISSUE (1<<1) /* display /etc/issue or /etc/issue.d */
212 #define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
214 #define F_INITSTRING (1<<4) /* initstring is set */
215 #define F_WAITCRLF (1<<5) /* wait for CR or LF */
217 #define F_NOPROMPT (1<<7) /* do not ask for login name! */
218 #define F_LCUC (1<<8) /* support for *LCUC stty modes */
219 #define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */
220 #define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */
221 #define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */
222 #define F_VCONSOLE (1<<12) /* This is a virtual console */
223 #define F_HANGUP (1<<13) /* Do call vhangup(2) */
224 #define F_UTF8 (1<<14) /* We can do UTF8 */
225 #define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */
226 #define F_NOCLEAR (1<<16) /* Do not clear the screen before prompting */
227 #define F_NONL (1<<17) /* No newline before issue */
228 #define F_NOHOSTNAME (1<<18) /* Do not show the hostname */
229 #define F_LONGHNAME (1<<19) /* Show Full qualified hostname */
230 #define F_NOHINTS (1<<20) /* Don't print hints */
231 #define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */
233 #define serial_tty_option(opt, flag) \
234 (((opt)->flags & (F_VCONSOLE|(flag))) == (flag))
241 static const struct Speedtab speedtab
[] = {
313 static void init_special_char(char* arg
, struct options
*op
);
314 static void parse_args(int argc
, char **argv
, struct options
*op
);
315 static void parse_speeds(struct options
*op
, char *arg
);
316 static void update_utmp(struct options
*op
);
317 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
);
318 static void termio_init(struct options
*op
, struct termios
*tp
);
319 static void reset_vc(const struct options
*op
, struct termios
*tp
, int canon
);
320 static void auto_baud(struct termios
*tp
);
321 static void list_speeds(void);
322 static void output_special_char (struct issue
*ie
, unsigned char c
, struct options
*op
,
323 struct termios
*tp
, FILE *fp
);
324 static void do_prompt(struct issue
*ie
, struct options
*op
, struct termios
*tp
);
325 static void next_speed(struct options
*op
, struct termios
*tp
);
326 static char *get_logname(struct issue
*ie
, struct options
*op
,
327 struct termios
*tp
, struct chardata
*cp
);
328 static void termio_final(struct options
*op
,
329 struct termios
*tp
, struct chardata
*cp
);
330 static int caps_lock(char *s
);
331 static speed_t
bcode(char *s
);
332 static void usage(void) __attribute__((__noreturn__
));
333 static void exit_slowly(int code
) __attribute__((__noreturn__
));
334 static void log_err(const char *, ...) __attribute__((__noreturn__
))
335 __attribute__((__format__(printf
, 1, 2)));
336 static void log_warn (const char *, ...)
337 __attribute__((__format__(printf
, 1, 2)));
338 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
);
339 static void check_username (const char* nm
);
340 static void login_options_to_argv(char *argv
[], int *argc
, char *str
, char *username
);
341 static void reload_agettys(void);
342 static void print_issue_file(struct issue
*ie
, struct options
*op
, struct termios
*tp
);
343 static void eval_issue_file(struct issue
*ie
, struct options
*op
, struct termios
*tp
);
345 /* Fake hostname for ut_host specified on command line. */
346 static char *fakehost
;
349 # include "closestream.h"
350 # ifndef DEBUG_OUTPUT
351 # define DEBUG_OUTPUT "/dev/tty10"
353 # define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
356 # define debug(s) do { ; } while (0)
359 int main(int argc
, char **argv
)
361 char *username
= NULL
; /* login name, given to /bin/login */
362 struct chardata chardata
; /* will be set by get_logname() */
363 struct termios termios
; /* terminal mode bits */
364 struct options options
= {
365 .flags
= F_ISSUE
, /* show /etc/issue (SYSV_STYLE) */
366 .login
= _PATH_LOGIN
, /* default login program */
367 .tty
= "tty1" /* default tty line */
369 struct issue issue
= {
372 char *login_argv
[LOGIN_ARGV_MAX
+ 1];
374 struct sigaction sa
, sa_hup
, sa_quit
, sa_int
;
377 setlocale(LC_ALL
, "");
378 bindtextdomain(PACKAGE
, LOCALEDIR
);
381 /* In case vhangup(2) has to called */
382 sa
.sa_handler
= SIG_IGN
;
383 sa
.sa_flags
= SA_RESTART
;
384 sigemptyset (&sa
.sa_mask
);
385 sigaction(SIGHUP
, &sa
, &sa_hup
);
386 sigaction(SIGQUIT
, &sa
, &sa_quit
);
387 sigaction(SIGINT
, &sa
, &sa_int
);
390 dbf
= fopen(DEBUG_OUTPUT
, "w");
391 for (int i
= 1; i
< argc
; i
++) {
397 #endif /* DEBUGGING */
399 /* Parse command-line arguments. */
400 parse_args(argc
, argv
, &options
);
402 login_argv
[login_argc
++] = options
.login
; /* set login program name */
404 /* Update the utmp file. */
406 update_utmp(&options
);
409 sleep(options
.delay
);
411 debug("calling open_tty\n");
413 /* Open the tty as standard { input, output, error }. */
414 open_tty(options
.tty
, &termios
, &options
);
416 /* Unmask SIGHUP if inherited */
418 sigaddset(&set
, SIGHUP
);
419 sigprocmask(SIG_UNBLOCK
, &set
, NULL
);
420 sigaction(SIGHUP
, &sa_hup
, NULL
);
422 tcsetpgrp(STDIN_FILENO
, getpid());
424 /* Default is to follow the current line speed and then default to 9600 */
425 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
== 0) {
426 options
.speeds
[options
.numspeed
++] = bcode("9600");
427 options
.flags
|= F_KEEPSPEED
;
430 /* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
431 debug("calling termio_init\n");
432 termio_init(&options
, &termios
);
434 /* Write the modem init string and DO NOT flush the buffers. */
435 if (serial_tty_option(&options
, F_INITSTRING
) &&
436 options
.initstring
&& *options
.initstring
!= '\0') {
437 debug("writing init string\n");
438 write_all(STDOUT_FILENO
, options
.initstring
,
439 strlen(options
.initstring
));
442 if (options
.flags
& F_VCONSOLE
|| options
.clocal
!= CLOCAL_MODE_ALWAYS
)
443 /* Go to blocking mode unless -L is specified, this change
444 * affects stdout, stdin and stderr as all the file descriptors
445 * are created by dup(). */
446 fcntl(STDOUT_FILENO
, F_SETFL
,
447 fcntl(STDOUT_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
449 /* Optionally detect the baud rate from the modem status message. */
450 debug("before autobaud\n");
451 if (serial_tty_option(&options
, F_PARSE
))
454 /* Set the optional timer. */
456 alarm(options
.timeout
);
458 /* Optionally wait for CR or LF before writing /etc/issue */
459 if (serial_tty_option(&options
, F_WAITCRLF
)) {
462 debug("waiting for cr-lf\n");
463 while (read(STDIN_FILENO
, &ch
, 1) == 1) {
464 /* Strip "parity bit". */
467 fprintf(dbf
, "read %c\n", ch
);
469 if (ch
== '\n' || ch
== '\r')
474 INIT_CHARDATA(&chardata
);
476 if (options
.autolog
) {
477 debug("doing auto login\n");
478 username
= options
.autolog
;
481 if (options
.flags
& F_NOPROMPT
) { /* --skip-login */
482 eval_issue_file(&issue
, &options
, &termios
);
483 print_issue_file(&issue
, &options
, &termios
);
484 } else { /* regular (auto)login */
485 if (options
.autolog
) {
486 /* Autologin prompt */
487 eval_issue_file(&issue
, &options
, &termios
);
488 do_prompt(&issue
, &options
, &termios
);
489 printf(_("%s%s (automatic login)\n"), LOGIN
, options
.autolog
);
491 /* Read the login name. */
492 debug("reading login name\n");
494 get_logname(&issue
, &options
, &termios
, &chardata
)) == NULL
)
495 if ((options
.flags
& F_VCONSOLE
) == 0 && options
.numspeed
)
496 next_speed(&options
, &termios
);
504 /* Finalize the termios settings. */
505 if ((options
.flags
& F_VCONSOLE
) == 0)
506 termio_final(&options
, &termios
, &chardata
);
508 reset_vc(&options
, &termios
, 1);
510 /* Now the newline character should be properly written. */
511 write_all(STDOUT_FILENO
, "\r\n", 2);
513 sigaction(SIGQUIT
, &sa_quit
, NULL
);
514 sigaction(SIGINT
, &sa_int
, NULL
);
517 check_username(username
);
519 if (options
.logopt
) {
521 * The --login-options completely overwrites the default
522 * way how agetty composes login(1) command line.
524 login_options_to_argv(login_argv
, &login_argc
,
525 options
.logopt
, username
);
527 if (options
.flags
& F_REMOTE
) {
529 login_argv
[login_argc
++] = "-h";
530 login_argv
[login_argc
++] = fakehost
;
531 } else if (options
.flags
& F_NOHOSTNAME
)
532 login_argv
[login_argc
++] = "-H";
536 login_argv
[login_argc
++] = "-f";
538 login_argv
[login_argc
++] = "--";
539 login_argv
[login_argc
++] = username
;
543 login_argv
[login_argc
] = NULL
; /* last login argv */
545 if (options
.chroot
&& chroot(options
.chroot
) < 0)
546 log_err(_("%s: can't change root directory %s: %m"),
547 options
.tty
, options
.chroot
);
548 if (options
.chdir
&& chdir(options
.chdir
) < 0)
549 log_err(_("%s: can't change working directory %s: %m"),
550 options
.tty
, options
.chdir
);
551 if (options
.nice
&& nice(options
.nice
) < 0)
552 log_warn(_("%s: can't change process priority: %m"),
555 free(options
.osrelease
);
557 if (close_stream(dbf
) != 0)
558 log_err("write failed: %s", DEBUG_OUTPUT
);
561 /* Let the login program take care of password validation. */
562 execv(options
.login
, login_argv
);
563 log_err(_("%s: can't exec %s: %m"), options
.tty
, login_argv
[0]);
567 * Returns : @str if \u not found
568 * : @username if @str equal to "\u"
569 * : newly allocated string if \u mixed with something other
571 static char *replace_u(char *str
, char *username
)
573 char *entry
= NULL
, *p
= str
;
574 size_t usz
= username
? strlen(username
) : 0;
578 char *tp
, *old
= entry
;
580 if (memcmp(p
, "\\u", 2)) {
582 continue; /* no \u */
586 if (p
== str
&& sz
== 2) {
587 /* 'str' contains only '\u' */
592 tp
= entry
= malloc(sz
+ usz
);
594 log_err(_("failed to allocate memory: %m"));
597 /* copy chars before \u */
598 memcpy(tp
, str
, p
- str
);
603 memcpy(tp
, username
, usz
);
607 /* copy chars after \u + \0 */
608 memcpy(tp
, p
+ 2, sz
- (p
- str
) - 1);
617 return entry
? entry
: str
;
620 static void login_options_to_argv(char *argv
[], int *argc
,
621 char *str
, char *username
)
626 while (str
&& isspace(*str
))
630 while (p
&& *p
&& i
< LOGIN_ARGV_MAX
) {
633 while (isspace(*++p
))
636 argv
[i
++] = replace_u(str
, username
);
642 if (str
&& *str
&& i
< LOGIN_ARGV_MAX
)
643 argv
[i
++] = replace_u(str
, username
);
647 static void output_version(void)
649 static const char *features
[] = {
662 #ifdef ISSUEDIR_SUPPORT
668 #ifdef USE_PLYMOUTH_SUPPORT
684 printf( _("%s from %s"), program_invocation_short_name
, PACKAGE_STRING
);
686 for (i
= 0; features
[i
]; i
++) {
689 printf("%s", features
[i
]);
691 fputs(")\n", stdout
);
694 #define is_speed(str) (strlen((str)) == strspn((str), "0123456789,"))
696 /* Parse command-line arguments. */
697 static void parse_args(int argc
, char **argv
, struct options
*op
)
702 VERSION_OPTION
= CHAR_MAX
+ 1,
712 const struct option longopts
[] = {
713 { "8bits", no_argument
, NULL
, '8' },
714 { "autologin", required_argument
, NULL
, 'a' },
715 { "noreset", no_argument
, NULL
, 'c' },
716 { "chdir", required_argument
, NULL
, 'C' },
717 { "delay", required_argument
, NULL
, 'd' },
718 { "remote", no_argument
, NULL
, 'E' },
719 { "issue-file", required_argument
, NULL
, 'f' },
720 { "flow-control", no_argument
, NULL
, 'h' },
721 { "host", required_argument
, NULL
, 'H' },
722 { "noissue", no_argument
, NULL
, 'i' },
723 { "init-string", required_argument
, NULL
, 'I' },
724 { "noclear", no_argument
, NULL
, 'J' },
725 { "login-program", required_argument
, NULL
, 'l' },
726 { "local-line", optional_argument
, NULL
, 'L' },
727 { "extract-baud", no_argument
, NULL
, 'm' },
728 { "list-speeds", no_argument
, NULL
, LIST_SPEEDS_OPTION
},
729 { "skip-login", no_argument
, NULL
, 'n' },
730 { "nonewline", no_argument
, NULL
, 'N' },
731 { "login-options", required_argument
, NULL
, 'o' },
732 { "login-pause", no_argument
, NULL
, 'p' },
733 { "nice", required_argument
, NULL
, 'P' },
734 { "chroot", required_argument
, NULL
, 'r' },
735 { "hangup", no_argument
, NULL
, 'R' },
736 { "keep-baud", no_argument
, NULL
, 's' },
737 { "timeout", required_argument
, NULL
, 't' },
738 { "detect-case", no_argument
, NULL
, 'U' },
739 { "wait-cr", no_argument
, NULL
, 'w' },
740 { "nohints", no_argument
, NULL
, NOHINTS_OPTION
},
741 { "nohostname", no_argument
, NULL
, NOHOSTNAME_OPTION
},
742 { "long-hostname", no_argument
, NULL
, LONGHOSTNAME_OPTION
},
743 { "reload", no_argument
, NULL
, RELOAD_OPTION
},
744 { "version", no_argument
, NULL
, VERSION_OPTION
},
745 { "help", no_argument
, NULL
, HELP_OPTION
},
746 { "erase-chars", required_argument
, NULL
, ERASE_CHARS_OPTION
},
747 { "kill-chars", required_argument
, NULL
, KILL_CHARS_OPTION
},
751 while ((c
= getopt_long(argc
, argv
,
752 "8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts
,
756 op
->flags
|= F_EIGHTBITS
;
759 op
->autolog
= optarg
;
762 op
->flags
|= F_KEEPCFLAGS
;
768 op
->delay
= strtou32_or_err(optarg
, _("invalid delay argument"));
771 op
->flags
|= F_REMOTE
;
777 op
->flags
|= F_RTSCTS
;
783 op
->flags
&= ~F_ISSUE
;
786 init_special_char(optarg
, op
);
787 op
->flags
|= F_INITSTRING
;
790 op
->flags
|= F_NOCLEAR
;
796 /* -L and -L=always have the same meaning */
797 op
->clocal
= CLOCAL_MODE_ALWAYS
;
799 if (strcmp(optarg
, "=always") == 0)
800 op
->clocal
= CLOCAL_MODE_ALWAYS
;
801 else if (strcmp(optarg
, "=never") == 0)
802 op
->clocal
= CLOCAL_MODE_NEVER
;
803 else if (strcmp(optarg
, "=auto") == 0)
804 op
->clocal
= CLOCAL_MODE_AUTO
;
806 log_err(_("invalid argument of --local-line"));
810 op
->flags
|= F_PARSE
;
813 op
->flags
|= F_NOPROMPT
;
822 op
->flags
|= F_LOGINPAUSE
;
825 op
->nice
= strtos32_or_err(optarg
, _("invalid nice argument"));
831 op
->flags
|= F_HANGUP
;
834 op
->flags
|= F_KEEPSPEED
;
837 op
->timeout
= strtou32_or_err(optarg
, _("invalid timeout argument"));
843 op
->flags
|= F_WAITCRLF
;
846 op
->flags
|= F_NOHINTS
;
848 case NOHOSTNAME_OPTION
:
849 op
->flags
|= F_NOHOSTNAME
;
851 case LONGHOSTNAME_OPTION
:
852 op
->flags
|= F_LONGHNAME
;
854 case ERASE_CHARS_OPTION
:
855 op
->erasechars
= optarg
;
857 case KILL_CHARS_OPTION
:
858 op
->killchars
= optarg
;
863 case LIST_SPEEDS_OPTION
:
872 errtryhelp(EXIT_FAILURE
);
876 debug("after getopt loop\n");
878 if (argc
< optind
+ 1) {
879 log_warn(_("not enough arguments"));
880 errx(EXIT_FAILURE
, _("not enough arguments"));
883 /* Accept "tty", "baudrate tty", and "tty baudrate". */
884 if (is_speed(argv
[optind
])) {
885 /* Assume BSD style speed. */
886 parse_speeds(op
, argv
[optind
++]);
887 if (argc
< optind
+ 1) {
888 log_warn(_("not enough arguments"));
889 errx(EXIT_FAILURE
, _("not enough arguments"));
891 op
->tty
= argv
[optind
++];
893 op
->tty
= argv
[optind
++];
895 char *v
= argv
[optind
];
903 /* On virtual console remember the line which is used for */
904 if (strncmp(op
->tty
, "tty", 3) == 0 &&
905 strspn(op
->tty
+ 3, "0123456789") == strlen(op
->tty
+3))
906 op
->vcline
= op
->tty
+3;
908 if (argc
> optind
&& argv
[optind
])
909 op
->term
= argv
[optind
];
911 debug("exiting parseargs\n");
914 /* Parse alternate baud rates. */
915 static void parse_speeds(struct options
*op
, char *arg
)
918 char *str
= strdup(arg
);
921 log_err(_("failed to allocate memory: %m"));
923 debug("entered parse_speeds:\n");
924 for (cp
= strtok(str
, ","); cp
!= NULL
; cp
= strtok((char *)0, ",")) {
925 if ((op
->speeds
[op
->numspeed
++] = bcode(cp
)) <= 0)
926 log_err(_("bad speed: %s"), cp
);
927 if (op
->numspeed
>= MAX_SPEED
)
928 log_err(_("too many alternate speeds"));
930 debug("exiting parsespeeds\n");
936 /* Update our utmp entry. */
937 static void update_utmp(struct options
*op
)
941 pid_t pid
= getpid();
942 pid_t sid
= getsid(0);
943 char *vcline
= op
->vcline
;
944 char *line
= op
->tty
;
948 * The utmp file holds miscellaneous information about things started by
949 * /sbin/init and other system-related events. Our purpose is to update
950 * the utmp entry for the current process, in particular the process type
951 * and the tty line we are listening to. Return successfully only if the
952 * utmp file can be opened for update, and if we are able to find our
953 * entry in the utmp file.
955 utmpxname(_PATH_UTMP
);
959 * Find my pid in utmp.
961 * FIXME: Earlier (when was that?) code here tested only utp->ut_type !=
962 * INIT_PROCESS, so maybe the >= here should be >.
964 * FIXME: The present code is taken from login.c, so if this is changed,
965 * maybe login has to be changed as well (is this true?).
967 while ((utp
= getutxent()))
968 if (utp
->ut_pid
== pid
969 && utp
->ut_type
>= INIT_PROCESS
970 && utp
->ut_type
<= DEAD_PROCESS
)
974 memcpy(&ut
, utp
, sizeof(ut
));
976 /* Some inits do not initialize utmp. */
977 memset(&ut
, 0, sizeof(ut
));
978 if (vcline
&& *vcline
)
979 /* Standard virtual console devices */
980 str2memcpy(ut
.ut_id
, vcline
, sizeof(ut
.ut_id
));
982 size_t len
= strlen(line
);
984 if (len
>= sizeof(ut
.ut_id
))
985 ptr
= line
+ len
- sizeof(ut
.ut_id
);
988 str2memcpy(ut
.ut_id
, ptr
, sizeof(ut
.ut_id
));
992 str2memcpy(ut
.ut_user
, "LOGIN", sizeof(ut
.ut_user
));
993 str2memcpy(ut
.ut_line
, line
, sizeof(ut
.ut_line
));
995 str2memcpy(ut
.ut_host
, fakehost
, sizeof(ut
.ut_host
));
998 ut
.ut_type
= LOGIN_PROCESS
;
1000 ut
.ut_session
= sid
;
1005 updwtmpx(_PATH_WTMP
, &ut
);
1008 #endif /* SYSV_STYLE */
1010 /* Set up tty as stdin, stdout & stderr. */
1011 static void open_tty(char *tty
, struct termios
*tp
, struct options
*op
)
1013 const pid_t pid
= getpid();
1019 /* Set up new standard input, unless we are given an already opened port. */
1021 if (strcmp(tty
, "-") != 0) {
1022 char buf
[PATH_MAX
+1];
1023 struct group
*gr
= NULL
;
1029 /* Use tty group if available */
1030 if ((gr
= getgrnam("tty")))
1033 len
= snprintf(buf
, sizeof(buf
), "/dev/%s", tty
);
1034 if (len
< 0 || (size_t)len
>= sizeof(buf
))
1035 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1037 /* Open the tty as standard input. */
1038 if ((fd
= open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0)) < 0)
1039 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1042 * There is always a race between this reset and the call to
1043 * vhangup() that s.o. can use to get access to your tty.
1044 * Linux login(1) will change tty permissions. Use root owner and group
1045 * with permission -rw------- for the period between getty and login.
1047 if (fchown(fd
, 0, gid
) || fchmod(fd
, (gid
? 0620 : 0600))) {
1049 log_warn("%s: %m", buf
);
1051 log_err("%s: %m", buf
);
1054 /* Sanity checks... */
1055 if (fstat(fd
, &st
) < 0)
1056 log_err("%s: %m", buf
);
1057 if ((st
.st_mode
& S_IFMT
) != S_IFCHR
)
1058 log_err(_("/dev/%s: not a character device"), tty
);
1060 log_err(_("/dev/%s: not a tty"), tty
);
1062 if (((tid
= tcgetsid(fd
)) < 0) || (pid
!= tid
)) {
1063 if (ioctl(fd
, TIOCSCTTY
, 1) == -1)
1064 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1067 close(STDIN_FILENO
);
1070 if (op
->flags
& F_HANGUP
) {
1072 if (ioctl(fd
, TIOCNOTTY
))
1073 debug("TIOCNOTTY ioctl failed\n");
1076 * Let's close all file descriptors before vhangup
1077 * https://lkml.org/lkml/2012/6/5/145
1080 close(STDOUT_FILENO
);
1081 close(STDERR_FILENO
);
1086 log_err(_("/dev/%s: vhangup() failed: %m"), tty
);
1091 if (open(buf
, O_RDWR
|O_NOCTTY
|O_NONBLOCK
, 0) != 0)
1092 log_err(_("/dev/%s: cannot open as standard input: %m"), tty
);
1094 if (((tid
= tcgetsid(STDIN_FILENO
)) < 0) || (pid
!= tid
)) {
1095 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 1) == -1)
1096 log_warn(_("/dev/%s: cannot get controlling tty: %m"), tty
);
1102 * Standard input should already be connected to an open port. Make
1103 * sure it is open for read/write.
1106 if ((fcntl(STDIN_FILENO
, F_GETFL
, 0) & O_RDWR
) != O_RDWR
)
1107 log_err(_("%s: not open for read/write"), tty
);
1111 if (tcsetpgrp(STDIN_FILENO
, pid
))
1112 log_warn(_("/dev/%s: cannot set process group: %m"), tty
);
1114 /* Get rid of the present outputs. */
1116 close(STDOUT_FILENO
);
1117 close(STDERR_FILENO
);
1121 /* Set up standard output and standard error file descriptors. */
1124 /* set up stdout and stderr */
1125 if (dup(STDIN_FILENO
) != 1 || dup(STDIN_FILENO
) != 2)
1126 log_err(_("%s: dup problem: %m"), tty
);
1128 /* make stdio unbuffered for slow modem lines */
1129 setvbuf(stdout
, NULL
, _IONBF
, 0);
1132 * The following ioctl will fail if stdin is not a tty, but also when
1133 * there is noise on the modem control lines. In the latter case, the
1134 * common course of action is (1) fix your cables (2) give the modem
1135 * more time to properly reset after hanging up.
1137 * SunOS users can achieve (2) by patching the SunOS kernel variable
1138 * "zsadtrlow" to a larger value; 5 seconds seems to be a good value.
1139 * http://www.sunmanagers.org/archives/1993/0574.html
1141 memset(tp
, 0, sizeof(struct termios
));
1142 if (tcgetattr(STDIN_FILENO
, tp
) < 0)
1143 log_err(_("%s: failed to get terminal attributes: %m"), tty
);
1145 #if defined (__s390__) || defined (__s390x__)
1148 * Special terminal on first serial line on a S/390(x) which
1149 * is due legacy reasons a block terminal of type 3270 or
1150 * higher. Whereas the second serial line on a S/390(x) is
1151 * a real character terminal which is compatible with VT220.
1153 if (strcmp(op
->tty
, "ttyS0") == 0) /* linux/drivers/s390/char/con3215.c */
1154 op
->term
= DEFAULT_TTYS0
;
1155 else if (strncmp(op
->tty
, "3270/tty", 8) == 0) /* linux/drivers/s390/char/con3270.c */
1156 op
->term
= DEFAULT_TTY32
;
1157 else if (strcmp(op
->tty
, "ttyS1") == 0) /* linux/drivers/s390/char/sclp_vt220.c */
1158 op
->term
= DEFAULT_TTYS1
;
1162 #if defined(__FreeBSD_kernel__)
1167 * Detect if this is a virtual console or serial/modem line.
1168 * In case of a virtual console the ioctl KDGKBMODE succeeds
1169 * whereas on other lines it will fails.
1172 if (ioctl(STDIN_FILENO
, KDGKBMODE
, &op
->kbmode
) == 0)
1174 if (ioctl(STDIN_FILENO
, TIOCMGET
, &serial
) < 0 && (errno
== EINVAL
))
1177 op
->flags
|= F_VCONSOLE
;
1179 op
->term
= DEFAULT_VCTERM
;
1185 op
->term
= DEFAULT_STERM
;
1188 if (setenv("TERM", op
->term
, 1) != 0)
1189 log_err(_("failed to set the %s environment variable"), "TERM");
1192 /* Initialize termios settings. */
1193 static void termio_clear(int fd
)
1196 * Do not write a full reset (ESC c) because this destroys
1197 * the unicode mode again if the terminal was in unicode
1198 * mode. Also it clears the CONSOLE_MAGIC features which
1199 * are required for some languages/console-fonts.
1200 * Just put the cursor to the home position (ESC [ H),
1201 * erase everything below the cursor (ESC [ J), and set the
1202 * scrolling region to the full window (ESC [ r)
1204 write_all(fd
, "\033[r\033[H\033[J", 9);
1207 /* Initialize termios settings. */
1208 static void termio_init(struct options
*op
, struct termios
*tp
)
1210 speed_t ispeed
, ospeed
;
1212 #ifdef USE_PLYMOUTH_SUPPORT
1213 struct termios lock
;
1214 int i
= (plymouth_command(MAGIC_PING
) == 0) ? PLYMOUTH_TERMIOS_FLAGS_DELAY
: 0;
1216 plymouth_command(MAGIC_QUIT
);
1219 * Even with TTYReset=no it seems with systemd or plymouth
1220 * the termios flags become changed from under the first
1221 * agetty on a serial system console as the flags are locked.
1223 memset(&lock
, 0, sizeof(struct termios
));
1224 if (ioctl(STDIN_FILENO
, TIOCGLCKTRMIOS
, &lock
) < 0)
1226 if (!lock
.c_iflag
&& !lock
.c_oflag
&& !lock
.c_cflag
&& !lock
.c_lflag
)
1228 debug("termios locked\n");
1231 memset(&lock
, 0, sizeof(struct termios
));
1232 ioctl(STDIN_FILENO
, TIOCSLCKTRMIOS
, &lock
);
1235 if (op
->flags
& F_VCONSOLE
) {
1236 #if defined(IUTF8) && defined(KDGKBMODE)
1237 switch(op
->kbmode
) {
1239 setlocale(LC_CTYPE
, "C.UTF-8");
1240 op
->flags
|= F_UTF8
;
1246 setlocale(LC_CTYPE
, "POSIX");
1247 op
->flags
&= ~F_UTF8
;
1251 setlocale(LC_CTYPE
, "POSIX");
1252 op
->flags
&= ~F_UTF8
;
1254 reset_vc(op
, tp
, 0);
1256 if ((tp
->c_cflag
& (CS8
|PARODD
|PARENB
)) == CS8
)
1257 op
->flags
|= F_EIGHTBITS
;
1259 if ((op
->flags
& F_NOCLEAR
) == 0)
1260 termio_clear(STDOUT_FILENO
);
1268 if (op
->flags
& F_KEEPSPEED
|| !op
->numspeed
) {
1269 /* Save the original setting. */
1270 ispeed
= cfgetispeed(tp
);
1271 ospeed
= cfgetospeed(tp
);
1273 if (!ispeed
) ispeed
= TTYDEF_SPEED
;
1274 if (!ospeed
) ospeed
= TTYDEF_SPEED
;
1277 ospeed
= ispeed
= op
->speeds
[FIRST_SPEED
];
1281 * Initial termios settings: 8-bit characters, raw-mode, blocking i/o.
1282 * Special characters are set after we have read the login name; all
1283 * reads will be done in raw mode anyway. Errors will be dealt with
1287 /* The default is set c_iflag in termio_final() according to chardata.
1288 * Unfortunately, the chardata are not set according to the serial line
1289 * if --autolog is enabled. In this case we do not read from the line
1290 * at all. The best what we can do in this case is to keep c_iflag
1291 * unmodified for --autolog.
1295 tp
->c_iflag
= tp
->c_iflag
& IUTF8
;
1296 if (tp
->c_iflag
& IUTF8
)
1297 op
->flags
|= F_UTF8
;
1304 tp
->c_oflag
&= OPOST
| ONLCR
;
1306 if ((op
->flags
& F_KEEPCFLAGS
) == 0)
1307 tp
->c_cflag
= CS8
| HUPCL
| CREAD
| (tp
->c_cflag
& CLOCAL
);
1310 * Note that the speed is stored in the c_cflag termios field, so we have
1311 * set the speed always when the cflag is reset.
1313 cfsetispeed(tp
, ispeed
);
1314 cfsetospeed(tp
, ospeed
);
1316 /* The default is to follow setting from kernel, but it's possible
1317 * to explicitly remove/add CLOCAL flag by -L[=<mode>]*/
1318 switch (op
->clocal
) {
1319 case CLOCAL_MODE_ALWAYS
:
1320 tp
->c_cflag
|= CLOCAL
; /* -L or -L=always */
1322 case CLOCAL_MODE_NEVER
:
1323 tp
->c_cflag
&= ~CLOCAL
; /* -L=never */
1325 case CLOCAL_MODE_AUTO
: /* -L=auto */
1329 #ifdef HAVE_STRUCT_TERMIOS_C_LINE
1333 tp
->c_cc
[VTIME
] = 0;
1335 /* Check for terminal size and if not found set default */
1336 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0) {
1341 if (ioctl(STDIN_FILENO
, TIOCSWINSZ
, &ws
))
1342 debug("TIOCSWINSZ ioctl failed\n");
1345 /* Optionally enable hardware flow control. */
1347 if (op
->flags
& F_RTSCTS
)
1348 tp
->c_cflag
|= CRTSCTS
;
1350 /* Flush input and output queues, important for modems! */
1351 tcflush(STDIN_FILENO
, TCIOFLUSH
);
1353 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
))
1354 log_warn(_("setting terminal attributes failed: %m"));
1356 /* Go to blocking input even in local mode. */
1357 fcntl(STDIN_FILENO
, F_SETFL
,
1358 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1360 debug("term_io 2\n");
1363 /* Reset virtual console on stdin to its defaults */
1364 static void reset_vc(const struct options
*op
, struct termios
*tp
, int canon
)
1368 fl
|= (op
->flags
& F_KEEPCFLAGS
) == 0 ? 0 : UL_TTY_KEEPCFLAGS
;
1369 fl
|= (op
->flags
& F_UTF8
) == 0 ? 0 : UL_TTY_UTF8
;
1371 reset_virtual_console(tp
, fl
);
1373 #ifdef AGETTY_RELOAD
1375 * Discard all the flags that makes the line go canonical with echoing.
1376 * We need to know when the user starts typing.
1382 if (tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
))
1383 log_warn(_("setting terminal attributes failed: %m"));
1385 /* Go to blocking input even in local mode. */
1386 fcntl(STDIN_FILENO
, F_SETFL
,
1387 fcntl(STDIN_FILENO
, F_GETFL
, 0) & ~O_NONBLOCK
);
1390 /* Extract baud rate from modem status message. */
1391 static void auto_baud(struct termios
*tp
)
1401 * This works only if the modem produces its status code AFTER raising
1402 * the DCD line, and if the computer is fast enough to set the proper
1403 * baud rate before the message has gone by. We expect a message of the
1406 * <junk><number><junk>
1408 * The number is interpreted as the baud rate of the incoming call. If the
1409 * modem does not tell us the baud rate within one second, we will keep
1410 * using the current baud rate. It is advisable to enable BREAK
1411 * processing (comma-separated list of baud rates) if the processing of
1412 * modem status messages is enabled.
1416 * Use 7-bit characters, don't block if input queue is empty. Errors will
1417 * be dealt with later on.
1419 iflag
= tp
->c_iflag
;
1420 /* Enable 8th-bit stripping. */
1421 tp
->c_iflag
|= ISTRIP
;
1422 vmin
= tp
->c_cc
[VMIN
];
1423 /* Do not block when queue is empty. */
1425 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1428 * Wait for a while, then read everything the modem has said so far and
1429 * try to extract the speed of the dial-in call.
1432 if ((nread
= read(STDIN_FILENO
, buf
, sizeof(buf
) - 1)) > 0) {
1434 for (bp
= buf
; bp
< buf
+ nread
; bp
++)
1435 if (isascii(*bp
) && isdigit(*bp
)) {
1436 if ((speed
= bcode(bp
))) {
1437 cfsetispeed(tp
, speed
);
1438 cfsetospeed(tp
, speed
);
1444 /* Restore terminal settings. Errors will be dealt with later on. */
1445 tp
->c_iflag
= iflag
;
1446 tp
->c_cc
[VMIN
] = vmin
;
1447 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
1450 static char *xgethostname(void)
1453 size_t sz
= get_hostname_max() + 1;
1455 name
= malloc(sizeof(char) * sz
);
1457 log_err(_("failed to allocate memory: %m"));
1459 if (gethostname(name
, sz
) != 0) {
1463 name
[sz
- 1] = '\0';
1467 static char *xgetdomainname(void)
1469 #ifdef HAVE_GETDOMAINNAME
1471 const size_t sz
= get_hostname_max() + 1;
1473 name
= malloc(sizeof(char) * sz
);
1475 log_err(_("failed to allocate memory: %m"));
1477 if (getdomainname(name
, sz
) != 0) {
1481 name
[sz
- 1] = '\0';
1489 static char *read_os_release(struct options
*op
, const char *varname
)
1493 size_t varsz
= strlen(varname
);
1494 char *p
, *buf
= NULL
, *ret
= NULL
;
1496 /* read the file only once */
1497 if (!op
->osrelease
) {
1498 fd
= open(_PATH_OS_RELEASE_ETC
, O_RDONLY
);
1500 fd
= open(_PATH_OS_RELEASE_USR
, O_RDONLY
);
1502 log_warn(_("cannot open os-release file"));
1507 if (fstat(fd
, &st
) < 0 || st
.st_size
> 4 * 1024 * 1024)
1510 op
->osrelease
= malloc(st
.st_size
+ 1);
1512 log_err(_("failed to allocate memory: %m"));
1513 if (read_all(fd
, op
->osrelease
, st
.st_size
) != (ssize_t
) st
.st_size
) {
1514 free(op
->osrelease
);
1515 op
->osrelease
= NULL
;
1518 op
->osrelease
[st
.st_size
] = 0;
1520 buf
= strdup(op
->osrelease
);
1522 log_err(_("failed to allocate memory: %m"));
1528 p
+= strspn(p
, "\n\r");
1529 p
+= strspn(p
, " \t\n\r");
1532 if (strspn(p
, "#;\n") != 0) {
1533 p
+= strcspn(p
, "\n\r");
1536 if (strncmp(p
, varname
, varsz
) != 0) {
1537 p
+= strcspn(p
, "\n\r");
1541 p
+= strspn(p
, " \t\n\r");
1546 p
+= strspn(p
, " \t\n\r=\"");
1547 eol
= p
+ strcspn(p
, "\n\r");
1551 if (*eon
== '\t' || *eon
== ' ') {
1564 log_err(_("failed to allocate memory: %m"));
1574 #ifdef AGETTY_RELOAD
1575 static void open_netlink(void)
1577 struct sockaddr_nl addr
= { 0, };
1580 if (netlink_fd
!= AGETTY_RELOAD_FDNONE
)
1583 sock
= socket(AF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
1585 addr
.nl_family
= AF_NETLINK
;
1586 addr
.nl_pid
= getpid();
1587 addr
.nl_groups
= netlink_groups
;
1588 if (bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
1595 static int process_netlink_msg(int *triggered
)
1598 struct sockaddr_nl snl
;
1602 struct iovec iov
= {
1604 .iov_len
= sizeof(buf
)
1606 struct msghdr msg
= {
1608 .msg_namelen
= sizeof(snl
),
1611 .msg_control
= NULL
,
1612 .msg_controllen
= 0,
1616 rc
= recvmsg(netlink_fd
, &msg
, MSG_DONTWAIT
);
1618 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
1621 /* Failure, just stop listening for changes */
1623 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1627 for (h
= (struct nlmsghdr
*)buf
; NLMSG_OK(h
, (unsigned int)rc
); h
= NLMSG_NEXT(h
, rc
)) {
1628 if (h
->nlmsg_type
== NLMSG_DONE
||
1629 h
->nlmsg_type
== NLMSG_ERROR
) {
1631 netlink_fd
= AGETTY_RELOAD_FDNONE
;
1642 static int process_netlink(void)
1645 while (process_netlink_msg(&triggered
));
1649 static int wait_for_term_input(int fd
)
1651 char buffer
[sizeof(struct inotify_event
) + NAME_MAX
+ 1];
1654 if (inotify_fd
== AGETTY_RELOAD_FDNONE
) {
1655 /* make sure the reload trigger file exists */
1656 int reload_fd
= open(AGETTY_RELOAD_FILENAME
,
1657 O_CREAT
|O_CLOEXEC
|O_RDONLY
,
1660 /* initialize reload trigger inotify stuff */
1661 if (reload_fd
>= 0) {
1662 inotify_fd
= inotify_init1(IN_NONBLOCK
| IN_CLOEXEC
);
1664 inotify_add_watch(inotify_fd
, AGETTY_RELOAD_FILENAME
,
1665 IN_ATTRIB
| IN_MODIFY
);
1669 log_warn(_("failed to create reload file: %s: %m"),
1670 AGETTY_RELOAD_FILENAME
);
1679 if (inotify_fd
>= 0) {
1680 FD_SET(inotify_fd
, &rfds
);
1681 nfds
= max(nfds
, inotify_fd
);
1683 if (netlink_fd
>= 0) {
1684 FD_SET(netlink_fd
, &rfds
);
1685 nfds
= max(nfds
, netlink_fd
);
1688 /* If waiting fails, just fall through, presumably reading input will fail */
1689 if (select(nfds
+ 1, &rfds
, NULL
, NULL
, NULL
) < 0)
1692 if (FD_ISSET(fd
, &rfds
)) {
1695 } else if (netlink_fd
>= 0 && FD_ISSET(netlink_fd
, &rfds
)) {
1696 if (!process_netlink())
1699 /* Just drain the inotify buffer */
1700 } else if (inotify_fd
>= 0 && FD_ISSET(inotify_fd
, &rfds
)) {
1701 while (read(inotify_fd
, buffer
, sizeof (buffer
)) > 0);
1707 #endif /* AGETTY_RELOAD */
1709 #ifdef ISSUEDIR_SUPPORT
1710 static int issuedir_filter(const struct dirent
*d
)
1714 #ifdef _DIRENT_HAVE_D_TYPE
1715 if (d
->d_type
!= DT_UNKNOWN
&& d
->d_type
!= DT_REG
&&
1716 d
->d_type
!= DT_LNK
)
1719 if (*d
->d_name
== '.')
1722 namesz
= strlen(d
->d_name
);
1723 if (!namesz
|| namesz
< ISSUEDIR_EXTSIZ
+ 1 ||
1724 strcmp(d
->d_name
+ (namesz
- ISSUEDIR_EXTSIZ
), ISSUEDIR_EXT
))
1731 static FILE *issuedir_next_file(int dd
, struct dirent
**namelist
, int nfiles
, int *n
)
1733 while (*n
< nfiles
) {
1734 struct dirent
*d
= namelist
[*n
];
1740 if (fstatat(dd
, d
->d_name
, &st
, 0) ||
1741 !S_ISREG(st
.st_mode
))
1744 f
= fopen_at(dd
, d
->d_name
, O_RDONLY
|O_CLOEXEC
, "r" UL_CLOEXECSTR
);
1751 #endif /* ISSUEDIR_SUPPORT */
1753 #ifndef ISSUE_SUPPORT
1754 static void print_issue_file(struct issue
*ie
__attribute__((__unused__
)),
1756 struct termios
*tp
__attribute__((__unused__
)))
1758 if ((op
->flags
& F_NONL
) == 0) {
1759 /* Issue not in use, start with a new line. */
1760 write_all(STDOUT_FILENO
, "\r\n", 2);
1764 static void eval_issue_file(struct issue
*ie
__attribute__((__unused__
)),
1765 struct options
*op
__attribute__((__unused__
)),
1766 struct termios
*tp
__attribute__((__unused__
)))
1769 #else /* ISSUE_SUPPORT */
1771 #ifdef AGETTY_RELOAD
1772 static int issue_is_changed(struct issue
*ie
)
1774 if (ie
->mem_old
&& ie
->mem
1775 && strcmp(ie
->mem_old
, ie
->mem
) == 0) {
1777 ie
->mem_old
= ie
->mem
;
1786 static void print_issue_file(struct issue
*ie
,
1790 int oflag
= tp
->c_oflag
; /* Save current setting. */
1792 if ((op
->flags
& F_NONL
) == 0) {
1793 /* Issue not in use, start with a new line. */
1794 write_all(STDOUT_FILENO
, "\r\n", 2);
1797 if (ie
->do_tcsetattr
) {
1798 if ((op
->flags
& F_VCONSOLE
) == 0) {
1799 /* Map new line in output to carriage return & new line. */
1800 tp
->c_oflag
|= (ONLCR
| OPOST
);
1801 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1806 write_all(STDOUT_FILENO
, ie
->mem
, ie
->mem_sz
);
1808 if (ie
->do_tcrestore
) {
1809 /* Restore settings. */
1810 tp
->c_oflag
= oflag
;
1811 /* Wait till output is gone. */
1812 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tp
);
1815 #ifdef AGETTY_RELOAD
1817 ie
->mem_old
= ie
->mem
;
1827 static void eval_issue_file(struct issue
*ie
,
1831 const char *filename
, *dirname
= NULL
;
1833 #ifdef ISSUEDIR_SUPPORT
1834 int dd
= -1, nfiles
= 0, i
;
1835 struct dirent
**namelist
= NULL
;
1837 #ifdef AGETTY_RELOAD
1841 if (!(op
->flags
& F_ISSUE
))
1845 * The custom issue file or directory specified by: agetty -f <path>.
1846 * Note that nothing is printed if the file/dir does not exist.
1848 filename
= op
->issue
;
1852 if (stat(filename
, &st
) < 0)
1854 if (S_ISDIR(st
.st_mode
)) {
1859 /* The default /etc/issue and optional /etc/issue.d directory
1860 * as extension to the file. The /etc/issue.d directory is
1861 * ignored if there is no /etc/issue file. The file may be
1864 if (access(_PATH_ISSUE
, F_OK
|R_OK
) != 0)
1866 filename
= _PATH_ISSUE
;
1867 dirname
= _PATH_ISSUEDIR
;
1870 ie
->output
= open_memstream(&ie
->mem
, &ie
->mem_sz
);
1871 #ifdef ISSUEDIR_SUPPORT
1873 dd
= open(dirname
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1875 nfiles
= scandirat(dd
, ".", &namelist
, issuedir_filter
, versionsort
);
1882 f
= fopen(filename
, "r");
1887 ie
->do_tcsetattr
= 1;
1890 #ifdef ISSUEDIR_SUPPORT
1891 if (!f
&& i
< nfiles
)
1892 f
= issuedir_next_file(dd
, namelist
, nfiles
, &i
);
1896 while ((c
= getc(f
)) != EOF
) {
1898 output_special_char(ie
, getc(f
), op
, tp
, f
);
1900 putc(c
, ie
->output
);
1908 if ((op
->flags
& F_VCONSOLE
) == 0)
1909 ie
->do_tcrestore
= 1;
1912 #ifdef ISSUEDIR_SUPPORT
1913 for (i
= 0; i
< nfiles
; i
++)
1919 #ifdef AGETTY_RELOAD
1920 if (netlink_groups
!= 0)
1925 #endif /* ISSUE_SUPPORT */
1927 /* Show login prompt, optionally preceded by /etc/issue contents. */
1928 static void do_prompt(struct issue
*ie
, struct options
*op
, struct termios
*tp
)
1930 #ifdef AGETTY_RELOAD
1933 print_issue_file(ie
, op
, tp
);
1935 if (op
->flags
& F_LOGINPAUSE
) {
1936 puts(_("[press ENTER to login]"));
1937 #ifdef AGETTY_RELOAD
1939 if (!wait_for_term_input(STDIN_FILENO
)) {
1940 eval_issue_file(ie
, op
, tp
);
1941 if (issue_is_changed(ie
)) {
1942 if (op
->flags
& F_VCONSOLE
)
1943 termio_clear(STDOUT_FILENO
);
1951 if (!(op
->flags
& F_NOHINTS
) && !op
->autolog
&&
1952 (op
->flags
& F_VCONSOLE
)) {
1955 if (ioctl(STDIN_FILENO
, KDGKBLED
, &kb
) == 0) {
1956 char hint
[256] = { '\0' };
1959 if (access(_PATH_NUMLOCK_ON
, F_OK
) == 0)
1962 if (nl
&& (kb
& 0x02) == 0)
1963 append(hint
, sizeof(hint
), NULL
, _("Num Lock off"));
1965 else if (nl
== 0 && (kb
& 2) && (kb
& 0x20) == 0)
1966 append(hint
, sizeof(hint
), NULL
, _("Num Lock on"));
1968 if ((kb
& 0x04) && (kb
& 0x40) == 0)
1969 append(hint
, sizeof(hint
), ", ", _("Caps Lock on"));
1971 if ((kb
& 0x01) && (kb
& 0x10) == 0)
1972 append(hint
, sizeof(hint
), ", ", _("Scroll Lock on"));
1975 printf(_("Hint: %s\n\n"), hint
);
1978 #endif /* KDGKBLED */
1979 if ((op
->flags
& F_NOHOSTNAME
) == 0) {
1980 char *hn
= xgethostname();
1983 char *dot
= strchr(hn
, '.');
1985 struct addrinfo
*res
= NULL
;
1987 if ((op
->flags
& F_LONGHNAME
) == 0) {
1991 } else if (dot
== NULL
) {
1992 struct addrinfo hints
;
1994 memset(&hints
, 0, sizeof(hints
));
1995 hints
.ai_flags
= AI_CANONNAME
;
1997 if (!getaddrinfo(hn
, NULL
, &hints
, &res
)
1998 && res
&& res
->ai_canonname
)
1999 cn
= res
->ai_canonname
;
2002 write_all(STDOUT_FILENO
, cn
, strlen(cn
));
2003 write_all(STDOUT_FILENO
, " ", 1);
2011 /* Always show login prompt. */
2012 write_all(STDOUT_FILENO
, LOGIN
, sizeof(LOGIN
) - 1);
2016 /* Select next baud rate. */
2017 static void next_speed(struct options
*op
, struct termios
*tp
)
2019 static int baud_index
= -1;
2021 if (baud_index
== -1)
2023 * If the F_KEEPSPEED flags is set then the FIRST_SPEED is not
2024 * tested yet (see termio_init()).
2027 (op
->flags
& F_KEEPSPEED
) ? FIRST_SPEED
: 1 % op
->numspeed
;
2029 baud_index
= (baud_index
+ 1) % op
->numspeed
;
2031 cfsetispeed(tp
, op
->speeds
[baud_index
]);
2032 cfsetospeed(tp
, op
->speeds
[baud_index
]);
2033 tcsetattr(STDIN_FILENO
, TCSANOW
, tp
);
2036 /* Get user name, establish parity, speed, erase, kill & eol. */
2037 static char *get_logname(struct issue
*ie
, struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
2039 static char logname
[BUFSIZ
];
2041 char c
; /* input character, full eight bits */
2042 char ascval
; /* low 7 bits of input character */
2044 static char *erase
[] = { /* backspace-space-backspace */
2045 "\010\040\010", /* space parity */
2046 "\010\040\010", /* odd parity */
2047 "\210\240\210", /* even parity */
2048 "\210\240\210", /* no parity */
2051 /* Initialize kill, erase, parity etc. (also after switching speeds). */
2055 * Flush pending input (especially important after parsing or switching
2058 if ((op
->flags
& F_VCONSOLE
) == 0)
2060 tcflush(STDIN_FILENO
, TCIFLUSH
);
2062 eightbit
= (op
->flags
& (F_EIGHTBITS
|F_UTF8
));
2066 eval_issue_file(ie
, op
, tp
);
2067 while (*logname
== '\0') {
2068 /* Write issue file and prompt */
2069 do_prompt(ie
, op
, tp
);
2072 #ifdef AGETTY_RELOAD
2073 if (!wait_for_term_input(STDIN_FILENO
)) {
2074 /* refresh prompt -- discard input data, clear terminal
2075 * and call do_prompt() again
2077 if ((op
->flags
& F_VCONSOLE
) == 0)
2079 eval_issue_file(ie
, op
, tp
);
2080 if (!issue_is_changed(ie
))
2082 tcflush(STDIN_FILENO
, TCIFLUSH
);
2083 if (op
->flags
& F_VCONSOLE
)
2084 termio_clear(STDOUT_FILENO
);
2092 /* Read name, watch for break and end-of-line. */
2093 while (cp
->eol
== '\0') {
2098 debug("read from FD\n");
2099 readres
= read(STDIN_FILENO
, &c
, 1);
2101 debug("read failed\n");
2103 /* The terminal could be open with O_NONBLOCK when
2104 * -L (force CLOCAL) is specified... */
2105 if (errno
== EINTR
|| errno
== EAGAIN
) {
2115 exit_slowly(EXIT_SUCCESS
);
2117 log_err(_("%s: read: %m"), op
->tty
);
2124 /* Do parity bit handling. */
2127 else if (c
!= (ascval
= (c
& 0177))) {
2128 uint32_t bits
; /* # of "1" bits per character */
2129 uint32_t mask
; /* mask with 1 bit up */
2130 for (bits
= 1, mask
= 1; mask
& 0177; mask
<<= 1) {
2134 cp
->parity
|= ((bits
& 1) ? 1 : 2);
2137 if (op
->killchars
&& strchr(op
->killchars
, ascval
))
2139 else if (op
->erasechars
&& strchr(op
->erasechars
, ascval
))
2144 /* Do erase, kill and end-of-line processing. */
2148 if (op
->numspeed
> 1 && !(op
->flags
& F_VCONSOLE
))
2151 exit_slowly(EXIT_SUCCESS
);
2155 *bp
= 0; /* terminate logname */
2156 cp
->eol
= ascval
; /* set end-of-line char */
2160 cp
->erase
= ascval
; /* set erase character */
2162 if ((tp
->c_lflag
& ECHO
) == 0)
2163 write_all(1, erase
[cp
->parity
], 3);
2168 cp
->kill
= ascval
; /* set kill character */
2169 while (bp
> logname
) {
2170 if ((tp
->c_lflag
& ECHO
) == 0)
2171 write_all(1, erase
[cp
->parity
], 3);
2178 if ((size_t)(bp
- logname
) >= sizeof(logname
) - 1)
2179 log_err(_("%s: input overrun"), op
->tty
);
2180 if ((tp
->c_lflag
& ECHO
) == 0)
2181 write_all(1, &c
, 1); /* echo the character */
2182 *bp
++ = ascval
; /* and store it */
2185 /* Everything was erased. */
2186 if (bp
== logname
&& cp
->eol
== '\0')
2191 #ifdef HAVE_WIDECHAR
2192 if ((op
->flags
& (F_EIGHTBITS
|F_UTF8
)) == (F_EIGHTBITS
|F_UTF8
)) {
2193 /* Check out UTF-8 multibyte characters */
2197 len
= mbstowcs((wchar_t *)0, logname
, 0);
2199 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
2201 wcs
= malloc((len
+ 1) * sizeof(wchar_t));
2203 log_err(_("failed to allocate memory: %m"));
2205 len
= mbstowcs(wcs
, logname
, len
+ 1);
2207 log_err(_("%s: invalid character conversion for login name"), op
->tty
);
2211 const wint_t wc
= *wcp
++;
2213 log_err(_("%s: invalid character 0x%x in login name"), op
->tty
, wc
);
2218 if ((op
->flags
& F_LCUC
) && (cp
->capslock
= caps_lock(logname
))) {
2220 /* Handle names with upper case and no lower case. */
2221 for (bp
= logname
; *bp
; bp
++)
2223 *bp
= tolower(*bp
); /* map name to lower case */
2229 /* Set the final tty mode bits. */
2230 static void termio_final(struct options
*op
, struct termios
*tp
, struct chardata
*cp
)
2232 /* General terminal-independent stuff. */
2234 /* 2-way flow control */
2235 tp
->c_iflag
|= IXON
| IXOFF
;
2236 tp
->c_lflag
|= ICANON
| ISIG
| ECHO
| ECHOE
| ECHOK
| ECHOKE
;
2237 /* no longer| ECHOCTL | ECHOPRT */
2238 tp
->c_oflag
|= OPOST
;
2239 /* tp->c_cflag = 0; */
2240 tp
->c_cc
[VINTR
] = DEF_INTR
;
2241 tp
->c_cc
[VQUIT
] = DEF_QUIT
;
2242 tp
->c_cc
[VEOF
] = DEF_EOF
;
2243 tp
->c_cc
[VEOL
] = DEF_EOL
;
2245 tp
->c_cc
[VSWTC
] = DEF_SWITCH
;
2246 #elif defined(VSWTCH)
2247 tp
->c_cc
[VSWTCH
] = DEF_SWITCH
;
2248 #endif /* __linux__ */
2250 /* Account for special characters seen in input. */
2251 if (cp
->eol
== CR
) {
2252 tp
->c_iflag
|= ICRNL
;
2253 tp
->c_oflag
|= ONLCR
;
2255 tp
->c_cc
[VERASE
] = cp
->erase
;
2256 tp
->c_cc
[VKILL
] = cp
->kill
;
2258 /* Account for the presence or absence of parity bits in input. */
2259 switch (cp
->parity
) {
2261 /* space (always 0) parity */
2265 tp
->c_cflag
|= PARODD
;
2269 tp
->c_cflag
|= PARENB
;
2270 tp
->c_iflag
|= INPCK
| ISTRIP
;
2274 tp
->c_cflag
&= ~CSIZE
;
2278 /* Account for upper case without lower case. */
2281 tp
->c_iflag
|= IUCLC
;
2284 tp
->c_lflag
|= XCASE
;
2287 tp
->c_oflag
|= OLCUC
;
2290 /* Optionally enable hardware flow control. */
2292 if (op
->flags
& F_RTSCTS
)
2293 tp
->c_cflag
|= CRTSCTS
;
2296 /* Finally, make the new settings effective. */
2297 if (tcsetattr(STDIN_FILENO
, TCSANOW
, tp
) < 0)
2298 log_err(_("%s: failed to set terminal attributes: %m"), op
->tty
);
2302 * String contains upper case without lower case.
2303 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52940
2304 * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=156242
2306 static int caps_lock(char *s
)
2310 for (capslock
= 0; *s
; s
++) {
2312 return EXIT_SUCCESS
;
2314 capslock
= isupper(*s
);
2319 /* Convert speed string to speed code; return 0 on failure. */
2320 static speed_t
bcode(char *s
)
2322 const struct Speedtab
*sp
;
2323 long speed
= atol(s
);
2325 for (sp
= speedtab
; sp
->speed
; sp
++)
2326 if (sp
->speed
== speed
)
2331 static void __attribute__((__noreturn__
)) usage(void)
2335 fputs(USAGE_HEADER
, out
);
2336 fprintf(out
, _(" %1$s [options] <line> [<baud_rate>,...] [<termtype>]\n"
2337 " %1$s [options] <baud_rate>,... <line> [<termtype>]\n"), program_invocation_short_name
);
2339 fputs(USAGE_SEPARATOR
, out
);
2340 fputs(_("Open a terminal and set its mode.\n"), out
);
2342 fputs(USAGE_OPTIONS
, out
);
2343 fputs(_(" -8, --8bits assume 8-bit tty\n"), out
);
2344 fputs(_(" -a, --autologin <user> login the specified user automatically\n"), out
);
2345 fputs(_(" -c, --noreset do not reset control mode\n"), out
);
2346 fputs(_(" -E, --remote use -r <hostname> for login(1)\n"), out
);
2347 fputs(_(" -f, --issue-file <file> display issue file\n"), out
);
2348 fputs(_(" -h, --flow-control enable hardware flow control\n"), out
);
2349 fputs(_(" -H, --host <hostname> specify login host\n"), out
);
2350 fputs(_(" -i, --noissue do not display issue file\n"), out
);
2351 fputs(_(" -I, --init-string <string> set init string\n"), out
);
2352 fputs(_(" -J --noclear do not clear the screen before prompt\n"), out
);
2353 fputs(_(" -l, --login-program <file> specify login program\n"), out
);
2354 fputs(_(" -L, --local-line[=<mode>] control the local line flag\n"), out
);
2355 fputs(_(" -m, --extract-baud extract baud rate during connect\n"), out
);
2356 fputs(_(" -n, --skip-login do not prompt for login\n"), out
);
2357 fputs(_(" -N --nonewline do not print a newline before issue\n"), out
);
2358 fputs(_(" -o, --login-options <opts> options that are passed to login\n"), out
);
2359 fputs(_(" -p, --login-pause wait for any key before the login\n"), out
);
2360 fputs(_(" -r, --chroot <dir> change root to the directory\n"), out
);
2361 fputs(_(" -R, --hangup do virtually hangup on the tty\n"), out
);
2362 fputs(_(" -s, --keep-baud try to keep baud rate after break\n"), out
);
2363 fputs(_(" -t, --timeout <number> login process timeout\n"), out
);
2364 fputs(_(" -U, --detect-case detect uppercase terminal\n"), out
);
2365 fputs(_(" -w, --wait-cr wait carriage-return\n"), out
);
2366 fputs(_(" --nohints do not print hints\n"), out
);
2367 fputs(_(" --nohostname no hostname at all will be shown\n"), out
);
2368 fputs(_(" --long-hostname show full qualified hostname\n"), out
);
2369 fputs(_(" --erase-chars <string> additional backspace chars\n"), out
);
2370 fputs(_(" --kill-chars <string> additional kill chars\n"), out
);
2371 fputs(_(" --chdir <directory> chdir before the login\n"), out
);
2372 fputs(_(" --delay <number> sleep seconds before prompt\n"), out
);
2373 fputs(_(" --nice <number> run login with this priority\n"), out
);
2374 fputs(_(" --reload reload prompts on running agetty instances\n"), out
);
2375 fputs(_(" --list-speeds display supported baud rates\n"), out
);
2376 printf( " --help %s\n", USAGE_OPTSTR_HELP
);
2377 printf( " --version %s\n", USAGE_OPTSTR_VERSION
);
2378 printf(USAGE_MAN_TAIL("agetty(8)"));
2383 static void list_speeds(void)
2385 const struct Speedtab
*sp
;
2387 for (sp
= speedtab
; sp
->speed
; sp
++)
2388 printf("%10ld\n", sp
->speed
);
2392 * Helper function reports errors to console or syslog.
2393 * Will be used by log_err() and log_warn() therefore
2394 * it takes a format as well as va_list.
2396 #define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
2398 static void dolog(int priority
, const char *fmt
, va_list ap
)
2407 * If the diagnostic is reported via syslog(3), the process name is
2408 * automatically prepended to the message. If we write directly to
2409 * /dev/console, we must prepend the process name ourselves.
2415 str2cpy(buf
, program_invocation_short_name
, ": ");
2416 bp
= buf
+ strlen(buf
);
2417 #endif /* USE_SYSLOG */
2418 vsnprintf(bp
, sizeof(buf
)-strlen(buf
), fmt
, ap
);
2421 * Write the diagnostic directly to /dev/console if we do not use the
2422 * syslog(3) facility.
2425 openlog(program_invocation_short_name
, LOG_PID
, LOG_AUTHPRIV
);
2426 syslog(priority
, "%s", buf
);
2429 /* Terminate with CR-LF since the console mode is unknown. */
2431 if ((fd
= open("/dev/console", 1)) >= 0) {
2432 write_all(fd
, buf
, strlen(buf
));
2435 #endif /* USE_SYSLOG */
2438 static void exit_slowly(int code
)
2440 /* Be kind to init(8). */
2445 static void log_err(const char *fmt
, ...)
2450 dolog(LOG_ERR
, fmt
, ap
);
2453 exit_slowly(EXIT_FAILURE
);
2456 static void log_warn(const char *fmt
, ...)
2461 dolog(LOG_WARNING
, fmt
, ap
);
2465 static void print_addr(struct issue
*ie
, sa_family_t family
, void *addr
)
2467 char buff
[INET6_ADDRSTRLEN
+ 1];
2469 inet_ntop(family
, addr
, buff
, sizeof(buff
));
2470 fprintf(ie
->output
, "%s", buff
);
2474 * Prints IP for the specified interface (@iface), if the interface is not
2475 * specified then prints the "best" one (UP, RUNNING, non-LOOPBACK). If not
2476 * found the "best" interface then prints at least host IP.
2478 static void output_iface_ip(struct issue
*ie
,
2479 struct ifaddrs
*addrs
,
2484 struct addrinfo hints
, *info
= NULL
;
2491 for (p
= addrs
; p
; p
= p
->ifa_next
) {
2495 p
->ifa_addr
->sa_family
!= family
)
2499 /* Filter out by interface name */
2500 if (strcmp(p
->ifa_name
, iface
) != 0)
2503 /* Select the "best" interface */
2504 if ((p
->ifa_flags
& IFF_LOOPBACK
) ||
2505 !(p
->ifa_flags
& IFF_UP
) ||
2506 !(p
->ifa_flags
& IFF_RUNNING
))
2511 switch (p
->ifa_addr
->sa_family
) {
2513 addr
= &((struct sockaddr_in
*) p
->ifa_addr
)->sin_addr
;
2516 addr
= &((struct sockaddr_in6
*) p
->ifa_addr
)->sin6_addr
;
2521 print_addr(ie
, family
, addr
);
2529 /* Hmm.. not found the best interface, print host IP at least */
2530 memset(&hints
, 0, sizeof(hints
));
2531 hints
.ai_family
= family
;
2532 if (family
== AF_INET6
)
2533 hints
.ai_flags
= AI_V4MAPPED
;
2535 host
= xgethostname();
2536 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2537 switch (info
->ai_family
) {
2539 addr
= &((struct sockaddr_in
*) info
->ai_addr
)->sin_addr
;
2542 addr
= &((struct sockaddr_in6
*) info
->ai_addr
)->sin6_addr
;
2546 print_addr(ie
, family
, addr
);
2554 * parses \x{argument}, if not argument specified then returns NULL, the @fd
2555 * has to point to one char after the sequence (it means '{').
2557 static char *get_escape_argument(FILE *fd
, char *buf
, size_t bufsz
)
2562 if (c
== EOF
|| (unsigned char) c
!= '{') {
2571 if ((unsigned char) c
!= '}' && i
< bufsz
- 1)
2572 buf
[i
++] = (unsigned char) c
;
2574 } while ((unsigned char) c
!= '}');
2580 static void output_special_char(struct issue
*ie
,
2591 char escname
[UL_COLORNAME_MAXSZ
];
2593 if (get_escape_argument(fp
, escname
, sizeof(escname
))) {
2594 const char *esc
= color_sequence_from_colorname(escname
);
2596 fputs(esc
, ie
->output
);
2598 fputs("\033", ie
->output
);
2603 fprintf(ie
->output
, "%s", uts
.sysname
);
2607 fprintf(ie
->output
, "%s", uts
.nodename
);
2611 fprintf(ie
->output
, "%s", uts
.release
);
2615 fprintf(ie
->output
, "%s", uts
.version
);
2619 fprintf(ie
->output
, "%s", uts
.machine
);
2623 char *dom
= xgetdomainname();
2625 fputs(dom
? dom
: "unknown_domain", ie
->output
);
2632 char *host
= xgethostname();
2633 struct addrinfo hints
, *info
= NULL
;
2635 memset(&hints
, 0, sizeof(hints
));
2636 hints
.ai_flags
= AI_CANONNAME
;
2638 if (host
&& getaddrinfo(host
, NULL
, &hints
, &info
) == 0 && info
) {
2641 if (info
->ai_canonname
&&
2642 (canon
= strchr(info
->ai_canonname
, '.')))
2645 fputs(dom
? dom
: "unknown_domain", ie
->output
);
2658 tm
= localtime(&now
);
2663 if (c
== 'd') /* ISO 8601 */
2664 fprintf(ie
->output
, "%s %s %d %d",
2665 nl_langinfo(ABDAY_1
+ tm
->tm_wday
),
2666 nl_langinfo(ABMON_1
+ tm
->tm_mon
),
2668 tm
->tm_year
< 70 ? tm
->tm_year
+ 2000 :
2669 tm
->tm_year
+ 1900);
2671 fprintf(ie
->output
, "%02d:%02d:%02d",
2672 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
2676 fprintf (ie
->output
, "%s", op
->tty
);
2680 const speed_t speed
= cfgetispeed(tp
);
2683 for (i
= 0; speedtab
[i
].speed
; i
++) {
2684 if (speedtab
[i
].code
== speed
) {
2685 fprintf(ie
->output
, "%ld", speedtab
[i
].speed
);
2693 char *var
= NULL
, varname
[64];
2696 if (get_escape_argument(fp
, varname
, sizeof(varname
))) {
2697 var
= read_os_release(op
, varname
);
2699 if (strcmp(varname
, "ANSI_COLOR") == 0)
2700 fprintf(ie
->output
, "\033[%sm", var
);
2702 fputs(var
, ie
->output
);
2705 } else if ((var
= read_os_release(op
, "PRETTY_NAME"))) {
2706 fputs(var
, ie
->output
);
2708 /* \S and PRETTY_NAME not found */
2711 fputs(uts
.sysname
, ie
->output
);
2724 while ((ut
= getutxent()))
2725 if (ut
->ut_type
== USER_PROCESS
)
2729 fprintf(ie
->output
, P_("%d user", "%d users", users
), users
);
2731 fprintf (ie
->output
, "%d ", users
);
2734 #if defined(RTMGRP_IPV4_IFADDR) && defined(RTMGRP_IPV6_IFADDR)
2738 sa_family_t family
= c
== '4' ? AF_INET
: AF_INET6
;
2739 struct ifaddrs
*addrs
= NULL
;
2742 if (getifaddrs(&addrs
))
2745 if (get_escape_argument(fp
, iface
, sizeof(iface
)))
2746 output_iface_ip(ie
, addrs
, iface
, family
);
2748 output_iface_ip(ie
, addrs
, NULL
, family
);
2753 netlink_groups
|= RTMGRP_IPV4_IFADDR
;
2755 netlink_groups
|= RTMGRP_IPV6_IFADDR
;
2760 putc(c
, ie
->output
);
2765 static void init_special_char(char* arg
, struct options
*op
)
2770 op
->initstring
= malloc(strlen(arg
) + 1);
2771 if (!op
->initstring
)
2772 log_err(_("failed to allocate memory: %m"));
2775 * Copy optarg into op->initstring decoding \ddd octal
2781 /* The \\ is converted to \ */
2788 /* Handle \000 - \177. */
2790 for (i
= 1; i
<= 3; i
++) {
2791 if (*p
>= '0' && *p
<= '7') {
2808 * Appends @str to @dest and if @dest is not empty then use @sep as a
2809 * separator. The maximal final length of the @dest is @len.
2811 * Returns the final @dest length or -1 in case of error.
2813 static ssize_t
append(char *dest
, size_t len
, const char *sep
, const char *src
)
2815 size_t dsz
= 0, ssz
= 0, sz
;
2818 if (!dest
|| !len
|| !src
)
2827 if (dsz
+ ssz
+ sz
+ 1 > len
)
2832 memcpy(p
, sep
, ssz
);
2838 return dsz
+ ssz
+ sz
;
2842 * Do not allow the user to pass an option as a user name
2843 * To be more safe: Use `--' to make sure the rest is
2844 * interpreted as non-options by the program, if it supports it.
2846 static void check_username(const char* nm
)
2851 if (strlen(nm
) > 42)
2860 log_err(_("checkname failed: %m"));
2863 static void reload_agettys(void)
2865 #ifdef AGETTY_RELOAD
2866 int fd
= open(AGETTY_RELOAD_FILENAME
, O_CREAT
|O_CLOEXEC
|O_WRONLY
,
2869 err(EXIT_FAILURE
, _("cannot open %s"), AGETTY_RELOAD_FILENAME
);
2871 if (futimens(fd
, NULL
) < 0 || close(fd
) < 0)
2872 err(EXIT_FAILURE
, _("cannot touch file %s"),
2873 AGETTY_RELOAD_FILENAME
);
2876 errx(EXIT_FAILURE
, _("--reload is unsupported on your system"));