getty, sulogin: convert to using bb_msg for syslog output

This commit is contained in:
Denis Vlasenko 2006-09-07 16:20:03 +00:00
parent b750dec40a
commit a9801658ee
14 changed files with 96 additions and 114 deletions

View File

@ -47,7 +47,7 @@ int nohup_main(int argc, char *argv[])
if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout); if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout);
dup2(temp ? 1 : nullfd, 2); dup2(temp ? 1 : nullfd, 2);
close(nullfd); close(nullfd);
signal (SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
// Exec our new program. // Exec our new program.

View File

@ -118,7 +118,9 @@ enum {
LOGMODE_SYSLOG = 1<<1, LOGMODE_SYSLOG = 1<<1,
LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO, LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
}; };
extern const char *msg_eol;
extern int logmode; extern int logmode;
extern int die_sleep;
extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE; extern void bb_show_usage(void) ATTRIBUTE_NORETURN ATTRIBUTE_EXTERNALLY_VISIBLE;
extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));

View File

@ -13,6 +13,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "libbb.h" #include "libbb.h"
int die_sleep;
void bb_error_msg_and_die(const char *s, ...) void bb_error_msg_and_die(const char *s, ...)
{ {
va_list p; va_list p;
@ -20,5 +22,7 @@ void bb_error_msg_and_die(const char *s, ...)
va_start(p, s); va_start(p, s);
bb_verror_msg(s, p, NULL); bb_verror_msg(s, p, NULL);
va_end(p); va_end(p);
if (die_sleep)
sleep(die_sleep);
exit(bb_default_error_retval); exit(bb_default_error_retval);
} }

View File

@ -20,5 +20,7 @@ void bb_fflush_stdout_and_exit(int retval)
if (fflush(stdout)) { if (fflush(stdout)) {
retval = bb_default_error_retval; retval = bb_default_error_retval;
} }
if (die_sleep)
sleep(die_sleep);
exit(retval); exit(retval);
} }

View File

@ -19,5 +19,7 @@ void bb_herror_msg_and_die(const char *s, ...)
va_start(p, s); va_start(p, s);
bb_vherror_msg(s, p); bb_vherror_msg(s, p);
va_end(p); va_end(p);
if (die_sleep)
sleep(die_sleep);
exit(bb_default_error_retval); exit(bb_default_error_retval);
} }

View File

@ -20,5 +20,7 @@ void bb_perror_msg_and_die(const char *s, ...)
va_start(p, s); va_start(p, s);
bb_vperror_msg(s, p); bb_vperror_msg(s, p);
va_end(p); va_end(p);
if (die_sleep)
sleep(die_sleep);
exit(bb_default_error_retval); exit(bb_default_error_retval);
} }

View File

@ -15,6 +15,7 @@
#include "libbb.h" #include "libbb.h"
int logmode = LOGMODE_STDIO; int logmode = LOGMODE_STDIO;
const char *msg_eol = "\n";
void bb_verror_msg(const char *s, va_list p, const char* strerr) 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); fprintf(stderr, "%s: ", bb_applet_name);
vfprintf(stderr, s, p); vfprintf(stderr, s, p);
if (!strerr) if (!strerr)
fputc('\n', stderr); fputs(msg_eol, stderr);
else else
fprintf(stderr, ": %s\n", strerr); fprintf(stderr, ": %s%s", strerr, msg_eol);
} }
if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) { if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) {
if (!strerr) if (!strerr)

View File

@ -22,7 +22,7 @@ void bb_vinfo_msg(const char *s, va_list p)
va_copy(p2, p); va_copy(p2, p);
if (logmode & LOGMODE_STDIO) { if (logmode & LOGMODE_STDIO) {
vprintf(s, p); vprintf(s, p);
putchar('\n'); fputs(msg_eol, stdout);
} }
if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG)) if (ENABLE_FEATURE_SYSLOG && (logmode & LOGMODE_SYSLOG))
vsyslog(LOG_INFO, s, p2); vsyslog(LOG_INFO, s, p2);

View File

@ -12,6 +12,6 @@
void bb_warn_ignoring_args(int n) void bb_warn_ignoring_args(int n)
{ {
if (n) { if (n) {
bb_perror_msg("ignoring all arguments"); bb_error_msg("ignoring all arguments");
} }
} }

View File

@ -408,7 +408,8 @@ char *xasprintf(const char *format, ...)
void xprint_and_close_file(FILE *file) void xprint_and_close_file(FILE *file)
{ {
// copyfd outputs error messages for us. // 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); fclose(file);
} }

View File

@ -223,51 +223,6 @@ FILE *dbf;
#endif #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 */ /* bcode - convert speed string to speed code; return 0 on failure */
static int bcode(const char *s) static int bcode(const char *s)
{ {
@ -291,15 +246,15 @@ static void parse_speeds(struct options *op, char *arg)
debug("entered parse_speeds\n"); debug("entered parse_speeds\n");
for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) { for (cp = strtok(arg, ","); cp != 0; cp = strtok((char *) 0, ",")) {
if ((op->speeds[op->numspeed++] = bcode(cp)) <= 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) if (op->numspeed > MAX_SPEED)
error("too many alternate speeds"); bb_error_msg_and_die("too many alternate speeds");
} }
debug("exiting parsespeeds\n"); 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) static void parse_args(int argc, char **argv, struct options *op)
{ {
char *ts; 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 */ op->flags ^= F_ISSUE; /* revert flag show /etc/issue */
if(op->flags & F_TIMEOUT) { if(op->flags & F_TIMEOUT) {
if ((op->timeout = atoi(ts)) <= 0) 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"); debug("after getopt loop\n");
if (argc < optind + 2) /* check parameter count */ 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"); 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 } */ /* open_tty - set up tty as standard { input, output, error } */
static void open_tty(char *tty, struct termio *tp, int local) 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... */ /* Sanity checks... */
if (chdir("/dev")) xchdir("/dev");
error("/dev: chdir() failed: %m");
chdir_to_root = 1; chdir_to_root = 1;
if (stat(tty, &st) < 0) xstat(tty, &st);
error("/dev/%s: %m", tty);
if ((st.st_mode & S_IFMT) != S_IFCHR) 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. */ /* Open the tty as standard input. */
close(0);
debug("open(2)\n"); debug("open(2)\n");
fd = open(tty, O_RDWR | O_NONBLOCK, 0); fd = xopen(tty, O_RDWR | O_NONBLOCK);
if (fd != 0) if(fd) {
error("/dev/%s: cannot open as standard input: %m", tty); xdup2(fd, 0, tty);
close(fd);
}
} else { } else {
/* /*
* Standard input should already be connected to an open port. Make * Standard input should already be connected to an open port. Make
* sure it is open for read/write. * sure it is open for read/write.
*/ */
if ((fcntl(0, F_GETFL, 0) & O_RDWR) != O_RDWR) 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 */ /* Replace current standard output/error fd's with new ones */
debug("duping\n"); debug("duping\n");
if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1 || xdup2(0, 1, tty);
dup2(STDIN_FILENO, STDERR_FILENO) == -1) xdup2(0, 2, tty);
error("%s: dup problem: %m", tty); /* we have a problem */
/* /*
* The following ioctl will fail if stdin is not a tty, but also when * 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) 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 * 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])) { if (!strncmp(tty, "tty", 3) && isdigit(tty[3])) {
char *vcs, *vcsa; char *vcs, *vcsa;
if (!(vcs = strdup(tty))) vcs = xstrdup(tty);
error("Can't malloc for vcs"); vcsa = xmalloc(strlen(tty) + 2);
if (!(vcsa = malloc(strlen(tty) + 2)))
error("Can't malloc for vcsa");
strcpy(vcs, "vcs"); strcpy(vcs, "vcs");
strcpy(vcs + 3, tty + 3); strcpy(vcs + 3, tty + 3);
strcpy(vcsa, "vcsa"); 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) chown(tty, 0, 0); /* root, sys */
(void) chmod(tty, 0622); /* crw--w--w- */ (void) chmod(tty, 0622); /* crw--w--w- */
#endif #endif
if(chdir_to_root && chdir("/")) if (chdir_to_root)
error("chdir to / failed: %m"); xchdir("/");
} }
/* termio_init - initialize termio settings */ /* 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 (read(0, &c, 1) < 1) {
if (errno == EINTR || errno == EIO) if (errno == EINTR || errno == EIO)
exit(0); exit(0);
error("%s: read: %m", op->tty); bb_perror_msg_and_die("%s: read", op->tty);
} }
/* Do BREAK handling elsewhere. */ /* 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)) { if (!isascii(ascval) || !isprint(ascval)) {
/* ignore garbage characters */ ; /* ignore garbage characters */ ;
} else if (bp - logname >= sizeof(logname) - 1) { } else if (bp - logname >= sizeof(logname) - 1) {
error("%s: input overrun", op->tty); bb_error_msg_and_die("%s: input overrun", op->tty);
} else { } else {
(void) write(1, &c, 1); /* echo the character */ (void) write(1, &c, 1); /* echo the character */
*bp++ = ascval; /* and store it */ *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 */ /* Finally, make the new settings effective */
if (ioctl(0, TCSETA, tp) < 0) 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 #undef logname
int getty_main(int argc, char **argv) int getty_main(int argc, char **argv)
{ {
int nullfd;
char *logname = NULL; /* login name, given to /bin/login */ char *logname = NULL; /* login name, given to /bin/login */
struct chardata chardata; /* set by get_logname() */ struct chardata chardata; /* set by get_logname() */
struct termio termio; /* terminal mode bits */ struct termio termio; /* terminal mode bits */
@ -845,6 +802,29 @@ int getty_main(int argc, char **argv)
0, /* no baud rates known yet */ 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 #ifdef DEBUGGING
dbf = xfopen(DEBUGTERM, "w"); dbf = xfopen(DEBUGTERM, "w");
@ -859,18 +839,11 @@ int getty_main(int argc, char **argv)
#endif #endif
/* Parse command-line arguments. */ /* Parse command-line arguments. */
parse_args(argc, argv, &options); parse_args(argc, argv, &options);
#ifdef __linux__ #ifdef SYSV_STYLE
setsid();
#endif
/* Update the utmp file. */
#ifdef SYSV_STYLE
#ifdef CONFIG_FEATURE_UTMP #ifdef CONFIG_FEATURE_UTMP
/* Update the utmp file. */
update_utmp(options.tty); update_utmp(options.tty);
#endif #endif
#endif #endif
@ -931,8 +904,9 @@ int getty_main(int argc, char **argv)
/* Read the login name. */ /* Read the login name. */
debug("reading login name\n"); debug("reading login name\n");
/* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */ /* while ((logname = get_logname(&options, &chardata, &termio)) == 0) */
while ((logname = get_logname(&options, &chardata, &termio)) == logname = get_logname(&options, &chardata, &termio);
NULL) next_speed(&termio, &options); while (logname == NULL)
next_speed(&termio, &options);
} }
/* Disable timer. */ /* Disable timer. */
@ -951,6 +925,6 @@ int getty_main(int argc, char **argv)
/* Let the login program take care of password validation. */ /* Let the login program take care of password validation. */
(void) execl(options.login, options.login, "--", logname, (char *) 0); (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);
} }

View File

@ -444,7 +444,7 @@ static void checkutmp(int picky)
} }
if (strncmp(line, "/dev/", 5) == 0) if (strncmp(line, "/dev/", 5) == 0)
line += 5; line += 5;
memset((void *) &utent, 0, sizeof utent); memset(&utent, 0, sizeof utent);
utent.ut_type = LOGIN_PROCESS; utent.ut_type = LOGIN_PROCESS;
utent.ut_pid = pid; utent.ut_pid = pid;
strncpy(utent.ut_line, line, sizeof utent.ut_line); strncpy(utent.ut_line, line, sizeof utent.ut_line);

View File

@ -8,7 +8,7 @@
#include "busybox.h" #include "busybox.h"
#include <syslog.h> #include <syslog.h>
int su_main ( int argc, char **argv ) int su_main(int argc, char **argv)
{ {
unsigned long flags; unsigned long flags;
char *opt_shell = 0; 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) { if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) {
flags |= SU_OPT_l; flags |= SU_OPT_l;
++optind; ++optind;
} }
/* get user if specified */ /* get user if specified */
if (optind < argc) opt_username = argv [optind++]; if (optind < argc) opt_username = argv [optind++];
@ -81,7 +81,7 @@ int su_main ( int argc, char **argv )
change_identity(pw); change_identity(pw);
setup_environment(opt_shell, flags & SU_OPT_l, !(flags & SU_OPT_mp), 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 */ /* Never returns */
run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args); run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args);

View File

@ -65,7 +65,8 @@ int sulogin_main(int argc, char **argv)
struct spwd *spwd = NULL; struct spwd *spwd = NULL;
#endif #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 (argc > 1) {
if (strncmp(argv[1], "-t", 2) == 0) { if (strncmp(argv[1], "-t", 2) == 0) {
if (argv[1][2] == '\0') { /* -t NN */ if (argv[1][2] == '\0') { /* -t NN */
@ -92,28 +93,24 @@ int sulogin_main(int argc, char **argv)
dup(0); dup(0);
dup(0); dup(0);
} else { } else {
syslog(LOG_WARNING, "cannot open %s\n", device); /* Well, it will go only to syslog :) */
exit(EXIT_FAILURE); 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)) { if (!isatty(0) || !isatty(1) || !isatty(2)) {
exit(EXIT_FAILURE); 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 */ /* Clear out anything dangerous from the environment */
for (p = forbid; *p; p++) for (p = forbid; *p; p++)
unsetenv(*p); unsetenv(*p);
signal(SIGALRM, catchalarm); signal(SIGALRM, catchalarm);
if (!(pwd = getpwnam(name))) { if (!(pwd = getpwnam(name))) {
syslog(LOG_WARNING, "No password entry for `root'");
bb_error_msg_and_die("No password entry for `root'"); bb_error_msg_and_die("No password entry for `root'");
} }
pwent = *pwd; pwent = *pwd;
@ -131,9 +128,10 @@ int sulogin_main(int argc, char **argv)
while (1) { while (1) {
cp = bb_askpass(timeout, SULOGIN_PROMPT); cp = bb_askpass(timeout, SULOGIN_PROMPT);
if (!cp || !*cp) { if (!cp || !*cp) {
puts("\n"); puts("\n"); /* Why only on error path? */
fflush(stdout); fflush(stdout);
syslog(LOG_INFO, "Normal startup\n"); /* Why only to syslog? */
syslog(LOG_INFO, "Normal startup");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} else { } else {
safe_strncpy(pass, cp, sizeof(pass)); safe_strncpy(pass, cp, sizeof(pass));
@ -143,15 +141,11 @@ int sulogin_main(int argc, char **argv)
break; break;
} }
bb_do_delay(FAIL_DELAY); bb_do_delay(FAIL_DELAY);
puts("Login incorrect"); bb_error_msg("Incorrect root password");
fflush(stdout);
syslog(LOG_WARNING, "Incorrect root password\n");
} }
memset(pass, 0, strlen(pass)); memset(pass, 0, strlen(pass));
signal(SIGALRM, SIG_DFL); signal(SIGALRM, SIG_DFL);
puts("Entering System Maintenance Mode\n"); bb_info_msg("Entering System Maintenance Mode");
fflush(stdout);
syslog(LOG_INFO, "System Maintenance Mode\n");
#if ENABLE_SELINUX #if ENABLE_SELINUX
renew_current_security_context(); renew_current_security_context();
@ -159,5 +153,5 @@ int sulogin_main(int argc, char **argv)
run_shell(pwent.pw_shell, 1, 0, 0); run_shell(pwent.pw_shell, 1, 0, 0);
return (0); return 0;
} }