aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-12-04 18:46:01 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-12-04 18:46:01 +0000
commitf3fca91e9f427f2b026df07607c8b903aed93395 (patch)
tree723f90d80ae122fb1392df1ce122f1d1607bbd05
parentc689fcfdb00a5a3a05f433806eeb10cf4c1e6f7d (diff)
downloadbusybox-w32-f3fca91e9f427f2b026df07607c8b903aed93395.tar.gz
busybox-w32-f3fca91e9f427f2b026df07607c8b903aed93395.tar.bz2
busybox-w32-f3fca91e9f427f2b026df07607c8b903aed93395.zip
getty: a small code shrink
touch - 40 +40 fakehost 4 - -4 getty_main 2546 2493 -53
-rw-r--r--loginutils/getty.c67
1 files changed, 33 insertions, 34 deletions
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 590a05de8..31230c9a5 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -28,12 +28,16 @@
28 * System V, assume it is SunOS 4. 28 * System V, assume it is SunOS 4.
29 */ 29 */
30#ifdef LOGIN_PROCESS /* defined in System V utmp.h */ 30#ifdef LOGIN_PROCESS /* defined in System V utmp.h */
31#define SYSV_STYLE /* select System V style getty */
32#include <sys/utsname.h> 31#include <sys/utsname.h>
33#include <time.h> 32#include <time.h>
34#if ENABLE_FEATURE_WTMP 33#if ENABLE_FEATURE_WTMP
35extern void updwtmp(const char *filename, const struct utmp *ut); 34extern void updwtmp(const char *filename, const struct utmp *ut);
36#endif 35#endif
36#else /* if !sysV style, wtmp/utmp code is off */
37#undef ENABLE_FEATURE_UTMP
38#undef ENABLE_FEATURE_WTMP
39#define ENABLE_FEATURE_UTMP 0
40#define ENABLE_FEATURE_WTMP 0
37#endif /* LOGIN_PROCESS */ 41#endif /* LOGIN_PROCESS */
38 42
39/* 43/*
@@ -116,7 +120,7 @@ static const struct chardata init_chardata = {
116static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn"; 120static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
117#define F_INITSTRING (1 << 0) /* -I initstring is set */ 121#define F_INITSTRING (1 << 0) /* -I initstring is set */
118#define F_LOCAL (1 << 1) /* -L force local */ 122#define F_LOCAL (1 << 1) /* -L force local */
119#define F_FAKEHOST (1 << 2) /* -H force fakehost */ 123#define F_FAKEHOST (1 << 2) /* -H fake hostname */
120#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */ 124#define F_CUSTISSUE (1 << 3) /* -f give alternative issue file */
121#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */ 125#define F_RTSCTS (1 << 4) /* -h enable RTS/CTS flow control */
122#define F_ISSUE (1 << 5) /* -i display /etc/issue */ 126#define F_ISSUE (1 << 5) /* -i display /etc/issue */
@@ -124,11 +128,9 @@ static const char opt_string[] ALIGN1 = "I:LH:f:hil:mt:wn";
124#define F_PARSE (1 << 7) /* -m process modem status messages */ 128#define F_PARSE (1 << 7) /* -m process modem status messages */
125#define F_TIMEOUT (1 << 8) /* -t time out */ 129#define F_TIMEOUT (1 << 8) /* -t time out */
126#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */ 130#define F_WAITCRLF (1 << 9) /* -w wait for CR or LF */
127#define F_NOPROMPT (1 << 10) /* -n don't ask for login name! */ 131#define F_NOPROMPT (1 << 10) /* -n don't ask for login name */
128 132
129 133
130/* Fake hostname for ut_host specified on command line. */
131static char *fakehost = NULL;
132#define line_buf bb_common_bufsiz1 134#define line_buf bb_common_bufsiz1
133 135
134/* The following is used for understandable diagnostics. */ 136/* The following is used for understandable diagnostics. */
@@ -175,20 +177,20 @@ static void parse_speeds(struct options *op, char *arg)
175} 177}
176 178
177/* parse_args - parse command-line arguments */ 179/* parse_args - parse command-line arguments */
178static void parse_args(char **argv, struct options *op) 180static void parse_args(char **argv, struct options *op, char **fakehost_p)
179{ 181{
180 char *ts; 182 char *ts;
181 183
182 opt_complementary = "-2"; /* at least 2 args */ 184 opt_complementary = "-2"; /* at least 2 args */
183 op->flags = getopt32(argv, opt_string, 185 op->flags = getopt32(argv, opt_string,
184 &(op->initstring), &fakehost, &(op->issue), 186 &(op->initstring), fakehost_p, &(op->issue),
185 &(op->login), &ts); 187 &(op->login), &ts);
186 argv += optind; 188 argv += optind;
187 if (op->flags & F_INITSTRING) { 189 if (op->flags & F_INITSTRING) {
188 const char *p = op->initstring; 190 const char *p = op->initstring;
189 char *q; 191 char *q;
190 192
191 op->initstring = q = xstrdup(op->initstring); 193 op->initstring = q = xstrdup(p);
192 /* copy optarg into op->initstring decoding \ddd 194 /* copy optarg into op->initstring decoding \ddd
193 octal codes into chars */ 195 octal codes into chars */
194 while (*p) { 196 while (*p) {
@@ -564,16 +566,24 @@ static void termios_final(struct options *op, struct termios *tp, struct chardat
564 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty); 566 ioctl_or_perror_and_die(0, TCSETS, tp, "%s: TCSETS", op->tty);
565} 567}
566 568
567#ifdef SYSV_STYLE
568#if ENABLE_FEATURE_UTMP 569#if ENABLE_FEATURE_UTMP
570static void touch(const char *filename)
571{
572 if (access(filename, R_OK | W_OK) == -1)
573 close(open(filename, O_WRONLY | O_CREAT, 0664));
574}
575
569/* update_utmp - update our utmp entry */ 576/* update_utmp - update our utmp entry */
570static void update_utmp(const char *line) 577static void update_utmp(const char *line, char *fakehost)
571{ 578{
572 struct utmp ut; 579 struct utmp ut;
573 struct utmp *utp; 580 struct utmp *utp;
574 time_t t;
575 int mypid = getpid(); 581 int mypid = getpid();
576 582
583 /* In case we won't find an entry below... */
584 memset(&ut, 0, sizeof(ut));
585 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
586
577 /* 587 /*
578 * The utmp file holds miscellaneous information about things started by 588 * The utmp file holds miscellaneous information about things started by
579 * /sbin/init and other system-related events. Our purpose is to update 589 * /sbin/init and other system-related events. Our purpose is to update
@@ -582,29 +592,22 @@ static void update_utmp(const char *line)
582 * utmp file can be opened for update, and if we are able to find our 592 * utmp file can be opened for update, and if we are able to find our
583 * entry in the utmp file. 593 * entry in the utmp file.
584 */ 594 */
585 if (access(_PATH_UTMP, R_OK|W_OK) == -1) { 595 touch(_PATH_UTMP);
586 close(creat(_PATH_UTMP, 0664)); 596
587 }
588 utmpname(_PATH_UTMP); 597 utmpname(_PATH_UTMP);
589 setutent(); 598 setutent();
590 while ((utp = getutent()) 599 while ((utp = getutent()) != NULL) {
591 && !(utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) 600 if (utp->ut_type == INIT_PROCESS && utp->ut_pid == mypid) {
592 ) { 601 memcpy(&ut, utp, sizeof(ut));
593 continue; 602 break;
603 }
594 } 604 }
595 605
596 /* some inits don't initialize utmp... */
597 memset(&ut, 0, sizeof(ut));
598 safe_strncpy(ut.ut_id, line + 3, sizeof(ut.ut_id));
599 if (utp)
600 memcpy(&ut, utp, sizeof(ut));
601
602 strcpy(ut.ut_user, "LOGIN"); 606 strcpy(ut.ut_user, "LOGIN");
603 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line)); 607 safe_strncpy(ut.ut_line, line, sizeof(ut.ut_line));
604 if (fakehost) 608 if (fakehost)
605 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host)); 609 safe_strncpy(ut.ut_host, fakehost, sizeof(ut.ut_host));
606 time(&t); 610 ut.ut_time = time(NULL);
607 ut.ut_time = t;
608 ut.ut_type = LOGIN_PROCESS; 611 ut.ut_type = LOGIN_PROCESS;
609 ut.ut_pid = mypid; 612 ut.ut_pid = mypid;
610 613
@@ -612,19 +615,17 @@ static void update_utmp(const char *line)
612 endutent(); 615 endutent();
613 616
614#if ENABLE_FEATURE_WTMP 617#if ENABLE_FEATURE_WTMP
615 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) 618 touch(bb_path_wtmp_file);
616 close(creat(bb_path_wtmp_file, 0664));
617 updwtmp(bb_path_wtmp_file, &ut); 619 updwtmp(bb_path_wtmp_file, &ut);
618#endif 620#endif
619} 621}
620
621#endif /* CONFIG_FEATURE_UTMP */ 622#endif /* CONFIG_FEATURE_UTMP */
622#endif /* SYSV_STYLE */
623 623
624int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 624int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
625int getty_main(int argc, char **argv) 625int getty_main(int argc, char **argv)
626{ 626{
627 int n; 627 int n;
628 char *fakehost = NULL; /* Fake hostname for ut_host */
628 char *logname; /* login name, given to /bin/login */ 629 char *logname; /* login name, given to /bin/login */
629 /* Merging these into "struct local" may _seem_ to reduce 630 /* Merging these into "struct local" may _seem_ to reduce
630 * parameter passing, but today's gcc will inline 631 * parameter passing, but today's gcc will inline
@@ -673,7 +674,7 @@ int getty_main(int argc, char **argv)
673#endif 674#endif
674 675
675 /* Parse command-line arguments. */ 676 /* Parse command-line arguments. */
676 parse_args(argv, &options); 677 parse_args(argv, &options, &fakehost);
677 678
678 debug("calling open_tty\n"); 679 debug("calling open_tty\n");
679 /* Open the tty as standard input, if it is not "-" */ 680 /* Open the tty as standard input, if it is not "-" */
@@ -694,11 +695,9 @@ int getty_main(int argc, char **argv)
694 */ 695 */
695 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty); 696 ioctl_or_perror_and_die(0, TCGETS, &termios, "%s: TCGETS", options.tty);
696 697
697#ifdef SYSV_STYLE
698#if ENABLE_FEATURE_UTMP 698#if ENABLE_FEATURE_UTMP
699 /* Update the utmp file */ 699 /* Update the utmp file */
700 update_utmp(options.tty); 700 update_utmp(options.tty, fakehost);
701#endif
702#endif 701#endif
703 702
704#ifdef __linux__ 703#ifdef __linux__