forked from util-linux/util-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagetty.c
2970 lines (2611 loc) · 71.8 KB
/
agetty.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Alternate Getty (agetty) 'agetty' is a versatile, portable, easy to use
* replacement for getty on SunOS 4.1.x or the SAC ttymon/ttyadm/sacadm/pmadm
* suite on Solaris and other SVR4 systems. 'agetty' was written by Wietse
* Venema, enhanced by John DiMarco, and further enhanced by Dennis Cronin.
*
* Ported to Linux by Peter Orbaek <[email protected]>
* Adopt the mingetty features for a better support
* of virtual consoles by Werner Fink <[email protected]>
*
* This program is freely distributable.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <signal.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdarg.h>
#include <ctype.h>
#include <utmpx.h>
#include <getopt.h>
#include <time.h>
#include <sys/socket.h>
#include <langinfo.h>
#include <grp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/utsname.h>
#include "strutils.h"
#include "all-io.h"
#include "nls.h"
#include "pathnames.h"
#include "c.h"
#include "cctype.h"
#include "widechar.h"
#include "ttyutils.h"
#include "color-names.h"
#include "env.h"
#ifdef USE_PLYMOUTH_SUPPORT
# include "plymouth-ctrl.h"
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#if defined(__FreeBSD_kernel__)
# include <pty.h>
# ifdef HAVE_UTMP_H
# include <utmp.h>
# endif
# ifdef HAVE_LIBUTIL_H
# include <libutil.h>
# endif
#endif
#ifdef __linux__
# include <sys/kd.h>
# define USE_SYSLOG
# ifndef DEFAULT_VCTERM
# define DEFAULT_VCTERM "linux"
# endif
# if defined (__s390__) || defined (__s390x__)
# define DEFAULT_TTYS0 "dumb"
# define DEFAULT_TTY32 "ibm327x"
# define DEFAULT_TTYS1 "vt220"
# endif
# ifndef DEFAULT_STERM
# define DEFAULT_STERM "vt102"
# endif
#elif defined(__GNU__)
# define USE_SYSLOG
# ifndef DEFAULT_VCTERM
# define DEFAULT_VCTERM "hurd"
# endif
# ifndef DEFAULT_STERM
# define DEFAULT_STERM "vt102"
# endif
#else
# ifndef DEFAULT_VCTERM
# define DEFAULT_VCTERM "vt100"
# endif
# ifndef DEFAULT_STERM
# define DEFAULT_STERM "vt100"
# endif
#endif
#ifdef __FreeBSD_kernel__
#define USE_SYSLOG
#endif
/* If USE_SYSLOG is undefined all diagnostics go to /dev/console. */
#ifdef USE_SYSLOG
# include <syslog.h>
#endif
/*
* Some heuristics to find out what environment we are in: if it is not
* System V, assume it is SunOS 4. The LOGIN_PROCESS is defined in System V
* utmp.h, which will select System V style getty.
*/
#ifdef LOGIN_PROCESS
# define SYSV_STYLE
#endif
/*
* Things you may want to modify.
*
* If ISSUE_SUPPORT is not defined, agetty will never display the contents of
* the /etc/issue file. You will not want to spit out large "issue" files at
* the wrong baud rate. Relevant for System V only.
*
* You may disagree with the default line-editing etc. characters defined
* below. Note, however, that DEL cannot be used for interrupt generation
* and for line editing at the same time.
*/
/* Displayed before the login prompt. */
#ifdef SYSV_STYLE
# define ISSUE_SUPPORT
# if defined(HAVE_SCANDIRAT) && defined(HAVE_OPENAT)
# include <dirent.h>
# include "fileutils.h"
# define ISSUEDIR_SUPPORT
# define ISSUEDIR_EXT ".issue"
# define ISSUEDIR_EXTSIZ (sizeof(ISSUEDIR_EXT) - 1)
# endif
#endif
/* Login prompt. */
#define LOGIN "login: "
#define LOGIN_ARGV_MAX 16 /* Numbers of args for login */
/*
* agetty --reload
*/
#ifdef AGETTY_RELOAD
# include <sys/inotify.h>
# include <linux/netlink.h>
# include <linux/rtnetlink.h>
# define AGETTY_RELOAD_FILENAME "/run/agetty.reload" /* trigger file */
# define AGETTY_RELOAD_FDNONE -2 /* uninitialized fd */
static int inotify_fd = AGETTY_RELOAD_FDNONE;
static int netlink_fd = AGETTY_RELOAD_FDNONE;
static uint32_t netlink_groups;
#endif
struct issue {
FILE *output;
char *mem;
size_t mem_sz;
#ifdef AGETTY_RELOAD
char *mem_old;
#endif
unsigned int do_tcsetattr : 1,
do_tcrestore : 1;
};
/*
* When multiple baud rates are specified on the command line, the first one
* we will try is the first one specified.
*/
#define FIRST_SPEED 0
/* Storage for command-line options. */
#define MAX_SPEED 10 /* max. nr. of baud rates */
struct options {
int flags; /* toggle switches, see below */
unsigned int timeout; /* time-out period */
char *autolog; /* login the user automatically */
char *chdir; /* Chdir before the login */
char *chroot; /* Chroot before the login */
char *login; /* login program */
char *logopt; /* options for login program */
char *tty; /* name of tty */
char *vcline; /* line of virtual console */
char *term; /* terminal type */
char *initstring; /* modem init string */
char *issue; /* alternative issue file or directory */
char *erasechars; /* string with erase chars */
char *killchars; /* string with kill chars */
char *osrelease; /* /etc/os-release data */
unsigned int delay; /* Sleep seconds before prompt */
int nice; /* Run login with this priority */
int numspeed; /* number of baud rates to try */
int clocal; /* CLOCAL_MODE_* */
int kbmode; /* Keyboard mode if virtual console */
speed_t speeds[MAX_SPEED]; /* baud rates to be tried */
};
enum {
CLOCAL_MODE_AUTO = 0,
CLOCAL_MODE_ALWAYS,
CLOCAL_MODE_NEVER
};
#define F_PARSE (1<<0) /* process modem status messages */
#define F_ISSUE (1<<1) /* display /etc/issue or /etc/issue.d */
#define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
#define F_INITSTRING (1<<4) /* initstring is set */
#define F_WAITCRLF (1<<5) /* wait for CR or LF */
#define F_NOPROMPT (1<<7) /* do not ask for login name! */
#define F_LCUC (1<<8) /* support for *LCUC stty modes */
#define F_KEEPSPEED (1<<9) /* follow baud rate from kernel */
#define F_KEEPCFLAGS (1<<10) /* reuse c_cflags setup from kernel */
#define F_EIGHTBITS (1<<11) /* Assume 8bit-clean tty */
#define F_VCONSOLE (1<<12) /* This is a virtual console */
#define F_HANGUP (1<<13) /* Do call vhangup(2) */
#define F_UTF8 (1<<14) /* We can do UTF8 */
#define F_LOGINPAUSE (1<<15) /* Wait for any key before dropping login prompt */
#define F_NOCLEAR (1<<16) /* Do not clear the screen before prompting */
#define F_NONL (1<<17) /* No newline before issue */
#define F_NOHOSTNAME (1<<18) /* Do not show the hostname */
#define F_LONGHNAME (1<<19) /* Show Full qualified hostname */
#define F_NOHINTS (1<<20) /* Don't print hints */
#define F_REMOTE (1<<21) /* Add '-h fakehost' to login(1) command line */
#define serial_tty_option(opt, flag) \
(((opt)->flags & (F_VCONSOLE|(flag))) == (flag))
struct Speedtab {
long speed;
speed_t code;
};
static const struct Speedtab speedtab[] = {
{50, B50},
{75, B75},
{110, B110},
{134, B134},
{150, B150},
{200, B200},
{300, B300},
{600, B600},
{1200, B1200},
{1800, B1800},
{2400, B2400},
{4800, B4800},
{9600, B9600},
#ifdef B19200
{19200, B19200},
#elif defined(EXTA)
{19200, EXTA},
#endif
#ifdef B38400
{38400, B38400},
#elif defined(EXTB)
{38400, EXTB},
#endif
#ifdef B57600
{57600, B57600},
#endif
#ifdef B115200
{115200, B115200},
#endif
#ifdef B230400
{230400, B230400},
#endif
#ifdef B460800
{460800, B460800},
#endif
#ifdef B500000
{500000, B500000},
#endif
#ifdef B576000
{576000, B576000},
#endif
#ifdef B921600
{921600, B921600},
#endif
#ifdef B1000000
{1000000, B1000000},
#endif
#ifdef B1152000
{1152000, B1152000},
#endif
#ifdef B1500000
{1500000, B1500000},
#endif
#ifdef B2000000
{2000000, B2000000},
#endif
#ifdef B2500000
{2500000, B2500000},
#endif
#ifdef B3000000
{3000000, B3000000},
#endif
#ifdef B3500000
{3500000, B3500000},
#endif
#ifdef B4000000
{4000000, B4000000},
#endif
{0, 0},
};
static void init_special_char(char* arg, struct options *op);
static void parse_args(int argc, char **argv, struct options *op);
static void parse_speeds(struct options *op, char *arg);
static void update_utmp(struct options *op);
static void open_tty(char *tty, struct termios *tp, struct options *op);
static void termio_init(struct options *op, struct termios *tp);
static void reset_vc(const struct options *op, struct termios *tp, int canon);
static void auto_baud(struct termios *tp);
static void list_speeds(void);
static void output_special_char (struct issue *ie, unsigned char c, struct options *op,
struct termios *tp, FILE *fp);
static void do_prompt(struct issue *ie, struct options *op, struct termios *tp);
static void next_speed(struct options *op, struct termios *tp);
static char *get_logname(struct issue *ie, struct options *op,
struct termios *tp, struct chardata *cp);
static void termio_final(struct options *op,
struct termios *tp, struct chardata *cp);
static int caps_lock(char *s);
static speed_t bcode(char *s);
static void usage(void) __attribute__((__noreturn__));
static void exit_slowly(int code) __attribute__((__noreturn__));
static void log_err(const char *, ...) __attribute__((__noreturn__))
__attribute__((__format__(printf, 1, 2)));
static void log_warn (const char *, ...)
__attribute__((__format__(printf, 1, 2)));
static ssize_t append(char *dest, size_t len, const char *sep, const char *src);
static void check_username (const char* nm);
static void login_options_to_argv(char *argv[], int *argc, char *str, char *username);
static void reload_agettys(void);
static void print_issue_file(struct issue *ie, struct options *op, struct termios *tp);
static void eval_issue_file(struct issue *ie, struct options *op, struct termios *tp);
static void show_issue(struct options *op);
/* Fake hostname for ut_host specified on command line. */
static char *fakehost;
#ifdef DEBUGGING
# include "closestream.h"
# ifndef DEBUG_OUTPUT
# define DEBUG_OUTPUT "/dev/tty10"
# endif
# define debug(s) do { fprintf(dbf,s); fflush(dbf); } while (0)
FILE *dbf;
#else
# define debug(s) do { ; } while (0)
#endif
int main(int argc, char **argv)
{
char *username = NULL; /* login name, given to /bin/login */
struct chardata chardata; /* will be set by get_logname() */
struct termios termios; /* terminal mode bits */
struct options options = {
.flags = F_ISSUE, /* show /etc/issue (SYSV_STYLE) */
.login = _PATH_LOGIN, /* default login program */
.tty = "tty1" /* default tty line */
};
struct issue issue = {
.mem = NULL,
};
char *login_argv[LOGIN_ARGV_MAX + 1];
int login_argc = 0;
struct sigaction sa, sa_hup, sa_quit, sa_int;
sigset_t set;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* In case vhangup(2) has to called */
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_RESTART;
sigemptyset (&sa.sa_mask);
sigaction(SIGHUP, &sa, &sa_hup);
sigaction(SIGQUIT, &sa, &sa_quit);
sigaction(SIGINT, &sa, &sa_int);
#ifdef DEBUGGING
dbf = fopen(DEBUG_OUTPUT, "w");
for (int i = 1; i < argc; i++) {
if (i > 1)
debug(" ");
debug(argv[i]);
}
debug("\n");
#endif /* DEBUGGING */
/* Parse command-line arguments. */
parse_args(argc, argv, &options);
login_argv[login_argc++] = options.login; /* set login program name */
/* Update the utmp file. */
#ifdef SYSV_STYLE
update_utmp(&options);
#endif
if (options.delay)
sleep(options.delay);
debug("calling open_tty\n");
/* Open the tty as standard { input, output, error }. */
open_tty(options.tty, &termios, &options);
/* Unmask SIGHUP if inherited */
sigemptyset(&set);
sigaddset(&set, SIGHUP);
sigprocmask(SIG_UNBLOCK, &set, NULL);
sigaction(SIGHUP, &sa_hup, NULL);
tcsetpgrp(STDIN_FILENO, getpid());
/* Default is to follow the current line speed and then default to 9600 */
if ((options.flags & F_VCONSOLE) == 0 && options.numspeed == 0) {
options.speeds[options.numspeed++] = bcode("9600");
options.flags |= F_KEEPSPEED;
}
/* Initialize the termios settings (raw mode, eight-bit, blocking i/o). */
debug("calling termio_init\n");
termio_init(&options, &termios);
/* Write the modem init string and DO NOT flush the buffers. */
if (serial_tty_option(&options, F_INITSTRING) &&
options.initstring && *options.initstring != '\0') {
debug("writing init string\n");
write_all(STDOUT_FILENO, options.initstring,
strlen(options.initstring));
}
if (options.flags & F_VCONSOLE || options.clocal != CLOCAL_MODE_ALWAYS)
/* Go to blocking mode unless -L is specified, this change
* affects stdout, stdin and stderr as all the file descriptors
* are created by dup(). */
fcntl(STDOUT_FILENO, F_SETFL,
fcntl(STDOUT_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
/* Optionally detect the baud rate from the modem status message. */
debug("before autobaud\n");
if (serial_tty_option(&options, F_PARSE))
auto_baud(&termios);
/* Set the optional timer. */
if (options.timeout)
alarm(options.timeout);
/* Optionally wait for CR or LF before writing /etc/issue */
if (serial_tty_option(&options, F_WAITCRLF)) {
char ch;
debug("waiting for cr-lf\n");
while (read(STDIN_FILENO, &ch, 1) == 1) {
/* Strip "parity bit". */
ch &= 0x7f;
#ifdef DEBUGGING
fprintf(dbf, "read %c\n", ch);
#endif
if (ch == '\n' || ch == '\r')
break;
}
}
INIT_CHARDATA(&chardata);
if (options.autolog) {
debug("doing auto login\n");
username = options.autolog;
}
if (options.flags & F_NOPROMPT) { /* --skip-login */
eval_issue_file(&issue, &options, &termios);
print_issue_file(&issue, &options, &termios);
} else { /* regular (auto)login */
if (options.autolog) {
/* Autologin prompt */
eval_issue_file(&issue, &options, &termios);
do_prompt(&issue, &options, &termios);
printf(_("%s%s (automatic login)\n"), LOGIN, options.autolog);
} else {
/* Read the login name. */
debug("reading login name\n");
while ((username =
get_logname(&issue, &options, &termios, &chardata)) == NULL)
if ((options.flags & F_VCONSOLE) == 0 && options.numspeed)
next_speed(&options, &termios);
}
}
/* Disable timer. */
if (options.timeout)
alarm(0);
/* Finalize the termios settings. */
if ((options.flags & F_VCONSOLE) == 0)
termio_final(&options, &termios, &chardata);
else
reset_vc(&options, &termios, 1);
/* Now the newline character should be properly written. */
write_all(STDOUT_FILENO, "\r\n", 2);
sigaction(SIGQUIT, &sa_quit, NULL);
sigaction(SIGINT, &sa_int, NULL);
if (username)
check_username(username);
if (options.logopt) {
/*
* The --login-options completely overwrites the default
* way how agetty composes login(1) command line.
*/
login_options_to_argv(login_argv, &login_argc,
options.logopt, username);
} else {
if (options.flags & F_REMOTE) {
if (fakehost) {
login_argv[login_argc++] = "-h";
login_argv[login_argc++] = fakehost;
} else if (options.flags & F_NOHOSTNAME)
login_argv[login_argc++] = "-H";
}
if (username) {
if (options.autolog)
login_argv[login_argc++] = "-f";
else
login_argv[login_argc++] = "--";
login_argv[login_argc++] = username;
}
}
login_argv[login_argc] = NULL; /* last login argv */
if (options.chroot && chroot(options.chroot) < 0)
log_err(_("%s: can't change root directory %s: %m"),
options.tty, options.chroot);
if (options.chdir && chdir(options.chdir) < 0)
log_err(_("%s: can't change working directory %s: %m"),
options.tty, options.chdir);
if (options.nice && nice(options.nice) < 0)
log_warn(_("%s: can't change process priority: %m"),
options.tty);
free(options.osrelease);
#ifdef DEBUGGING
if (close_stream(dbf) != 0)
log_err("write failed: %s", DEBUG_OUTPUT);
#endif
/* Let the login program take care of password validation. */
execv(options.login, login_argv);
log_err(_("%s: can't exec %s: %m"), options.tty, login_argv[0]);
}
/*
* Returns : @str if \u not found
* : @username if @str equal to "\u"
* : newly allocated string if \u mixed with something other
*/
static char *replace_u(char *str, char *username)
{
char *entry = NULL, *p = str;
size_t usz = username ? strlen(username) : 0;
while (*p) {
size_t sz;
char *tp, *old = entry;
if (memcmp(p, "\\u", 2) != 0) {
p++;
continue; /* no \u */
}
sz = strlen(str);
if (p == str && sz == 2) {
/* 'str' contains only '\u' */
free(old);
return username;
}
tp = entry = malloc(sz + usz);
if (!tp)
log_err(_("failed to allocate memory: %m"));
if (p != str) {
/* copy chars before \u */
memcpy(tp, str, p - str);
tp += p - str;
}
if (usz) {
/* copy username */
memcpy(tp, username, usz);
tp += usz;
}
if (*(p + 2))
/* copy chars after \u + \0 */
memcpy(tp, p + 2, sz - (p - str) - 1);
else
*tp = '\0';
p = tp;
str = entry;
free(old);
}
return entry ? entry : str;
}
static void login_options_to_argv(char *argv[], int *argc,
char *str, char *username)
{
char *p;
int i = *argc;
while (str && isspace(*str))
str++;
p = str;
while (p && *p && i < LOGIN_ARGV_MAX) {
if (isspace(*p)) {
*p = '\0';
while (isspace(*++p))
;
if (*p) {
argv[i++] = replace_u(str, username);
str = p;
}
} else
p++;
}
if (str && *str && i < LOGIN_ARGV_MAX)
argv[i++] = replace_u(str, username);
*argc = i;
}
static void output_version(void)
{
static const char *features[] = {
#ifdef DEBUGGING
"debug",
#endif
#ifdef CRTSCTS
"flow control",
#endif
#ifdef KDGKBLED
"hints",
#endif
#ifdef ISSUE_SUPPORT
"issue",
#endif
#ifdef ISSUEDIR_SUPPORT
"issue.d",
#endif
#ifdef KDGKBMODE
"keyboard mode",
#endif
#ifdef USE_PLYMOUTH_SUPPORT
"plymouth",
#endif
#ifdef AGETTY_RELOAD
"reload",
#endif
#ifdef USE_SYSLOG
"syslog",
#endif
#ifdef HAVE_WIDECHAR
"widechar",
#endif
NULL
};
unsigned int i;
printf( _("%s from %s"), program_invocation_short_name, PACKAGE_STRING);
fputs(" (", stdout);
for (i = 0; features[i]; i++) {
if (0 < i)
fputs(", ", stdout);
printf("%s", features[i]);
}
fputs(")\n", stdout);
}
#define is_speed(str) (strlen((str)) == strspn((str), "0123456789,"))
/* Parse command-line arguments. */
static void parse_args(int argc, char **argv, struct options *op)
{
int c;
int opt_show_issue = 0;
enum {
VERSION_OPTION = CHAR_MAX + 1,
NOHINTS_OPTION,
NOHOSTNAME_OPTION,
LONGHOSTNAME_OPTION,
HELP_OPTION,
ERASE_CHARS_OPTION,
KILL_CHARS_OPTION,
RELOAD_OPTION,
LIST_SPEEDS_OPTION,
ISSUE_SHOW_OPTION,
};
const struct option longopts[] = {
{ "8bits", no_argument, NULL, '8' },
{ "autologin", required_argument, NULL, 'a' },
{ "noreset", no_argument, NULL, 'c' },
{ "chdir", required_argument, NULL, 'C' },
{ "delay", required_argument, NULL, 'd' },
{ "remote", no_argument, NULL, 'E' },
{ "issue-file", required_argument, NULL, 'f' },
{ "show-issue", no_argument, NULL, ISSUE_SHOW_OPTION },
{ "flow-control", no_argument, NULL, 'h' },
{ "host", required_argument, NULL, 'H' },
{ "noissue", no_argument, NULL, 'i' },
{ "init-string", required_argument, NULL, 'I' },
{ "noclear", no_argument, NULL, 'J' },
{ "login-program", required_argument, NULL, 'l' },
{ "local-line", optional_argument, NULL, 'L' },
{ "extract-baud", no_argument, NULL, 'm' },
{ "list-speeds", no_argument, NULL, LIST_SPEEDS_OPTION },
{ "skip-login", no_argument, NULL, 'n' },
{ "nonewline", no_argument, NULL, 'N' },
{ "login-options", required_argument, NULL, 'o' },
{ "login-pause", no_argument, NULL, 'p' },
{ "nice", required_argument, NULL, 'P' },
{ "chroot", required_argument, NULL, 'r' },
{ "hangup", no_argument, NULL, 'R' },
{ "keep-baud", no_argument, NULL, 's' },
{ "timeout", required_argument, NULL, 't' },
{ "detect-case", no_argument, NULL, 'U' },
{ "wait-cr", no_argument, NULL, 'w' },
{ "nohints", no_argument, NULL, NOHINTS_OPTION },
{ "nohostname", no_argument, NULL, NOHOSTNAME_OPTION },
{ "long-hostname", no_argument, NULL, LONGHOSTNAME_OPTION },
{ "reload", no_argument, NULL, RELOAD_OPTION },
{ "version", no_argument, NULL, VERSION_OPTION },
{ "help", no_argument, NULL, HELP_OPTION },
{ "erase-chars", required_argument, NULL, ERASE_CHARS_OPTION },
{ "kill-chars", required_argument, NULL, KILL_CHARS_OPTION },
{ NULL, 0, NULL, 0 }
};
while ((c = getopt_long(argc, argv,
"8a:cC:d:Ef:hH:iI:Jl:L::mnNo:pP:r:Rst:Uw", longopts,
NULL)) != -1) {
switch (c) {
case '8':
op->flags |= F_EIGHTBITS;
break;
case 'a':
op->autolog = optarg;
break;
case 'c':
op->flags |= F_KEEPCFLAGS;
break;
case 'C':
op->chdir = optarg;
break;
case 'd':
op->delay = strtou32_or_err(optarg, _("invalid delay argument"));
break;
case 'E':
op->flags |= F_REMOTE;
break;
case 'f':
op->issue = optarg;
break;
case 'h':
op->flags |= F_RTSCTS;
break;
case 'H':
fakehost = optarg;
break;
case 'i':
op->flags &= ~F_ISSUE;
break;
case 'I':
init_special_char(optarg, op);
op->flags |= F_INITSTRING;
break;
case 'J':
op->flags |= F_NOCLEAR;
break;
case 'l':
op->login = optarg;
break;
case 'L':
/* -L and -L=always have the same meaning */
op->clocal = CLOCAL_MODE_ALWAYS;
if (optarg) {
if (strcmp(optarg, "=always") == 0)
op->clocal = CLOCAL_MODE_ALWAYS;
else if (strcmp(optarg, "=never") == 0)
op->clocal = CLOCAL_MODE_NEVER;
else if (strcmp(optarg, "=auto") == 0)
op->clocal = CLOCAL_MODE_AUTO;
else
log_err(_("invalid argument of --local-line"));
}
break;
case 'm':
op->flags |= F_PARSE;
break;
case 'n':
op->flags |= F_NOPROMPT;
break;
case 'N':
op->flags |= F_NONL;
break;
case 'o':
op->logopt = optarg;
break;
case 'p':
op->flags |= F_LOGINPAUSE;
break;
case 'P':
op->nice = strtos32_or_err(optarg, _("invalid nice argument"));
break;
case 'r':
op->chroot = optarg;
break;
case 'R':
op->flags |= F_HANGUP;
break;
case 's':
op->flags |= F_KEEPSPEED;
break;
case 't':
op->timeout = strtou32_or_err(optarg, _("invalid timeout argument"));
break;
case 'U':
op->flags |= F_LCUC;
break;
case 'w':
op->flags |= F_WAITCRLF;
break;
case NOHINTS_OPTION:
op->flags |= F_NOHINTS;
break;
case NOHOSTNAME_OPTION:
op->flags |= F_NOHOSTNAME;
break;
case LONGHOSTNAME_OPTION:
op->flags |= F_LONGHNAME;
break;
case ERASE_CHARS_OPTION:
op->erasechars = optarg;
break;
case KILL_CHARS_OPTION:
op->killchars = optarg;
break;
case RELOAD_OPTION:
reload_agettys();
exit(EXIT_SUCCESS);
case LIST_SPEEDS_OPTION:
list_speeds();
exit(EXIT_SUCCESS);
case ISSUE_SHOW_OPTION:
opt_show_issue = 1;
break;
case VERSION_OPTION:
output_version();
exit(EXIT_SUCCESS);
case HELP_OPTION:
usage();
default:
errtryhelp(EXIT_FAILURE);
}
}
if (opt_show_issue) {
show_issue(op);
exit(EXIT_SUCCESS);
}
debug("after getopt loop\n");
if (argc < optind + 1) {
log_warn(_("not enough arguments"));
errx(EXIT_FAILURE, _("not enough arguments"));
}
/* Accept "tty", "baudrate tty", and "tty baudrate". */
if (is_speed(argv[optind])) {
/* Assume BSD style speed. */
parse_speeds(op, argv[optind++]);
if (argc < optind + 1) {
log_warn(_("not enough arguments"));
errx(EXIT_FAILURE, _("not enough arguments"));
}
op->tty = argv[optind++];
} else {
op->tty = argv[optind++];
if (argc > optind) {
char *v = argv[optind];
if (is_speed(v)) {
parse_speeds(op, v);
optind++;
}
}
}
/* On virtual console remember the line which is used for */
if (strncmp(op->tty, "tty", 3) == 0 &&
strspn(op->tty + 3, "0123456789") == strlen(op->tty+3))
op->vcline = op->tty+3;
if (argc > optind && argv[optind])
op->term = argv[optind];
debug("exiting parseargs\n");
}
/* Parse alternate baud rates. */
static void parse_speeds(struct options *op, char *arg)
{
char *cp;
char *str = strdup(arg);
if (!str)
log_err(_("failed to allocate memory: %m"));
debug("entered parse_speeds:\n");
for (cp = strtok(str, ","); cp != NULL; cp = strtok((char *)0, ",")) {
if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0)
log_err(_("bad speed: %s"), cp);
if (op->numspeed >= MAX_SPEED)
log_err(_("too many alternate speeds"));
}
debug("exiting parsespeeds\n");
free(str);
}
#ifdef SYSV_STYLE
/* Update our utmp entry. */
static void update_utmp(struct options *op)
{
struct utmpx ut;
time_t t;
pid_t pid = getpid();
pid_t sid = getsid(0);
char *vcline = op->vcline;
char *line = op->tty;
struct utmpx *utp;
/*
* The utmp file holds miscellaneous information about things started by
* /sbin/init and other system-related events. Our purpose is to update
* the utmp entry for the current process, in particular the process type
* and the tty line we are listening to. Return successfully only if the
* utmp file can be opened for update, and if we are able to find our
* entry in the utmp file.
*/
utmpxname(_PATH_UTMP);
setutxent();
/*
* Find my pid in utmp.
*
* FIXME: Earlier (when was that?) code here tested only utp->ut_type !=
* INIT_PROCESS, so maybe the >= here should be >.
*
* FIXME: The present code is taken from login.c, so if this is changed,
* maybe login has to be changed as well (is this true?).
*/
while ((utp = getutxent()))
if (utp->ut_pid == pid
&& utp->ut_type >= INIT_PROCESS
&& utp->ut_type <= DEAD_PROCESS)
break;
if (utp) {
memcpy(&ut, utp, sizeof(ut));
} else {
/* Some inits do not initialize utmp. */
memset(&ut, 0, sizeof(ut));
if (vcline && *vcline)
/* Standard virtual console devices */
str2memcpy(ut.ut_id, vcline, sizeof(ut.ut_id));
else {
size_t len = strlen(line);
char * ptr;
if (len >= sizeof(ut.ut_id))
ptr = line + len - sizeof(ut.ut_id);
else