From a9801658ee4b7f5717d145818428452f864e1015 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 7 Sep 2006 16:20:03 +0000 Subject: getty, sulogin: convert to using bb_msg for syslog output --- coreutils/nohup.c | 2 +- include/libbb.h | 2 + libbb/error_msg_and_die.c | 4 ++ libbb/fflush_stdout_and_exit.c | 2 + libbb/herror_msg_and_die.c | 2 + libbb/perror_msg_and_die.c | 2 + libbb/verror_msg.c | 5 +- libbb/vinfo_msg.c | 2 +- libbb/warn_ignoring_args.c | 2 +- libbb/xfuncs.c | 3 +- loginutils/getty.c | 144 +++++++++++++++++------------------------ loginutils/login.c | 2 +- loginutils/su.c | 6 +- loginutils/sulogin.c | 32 ++++----- 14 files changed, 96 insertions(+), 114 deletions(-) diff --git a/coreutils/nohup.c b/coreutils/nohup.c index 86d788683..5dd90adcc 100644 --- a/coreutils/nohup.c +++ b/coreutils/nohup.c @@ -47,7 +47,7 @@ int nohup_main(int argc, char *argv[]) if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout); dup2(temp ? 1 : nullfd, 2); close(nullfd); - signal (SIGHUP, SIG_IGN); + signal(SIGHUP, SIG_IGN); // Exec our new program. diff --git a/include/libbb.h b/include/libbb.h index c6a9ae577..6bea048a3 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -118,7 +118,9 @@ enum { LOGMODE_SYSLOG = 1<<1, LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO, }; +extern const char *msg_eol; extern int logmode; +extern int die_sleep; extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE; extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); diff --git a/libbb/error_msg_and_die.c b/libbb/error_msg_and_die.c index f25a1da32..29a260bde 100644 --- a/libbb/error_msg_and_die.c +++ b/libbb/error_msg_and_die.c @@ -13,6 +13,8 @@ #include #include "libbb.h" +int die_sleep; + void bb_error_msg_and_die(const char *s, ...) { va_list p; @@ -20,5 +22,7 @@ void bb_error_msg_and_die(const char *s, ...) va_start(p, s); bb_verror_msg(s, p, NULL); va_end(p); + if (die_sleep) + sleep(die_sleep); exit(bb_default_error_retval); } diff --git a/libbb/fflush_stdout_and_exit.c b/libbb/fflush_stdout_and_exit.c index 7e8152dd6..245f50864 100644 --- a/libbb/fflush_stdout_and_exit.c +++ b/libbb/fflush_stdout_and_exit.c @@ -20,5 +20,7 @@ void bb_fflush_stdout_and_exit(int retval) if (fflush(stdout)) { retval = bb_default_error_retval; } + if (die_sleep) + sleep(die_sleep); exit(retval); } diff --git a/libbb/herror_msg_and_die.c b/libbb/herror_msg_and_die.c index 285b195ef..f115c8e0a 100644 --- a/libbb/herror_msg_and_die.c +++ b/libbb/herror_msg_and_die.c @@ -19,5 +19,7 @@ void bb_herror_msg_and_die(const char *s, ...) va_start(p, s); bb_vherror_msg(s, p); va_end(p); + if (die_sleep) + sleep(die_sleep); exit(bb_default_error_retval); } diff --git a/libbb/perror_msg_and_die.c b/libbb/perror_msg_and_die.c index 5b0464077..c1cfb956f 100644 --- a/libbb/perror_msg_and_die.c +++ b/libbb/perror_msg_and_die.c @@ -20,5 +20,7 @@ void bb_perror_msg_and_die(const char *s, ...) va_start(p, s); bb_vperror_msg(s, p); va_end(p); + if (die_sleep) + sleep(die_sleep); exit(bb_default_error_retval); } diff --git a/libbb/verror_msg.c b/libbb/verror_msg.c index d55da73ff..988a7a293 100644 --- a/libbb/verror_msg.c +++ b/libbb/verror_msg.c @@ -15,6 +15,7 @@ #include "libbb.h" int logmode = LOGMODE_STDIO; +const char *msg_eol = "\n"; void bb_verror_msg(const char *s, va_list p, const char* strerr) { @@ -28,9 +29,9 @@ void bb_verror_msg(const char *s, va_list p, const char* strerr) fprintf(stderr, "%s: ", bb_applet_name); vfprintf(stderr, s, p); if (!strerr) - fputc('\n', stderr); + fputs(msg_eol, stderr); else - fprintf(stderr, ": %s\n", strerr); + fprintf(stderr, ": %s%s", strerr, msg_eol); } if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { if (!strerr) diff --git a/libbb/vinfo_msg.c b/libbb/vinfo_msg.c index 82fbda221..613b013cd 100644 --- a/libbb/vinfo_msg.c +++ b/libbb/vinfo_msg.c @@ -22,7 +22,7 @@ void bb_vinfo_msg(const char *s, va_list p) va_copy(p2, p); if (logmode & LOGMODE_STDIO) { vprintf(s, p); - putchar('\n'); + fputs(msg_eol, stdout); } if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) vsyslog(LOG_INFO, s, p2); diff --git a/libbb/warn_ignoring_args.c b/libbb/warn_ignoring_args.c index af82a6b5b..6405ff826 100644 --- a/libbb/warn_ignoring_args.c +++ b/libbb/warn_ignoring_args.c @@ -12,6 +12,6 @@ void bb_warn_ignoring_args(int n) { if (n) { - bb_perror_msg("ignoring all arguments"); + bb_error_msg("ignoring all arguments"); } } diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 435379de2..4bb05f248 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -408,7 +408,8 @@ char *xasprintf(const char *format, ...) void xprint_and_close_file(FILE *file) { // copyfd outputs error messages for us. - if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval); + if (bb_copyfd_eof(fileno(file), 1) == -1) + exit(bb_default_error_retval); fclose(file); } diff --git a/loginutils/getty.c b/loginutils/getty.c index 3da7c560a..71f6b2458 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -223,51 +223,6 @@ FILE *dbf; #endif -/* - * output error messages - */ -static void error(const char *fmt, ...) ATTRIBUTE_NORETURN; -static void error(const char *fmt, ...) -{ - va_list va_alist; - char buf[256]; - -#ifdef CONFIG_SYSLOGD - va_start(va_alist, fmt); - vsnprintf(buf, sizeof(buf), fmt, va_alist); - openlog(bb_applet_name, 0, LOG_AUTH); - syslog(LOG_ERR, "%s", buf); - closelog(); -#else - 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, l); - close(fd); - } -#endif - - va_end(va_alist); - - (void) sleep((unsigned) 10); /* be kind to init(8) */ - exit(1); -} - - - /* bcode - convert speed string to speed code; return 0 on failure */ static int bcode(const char *s) { @@ -291,15 +246,15 @@ static void parse_speeds(struct options *op, char *arg) debug("entered parse_speeds\n"); for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) { if ((op->speeds[op->numspeed++] = bcode(cp)) <= 0) - error("bad speed: %s", cp); + bb_error_msg_and_die("bad speed: %s", cp); if (op->numspeed > MAX_SPEED) - error("too many alternate speeds"); + bb_error_msg_and_die("too many alternate speeds"); } debug("exiting parsespeeds\n"); } -/* parse-args - parse command-line arguments */ +/* parse_args - parse command-line arguments */ static void parse_args(int argc, char **argv, struct options *op) { char *ts; @@ -327,7 +282,7 @@ static void parse_args(int argc, char **argv, struct options *op) 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); + bb_error_msg_and_die("bad timeout value: %s", ts); } debug("after getopt loop\n"); if (argc < optind + 2) /* check parameter count */ @@ -350,6 +305,12 @@ static void parse_args(int argc, char **argv, struct options *op) debug("exiting parseargs\n"); } +static void xdup2(int srcfd, int dstfd, const char *tty) +{ + if(dup2(srcfd, dstfd) == -1) + bb_perror_msg_and_die("%s: dup", tty); +} + /* open_tty - set up tty as standard { input, output, error } */ static void open_tty(char *tty, struct termio *tp, int local) { @@ -363,37 +324,34 @@ static void open_tty(char *tty, struct termio *tp, int local) /* Sanity checks... */ - if (chdir("/dev")) - error("/dev: chdir() failed: %m"); + xchdir("/dev"); chdir_to_root = 1; - if (stat(tty, &st) < 0) - error("/dev/%s: %m", tty); + xstat(tty, &st); if ((st.st_mode & S_IFMT) != S_IFCHR) - error("/dev/%s: not a character device", tty); + bb_error_msg_and_die("%s: not a character device", tty); /* Open the tty as standard input. */ - close(0); debug("open(2)\n"); - fd = open(tty, O_RDWR | O_NONBLOCK, 0); - if (fd != 0) - error("/dev/%s: cannot open as standard input: %m", tty); + fd = xopen(tty, O_RDWR | O_NONBLOCK); + if(fd) { + xdup2(fd, 0, tty); + close(fd); + } } else { - /* * Standard input should already be connected to an open port. Make * sure it is open for read/write. */ if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR) - error("%s: not open for read/write", tty); + bb_error_msg_and_die("%s: not open for read/write", tty); } /* Replace current standard output/error fd's with new ones */ debug("duping\n"); - if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1 || - dup2(STDIN_FILENO, STDERR_FILENO) == -1) - error("%s: dup problem: %m", tty); /* we have a problem */ + xdup2(0, 1, tty); + xdup2(0, 2, tty); /* * The following ioctl will fail if stdin is not a tty, but also when @@ -405,7 +363,7 @@ static void open_tty(char *tty, struct termio *tp, int local) */ if (ioctl(0, TCGETA, tp) < 0) - error("%s: ioctl: %m", tty); + bb_perror_msg_and_die("%s: ioctl(TCGETA)", tty); /* * It seems to be a terminal. Set proper protections and ownership. Mode @@ -428,10 +386,8 @@ static void open_tty(char *tty, struct termio *tp, int local) if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) { char *vcs, *vcsa; - if (!(vcs = strdup(tty))) - error("Can't malloc for vcs"); - if (!(vcsa = malloc(strlen(tty) + 2))) - error("Can't malloc for vcsa"); + vcs = xstrdup(tty); + vcsa = xmalloc(strlen(tty) + 2); strcpy(vcs, "vcs"); strcpy(vcs + 3, tty + 3); strcpy(vcsa, "vcsa"); @@ -451,8 +407,8 @@ static void open_tty(char *tty, struct termio *tp, int local) (void) chown(tty, 0, 0); /* root, sys */ (void) chmod(tty, 0622); /* crw--w--w- */ #endif - if(chdir_to_root && chdir("/")) - error("chdir to / failed: %m"); + if (chdir_to_root) + xchdir("/"); } /* termio_init - initialize termio settings */ @@ -634,7 +590,7 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio if (read(0, &c, 1) < 1) { if (errno == EINTR || errno == EIO) exit(0); - error("%s: read: %m", op->tty); + bb_perror_msg_and_die("%s: read", op->tty); } /* Do BREAK handling elsewhere. */ @@ -681,7 +637,7 @@ static char *get_logname(struct options *op, struct chardata *cp, struct termio if (!isascii(ascval) || !isprint(ascval)) { /* ignore garbage characters */ ; } else if (bp - logname >= sizeof(logname) - 1) { - error("%s: input overrun", op->tty); + bb_error_msg_and_die("%s: input overrun", op->tty); } else { (void) write(1, &c, 1); /* echo the character */ *bp++ = ascval; /* and store it */ @@ -759,7 +715,7 @@ static void termio_final(struct options *op, struct termio *tp, struct chardata /* Finally, make the new settings effective */ if (ioctl(0, TCSETA, tp) < 0) - error("%s: ioctl: TCSETA: %m", op->tty); + bb_perror_msg_and_die("%s: ioctl(TCSETA)", op->tty); } @@ -828,6 +784,7 @@ static void update_utmp(char *line) #undef logname int getty_main(int argc, char **argv) { + int nullfd; char *logname = NULL; /* login name, given to /bin/login */ struct chardata chardata; /* set by get_logname() */ struct termio termio; /* terminal mode bits */ @@ -845,6 +802,29 @@ int getty_main(int argc, char **argv) 0, /* no baud rates known yet */ }; + /* Already too late because of theoretical + * possibility of getty --help somehow triggered + * inadvertently before we reach this. Oh well. */ + close(0); + close(1); + close(2); +#ifdef __linux__ + setsid(); +#endif + /* We want special flavor of error_msg_and_die */ + die_sleep = 10; + msg_eol = "\r\n"; + /* Was "/dev/console". Why should we spam *system console* + * if there is a problem with getty on /dev/ttyS15?... */ + nullfd = xopen(bb_dev_null, O_RDWR); + dup2(nullfd, 0); + dup2(nullfd, 1); + dup2(nullfd, 2); + if(nullfd > 2) + close(nullfd); + openlog(bb_applet_name, LOG_PID, LOG_AUTH); + logmode = LOGMODE_BOTH; + #ifdef DEBUGGING dbf = xfopen(DEBUGTERM, "w"); @@ -859,18 +839,11 @@ int getty_main(int argc, char **argv) #endif /* Parse command-line arguments. */ - parse_args(argc, argv, &options); -#ifdef __linux__ - setsid(); -#endif - - /* Update the utmp file. */ - - -#ifdef SYSV_STYLE +#ifdef SYSV_STYLE #ifdef CONFIG_FEATURE_UTMP + /* Update the utmp file. */ update_utmp(options.tty); #endif #endif @@ -931,8 +904,9 @@ int getty_main(int argc, char **argv) /* Read the login name. */ debug("reading login name\n"); /* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */ - while ((logname = get_logname(&options, &chardata, &termio)) == - NULL) next_speed(&termio, &options); + logname = get_logname(&options, &chardata, &termio); + while (logname == NULL) + next_speed(&termio, &options); } /* Disable timer. */ @@ -951,6 +925,6 @@ int getty_main(int argc, char **argv) /* Let the login program take care of password validation. */ (void) execl(options.login, options.login, "--", logname, (char *) 0); - error("%s: can't exec %s: %m", options.tty, options.login); + bb_error_msg_and_die("%s: can't exec %s", options.tty, options.login); } diff --git a/loginutils/login.c b/loginutils/login.c index f3c7e70f4..5b4edd8de 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -444,7 +444,7 @@ static void checkutmp(int picky) } if (strncmp(line, "/dev/", 5) == 0) line += 5; - memset((void *) &utent, 0, sizeof utent); + memset(&utent, 0, sizeof utent); utent.ut_type = LOGIN_PROCESS; utent.ut_pid = pid; strncpy(utent.ut_line, line, sizeof utent.ut_line); diff --git a/loginutils/su.c b/loginutils/su.c index 6410e748f..2e0aed6b1 100644 --- a/loginutils/su.c +++ b/loginutils/su.c @@ -8,7 +8,7 @@ #include "busybox.h" #include -int su_main ( int argc, char **argv ) +int su_main(int argc, char **argv) { unsigned long flags; char *opt_shell = 0; @@ -27,7 +27,7 @@ int su_main ( int argc, char **argv ) if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) { flags |= SU_OPT_l; ++optind; - } + } /* get user if specified */ if (optind < argc) opt_username = argv [optind++]; @@ -81,7 +81,7 @@ int su_main ( int argc, char **argv ) change_identity(pw); setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), pw); - USE_SELINUX(set_current_security_context(NULL);) + USE_SELINUX(set_current_security_context(NULL);) /* Never returns */ run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args); diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c index 1c49d324d..763a9913a 100644 --- a/loginutils/sulogin.c +++ b/loginutils/sulogin.c @@ -65,7 +65,8 @@ int sulogin_main(int argc, char **argv) struct spwd *spwd = NULL; #endif - openlog("sulogin", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH); + openlog("sulogin", LOG_PID | LOG_NOWAIT, LOG_AUTH); + logmode = LOGMODE_BOTH; if (argc > 1) { if (strncmp(argv[1], "-t", 2) == 0) { if (argv[1][2] == '\0') { /* -t NN */ @@ -92,28 +93,24 @@ int sulogin_main(int argc, char **argv) dup(0); dup(0); } else { - syslog(LOG_WARNING, "cannot open %s\n", device); - exit(EXIT_FAILURE); + /* Well, it will go only to syslog :) */ + bb_perror_msg_and_die("Cannot open %s", device); } } } - if (access(bb_path_passwd_file, 0) == -1) { - syslog(LOG_WARNING, "No password file\n"); - bb_error_msg_and_die("No password file"); - } if (!isatty(0) || !isatty(1) || !isatty(2)) { exit(EXIT_FAILURE); } - + if (access(bb_path_passwd_file, 0) == -1) { + bb_error_msg_and_die("No password file"); + } /* Clear out anything dangerous from the environment */ for (p = forbid; *p; p++) unsetenv(*p); - signal(SIGALRM, catchalarm); if (!(pwd = getpwnam(name))) { - syslog(LOG_WARNING, "No password entry for `root'"); bb_error_msg_and_die("No password entry for `root'"); } pwent = *pwd; @@ -131,9 +128,10 @@ int sulogin_main(int argc, char **argv) while (1) { cp = bb_askpass(timeout, SULOGIN_PROMPT); if (!cp || !*cp) { - puts("\n"); + puts("\n"); /* Why only on error path? */ fflush(stdout); - syslog(LOG_INFO, "Normal startup\n"); + /* Why only to syslog? */ + syslog(LOG_INFO, "Normal startup"); exit(EXIT_SUCCESS); } else { safe_strncpy(pass, cp, sizeof(pass)); @@ -143,15 +141,11 @@ int sulogin_main(int argc, char **argv) break; } bb_do_delay(FAIL_DELAY); - puts("Login incorrect"); - fflush(stdout); - syslog(LOG_WARNING, "Incorrect root password\n"); + bb_error_msg("Incorrect root password"); } memset(pass, 0, strlen(pass)); signal(SIGALRM, SIG_DFL); - puts("Entering System Maintenance Mode\n"); - fflush(stdout); - syslog(LOG_INFO, "System Maintenance Mode\n"); + bb_info_msg("Entering System Maintenance Mode"); #if ENABLE_SELINUX renew_current_security_context(); @@ -159,5 +153,5 @@ int sulogin_main(int argc, char **argv) run_shell(pwent.pw_shell, 1, 0, 0); - return (0); + return 0; } -- cgit v1.2.3