Implement optional syslog logging using ordinary

bb_xx_msg calls, and convert networking/* to it.
The rest of bbox will be converted gradually.
This commit is contained in:
Denis Vlasenko 2006-09-06 18:36:50 +00:00
parent 5d725462d4
commit 3538b9a882
39 changed files with 296 additions and 366 deletions

View File

@ -112,6 +112,14 @@ extern void *llist_pop(llist_t **elm);
extern void llist_free(llist_t *elm, void (*freeit)(void *data)); extern void llist_free(llist_t *elm, void (*freeit)(void *data));
enum {
LOGMODE_NONE = 0,
LOGMODE_STDIO = 1<<0,
LOGMODE_SYSLOG = 1<<1,
LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
};
extern int logmode;
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)));
extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
@ -124,9 +132,12 @@ extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn,
extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN; extern void bb_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
extern void bb_perror_nomsg(void); extern void bb_perror_nomsg(void);
extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
/* These two are used internally -- you shouldn't need to use them */ /* These two are used internally -- you shouldn't need to use them */
extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0))); extern void bb_verror_msg(const char *s, va_list p, const char *strerr) __attribute__ ((format (printf, 1, 0)));
extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0))); extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
extern void bb_vinfo_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
extern int bb_echo(int argc, char** argv); extern int bb_echo(int argc, char** argv);
extern int bb_test(int argc, char** argv); extern int bb_test(int argc, char** argv);

View File

@ -2092,7 +2092,7 @@ USE_FEATURE_MDEV_CONFIG( \
#define nameif_trivial_usage \ #define nameif_trivial_usage \
"[-s] [-c FILE] [{IFNAME MACADDR}]" "[-s] [-c FILE] [{IFNAME MACADDR}]"
#define nameif_full_usage \ #define nameif_full_usage \
"Nameif renaming network interface while it in the down state.\n\n" \ "Nameif renames network interface while it in the down state.\n\n" \
"Options:\n" \ "Options:\n" \
"\t-c FILE\t\tUse configuration file (default is /etc/mactab)\n" \ "\t-c FILE\t\tUse configuration file (default is /etc/mactab)\n" \
"\t-s\t\tUse syslog (LOCAL0 facility)\n" \ "\t-s\t\tUse syslog (LOCAL0 facility)\n" \

View File

@ -28,6 +28,7 @@ LIBBB-y:= \
restricted_shell.c run_parts.c run_shell.c safe_read.c safe_write.c \ restricted_shell.c run_parts.c run_shell.c safe_read.c safe_write.c \
safe_strncpy.c setup_environment.c sha1.c simplify_path.c \ safe_strncpy.c setup_environment.c sha1.c simplify_path.c \
trim.c u_signal_names.c vdprintf.c verror_msg.c \ trim.c u_signal_names.c vdprintf.c verror_msg.c \
info_msg.c vinfo_msg.c \
vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \ vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \
xgethostbyname.c xgethostbyname2.c xreadlink.c xgetlarg.c \ xgethostbyname.c xgethostbyname2.c xreadlink.c xgetlarg.c \
get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \ get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \

View File

@ -220,7 +220,7 @@ static void rewrite(FS * fs)
} }
} else { } else {
DO_BAD_CONV_CHAR: DO_BAD_CONV_CHAR:
bb_error_msg_and_die("bad conversion character %%%s.\n", p1); bb_error_msg_and_die("bad conversion character %%%s.", p1);
} }
/* /*
@ -253,7 +253,7 @@ static void rewrite(FS * fs)
/* only one conversion character if byte count */ /* only one conversion character if byte count */
if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) { if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
bb_error_msg_and_die("byte count with multiple conversion characters.\n"); bb_error_msg_and_die("byte count with multiple conversion characters.");
} }
} }
/* /*

View File

@ -18,7 +18,6 @@ void bb_error_msg(const char *s, ...)
va_list p; va_list p;
va_start(p, s); va_start(p, s);
bb_verror_msg(s, p); bb_verror_msg(s, p, NULL);
va_end(p); va_end(p);
putc('\n', stderr);
} }

View File

@ -18,8 +18,7 @@ void bb_error_msg_and_die(const char *s, ...)
va_list p; va_list p;
va_start(p, s); va_start(p, s);
bb_verror_msg(s, p); bb_verror_msg(s, p, NULL);
va_end(p); va_end(p);
putc('\n', stderr);
exit(bb_default_error_retval); exit(bb_default_error_retval);
} }

View File

@ -205,7 +205,7 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
/* Grmpf. -FvK */ /* Grmpf. -FvK */
if (sin6->sin6_family != AF_INET6) { if (sin6->sin6_family != AF_INET6) {
#ifdef DEBUG #ifdef DEBUG
bb_error_msg(_("rresolve: unsupport address family %d !\n"), bb_error_msg(_("rresolve: unsupport address family %d!"),
sin6->sin6_family); sin6->sin6_family);
#endif #endif
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;

View File

@ -60,10 +60,7 @@ void setup_environment ( const char *shell, int loginshell, int changeenv, const
* Some systems default to HOME=/ * Some systems default to HOME=/
*/ */
if ( chdir ( pw-> pw_dir )) { if ( chdir ( pw-> pw_dir )) {
if ( chdir ( "/" )) { xchdir ( "/" );
syslog ( LOG_WARNING, "unable to cd to %s' for user %s'\n", pw-> pw_dir, pw-> pw_name );
bb_error_msg_and_die ( "cannot cd to home directory or /" );
}
fputs ( "warning: cannot change to home directory\n", stderr ); fputs ( "warning: cannot change to home directory\n", stderr );
} }

View File

@ -11,11 +11,37 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <syslog.h>
#include "libbb.h" #include "libbb.h"
void bb_verror_msg(const char *s, va_list p) int logmode = LOGMODE_STDIO;
void bb_verror_msg(const char *s, va_list p, const char* strerr)
{ {
fflush(stdout); /* va_copy is used because it is not portable
fprintf(stderr, "%s: ", bb_applet_name); * to use va_list p twice */
vfprintf(stderr, s, p); va_list p2;
va_copy(p2, p);
if (logmode & LOGMODE_STDIO) {
fflush(stdout);
fprintf(stderr, "%s: ", bb_applet_name);
vfprintf(stderr, s, p);
if (!strerr)
fputc('\n', stderr);
else
fprintf(stderr, ": %s\n", strerr);
}
if (logmode & LOGMODE_SYSLOG) {
if (!strerr)
vsyslog(LOG_ERR, s, p2);
else {
char *msg;
if (vasprintf(&msg, s, p2) < 0)
bb_error_msg_and_die(bb_msg_memory_exhausted);
syslog(LOG_ERR, "%s: %s", msg, strerr);
free(msg);
}
}
va_end(p2);
} }

View File

@ -16,10 +16,5 @@
void bb_vherror_msg(const char *s, va_list p) void bb_vherror_msg(const char *s, va_list p)
{ {
if(s == 0) bb_verror_msg(s, p, hstrerror(h_errno));
s = "";
bb_verror_msg(s, p);
if (*s)
fputs(": ", stderr);
herror("");
} }

View File

@ -15,9 +15,5 @@
void bb_vperror_msg(const char *s, va_list p) void bb_vperror_msg(const char *s, va_list p)
{ {
int err=errno; bb_verror_msg(s, p, strerror(errno));
if(s == 0) s = "";
bb_verror_msg(s, p);
if (*s) s = ": ";
fprintf(stderr, "%s%s\n", s, strerror(err));
} }

View File

@ -397,7 +397,7 @@ char *xasprintf(const char *format, ...)
va_end(p); va_end(p);
#endif #endif
if (r < 0) bb_perror_msg_and_die("xasprintf"); if (r < 0) bb_error_msg_and_die(bb_msg_memory_exhausted);
return string_ptr; return string_ptr;
} }
#endif #endif

View File

@ -157,7 +157,6 @@ static int godaemon(void)
setsid(); setsid();
openlog(bb_applet_name, 0, LOG_DAEMON);
return 1; return 1;
} }
@ -219,6 +218,10 @@ static int checkInput(char *buf, int len, int l)
int fakeidentd_main(int argc, char **argv) int fakeidentd_main(int argc, char **argv)
{ {
/* This applet is an inetd-style daemon */
openlog(bb_applet_name, 0, LOG_DAEMON);
logmode = LOGMODE_SYSLOG;
memset(conns, 0, sizeof(conns)); memset(conns, 0, sizeof(conns));
memset(&G, 0, sizeof(G)); memset(&G, 0, sizeof(G));
FD_ZERO(&G.readfds); FD_ZERO(&G.readfds);
@ -286,7 +289,7 @@ deleteconn:
if (s < 0) { if (s < 0) {
if (errno != EINTR) /* EINTR */ if (errno != EINTR) /* EINTR */
syslog(LOG_ERR, "accept: %s", strerror(errno)); bb_perror_msg("accept");
} else { } else {
if (G.conncnt == MAXCONNS) if (G.conncnt == MAXCONNS)
i = closeOldest(); i = closeOldest();

View File

@ -1809,7 +1809,7 @@ static int miniHttpd(int server)
config->rmt_ip & 0xff); config->rmt_ip & 0xff);
config->port = ntohs(fromAddr.sin_port); config->port = ntohs(fromAddr.sin_port);
#if DEBUG #if DEBUG
bb_error_msg("connection from IP=%s, port %u\n", bb_error_msg("connection from IP=%s, port %u",
config->rmt_ip_str, config->port); config->rmt_ip_str, config->port);
#endif #endif
#endif /* CONFIG_FEATURE_HTTPD_CGI */ #endif /* CONFIG_FEATURE_HTTPD_CGI */

View File

@ -311,18 +311,19 @@ static FILE *fconfig;
static char line[1024]; static char line[1024];
static char *defhost; static char *defhost;
static char *newstr (char *cp) /* xstrdup(NULL) returns NULL, but this one
* will return newly-allocated "" if called with NULL arg
* TODO: audit whether this makes any real difference
*/
static char *xxstrdup (char *cp)
{ {
if ((cp = strdup (cp ? cp : ""))) return xstrdup (cp ? cp : "");
return (cp);
syslog (LOG_ERR, "strdup: %m");
exit (1);
} }
static int setconfig (void) static int setconfig (void)
{ {
free (defhost); free (defhost);
defhost = newstr ("*"); defhost = xstrdup ("*");
if (fconfig != NULL) { if (fconfig != NULL) {
fseek (fconfig, 0L, SEEK_SET); fseek (fconfig, 0L, SEEK_SET);
return (1); return (1);
@ -350,12 +351,12 @@ static void register_rpc (servtab_t *sep)
socklen_t size; socklen_t size;
if ((pp = getprotobyname (sep->se_proto + 4)) == NULL) { if ((pp = getprotobyname (sep->se_proto + 4)) == NULL) {
syslog (LOG_ERR, "%s: getproto: %m", sep->se_proto); bb_perror_msg ("%s: getproto", sep->se_proto);
return; return;
} }
size = sizeof ir_sin; size = sizeof ir_sin;
if (getsockname (sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) { if (getsockname (sep->se_fd, (struct sockaddr *) &ir_sin, &size) < 0) {
syslog (LOG_ERR, "%s/%s: getsockname: %m", bb_perror_msg ("%s/%s: getsockname",
sep->se_service, sep->se_proto); sep->se_service, sep->se_proto);
return; return;
} }
@ -363,7 +364,7 @@ static void register_rpc (servtab_t *sep)
for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
(void) pmap_unset (sep->se_rpcprog, n); (void) pmap_unset (sep->se_rpcprog, n);
if (!pmap_set (sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port))) if (!pmap_set (sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port)))
syslog (LOG_ERR, "%s %s: pmap_set: %u %u %u %u: %m", bb_perror_msg ("%s %s: pmap_set: %u %u %u %u",
sep->se_service, sep->se_proto, sep->se_service, sep->se_proto,
sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port)); sep->se_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port));
} }
@ -375,7 +376,7 @@ static void unregister_rpc (servtab_t *sep)
for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) { for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
if (!pmap_unset (sep->se_rpcprog, n)) if (!pmap_unset (sep->se_rpcprog, n))
syslog (LOG_ERR, "pmap_unset(%u, %u)", sep->se_rpcprog, n); bb_error_msg ("pmap_unset(%u, %u)", sep->se_rpcprog, n);
} }
} }
#endif /* CONFIG_FEATURE_INETD_RPC */ #endif /* CONFIG_FEATURE_INETD_RPC */
@ -401,19 +402,19 @@ static int bump_nofile (void)
struct rlimit rl; struct rlimit rl;
if (getrlimit (RLIMIT_NOFILE, &rl) < 0) { if (getrlimit (RLIMIT_NOFILE, &rl) < 0) {
syslog (LOG_ERR, "getrlimit: %m"); bb_perror_msg ("getrlimit");
return -1; return -1;
} }
rl.rlim_cur = MIN (rl.rlim_max, rl.rlim_cur + FD_CHUNK); rl.rlim_cur = MIN (rl.rlim_max, rl.rlim_cur + FD_CHUNK);
rl.rlim_cur = MIN (FD_SETSIZE, rl.rlim_cur + FD_CHUNK); rl.rlim_cur = MIN (FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
if (rl.rlim_cur <= rlim_ofile_cur) { if (rl.rlim_cur <= rlim_ofile_cur) {
syslog (LOG_ERR, "bump_nofile: cannot extend file limit, max = %d", bb_error_msg ("bump_nofile: cannot extend file limit, max = %d",
(int) rl.rlim_cur); (int) rl.rlim_cur);
return -1; return -1;
} }
if (setrlimit (RLIMIT_NOFILE, &rl) < 0) { if (setrlimit (RLIMIT_NOFILE, &rl) < 0) {
syslog (LOG_ERR, "setrlimit: %m"); bb_perror_msg ("setrlimit");
return -1; return -1;
} }
@ -427,13 +428,13 @@ static void setup (servtab_t *sep)
int r; int r;
if ((sep->se_fd = socket (sep->se_family, sep->se_socktype, 0)) < 0) { if ((sep->se_fd = socket (sep->se_family, sep->se_socktype, 0)) < 0) {
syslog (LOG_ERR, "%s/%s: socket: %m", sep->se_service, sep->se_proto); bb_perror_msg ("%s/%s: socket", sep->se_service, sep->se_proto);
return; return;
} }
#define turnon(fd, opt) \ #define turnon(fd, opt) \
setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
if (turnon (sep->se_fd, SO_REUSEADDR) < 0) if (turnon (sep->se_fd, SO_REUSEADDR) < 0)
syslog (LOG_ERR, "setsockopt (SO_REUSEADDR): %m"); bb_perror_msg ("setsockopt (SO_REUSEADDR)");
#undef turnon #undef turnon
#ifdef CONFIG_FEATURE_INETD_RPC #ifdef CONFIG_FEATURE_INETD_RPC
@ -467,7 +468,7 @@ setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
#endif #endif
r = bind (sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size); r = bind (sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
if (r < 0) { if (r < 0) {
syslog (LOG_ERR, "%s/%s (%d): bind: %m", bb_perror_msg ("%s/%s (%d): bind",
sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family); sep->se_service, sep->se_proto, sep->se_ctrladdr.sa_family);
close (sep->se_fd); close (sep->se_fd);
sep->se_fd = -1; sep->se_fd = -1;
@ -510,7 +511,7 @@ static char *skip (char **cpp) /* int report; */
/* erp: */ /* erp: */
if (*cpp == NULL) { if (*cpp == NULL) {
/* if (report) */ /* if (report) */
/* syslog(LOG_ERR, "syntax error in inetd config file"); */ /* bb_error_msg ("syntax error in inetd config file"); */
return (NULL); return (NULL);
} }
@ -543,14 +544,7 @@ again:
static servtab_t *new_servtab(void) static servtab_t *new_servtab(void)
{ {
servtab_t *sep; return xmalloc (sizeof (servtab_t));
sep = (servtab_t *) malloc (sizeof (servtab_t));
if (sep == NULL) {
syslog (LOG_ERR, bb_msg_memory_exhausted);
exit (1);
}
return sep;
} }
static servtab_t *dupconfig (servtab_t *sep) static servtab_t *dupconfig (servtab_t *sep)
@ -560,26 +554,25 @@ static servtab_t *dupconfig (servtab_t *sep)
newtab = new_servtab(); newtab = new_servtab();
memset (newtab, 0, sizeof (servtab_t)); memset (newtab, 0, sizeof (servtab_t));
newtab->se_service = sep->se_service ? newstr (sep->se_service) : NULL; newtab->se_service = xstrdup (sep->se_service);
newtab->se_socktype = sep->se_socktype; newtab->se_socktype = sep->se_socktype;
newtab->se_family = sep->se_family; newtab->se_family = sep->se_family;
newtab->se_proto = sep->se_proto ? newstr (sep->se_proto) : NULL; newtab->se_proto = xstrdup (sep->se_proto);
#ifdef CONFIG_FEATURE_INETD_RPC #ifdef CONFIG_FEATURE_INETD_RPC
newtab->se_rpcprog = sep->se_rpcprog; newtab->se_rpcprog = sep->se_rpcprog;
newtab->se_rpcversl = sep->se_rpcversl; newtab->se_rpcversl = sep->se_rpcversl;
newtab->se_rpcversh = sep->se_rpcversh; newtab->se_rpcversh = sep->se_rpcversh;
#endif #endif
newtab->se_wait = sep->se_wait; newtab->se_wait = sep->se_wait;
newtab->se_user = sep->se_user ? newstr (sep->se_user) : NULL; newtab->se_user = xstrdup (sep->se_user);
newtab->se_group = sep->se_group ? newstr (sep->se_group) : NULL; newtab->se_group = xstrdup (sep->se_group);
#ifdef INETD_FEATURE_ENABLED #ifdef INETD_FEATURE_ENABLED
newtab->se_bi = sep->se_bi; newtab->se_bi = sep->se_bi;
#endif #endif
newtab->se_server = sep->se_server ? newstr (sep->se_server) : 0; newtab->se_server = xstrdup (sep->se_server);
for (argc = 0; argc <= MAXARGV; argc++) for (argc = 0; argc <= MAXARGV; argc++)
newtab->se_argv[argc] = sep->se_argv[argc] ? newtab->se_argv[argc] = xstrdup (sep->se_argv[argc]);
newstr (sep->se_argv[argc]) : NULL;
newtab->se_max = sep->se_max; newtab->se_max = sep->se_max;
return (newtab); return (newtab);
@ -617,7 +610,7 @@ more:
hostdelim = strrchr (arg, ':'); hostdelim = strrchr (arg, ':');
if (hostdelim) { if (hostdelim) {
*hostdelim = '\0'; *hostdelim = '\0';
sep->se_hostaddr = newstr (arg); sep->se_hostaddr = xstrdup (arg);
arg = hostdelim + 1; arg = hostdelim + 1;
/* /*
* If the line is of the form `host:', then just change the * If the line is of the form `host:', then just change the
@ -632,9 +625,9 @@ more:
} }
} }
} else } else
sep->se_hostaddr = newstr (defhost); sep->se_hostaddr = xxstrdup (defhost);
sep->se_service = newstr (arg); sep->se_service = xxstrdup (arg);
arg = skip (&cp); arg = skip (&cp);
if (strcmp (arg, "stream") == 0) if (strcmp (arg, "stream") == 0)
@ -650,7 +643,7 @@ more:
else else
sep->se_socktype = -1; sep->se_socktype = -1;
sep->se_proto = newstr (skip (&cp)); sep->se_proto = xxstrdup (skip (&cp));
if (strcmp (sep->se_proto, "unix") == 0) { if (strcmp (sep->se_proto, "unix") == 0) {
sep->se_family = AF_UNIX; sep->se_family = AF_UNIX;
@ -660,7 +653,7 @@ more:
#ifdef CONFIG_FEATURE_IPV6 #ifdef CONFIG_FEATURE_IPV6
sep->se_family = AF_INET6; sep->se_family = AF_INET6;
#else #else
syslog (LOG_ERR, "%s: IPV6 not supported", sep->se_proto); bb_error_msg ("%s: IPV6 not supported", sep->se_proto);
#endif #endif
if (strncmp (sep->se_proto, "rpc/", 4) == 0) { if (strncmp (sep->se_proto, "rpc/", 4) == 0) {
#ifdef CONFIG_FEATURE_INETD_RPC #ifdef CONFIG_FEATURE_INETD_RPC
@ -669,14 +662,14 @@ more:
p = strchr (sep->se_service, '/'); p = strchr (sep->se_service, '/');
if (p == 0) { if (p == 0) {
syslog (LOG_ERR, "%s: no rpc version", sep->se_service); bb_error_msg ("%s: no rpc version", sep->se_service);
goto more; goto more;
} }
*p++ = '\0'; *p++ = '\0';
l = strtol (p, &ccp, 0); l = strtol (p, &ccp, 0);
if (ccp == p || l < 0 || l > INT_MAX) { if (ccp == p || l < 0 || l > INT_MAX) {
badafterall: badafterall:
syslog (LOG_ERR, "%s/%s: bad rpc version", sep->se_service, p); bb_error_msg ("%s/%s: bad rpc version", sep->se_service, p);
goto more; goto more;
} }
sep->se_rpcversl = sep->se_rpcversh = l; sep->se_rpcversl = sep->se_rpcversh = l;
@ -689,7 +682,7 @@ more:
} else if (*ccp != '\0') } else if (*ccp != '\0')
goto badafterall; goto badafterall;
#else #else
syslog (LOG_ERR, "%s: rpc services not supported", sep->se_service); bb_error_msg ("%s: rpc services not supported", sep->se_service);
#endif #endif
} }
} }
@ -708,18 +701,18 @@ more:
sep->se_wait = strcmp (arg, "wait") == 0; sep->se_wait = strcmp (arg, "wait") == 0;
/* if ((arg = skip(&cp, 1)) == NULL) */ /* if ((arg = skip(&cp, 1)) == NULL) */
/* goto more; */ /* goto more; */
sep->se_user = newstr (skip (&cp)); sep->se_user = xxstrdup (skip (&cp));
arg = strchr (sep->se_user, '.'); arg = strchr (sep->se_user, '.');
if (arg == NULL) if (arg == NULL)
arg = strchr (sep->se_user, ':'); arg = strchr (sep->se_user, ':');
if (arg) { if (arg) {
*arg++ = '\0'; *arg++ = '\0';
sep->se_group = newstr (arg); sep->se_group = xstrdup (arg);
} }
/* if ((arg = skip(&cp, 1)) == NULL) */ /* if ((arg = skip(&cp, 1)) == NULL) */
/* goto more; */ /* goto more; */
sep->se_server = newstr (skip (&cp)); sep->se_server = xxstrdup (skip (&cp));
if (strcmp (sep->se_server, "internal") == 0) { if (strcmp (sep->se_server, "internal") == 0) {
#ifdef INETD_FEATURE_ENABLED #ifdef INETD_FEATURE_ENABLED
const struct builtin *bi; const struct builtin *bi;
@ -729,13 +722,13 @@ more:
strcmp (bi->bi_service, sep->se_service) == 0) strcmp (bi->bi_service, sep->se_service) == 0)
break; break;
if (bi->bi_service == 0) { if (bi->bi_service == 0) {
syslog (LOG_ERR, "internal service %s unknown", sep->se_service); bb_error_msg ("internal service %s unknown", sep->se_service);
goto more; goto more;
} }
sep->se_bi = bi; sep->se_bi = bi;
sep->se_wait = bi->bi_wait; sep->se_wait = bi->bi_wait;
#else #else
syslog (LOG_ERR, "internal service %s unknown", sep->se_service); bb_perror_msg ("internal service %s unknown", sep->se_service);
goto more; goto more;
#endif #endif
} }
@ -746,7 +739,7 @@ more:
argc = 0; argc = 0;
for (arg = skip (&cp); cp; arg = skip (&cp)) { for (arg = skip (&cp); cp; arg = skip (&cp)) {
if (argc < MAXARGV) if (argc < MAXARGV)
sep->se_argv[argc++] = newstr (arg); sep->se_argv[argc++] = xxstrdup (arg);
} }
while (argc <= MAXARGV) while (argc <= MAXARGV)
sep->se_argv[argc++] = NULL; sep->se_argv[argc++] = NULL;
@ -764,7 +757,7 @@ more:
* and make a dup for the new entry. * and make a dup for the new entry.
*/ */
*hostdelim++ = '\0'; *hostdelim++ = '\0';
nsep->se_hostaddr = newstr (hostdelim); nsep->se_hostaddr = xstrdup (hostdelim);
nsep->se_next = sep->se_next; nsep->se_next = sep->se_next;
sep->se_next = nsep; sep->se_next = nsep;
@ -781,12 +774,11 @@ more:
hp = gethostbyname (nsep->se_hostaddr); hp = gethostbyname (nsep->se_hostaddr);
if (hp == 0) { if (hp == 0) {
syslog (LOG_ERR, "%s: unknown host", nsep->se_hostaddr); bb_error_msg ("%s: unknown host", nsep->se_hostaddr);
nsep->se_checked = 0; nsep->se_checked = 0;
goto skip; goto skip;
} else if (hp->h_addrtype != AF_INET) { } else if (hp->h_addrtype != AF_INET) {
syslog (LOG_ERR, bb_error_msg ("%s: address isn't an Internet "
"%s: address isn't an Internet "
"address", nsep->se_hostaddr); "address", nsep->se_hostaddr);
nsep->se_checked = 0; nsep->se_checked = 0;
goto skip; goto skip;
@ -797,7 +789,7 @@ more:
hp->h_addr_list[0], sizeof (struct in_addr)); hp->h_addr_list[0], sizeof (struct in_addr));
while (hp->h_addr_list[i] != NULL) { while (hp->h_addr_list[i] != NULL) {
psep = dupconfig (nsep); psep = dupconfig (nsep);
psep->se_hostaddr = newstr (nsep->se_hostaddr); psep->se_hostaddr = xxstrdup (nsep->se_hostaddr);
psep->se_checked = 1; psep->se_checked = 1;
memmove (&psep->se_ctrladdr_in.sin_addr, memmove (&psep->se_ctrladdr_in.sin_addr,
hp->h_addr_list[i], sizeof (struct in_addr)); hp->h_addr_list[i], sizeof (struct in_addr));
@ -913,7 +905,7 @@ static void config (int sig ATTRIBUTE_UNUSED)
char protoname[10]; char protoname[10];
if (!setconfig ()) { if (!setconfig ()) {
syslog (LOG_ERR, "%s: %m", CONFIG); bb_perror_msg ("%s", CONFIG);
return; return;
} }
for (sep = servtab; sep; sep = sep->se_next) for (sep = servtab; sep; sep = sep->se_next)
@ -989,7 +981,7 @@ static void config (int sig ATTRIBUTE_UNUSED)
if (sep->se_rpcprog == 0) { if (sep->se_rpcprog == 0) {
rp = getrpcbyname (sep->se_service); rp = getrpcbyname (sep->se_service);
if (rp == 0) { if (rp == 0) {
syslog (LOG_ERR, "%s: unknown rpc service", sep->se_service); bb_error_msg ("%s: unknown rpc service", sep->se_service);
goto serv_unknown; goto serv_unknown;
} }
sep->se_rpcprog = rp->r_number; sep->se_rpcprog = rp->r_number;
@ -1009,8 +1001,8 @@ static void config (int sig ATTRIBUTE_UNUSED)
protoname[strlen (protoname) - 1] = '\0'; protoname[strlen (protoname) - 1] = '\0';
sp = getservbyname (sep->se_service, protoname); sp = getservbyname (sep->se_service, protoname);
if (sp == 0) { if (sp == 0) {
syslog (LOG_ERR, bb_error_msg ("%s/%s: unknown service",
"%s/%s: unknown service", sep->se_service, sep->se_proto); sep->se_service, sep->se_proto);
goto serv_unknown; goto serv_unknown;
} }
port = sp->s_port; port = sp->s_port;
@ -1042,7 +1034,7 @@ static void config (int sig ATTRIBUTE_UNUSED)
if (sep->se_rpcprog == 0) { if (sep->se_rpcprog == 0) {
rp = getrpcbyname (sep->se_service); rp = getrpcbyname (sep->se_service);
if (rp == 0) { if (rp == 0) {
syslog (LOG_ERR, "%s: unknown rpc service", sep->se_service); bb_error_msg ("%s: unknown rpc service", sep->se_service);
goto serv_unknown; goto serv_unknown;
} }
sep->se_rpcprog = rp->r_number; sep->se_rpcprog = rp->r_number;
@ -1062,8 +1054,8 @@ static void config (int sig ATTRIBUTE_UNUSED)
protoname[strlen (protoname) - 1] = '\0'; protoname[strlen (protoname) - 1] = '\0';
sp = getservbyname (sep->se_service, protoname); sp = getservbyname (sep->se_service, protoname);
if (sp == 0) { if (sp == 0) {
syslog (LOG_ERR, bb_error_msg ("%s/%s: unknown service",
"%s/%s: unknown service", sep->se_service, sep->se_proto); sep->se_service, sep->se_proto);
goto serv_unknown; goto serv_unknown;
} }
port = sp->s_port; port = sp->s_port;
@ -1137,12 +1129,11 @@ static void reapchild (int sig ATTRIBUTE_UNUSED)
for (sep = servtab; sep; sep = sep->se_next) for (sep = servtab; sep; sep = sep->se_next)
if (sep->se_wait == pid) { if (sep->se_wait == pid) {
if (WIFEXITED (status) && WEXITSTATUS (status)) if (WIFEXITED (status) && WEXITSTATUS (status))
syslog (LOG_WARNING, bb_error_msg("%s: exit status 0x%x",
"%s: exit status 0x%x",
sep->se_server, WEXITSTATUS (status)); sep->se_server, WEXITSTATUS (status));
else if (WIFSIGNALED (status)) else if (WIFSIGNALED (status))
syslog (LOG_WARNING, bb_error_msg("%s: exit signal 0x%x",
"%s: exit signal 0x%x", sep->se_server, WTERMSIG (status)); sep->se_server, WTERMSIG (status));
sep->se_wait = 1; sep->se_wait = 1;
FD_SET (sep->se_fd, &allsock); FD_SET (sep->se_fd, &allsock);
nsock++; nsock++;
@ -1271,7 +1262,7 @@ inetd_main (int argc, char *argv[])
toomany = strtoul (stoomany, &e, 0); toomany = strtoul (stoomany, &e, 0);
if (!(toomany >= 0 && *e == '\0')) { if (!(toomany >= 0 && *e == '\0')) {
toomany = TOOMANY; toomany = TOOMANY;
syslog (LOG_ERR, "-R %s: bad value for service invocation rate", stoomany); bb_perror_msg ("-R %s: bad value for service invocation rate", stoomany);
} }
} }
argc -= optind; argc -= optind;
@ -1295,6 +1286,7 @@ inetd_main (int argc, char *argv[])
} else { } else {
setsid (); setsid ();
} }
logmode = LOGMODE_SYSLOG;
if (uid == 0) { if (uid == 0) {
gid_t gid = getgid (); gid_t gid = getgid ();
@ -1313,7 +1305,7 @@ inetd_main (int argc, char *argv[])
} }
if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) { if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) {
syslog (LOG_ERR, "getrlimit: %m"); bb_perror_msg ("getrlimit");
} else { } else {
rlim_ofile_cur = rlim_ofile.rlim_cur; rlim_ofile_cur = rlim_ofile.rlim_cur;
if (rlim_ofile_cur == RLIM_INFINITY) /* ! */ if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
@ -1365,7 +1357,7 @@ inetd_main (int argc, char *argv[])
readable = allsock; readable = allsock;
if ((n = select (maxsock + 1, &readable, NULL, NULL, NULL)) <= 0) { if ((n = select (maxsock + 1, &readable, NULL, NULL, NULL)) <= 0) {
if (n < 0 && errno != EINTR) { if (n < 0 && errno != EINTR) {
syslog (LOG_WARNING, "select: %m"); bb_perror_msg("select");
sleep (1); sleep (1);
} }
continue; continue;
@ -1378,7 +1370,7 @@ inetd_main (int argc, char *argv[])
if (ctrl < 0) { if (ctrl < 0) {
if (errno == EINTR) if (errno == EINTR)
continue; continue;
syslog (LOG_WARNING, "accept (for %s): %m", sep->se_service); bb_perror_msg("accept (for %s)", sep->se_service);
continue; continue;
} }
if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) { if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) {
@ -1386,7 +1378,7 @@ inetd_main (int argc, char *argv[])
socklen_t plen = sizeof (peer); socklen_t plen = sizeof (peer);
if (getpeername (ctrl, (struct sockaddr *) &peer, &plen) < 0) { if (getpeername (ctrl, (struct sockaddr *) &peer, &plen) < 0) {
syslog (LOG_WARNING, "could not getpeername"); bb_error_msg("could not getpeername");
close (ctrl); close (ctrl);
continue; continue;
} }
@ -1426,8 +1418,7 @@ inetd_main (int argc, char *argv[])
--sep->se_count; --sep->se_count;
continue; continue;
} }
syslog (LOG_ERR, bb_error_msg ("%s/%s server failing (looping), service terminated",
"%s/%s server failing (looping), service terminated",
sep->se_service, sep->se_proto); sep->se_service, sep->se_proto);
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close (ctrl); close (ctrl);
@ -1447,7 +1438,7 @@ inetd_main (int argc, char *argv[])
pid = fork (); pid = fork ();
} }
if (pid < 0) { if (pid < 0) {
syslog (LOG_ERR, "fork: %m"); bb_perror_msg ("fork");
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close (ctrl); close (ctrl);
sigprocmask(SIG_UNBLOCK, &omask, NULL); sigprocmask(SIG_UNBLOCK, &omask, NULL);
@ -1468,15 +1459,15 @@ inetd_main (int argc, char *argv[])
#endif #endif
{ {
if ((pwd = getpwnam (sep->se_user)) == NULL) { if ((pwd = getpwnam (sep->se_user)) == NULL) {
syslog (LOG_ERR, "getpwnam: %s: No such user", sep->se_user); bb_error_msg ("getpwnam: %s: no such user", sep->se_user);
if (sep->se_socktype != SOCK_STREAM) if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof (buf), 0); recv (0, buf, sizeof (buf), 0);
_exit (1); _exit (1);
} }
if (setsid () < 0) if (setsid () < 0)
syslog (LOG_ERR, "%s: setsid: %m", sep->se_service); bb_perror_msg ("%s: setsid", sep->se_service);
if (sep->se_group && (grp = getgrnam (sep->se_group)) == NULL) { if (sep->se_group && (grp = getgrnam (sep->se_group)) == NULL) {
syslog (LOG_ERR, "getgrnam: %s: No such group", sep->se_group); bb_error_msg ("getgrnam: %s: no such group", sep->se_group);
if (sep->se_socktype != SOCK_STREAM) if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof (buf), 0); recv (0, buf, sizeof (buf), 0);
_exit (1); _exit (1);
@ -1502,7 +1493,7 @@ inetd_main (int argc, char *argv[])
dup2 (0, 2); dup2 (0, 2);
if (rlim_ofile.rlim_cur != rlim_ofile_cur) if (rlim_ofile.rlim_cur != rlim_ofile_cur)
if (setrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) if (setrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0)
syslog (LOG_ERR, "setrlimit: %m"); bb_perror_msg ("setrlimit");
closelog (); closelog ();
for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;) for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;)
(void) close (tmpint); (void) close (tmpint);
@ -1510,7 +1501,7 @@ inetd_main (int argc, char *argv[])
execv (sep->se_server, sep->se_argv); execv (sep->se_server, sep->se_argv);
if (sep->se_socktype != SOCK_STREAM) if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof (buf), 0); recv (0, buf, sizeof (buf), 0);
syslog (LOG_ERR, "execv %s: %m", sep->se_server); bb_perror_msg ("execv %s", sep->se_server);
_exit (1); _exit (1);
} }
} }

View File

@ -119,7 +119,7 @@ int ipcalc_main(int argc, char **argv)
unsigned int msk; unsigned int msk;
if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) { if (safe_strtoul(prefixstr, &netprefix) || netprefix > 32) {
IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s\n", prefixstr), IPCALC_MSG(bb_error_msg_and_die("bad IP prefix: %s", prefixstr),
exit(EXIT_FAILURE)); exit(EXIT_FAILURE));
} }
netmask = 0; netmask = 0;
@ -149,7 +149,7 @@ int ipcalc_main(int argc, char **argv)
if (argc == 2) { if (argc == 2) {
if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) { if (ENABLE_FEATURE_IPCALC_FANCY && have_netmask) {
IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both.\n"), IPCALC_MSG(bb_error_msg_and_die("Use prefix or netmask, not both"),
exit(EXIT_FAILURE)); exit(EXIT_FAILURE));
} }

View File

@ -608,7 +608,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
} }
filter.flushed = 0; filter.flushed = 0;
if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) { if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
bb_error_msg("Flush terminated\n"); bb_error_msg("Flush terminated");
return -1; return -1;
} }
if (filter.flushed == 0) { if (filter.flushed == 0) {

View File

@ -41,29 +41,6 @@ typedef struct mactable_s {
struct ether_addr *mac; struct ether_addr *mac;
} mactable_t; } mactable_t;
static unsigned long flags;
static void serror(const char *s, ...) ATTRIBUTE_NORETURN;
static void serror(const char *s, ...)
{
va_list ap;
va_start(ap, s);
if (flags & 1) {
openlog(bb_applet_name, 0, LOG_LOCAL0);
vsyslog(LOG_ERR, s, ap);
closelog();
} else {
bb_verror_msg(s, ap);
putc('\n', stderr);
}
va_end(ap);
exit(EXIT_FAILURE);
}
/* Check ascii str_macaddr, convert and copy to *mac */ /* Check ascii str_macaddr, convert and copy to *mac */
static struct ether_addr *cc_macaddr(const char *str_macaddr) static struct ether_addr *cc_macaddr(const char *str_macaddr)
{ {
@ -71,7 +48,7 @@ static struct ether_addr *cc_macaddr(const char *str_macaddr)
lmac = ether_aton(str_macaddr); lmac = ether_aton(str_macaddr);
if (lmac == NULL) if (lmac == NULL)
serror("cannot parse MAC %s", str_macaddr); bb_error_msg_and_die("cannot parse MAC %s", str_macaddr);
mac = xmalloc(ETH_ALEN); mac = xmalloc(ETH_ALEN);
memcpy(mac, lmac, ETH_ALEN); memcpy(mac, lmac, ETH_ALEN);
@ -88,7 +65,10 @@ int nameif_main(int argc, char **argv)
int if_index = 1; int if_index = 1;
mactable_t *ch; mactable_t *ch;
flags = bb_getopt_ulflags(argc, argv, "sc:", &fname); if (1 & bb_getopt_ulflags(argc, argv, "sc:", &fname)) {
openlog(bb_applet_name, 0, LOG_LOCAL0);
logmode = LOGMODE_SYSLOG;
}
if ((argc - optind) & 1) if ((argc - optind) & 1)
bb_show_usage(); bb_show_usage();
@ -97,9 +77,9 @@ int nameif_main(int argc, char **argv)
char **a = argv + optind; char **a = argv + optind;
while (*a) { while (*a) {
if (strlen(*a) > IF_NAMESIZE) if (strlen(*a) > IF_NAMESIZE)
serror("interface name `%s' too long", *a); bb_error_msg_and_die("interface name `%s' "
"too long", *a);
ch = xzalloc(sizeof(mactable_t)); ch = xzalloc(sizeof(mactable_t));
ch->ifname = xstrdup(*a++); ch->ifname = xstrdup(*a++);
ch->mac = cc_macaddr(*a++); ch->mac = cc_macaddr(*a++);
@ -124,7 +104,8 @@ int nameif_main(int argc, char **argv)
ch = xzalloc(sizeof(mactable_t)); ch = xzalloc(sizeof(mactable_t));
ch->ifname = xstrndup(line_ptr, name_length); ch->ifname = xstrndup(line_ptr, name_length);
if (name_length > IF_NAMESIZE) if (name_length > IF_NAMESIZE)
serror("interface name `%s' too long", ch->ifname); bb_error_msg_and_die("interface name `%s' "
"too long", ch->ifname);
line_ptr += name_length; line_ptr += name_length;
line_ptr += strspn(line_ptr, " \t"); line_ptr += strspn(line_ptr, " \t");
name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:"); name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
@ -139,8 +120,7 @@ int nameif_main(int argc, char **argv)
fclose(ifh); fclose(ifh);
} }
if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1) ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
serror("socket: %m");
while (clist) { while (clist) {
struct ifreq ifr; struct ifreq ifr;
@ -168,7 +148,7 @@ int nameif_main(int argc, char **argv)
strcpy(ifr.ifr_newname, ch->ifname); strcpy(ifr.ifr_newname, ch->ifname);
if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0) if (ioctl(ctl_sk, SIOCSIFNAME, &ifr) < 0)
serror("cannot change ifname %s to %s: %m", bb_perror_msg_and_die("cannot change ifname %s to %s",
ifr.ifr_name, ch->ifname); ifr.ifr_name, ch->ifname);
/* Remove list entry of renamed interface */ /* Remove list entry of renamed interface */

View File

@ -263,7 +263,7 @@ make_new_session(int sockfd)
pty = getpty(tty_name); pty = getpty(tty_name);
if (pty < 0) { if (pty < 0) {
syslog(LOG_ERR, "All terminals in use!"); bb_error_msg("all terminals in use");
return 0; return 0;
} }
@ -285,7 +285,7 @@ make_new_session(int sockfd)
send_iac(ts, WILL, TELOPT_SGA); send_iac(ts, WILL, TELOPT_SGA);
if ((pid = fork()) < 0) { if ((pid = fork()) < 0) {
syslog(LOG_ERR, "Could not fork"); bb_perror_msg("fork");
} }
if (pid == 0) { if (pid == 0) {
/* In child, open the child's side of the tty. */ /* In child, open the child's side of the tty. */
@ -296,10 +296,7 @@ make_new_session(int sockfd)
/* make new process group */ /* make new process group */
setsid(); setsid();
if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) { xopen(tty_name, O_RDWR /*| O_NOCTTY*/);
syslog(LOG_ERR, "Could not open tty");
exit(1);
}
dup(0); dup(0);
dup(0); dup(0);
@ -323,8 +320,7 @@ make_new_session(int sockfd)
execv(loginpath, (char *const *)argv_init); execv(loginpath, (char *const *)argv_init);
/* NOT REACHED */ /* NOT REACHED */
syslog(LOG_ERR, "execv error"); bb_perror_msg_and_die("execv");
exit(1);
} }
ts->shell_pid = pid; ts->shell_pid = pid;
@ -390,6 +386,14 @@ telnetd_main(int argc, char **argv)
loginpath = DEFAULT_SHELL; loginpath = DEFAULT_SHELL;
#endif #endif
/* We use inetd-style operation unconditionally
* (no --foreground option), user most likely will
* look into syslog for all errors, even early ones.
* Direct all output to syslog at once.
*/
openlog(bb_applet_name, 0, LOG_USER);
logmode = LOGMODE_SYSLOG;
for (;;) { for (;;) {
c = getopt( argc, argv, options); c = getopt( argc, argv, options);
if (c == EOF) break; if (c == EOF) break;
@ -415,13 +419,11 @@ telnetd_main(int argc, char **argv)
} }
if (access(loginpath, X_OK) < 0) { if (access(loginpath, X_OK) < 0) {
bb_error_msg_and_die ("'%s' unavailable.", loginpath); bb_error_msg_and_die("'%s' unavailable", loginpath);
} }
argv_init[0] = loginpath; argv_init[0] = loginpath;
openlog(bb_applet_name, 0, LOG_USER);
#ifdef CONFIG_FEATURE_TELNETD_INETD #ifdef CONFIG_FEATURE_TELNETD_INETD
maxfd = 1; maxfd = 1;
sessions = make_new_session(); sessions = make_new_session();

View File

@ -515,16 +515,16 @@ str2val(const char *str, const char *what, int mi, int ma)
} else } else
val = (int)strtol(str, &ep, 10); val = (int)strtol(str, &ep, 10);
if (*ep != '\0') { if (*ep != '\0') {
bb_error_msg_and_die("\"%s\" bad value for %s \n", str, what); bb_error_msg_and_die("\"%s\" bad value for %s", str, what);
} }
if (val < mi && mi >= 0) { if (val < mi && mi >= 0) {
if (mi == 0) if (mi == 0)
bb_error_msg_and_die("%s must be >= %d\n", what, mi); bb_error_msg_and_die("%s must be >= %d", what, mi);
else else
bb_error_msg_and_die("%s must be > %d\n", what, mi - 1); bb_error_msg_and_die("%s must be > %d", what, mi - 1);
} }
if (val > ma && ma >= 0) if (val > ma && ma >= 0)
bb_error_msg_and_die("%s must be <= %d\n", what, ma); bb_error_msg_and_die("%s must be <= %d", what, ma);
return val; return val;
} }

View File

@ -43,13 +43,13 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
time_t prevTime; time_t prevTime;
if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) { if ((s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
LOG(LOG_ERR, bb_msg_can_not_create_raw_socket); bb_perror_msg(bb_msg_can_not_create_raw_socket);
return -1; return -1;
} }
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) { if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) {
LOG(LOG_ERR, "Could not setsocketopt on raw socket"); bb_perror_msg("Could not setsocketopt on raw socket");
close(s); close(s);
return -1; return -1;
} }
@ -81,14 +81,14 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
FD_SET(s, &fdset); FD_SET(s, &fdset);
tm.tv_sec = timeout; tm.tv_sec = timeout;
if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) { if (select(s + 1, &fdset, (fd_set *) NULL, (fd_set *) NULL, &tm) < 0) {
DEBUG(LOG_ERR, "Error on ARPING request: %m"); bb_perror_msg("Error on ARPING request");
if (errno != EINTR) rv = 0; if (errno != EINTR) rv = 0;
} else if (FD_ISSET(s, &fdset)) { } else if (FD_ISSET(s, &fdset)) {
if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0; if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
if (arp.operation == htons(ARPOP_REPLY) && if (arp.operation == htons(ARPOP_REPLY) &&
memcmp(arp.tHaddr, mac, 6) == 0 && memcmp(arp.tHaddr, mac, 6) == 0 &&
*((uint32_t *) arp.sInaddr) == yiaddr) { *((uint32_t *) arp.sInaddr) == yiaddr) {
DEBUG(LOG_INFO, "Valid arp reply receved for this address"); DEBUG("Valid arp reply received for this address");
rv = 0; rv = 0;
break; break;
} }
@ -97,6 +97,6 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface)
prevTime = uptime(); prevTime = uptime();
} }
close(s); close(s);
DEBUG(LOG_INFO, "%salid arp replies for this address", rv ? "No v" : "V"); DEBUG("%salid arp replies for this address", rv ? "No v" : "V");
return rv; return rv;
} }

View File

@ -44,7 +44,8 @@ unsigned long random_xid(void)
fd = open("/dev/urandom", 0); fd = open("/dev/urandom", 0);
if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) { if (fd < 0 || read(fd, &seed, sizeof(seed)) < 0) {
LOG(LOG_WARNING, "Could not load seed from /dev/urandom: %m"); bb_info_msg("Could not load seed "
"from /dev/urandom: %s", strerror(errno));
seed = time(0); seed = time(0);
} }
if (fd >= 0) close(fd); if (fd >= 0) close(fd);
@ -97,7 +98,7 @@ int send_discover(unsigned long xid, unsigned long requested)
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested); add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
add_requests(&packet); add_requests(&packet);
LOG(LOG_DEBUG, "Sending discover..."); bb_info_msg("Sending discover...");
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
} }
@ -117,7 +118,7 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques
add_requests(&packet); add_requests(&packet);
addr.s_addr = requested; addr.s_addr = requested;
LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr)); bb_info_msg("Sending select for %s...", inet_ntoa(addr));
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex); SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
} }
@ -134,7 +135,7 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
packet.ciaddr = ciaddr; packet.ciaddr = ciaddr;
add_requests(&packet); add_requests(&packet);
LOG(LOG_DEBUG, "Sending renew..."); bb_info_msg("Sending renew...");
if (server) if (server)
ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
@ -155,7 +156,7 @@ int send_release(unsigned long server, unsigned long ciaddr)
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr); add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
add_simple_option(packet.options, DHCP_SERVER_ID, server); add_simple_option(packet.options, DHCP_SERVER_ID, server);
LOG(LOG_DEBUG, "Sending release..."); bb_info_msg("Sending release...");
return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT); return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
} }
@ -171,18 +172,18 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
memset(&packet, 0, sizeof(struct udp_dhcp_packet)); memset(&packet, 0, sizeof(struct udp_dhcp_packet));
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet)); bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
if (bytes < 0) { if (bytes < 0) {
DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring"); DEBUG("Couldn't read on raw listening socket - ignoring");
usleep(500000); /* possible down interface, looping condition */ usleep(500000); /* possible down interface, looping condition */
return -1; return -1;
} }
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) { if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
DEBUG(LOG_INFO, "message too short, ignoring"); DEBUG("Message too short, ignoring");
return -2; return -2;
} }
if (bytes < ntohs(packet.ip.tot_len)) { if (bytes < ntohs(packet.ip.tot_len)) {
DEBUG(LOG_INFO, "Truncated packet"); DEBUG("Truncated packet");
return -2; return -2;
} }
@ -194,7 +195,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) || packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
bytes > (int) sizeof(struct udp_dhcp_packet) || bytes > (int) sizeof(struct udp_dhcp_packet) ||
ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) { ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
DEBUG(LOG_INFO, "unrelated/bogus packet"); DEBUG("Unrelated/bogus packet");
return -2; return -2;
} }
@ -202,7 +203,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
check = packet.ip.check; check = packet.ip.check;
packet.ip.check = 0; packet.ip.check = 0;
if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) { if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
DEBUG(LOG_INFO, "bad IP header checksum, ignoring"); DEBUG("bad IP header checksum, ignoring");
return -1; return -1;
} }
@ -218,17 +219,17 @@ int get_raw_packet(struct dhcpMessage *payload, int fd)
packet.ip.daddr = dest; packet.ip.daddr = dest;
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */ packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
if (check && check != udhcp_checksum(&packet, bytes)) { if (check && check != udhcp_checksum(&packet, bytes)) {
DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring"); bb_error_msg("Packet with bad UDP checksum received, ignoring");
return -2; return -2;
} }
memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp))); memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
if (ntohl(payload->cookie) != DHCP_MAGIC) { if (ntohl(payload->cookie) != DHCP_MAGIC) {
LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring"); bb_error_msg("Received bogus message (bad magic) - ignoring");
return -2; return -2;
} }
DEBUG(LOG_INFO, "oooooh!!! got some!"); DEBUG("oooooh!!! got some!");
return bytes - (sizeof(packet.ip) + sizeof(packet.udp)); return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
} }

View File

@ -44,9 +44,9 @@ int raw_socket(int ifindex)
int fd; int fd;
struct sockaddr_ll sock; struct sockaddr_ll sock;
DEBUG(LOG_INFO, "Opening raw socket on ifindex %d", ifindex); DEBUG("Opening raw socket on ifindex %d", ifindex);
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %m"); bb_perror_msg("socket");
return -1; return -1;
} }
@ -54,7 +54,7 @@ int raw_socket(int ifindex)
sock.sll_protocol = htons(ETH_P_IP); sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex; sock.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) { if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %m"); bb_perror_msg("bind:");
close(fd); close(fd);
return -1; return -1;
} }

View File

@ -19,6 +19,7 @@
#include <paths.h> #include <paths.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <stdarg.h> #include <stdarg.h>
#include <syslog.h>
#include "common.h" #include "common.h"
#include "pidfile.h" #include "pidfile.h"
@ -33,7 +34,6 @@ long uptime(void)
return info.uptime; return info.uptime;
} }
/* /*
* This function makes sure our first socket calls * This function makes sure our first socket calls
* aren't going to fd 1 (printf badness...) and are * aren't going to fd 1 (printf badness...) and are
@ -41,77 +41,32 @@ long uptime(void)
*/ */
static inline void sanitize_fds(void) static inline void sanitize_fds(void)
{ {
int zero; int fd = open(bb_dev_null, O_RDWR, 0);
if ((zero = open(bb_dev_null, O_RDWR, 0)) < 0) if (fd < 0)
return; return;
while (zero < 3) while (fd < 3)
zero = dup(zero); fd = dup(fd);
close(zero); close(fd);
} }
void udhcp_background(const char *pidfile) void udhcp_background(const char *pidfile)
{ {
#ifdef __uClinux__ #ifdef __uClinux__
LOG(LOG_ERR, "Cannot background in uclinux (yet)"); bb_error_msg("Cannot background in uclinux (yet)");
#else /* __uClinux__ */ #else /* __uClinux__ */
int pid_fd; int pid_fd;
/* hold lock during fork. */ /* hold lock during fork. */
pid_fd = pidfile_acquire(pidfile); pid_fd = pidfile_acquire(pidfile);
setsid();
xdaemon(0, 0); xdaemon(0, 0);
daemonized++; daemonized++;
logmode &= ~LOGMODE_STDIO;
pidfile_write_release(pid_fd); pidfile_write_release(pid_fd);
#endif /* __uClinux__ */ #endif /* __uClinux__ */
} }
#ifdef CONFIG_FEATURE_UDHCP_SYSLOG
void udhcp_logging(int level, const char *fmt, ...)
{
va_list p;
va_list p2;
va_start(p, fmt);
__va_copy(p2, p);
if (!daemonized) {
vprintf(fmt, p);
putchar('\n');
}
vsyslog(level, fmt, p2);
va_end(p);
}
#else
static char *syslog_level_msg[] = {
[LOG_EMERG] = "EMERGENCY!",
[LOG_ALERT] = "ALERT!",
[LOG_CRIT] = "critical!",
[LOG_WARNING] = "warning",
[LOG_ERR] = "error",
[LOG_INFO] = "info",
[LOG_DEBUG] = "debug"
};
void udhcp_logging(int level, const char *fmt, ...)
{
va_list p;
va_start(p, fmt);
if (!daemonized) {
printf("%s, ", syslog_level_msg[level]);
vprintf(fmt, p);
putchar('\n');
}
va_end(p);
}
#endif
void udhcp_start_log_and_pid(const char *client_server, const char *pidfile) void udhcp_start_log_and_pid(const char *client_server, const char *pidfile)
{ {
int pid_fd; int pid_fd;
@ -126,8 +81,10 @@ void udhcp_start_log_and_pid(const char *client_server, const char *pidfile)
/* equivelent of doing a fflush after every \n */ /* equivelent of doing a fflush after every \n */
setlinebuf(stdout); setlinebuf(stdout);
if (ENABLE_FEATURE_UDHCP_SYSLOG) if (ENABLE_FEATURE_UDHCP_SYSLOG) {
openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0); openlog(client_server, LOG_PID, LOG_LOCAL0);
logmode |= LOGMODE_SYSLOG;
}
udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, BB_VER); bb_info_msg("%s (v%s) started", client_server, BB_VER);
} }

View File

@ -12,26 +12,12 @@
#include "libbb_udhcp.h" #include "libbb_udhcp.h"
enum syslog_levels {
LOG_EMERG = 0,
LOG_ALERT,
LOG_CRIT,
LOG_WARNING,
LOG_ERR,
LOG_INFO,
LOG_DEBUG
};
#include <syslog.h>
long uptime(void); long uptime(void);
#define LOG(level, str, args...) udhcp_logging(level, str, ## args)
#if ENABLE_FEATURE_UDHCP_DEBUG #if ENABLE_FEATURE_UDHCP_DEBUG
# define DEBUG(level, str, args...) LOG(level, str, ## args) # define DEBUG(str, args...) bb_info_msg(str, ## args)
#else #else
# define DEBUG(level, str, args...) do {;} while(0) # define DEBUG(str, args...) do {;} while(0)
#endif #endif
#endif #endif

View File

@ -65,7 +65,7 @@ struct client_config_t client_config = {
/* just a little helper */ /* just a little helper */
static void change_mode(int new_mode) static void change_mode(int new_mode)
{ {
DEBUG(LOG_INFO, "entering %s listen mode", DEBUG("entering %s listen mode",
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none"); new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
if (fd >= 0) close(fd); if (fd >= 0) close(fd);
fd = -1; fd = -1;
@ -76,7 +76,7 @@ static void change_mode(int new_mode)
/* perform a renew */ /* perform a renew */
static void perform_renew(void) static void perform_renew(void)
{ {
LOG(LOG_INFO, "Performing a DHCP renew"); bb_info_msg("Performing a DHCP renew");
switch (state) { switch (state) {
case BOUND: case BOUND:
change_mode(LISTEN_KERNEL); change_mode(LISTEN_KERNEL);
@ -114,12 +114,12 @@ static void perform_release(void)
temp_addr.s_addr = server_addr; temp_addr.s_addr = server_addr;
sprintf(buffer, "%s", inet_ntoa(temp_addr)); sprintf(buffer, "%s", inet_ntoa(temp_addr));
temp_addr.s_addr = requested_ip; temp_addr.s_addr = requested_ip;
LOG(LOG_INFO, "Unicasting a release of %s to %s", bb_info_msg("Unicasting a release of %s to %s",
inet_ntoa(temp_addr), buffer); inet_ntoa(temp_addr), buffer);
send_release(server_addr, requested_ip); /* unicast */ send_release(server_addr, requested_ip); /* unicast */
udhcp_run_script(NULL, "deconfig"); udhcp_run_script(NULL, "deconfig");
} }
LOG(LOG_INFO, "Entering released state"); bb_info_msg("Entering released state");
change_mode(LISTEN_NONE); change_mode(LISTEN_NONE);
state = RELEASED; state = RELEASED;
@ -310,14 +310,14 @@ int udhcpc_main(int argc, char *argv[])
else else
fd = raw_socket(client_config.ifindex); fd = raw_socket(client_config.ifindex);
if (fd < 0) { if (fd < 0) {
LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m"); bb_perror_msg("FATAL: couldn't listen on socket");
return 0; return 0;
} }
} }
max_fd = udhcp_sp_fd_set(&rfds, fd); max_fd = udhcp_sp_fd_set(&rfds, fd);
if (tv.tv_sec > 0) { if (tv.tv_sec > 0) {
DEBUG(LOG_INFO, "Waiting on select..."); DEBUG("Waiting on select...");
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
} else retval = 0; /* If we already timed out, fall through */ } else retval = 0; /* If we already timed out, fall through */
@ -338,10 +338,10 @@ int udhcpc_main(int argc, char *argv[])
} else { } else {
udhcp_run_script(NULL, "leasefail"); udhcp_run_script(NULL, "leasefail");
if (client_config.background_if_no_lease) { if (client_config.background_if_no_lease) {
LOG(LOG_INFO, "No lease, forking to background."); bb_info_msg("No lease, forking to background");
client_background(); client_background();
} else if (client_config.abort_if_no_lease) { } else if (client_config.abort_if_no_lease) {
LOG(LOG_INFO, "No lease, failing."); bb_info_msg("No lease, failing");
return 1; return 1;
} }
/* wait to try again */ /* wait to try again */
@ -372,7 +372,7 @@ int udhcpc_main(int argc, char *argv[])
/* Lease is starting to run out, time to enter renewing state */ /* Lease is starting to run out, time to enter renewing state */
state = RENEWING; state = RENEWING;
change_mode(LISTEN_KERNEL); change_mode(LISTEN_KERNEL);
DEBUG(LOG_INFO, "Entering renew state"); DEBUG("Entering renew state");
/* fall right through */ /* fall right through */
case RENEWING: case RENEWING:
/* Either set a new T1, or enter REBINDING state */ /* Either set a new T1, or enter REBINDING state */
@ -380,7 +380,7 @@ int udhcpc_main(int argc, char *argv[])
/* timed out, enter rebinding state */ /* timed out, enter rebinding state */
state = REBINDING; state = REBINDING;
timeout = now + (t2 - t1); timeout = now + (t2 - t1);
DEBUG(LOG_INFO, "Entering rebinding state"); DEBUG("Entering rebinding state");
} else { } else {
/* send a request packet */ /* send a request packet */
send_renew(xid, server_addr, requested_ip); /* unicast */ send_renew(xid, server_addr, requested_ip); /* unicast */
@ -394,7 +394,7 @@ int udhcpc_main(int argc, char *argv[])
if ((lease - t2) <= (lease / 14400 + 1)) { if ((lease - t2) <= (lease / 14400 + 1)) {
/* timed out, enter init state */ /* timed out, enter init state */
state = INIT_SELECTING; state = INIT_SELECTING;
LOG(LOG_INFO, "Lease lost, entering init state"); bb_info_msg("Lease lost, entering init state");
udhcp_run_script(NULL, "deconfig"); udhcp_run_script(NULL, "deconfig");
timeout = now; timeout = now;
packet_num = 0; packet_num = 0;
@ -420,25 +420,25 @@ int udhcpc_main(int argc, char *argv[])
else len = get_raw_packet(&packet, fd); else len = get_raw_packet(&packet, fd);
if (len == -1 && errno != EINTR) { if (len == -1 && errno != EINTR) {
DEBUG(LOG_INFO, "error on read, %m, reopening socket"); DEBUG("error on read, %s, reopening socket", strerror(errno));
change_mode(listen_mode); /* just close and reopen */ change_mode(listen_mode); /* just close and reopen */
} }
if (len < 0) continue; if (len < 0) continue;
if (packet.xid != xid) { if (packet.xid != xid) {
DEBUG(LOG_INFO, "Ignoring XID %lx (our xid is %lx)", DEBUG("Ignoring XID %lx (our xid is %lx)",
(unsigned long) packet.xid, xid); (unsigned long) packet.xid, xid);
continue; continue;
} }
/* Ignore packets that aren't for us */ /* Ignore packets that aren't for us */
if (memcmp(packet.chaddr, client_config.arp, 6)) { if (memcmp(packet.chaddr, client_config.arp, 6)) {
DEBUG(LOG_INFO, "packet does not have our chaddr -- ignoring"); DEBUG("Packet does not have our chaddr - ignoring");
continue; continue;
} }
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldnt get option from packet -- ignoring"); bb_error_msg("Couldnt get option from packet - ignoring");
continue; continue;
} }
@ -456,7 +456,7 @@ int udhcpc_main(int argc, char *argv[])
timeout = now; timeout = now;
packet_num = 0; packet_num = 0;
} else { } else {
DEBUG(LOG_ERR, "No server ID in message"); bb_error_msg("No server ID in message");
} }
} }
break; break;
@ -466,7 +466,7 @@ int udhcpc_main(int argc, char *argv[])
case REBINDING: case REBINDING:
if (*message == DHCPACK) { if (*message == DHCPACK) {
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) { if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
LOG(LOG_ERR, "No lease time with ACK, using 1 hour lease"); bb_error_msg("No lease time with ACK, using 1 hour lease");
lease = 60 * 60; lease = 60 * 60;
} else { } else {
memcpy(&lease, temp, 4); memcpy(&lease, temp, 4);
@ -479,7 +479,7 @@ int udhcpc_main(int argc, char *argv[])
/* little fixed point for n * .875 */ /* little fixed point for n * .875 */
t2 = (lease * 0x7) >> 3; t2 = (lease * 0x7) >> 3;
temp_addr.s_addr = packet.yiaddr; temp_addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "Lease of %s obtained, lease time %ld", bb_info_msg("Lease of %s obtained, lease time %ld",
inet_ntoa(temp_addr), lease); inet_ntoa(temp_addr), lease);
start = now; start = now;
timeout = t1 + start; timeout = t1 + start;
@ -496,7 +496,7 @@ int udhcpc_main(int argc, char *argv[])
} else if (*message == DHCPNAK) { } else if (*message == DHCPNAK) {
/* return to init state */ /* return to init state */
LOG(LOG_INFO, "Received DHCP NAK"); bb_info_msg("Received DHCP NAK");
udhcp_run_script(&packet, "nak"); udhcp_run_script(&packet, "nak");
if (state != REQUESTING) if (state != REQUESTING)
udhcp_run_script(NULL, "deconfig"); udhcp_run_script(NULL, "deconfig");
@ -519,14 +519,14 @@ int udhcpc_main(int argc, char *argv[])
perform_release(); perform_release();
break; break;
case SIGTERM: case SIGTERM:
LOG(LOG_INFO, "Received SIGTERM"); bb_info_msg("Received SIGTERM");
return 0; return 0;
} }
} else if (retval == -1 && errno == EINTR) { } else if (retval == -1 && errno == EINTR) {
/* a signal was caught */ /* a signal was caught */
} else { } else {
/* An error occured */ /* An error occured */
DEBUG(LOG_ERR, "Error on select"); bb_perror_msg("select");
} }
} }

View File

@ -66,7 +66,7 @@ int udhcpd_main(int argc, char *argv[])
/* Sanity check */ /* Sanity check */
num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1; num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
if (server_config.max_leases > num_ips) { if (server_config.max_leases > num_ips) {
LOG(LOG_ERR, "max_leases value (%lu) not sane, " bb_error_msg("max_leases value (%lu) not sane, "
"setting to %lu instead", "setting to %lu instead",
server_config.max_leases, num_ips); server_config.max_leases, num_ips);
server_config.max_leases = num_ips; server_config.max_leases = num_ips;
@ -90,7 +90,7 @@ int udhcpd_main(int argc, char *argv[])
if (server_socket < 0) if (server_socket < 0)
if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) { if ((server_socket = listen_socket(INADDR_ANY, SERVER_PORT, server_config.interface)) < 0) {
LOG(LOG_ERR, "FATAL: couldn't create server socket, %m"); bb_perror_msg("FATAL: couldn't create server socket");
return 2; return 2;
} }
@ -109,19 +109,19 @@ int udhcpd_main(int argc, char *argv[])
timeout_end = time(0) + server_config.auto_time; timeout_end = time(0) + server_config.auto_time;
continue; continue;
} else if (retval < 0 && errno != EINTR) { } else if (retval < 0 && errno != EINTR) {
DEBUG(LOG_INFO, "error on select"); DEBUG("error on select");
continue; continue;
} }
switch (udhcp_sp_read(&rfds)) { switch (udhcp_sp_read(&rfds)) {
case SIGUSR1: case SIGUSR1:
LOG(LOG_INFO, "Received a SIGUSR1"); bb_info_msg("Received a SIGUSR1");
write_leases(); write_leases();
/* why not just reset the timeout, eh */ /* why not just reset the timeout, eh */
timeout_end = time(0) + server_config.auto_time; timeout_end = time(0) + server_config.auto_time;
continue; continue;
case SIGTERM: case SIGTERM:
LOG(LOG_INFO, "Received a SIGTERM"); bb_info_msg("Received a SIGTERM");
return 0; return 0;
case 0: break; /* no signal */ case 0: break; /* no signal */
default: continue; /* signal or error (probably EINTR) */ default: continue; /* signal or error (probably EINTR) */
@ -129,7 +129,7 @@ int udhcpd_main(int argc, char *argv[])
if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */ if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
if (bytes == -1 && errno != EINTR) { if (bytes == -1 && errno != EINTR) {
DEBUG(LOG_INFO, "error on read, %m, reopening socket"); DEBUG("error on read, %s, reopening socket", strerror(errno));
close(server_socket); close(server_socket);
server_socket = -1; server_socket = -1;
} }
@ -137,7 +137,7 @@ int udhcpd_main(int argc, char *argv[])
} }
if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) { if ((state = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
DEBUG(LOG_ERR, "couldn't get option from packet, ignoring"); bb_error_msg("Couldn't get option from packet, ignoring");
continue; continue;
} }
@ -146,7 +146,7 @@ int udhcpd_main(int argc, char *argv[])
if(static_lease_ip) if(static_lease_ip)
{ {
printf("Found static lease: %x\n", static_lease_ip); bb_info_msg("Found static lease: %x", static_lease_ip);
memcpy(&static_lease.chaddr, &packet.chaddr, 16); memcpy(&static_lease.chaddr, &packet.chaddr, 16);
static_lease.yiaddr = static_lease_ip; static_lease.yiaddr = static_lease_ip;
@ -162,14 +162,14 @@ int udhcpd_main(int argc, char *argv[])
switch (state[0]) { switch (state[0]) {
case DHCPDISCOVER: case DHCPDISCOVER:
DEBUG(LOG_INFO,"received DISCOVER"); DEBUG("Received DISCOVER");
if (sendOffer(&packet) < 0) { if (sendOffer(&packet) < 0) {
LOG(LOG_ERR, "send OFFER failed"); bb_error_msg("Send OFFER failed");
} }
break; break;
case DHCPREQUEST: case DHCPREQUEST:
DEBUG(LOG_INFO, "received REQUEST"); DEBUG("received REQUEST");
requested = get_option(&packet, DHCP_REQUESTED_IP); requested = get_option(&packet, DHCP_REQUESTED_IP);
server_id = get_option(&packet, DHCP_SERVER_ID); server_id = get_option(&packet, DHCP_SERVER_ID);
@ -180,7 +180,7 @@ int udhcpd_main(int argc, char *argv[])
if (lease) { if (lease) {
if (server_id) { if (server_id) {
/* SELECTING State */ /* SELECTING State */
DEBUG(LOG_INFO, "server_id = %08x", ntohl(server_id_align)); DEBUG("server_id = %08x", ntohl(server_id_align));
if (server_id_align == server_config.server && requested && if (server_id_align == server_config.server && requested &&
requested_align == lease->yiaddr) { requested_align == lease->yiaddr) {
sendACK(&packet, lease->yiaddr); sendACK(&packet, lease->yiaddr);
@ -224,22 +224,22 @@ int udhcpd_main(int argc, char *argv[])
} }
break; break;
case DHCPDECLINE: case DHCPDECLINE:
DEBUG(LOG_INFO,"received DECLINE"); DEBUG("Received DECLINE");
if (lease) { if (lease) {
memset(lease->chaddr, 0, 16); memset(lease->chaddr, 0, 16);
lease->expires = time(0) + server_config.decline_time; lease->expires = time(0) + server_config.decline_time;
} }
break; break;
case DHCPRELEASE: case DHCPRELEASE:
DEBUG(LOG_INFO,"received RELEASE"); DEBUG("Received RELEASE");
if (lease) lease->expires = time(0); if (lease) lease->expires = time(0);
break; break;
case DHCPINFORM: case DHCPINFORM:
DEBUG(LOG_INFO,"received INFORM"); DEBUG("Received INFORM");
send_inform(&packet); send_inform(&packet);
break; break;
default: default:
LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]); bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
} }
} }

View File

@ -112,7 +112,7 @@ static void attach_option(struct option_set **opt_list, struct dhcp_option *opti
/* add it to an existing option */ /* add it to an existing option */
if ((existing = find_option(*opt_list, option->code))) { if ((existing = find_option(*opt_list, option->code))) {
DEBUG(LOG_INFO, "Attaching option %s to existing member of list", option->name); DEBUG("Attaching option %s to existing member of list", option->name);
if (option->flags & OPTION_LIST) { if (option->flags & OPTION_LIST) {
if (existing->data[OPT_LEN] + length <= 255) { if (existing->data[OPT_LEN] + length <= 255) {
existing->data = realloc(existing->data, existing->data = realloc(existing->data,
@ -122,7 +122,7 @@ static void attach_option(struct option_set **opt_list, struct dhcp_option *opti
} /* else, ignore the data, we could put this in a second option in the future */ } /* else, ignore the data, we could put this in a second option in the future */
} /* else, ignore the new data */ } /* else, ignore the new data */
} else { } else {
DEBUG(LOG_INFO, "Attaching option %s to list", option->name); DEBUG("Attaching option %s to list", option->name);
/* make a new option */ /* make a new option */
new = xmalloc(sizeof(struct option_set)); new = xmalloc(sizeof(struct option_set));
@ -286,7 +286,7 @@ int read_config(const char *file)
keywords[i].handler(keywords[i].def, keywords[i].var); keywords[i].handler(keywords[i].def, keywords[i].var);
if (!(in = fopen(file, "r"))) { if (!(in = fopen(file, "r"))) {
LOG(LOG_ERR, "unable to open config file: %s", file); bb_error_msg("Unable to open config file: %s", file);
return 0; return 0;
} }
@ -310,8 +310,9 @@ int read_config(const char *file)
for (i = 0; keywords[i].keyword[0]; i++) for (i = 0; keywords[i].keyword[0]; i++)
if (!strcasecmp(token, keywords[i].keyword)) if (!strcasecmp(token, keywords[i].keyword))
if (!keywords[i].handler(line, keywords[i].var)) { if (!keywords[i].handler(line, keywords[i].var)) {
LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file); bb_error_msg("Failure parsing line %d of %s", lm, file);
DEBUG(LOG_ERR, "unable to parse '%s'", debug_orig); if (ENABLE_FEATURE_UDHCP_DEBUG)
bb_error_msg("unable to parse '%s'", debug_orig);
/* reset back to the default value */ /* reset back to the default value */
keywords[i].handler(keywords[i].def, keywords[i].var); keywords[i].handler(keywords[i].def, keywords[i].var);
} }
@ -330,7 +331,7 @@ void write_leases(void)
unsigned long tmp_time; unsigned long tmp_time;
if (!(fp = fopen(server_config.lease_file, "w"))) { if (!(fp = fopen(server_config.lease_file, "w"))) {
LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file); bb_error_msg("Unable to open %s for writing", server_config.lease_file);
return; return;
} }
@ -368,7 +369,7 @@ void read_leases(const char *file)
struct dhcpOfferedAddr lease; struct dhcpOfferedAddr lease;
if (!(fp = fopen(file, "r"))) { if (!(fp = fopen(file, "r"))) {
LOG(LOG_ERR, "Unable to open %s for reading", file); bb_error_msg("Unable to open %s for reading", file);
return; return;
} }
@ -378,12 +379,12 @@ void read_leases(const char *file)
lease.expires = ntohl(lease.expires); lease.expires = ntohl(lease.expires);
if (!server_config.remaining) lease.expires -= time(0); if (!server_config.remaining) lease.expires -= time(0);
if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) { if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
LOG(LOG_WARNING, "Too many leases while loading %s\n", file); bb_error_msg("Too many leases while loading %s", file);
break; break;
} }
i++; i++;
} }
} }
DEBUG(LOG_INFO, "Read %d leases", i); DEBUG("Read %d leases", i);
fclose(fp); fclose(fp);
} }

View File

@ -113,7 +113,7 @@ static int check_ip(uint32_t addr)
if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) { if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
temp.s_addr = addr; temp.s_addr = addr;
LOG(LOG_INFO, "%s belongs to someone, reserving it for %ld seconds", bb_info_msg("%s belongs to someone, reserving it for %ld seconds",
inet_ntoa(temp), server_config.conflict_time); inet_ntoa(temp), server_config.conflict_time);
add_lease(blank_chaddr, addr, server_config.conflict_time); add_lease(blank_chaddr, addr, server_config.conflict_time);
return 1; return 1;

View File

@ -22,7 +22,6 @@
void udhcp_background(const char *pidfile); void udhcp_background(const char *pidfile);
void udhcp_start_log_and_pid(const char *client_server, const char *pidfile); void udhcp_start_log_and_pid(const char *client_server, const char *pidfile);
void udhcp_logging(int level, const char *fmt, ...);
void udhcp_run_script(struct dhcpMessage *packet, const char *name); void udhcp_run_script(struct dhcpMessage *packet, const char *name);

View File

@ -73,12 +73,12 @@ uint8_t *get_option(struct dhcpMessage *packet, int code)
length = 308; length = 308;
while (!done) { while (!done) {
if (i >= length) { if (i >= length) {
LOG(LOG_WARNING, "bogus packet, option fields too long."); bb_error_msg("Bogus packet, option fields too long");
return NULL; return NULL;
} }
if (optionptr[i + OPT_CODE] == code) { if (optionptr[i + OPT_CODE] == code) {
if (i + 1 + optionptr[i + OPT_LEN] >= length) { if (i + 1 + optionptr[i + OPT_LEN] >= length) {
LOG(LOG_WARNING, "bogus packet, option fields too long."); bb_error_msg("Bogus packet, option fields too long");
return NULL; return NULL;
} }
return optionptr + i + 2; return optionptr + i + 2;
@ -89,7 +89,7 @@ uint8_t *get_option(struct dhcpMessage *packet, int code)
break; break;
case DHCP_OPTION_OVER: case DHCP_OPTION_OVER:
if (i + 1 + optionptr[i + OPT_LEN] >= length) { if (i + 1 + optionptr[i + OPT_LEN] >= length) {
LOG(LOG_WARNING, "bogus packet, option fields too long."); bb_error_msg("Bogus packet, option fields too long");
return NULL; return NULL;
} }
over = optionptr[i + 3]; over = optionptr[i + 3];
@ -137,10 +137,11 @@ int add_option_string(uint8_t *optionptr, uint8_t *string)
/* end position + string length + option code/length + end option */ /* end position + string length + option code/length + end option */
if (end + string[OPT_LEN] + 2 + 1 >= 308) { if (end + string[OPT_LEN] + 2 + 1 >= 308) {
LOG(LOG_ERR, "Option 0x%02x did not fit into the packet!", string[OPT_CODE]); bb_error_msg("Option 0x%02x did not fit into the packet",
string[OPT_CODE]);
return 0; return 0;
} }
DEBUG(LOG_INFO, "adding option 0x%02x", string[OPT_CODE]); DEBUG("adding option 0x%02x", string[OPT_CODE]);
memcpy(optionptr + end, string, string[OPT_LEN] + 2); memcpy(optionptr + end, string, string[OPT_LEN] + 2);
optionptr[end + string[OPT_LEN] + 2] = DHCP_END; optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
return string[OPT_LEN] + 2; return string[OPT_LEN] + 2;
@ -167,6 +168,6 @@ int add_simple_option(uint8_t *optionptr, uint8_t code, uint32_t data)
} }
} }
DEBUG(LOG_ERR, "Could not add option 0x%02x", code); bb_error_msg("Could not add option 0x%02x", code);
return 0; return 0;
} }

View File

@ -58,21 +58,21 @@ int udhcp_get_packet(struct dhcpMessage *packet, int fd)
memset(packet, 0, sizeof(struct dhcpMessage)); memset(packet, 0, sizeof(struct dhcpMessage));
bytes = read(fd, packet, sizeof(struct dhcpMessage)); bytes = read(fd, packet, sizeof(struct dhcpMessage));
if (bytes < 0) { if (bytes < 0) {
DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring"); DEBUG("couldn't read on listening socket, ignoring");
return -1; return -1;
} }
if (ntohl(packet->cookie) != DHCP_MAGIC) { if (ntohl(packet->cookie) != DHCP_MAGIC) {
LOG(LOG_ERR, "received bogus message, ignoring"); bb_error_msg("Received bogus message, ignoring");
return -2; return -2;
} }
DEBUG(LOG_INFO, "Received a packet"); DEBUG("Received a packet");
if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) { if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
for (i = 0; broken_vendors[i][0]; i++) { for (i = 0; broken_vendors[i][0]; i++) {
if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) && if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
!strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) { !strncmp((char*)vendor, broken_vendors[i], vendor[OPT_LEN - 2])) {
DEBUG(LOG_INFO, "broken client (%s), forcing broadcast", DEBUG("broken client (%s), forcing broadcast",
broken_vendors[i]); broken_vendors[i]);
packet->flags |= htons(BROADCAST_FLAG); packet->flags |= htons(BROADCAST_FLAG);
} }
@ -123,7 +123,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source
struct udp_dhcp_packet packet; struct udp_dhcp_packet packet;
if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) {
DEBUG(LOG_ERR, "socket call failed: %m"); bb_perror_msg("socket");
return -1; return -1;
} }
@ -136,7 +136,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source
dest.sll_halen = 6; dest.sll_halen = 6;
memcpy(dest.sll_addr, dest_arp, 6); memcpy(dest.sll_addr, dest_arp, 6);
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) { if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
DEBUG(LOG_ERR, "bind call failed: %m"); bb_perror_msg("bind");
close(fd); close(fd);
return -1; return -1;
} }
@ -159,7 +159,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source
result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest)); result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
if (result <= 0) { if (result <= 0) {
DEBUG(LOG_ERR, "write on socket failed: %m"); bb_perror_msg("sendto");
} }
close(fd); close(fd);
return result; return result;

View File

@ -45,7 +45,7 @@ int pidfile_acquire(const char *pidfile)
pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644); pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
if (pid_fd < 0) { if (pid_fd < 0) {
LOG(LOG_ERR, "Unable to open pidfile %s: %m\n", pidfile); bb_perror_msg("Unable to open pidfile %s", pidfile);
} else { } else {
lockf(pid_fd, F_LOCK, 0); lockf(pid_fd, F_LOCK, 0);
if (!saved_pidfile) if (!saved_pidfile)

View File

@ -200,7 +200,7 @@ void udhcp_run_script(struct dhcpMessage *packet, const char *name)
if (client_config.script == NULL) if (client_config.script == NULL)
return; return;
DEBUG(LOG_INFO, "vforking and execle'ing %s", client_config.script); DEBUG("vfork'ing and execle'ing %s", client_config.script);
envp = fill_envp(packet); envp = fill_envp(packet);
/* call script */ /* call script */
@ -216,7 +216,7 @@ void udhcp_run_script(struct dhcpMessage *packet, const char *name)
/* exec script */ /* exec script */
execle(client_config.script, client_config.script, execle(client_config.script, client_config.script,
name, NULL, envp); name, NULL, envp);
LOG(LOG_ERR, "script %s failed: %m", client_config.script); bb_perror_msg("script %s failed", client_config.script);
exit(1); exit(1);
} }
} }

View File

@ -35,7 +35,7 @@
/* send a packet to giaddr using the kernel ip stack */ /* send a packet to giaddr using the kernel ip stack */
static int send_packet_to_relay(struct dhcpMessage *payload) static int send_packet_to_relay(struct dhcpMessage *payload)
{ {
DEBUG(LOG_INFO, "Forwarding packet to relay"); DEBUG("Forwarding packet to relay");
return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT, return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,
payload->giaddr, SERVER_PORT); payload->giaddr, SERVER_PORT);
@ -49,19 +49,19 @@ static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcas
uint32_t ciaddr; uint32_t ciaddr;
if (force_broadcast) { if (force_broadcast) {
DEBUG(LOG_INFO, "broadcasting packet to client (NAK)"); DEBUG("broadcasting packet to client (NAK)");
ciaddr = INADDR_BROADCAST; ciaddr = INADDR_BROADCAST;
chaddr = MAC_BCAST_ADDR; chaddr = MAC_BCAST_ADDR;
} else if (payload->ciaddr) { } else if (payload->ciaddr) {
DEBUG(LOG_INFO, "unicasting packet to client ciaddr"); DEBUG("unicasting packet to client ciaddr");
ciaddr = payload->ciaddr; ciaddr = payload->ciaddr;
chaddr = payload->chaddr; chaddr = payload->chaddr;
} else if (ntohs(payload->flags) & BROADCAST_FLAG) { } else if (ntohs(payload->flags) & BROADCAST_FLAG) {
DEBUG(LOG_INFO, "broadcasting packet to client (requested)"); DEBUG("broadcasting packet to client (requested)");
ciaddr = INADDR_BROADCAST; ciaddr = INADDR_BROADCAST;
chaddr = MAC_BCAST_ADDR; chaddr = MAC_BCAST_ADDR;
} else { } else {
DEBUG(LOG_INFO, "unicasting packet to client yiaddr"); DEBUG("unicasting packet to client yiaddr");
ciaddr = payload->yiaddr; ciaddr = payload->yiaddr;
chaddr = payload->chaddr; chaddr = payload->chaddr;
} }
@ -158,12 +158,12 @@ int sendOffer(struct dhcpMessage *oldpacket)
} }
if(!packet.yiaddr) { if(!packet.yiaddr) {
LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned"); bb_error_msg("No IP addresses to give - OFFER abandoned");
return -1; return -1;
} }
if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) { if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {
LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned"); bb_error_msg("Lease pool is full - OFFER abandoned");
return -1; return -1;
} }
@ -197,7 +197,7 @@ int sendOffer(struct dhcpMessage *oldpacket)
add_bootp_options(&packet); add_bootp_options(&packet);
addr.s_addr = packet.yiaddr; addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr)); bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
return send_packet(&packet, 0); return send_packet(&packet, 0);
} }
@ -208,7 +208,7 @@ int sendNAK(struct dhcpMessage *oldpacket)
init_packet(&packet, oldpacket, DHCPNAK); init_packet(&packet, oldpacket, DHCPNAK);
DEBUG(LOG_INFO, "sending NAK"); DEBUG("Sending NAK");
return send_packet(&packet, 1); return send_packet(&packet, 1);
} }
@ -245,7 +245,7 @@ int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
add_bootp_options(&packet); add_bootp_options(&packet);
addr.s_addr = packet.yiaddr; addr.s_addr = packet.yiaddr;
LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr)); bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
if (send_packet(&packet, 0) < 0) if (send_packet(&packet, 0) < 0)
return -1; return -1;

View File

@ -36,7 +36,7 @@ static int signal_pipe[2];
static void signal_handler(int sig) static void signal_handler(int sig)
{ {
if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0)
DEBUG(LOG_ERR, "Could not send signal: %m"); bb_perror_msg("Could not send signal");
} }

View File

@ -60,33 +60,33 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp)
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
our_ip = (struct sockaddr_in *) &ifr.ifr_addr; our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
*addr = our_ip->sin_addr.s_addr; *addr = our_ip->sin_addr.s_addr;
DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr));
} else { } else {
LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m"); bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?");
close(fd); close(fd);
return -1; return -1;
} }
} }
if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) {
DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex); DEBUG("adapter index %d", ifr.ifr_ifindex);
*ifindex = ifr.ifr_ifindex; *ifindex = ifr.ifr_ifindex;
} else { } else {
LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m"); bb_perror_msg("SIOCGIFINDEX failed");
close(fd); close(fd);
return -1; return -1;
} }
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); memcpy(arp, ifr.ifr_hwaddr.sa_data, 6);
DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x",
arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]);
} else { } else {
LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m"); bb_perror_msg("SIOCGIFHWADDR failed");
close(fd); close(fd);
return -1; return -1;
} }
} else { } else {
LOG(LOG_ERR, "socket failed!: %m"); bb_perror_msg("socket failed");
return -1; return -1;
} }
close(fd); close(fd);
@ -101,9 +101,9 @@ int listen_socket(uint32_t ip, int port, char *inf)
struct sockaddr_in addr; struct sockaddr_in addr;
int n = 1; int n = 1;
DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s", ip, port, inf); DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf);
if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
DEBUG(LOG_ERR, "socket call failed: %m"); bb_perror_msg("socket");
return -1; return -1;
} }

View File

@ -133,7 +133,7 @@ int vconfig_main(int argc, char **argv)
ifr.u.name_type = *xfind_str(name_types+1, argv[1]); ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
} else { } else {
if (strlen(argv[1]) >= IF_NAMESIZE) { if (strlen(argv[1]) >= IF_NAMESIZE) {
bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE); bb_error_msg_and_die("if_name >= %d chars", IF_NAMESIZE);
} }
strcpy(ifr.device1, argv[1]); strcpy(ifr.device1, argv[1]);
p = argv[2]; p = argv[2];

View File

@ -124,10 +124,7 @@ static void arp(int fd, struct sockaddr *saddr, int op,
// send it // send it
if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
if (FOREGROUND) bb_perror_msg("sendto");
perror("sendto");
else
syslog(LOG_ERR, "sendto: %s", strerror(errno));
//return -errno; //return -errno;
} }
// Currently all callers ignore errors, that's why returns are // Currently all callers ignore errors, that's why returns are
@ -148,8 +145,7 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip)
if (ip != NULL) { if (ip != NULL) {
char *addr = inet_ntoa(*ip); char *addr = inet_ntoa(*ip);
setenv("ip", addr, 1); setenv("ip", addr, 1);
if (!FOREGROUND) bb_info_msg("%s %s %s", arg, intf, addr);
syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
} }
pid = vfork(); pid = vfork();
@ -158,10 +154,7 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip)
goto bad; goto bad;
} else if (pid == 0) { // child } else if (pid == 0) { // child
execl(script, script, arg, NULL); execl(script, script, arg, NULL);
if (FOREGROUND) bb_perror_msg("execl");
perror("execl");
else
syslog(LOG_ERR, "execl: %s", strerror(errno));
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
@ -170,24 +163,15 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip)
goto bad; goto bad;
} }
if (WEXITSTATUS(status) != 0) { if (WEXITSTATUS(status) != 0) {
if (FOREGROUND) bb_error_msg("script %s failed, exit=%d",
bb_error_msg("script %s failed, exit=%d", script, WEXITSTATUS(status));
script, WEXITSTATUS(status));
else
syslog(LOG_ERR, "script %s failed, exit=%d",
script, WEXITSTATUS(status));
return -errno; return -errno;
} }
} }
return 0; return 0;
bad: bad:
status = -errno; status = -errno;
if (FOREGROUND) bb_perror_msg("%s %s, %s", arg, intf, why);
bb_perror_msg("%s %s, %s",
arg, intf, why);
else
syslog(LOG_ERR, "%s %s, %s: %s",
arg, intf, why, strerror(errno));
return status; return status;
} }
@ -235,6 +219,11 @@ int zcip_main(int argc, char *argv[])
char *r_opt; char *r_opt;
bb_opt_complementally = "vv:vf"; // -v accumulates and implies -f bb_opt_complementally = "vv:vf"; // -v accumulates and implies -f
opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose); opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose);
if (!FOREGROUND) {
/* Do it early, before all bb_xx_msg calls */
logmode = LOGMODE_SYSLOG;
openlog(bb_applet_name, 0, LOG_DAEMON);
}
if (opts & 4) { // -r n.n.n.n if (opts & 4) { // -r n.n.n.n
if (inet_aton(r_opt, &ip) == 0 if (inet_aton(r_opt, &ip) == 0
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) { || (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
@ -285,9 +274,9 @@ int zcip_main(int argc, char *argv[])
// daemonize now; don't delay system startup // daemonize now; don't delay system startup
if (!FOREGROUND) { if (!FOREGROUND) {
xdaemon(0, verbose); setsid();
openlog(bb_applet_name, 0, LOG_DAEMON); xdaemon(0, 0);
syslog(LOG_INFO, "start, interface %s", intf); bb_info_msg("start, interface %s", intf);
} }
// run the dynamic address negotiation protocol, // run the dynamic address negotiation protocol,
@ -557,10 +546,6 @@ int zcip_main(int argc, char *argv[])
} // switch poll } // switch poll
} }
bad: bad:
if (FOREGROUND) bb_perror_msg("%s, %s", intf, why);
perror(why);
else
syslog(LOG_ERR, "%s %s, %s error: %s",
bb_applet_name, intf, why, strerror(errno));
return EXIT_FAILURE; return EXIT_FAILURE;
} }