getty must chdir(/). Use bb_getopt_ulflags. Indent. error() perfect

This commit is contained in:
"Vladimir N. Oleynik" 2005-09-29 11:31:26 +00:00
parent 6160d45e05
commit 3e245c9e21

View File

@ -38,7 +38,6 @@
/* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
#ifdef CONFIG_SYSLOGD
#include <sys/param.h>
#define USE_SYSLOG
#include <syslog.h>
#endif
@ -144,14 +143,18 @@ struct options {
int speeds[MAX_SPEED]; /* baud rates to be tried */
};
#define F_PARSE (1<<0) /* process modem status messages */
#define F_ISSUE (1<<1) /* display /etc/issue */
#define F_RTSCTS (1<<2) /* enable RTS/CTS flow control */
#define F_LOCAL (1<<3) /* force local */
#define F_INITSTRING (1<<4) /* initstring is set */
#define F_WAITCRLF (1<<5) /* wait for CR or LF */
#define F_CUSTISSUE (1<<6) /* give alternative issue file */
#define F_NOPROMPT (1<<7) /* don't ask for login name! */
static const char opt_string[] = "I:LH:f:hil:mt:wn";
#define F_INITSTRING (1<<0) /* initstring is set */
#define F_LOCAL (1<<1) /* force local */
#define F_FAKEHOST (1<<2) /* force fakehost */
#define F_CUSTISSUE (1<<3) /* give alternative issue file */
#define F_RTSCTS (1<<4) /* enable RTS/CTS flow control */
#define F_ISSUE (1<<5) /* display /etc/issue */
#define F_LOGIN (1<<6) /* non-default login program */
#define F_PARSE (1<<7) /* process modem status messages */
#define F_TIMEOUT (1<<8) /* time out */
#define F_WAITCRLF (1<<9) /* wait for CR or LF */
#define F_NOPROMPT (1<<10) /* don't ask for login name! */
/* Storage for things detected while the login name was read. */
@ -226,13 +229,11 @@ static void auto_baud(struct termio *tp);
static void do_prompt(struct options *op, struct termio *tp);
static void next_speed(struct termio *tp, struct options *op);
static char *get_logname(struct options *op, struct chardata *cp,
struct termio *tp);
static void termio_final(struct options *op, struct termio *tp,
struct chardata *cp);
static int caps_lock(const char *s);
static int bcode(char *s);
static int bcode(const char *s);
static void error(const char *fmt, ...) __attribute__ ((noreturn));
#ifdef SYSV_STYLE
@ -261,7 +262,7 @@ int getty_main(int argc, char **argv)
struct chardata chardata; /* set by get_logname() */
struct termio termio; /* terminal mode bits */
static struct options options = {
F_ISSUE, /* show /etc/issue (SYSV_STYLE) */
0, /* show /etc/issue (SYSV_STYLE) */
0, /* no timeout */
_PATH_LOGIN, /* default login program */
"tty1", /* default tty line */
@ -387,24 +388,18 @@ int getty_main(int argc, char **argv)
static void parse_args(int argc, char **argv, struct options *op)
{
extern char *optarg; /* getopt */
extern int optind; /* getopt */
int c;
char *ts;
while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) {
switch (c) {
case 'I':
if (!(op->initstring = strdup(optarg)))
error(bb_msg_memory_exhausted);
{
const char *p;
op->flags = bb_getopt_ulflags(argc, argv, opt_string,
&(op->initstring), &fakehost, &(op->issue),
&(op->login), &ts);
if(op->flags & F_INITSTRING) {
const char *p = op->initstring;
char *q;
q = op->initstring = bb_xstrdup(op->initstring);
/* copy optarg into op->initstring decoding \ddd
octal codes into chars */
q = op->initstring;
p = optarg;
while (*p) {
if (*p == '\\') {
p++;
@ -415,44 +410,10 @@ static void parse_args(int argc, char **argv, struct options *op)
}
*q = '\0';
}
op->flags |= F_INITSTRING;
break;
case 'L': /* force local */
op->flags |= F_LOCAL;
break;
case 'H': /* fake login host */
fakehost = optarg;
break;
case 'f': /* custom issue file */
op->flags |= F_CUSTISSUE;
op->issue = optarg;
break;
case 'h': /* enable h/w flow control */
op->flags |= F_RTSCTS;
break;
case 'i': /* do not show /etc/issue */
op->flags &= ~F_ISSUE;
break;
case 'l':
op->login = optarg; /* non-default login program */
break;
case 'm': /* parse modem status message */
op->flags |= F_PARSE;
break;
case 'n':
op->flags |= F_NOPROMPT;
break;
case 't': /* time out */
if ((op->timeout = atoi(optarg)) <= 0)
error("bad timeout value: %s", optarg);
break;
case 'w':
op->flags |= F_WAITCRLF;
break;
default:
bb_show_usage();
}
op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
if(op->flags & F_TIMEOUT) {
if ((op->timeout = atoi(ts)) <= 0)
error("bad timeout value: %s", ts);
}
debug("after getopt loop\n");
if (argc < optind + 2) /* check parameter count */
@ -556,6 +517,8 @@ static void update_utmp(char *line)
/* open_tty - set up tty as standard { input, output, error } */
static void open_tty(char *tty, struct termio *tp, int local)
{
int chdir_to_root = 0;
/* Set up new standard input, unless we are given an already opened port. */
if (strcmp(tty, "-")) {
@ -566,6 +529,7 @@ static void open_tty(char *tty, struct termio *tp, int local)
if (chdir("/dev"))
error("/dev: chdir() failed: %m");
chdir_to_root = 1;
if (stat(tty, &st) < 0)
error("/dev/%s: %m", tty);
if ((st.st_mode & S_IFMT) != S_IFCHR)
@ -651,15 +615,15 @@ static void open_tty(char *tty, struct termio *tp, int local)
#else
(void) chown(tty, 0, 0); /* root, sys */
(void) chmod(tty, 0622); /* crw--w--w- */
errno = 0; /* ignore above errors */
#endif
if(chdir_to_root && chdir("/"))
error("chdir to / failed: %m");
}
/* termio_init - initialize termio settings */
static void termio_init(struct termio *tp, int speed, struct options *op)
{
/*
* Initial termio settings: 8-bit characters, raw-mode, blocking i/o.
* Special characters are set after we have read the login name; all
@ -963,11 +927,11 @@ static int caps_lock(const char *s)
}
/* bcode - convert speed string to speed code; return 0 on failure */
static int bcode(char *s)
static int bcode(const char *s)
{
int r;
unsigned long value;
if (safe_strtoul(s, &value)) {
if (safe_strtoul((char *)s, &value)) {
return -1;
}
if ((r = bb_value_to_baud(value)) > 0) {
@ -976,49 +940,44 @@ static int bcode(char *s)
return 0;
}
/* error - report errors to console or syslog; only understands %s and %m */
#define str2cpy(b,s1,s2) strcat(strcpy(b,s1),s2)
/*
* output error messages
*/
static void error(const char *fmt, ...)
{
va_list va_alist;
char buf[256], *bp;
#ifndef USE_SYSLOG
int fd;
#endif
#ifdef USE_SYSLOG
buf[0] = '\0';
bp = buf;
#else
strncpy(buf, bb_applet_name, 256);
strncat(buf, ": ", 256);
buf[255] = 0;
bp = buf + strlen(buf);
#endif
char buf[256];
#ifdef CONFIG_SYSLOGD
va_start(va_alist, fmt);
vsnprintf(bp, 256 - strlen(buf), fmt, va_alist);
buf[255] = 0;
va_end(va_alist);
#ifdef USE_SYSLOG
vsnprintf(buf, sizeof(buf), fmt, va_alist);
openlog(bb_applet_name, 0, LOG_AUTH);
syslog(LOG_ERR, "%s", buf);
closelog();
#else
strncat(bp, "\r\n", 256 - strlen(buf));
buf[255] = 0;
int fd;
size_t l;
snprintf(buf, sizeof(buf), "%s: ", bb_applet_name);
l = strlen(buf);
va_start(va_alist, fmt);
vsnprintf(buf + l, sizeof(buf) - l, fmt, va_alist);
l = strlen(buf);
/* truncate if need */
if((l + 3) > sizeof(buf))
l = sizeof(buf) - 3;
/* add \r\n always */
buf[l++] = '\r';
buf[l++] = '\n';
buf[l] = 0;
if ((fd = open("/dev/console", 1)) >= 0) {
write(fd, buf, strlen(buf));
write(fd, buf, l);
close(fd);
}
#endif
va_end(va_alist);
(void) sleep((unsigned) 10); /* be kind to init(8) */
exit(1);
}