bb_xx_msg calls, and convert networking/* to it.
The rest of bbox will be converted gradually.
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_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_perror_nomsg_and_die(void) ATTRIBUTE_NORETURN;
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 */
-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_vinfo_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
extern int bb_echo(int argc, char** argv);
extern int bb_test(int argc, char** argv);
#define nameif_trivial_usage \
"[-s] [-c FILE] [{IFNAME MACADDR}]"
#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" \
"\t-c FILE\t\tUse configuration file (default is /etc/mactab)\n" \
"\t-s\t\tUse syslog (LOCAL0 facility)\n" \
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 \
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 \
xgethostbyname.c xgethostbyname2.c xreadlink.c xgetlarg.c \
get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \
}
} else {
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);
}
/*
/* only one conversion character if byte count */
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.");
}
}
/*
va_list p;
va_start(p, s);
- bb_verror_msg(s, p);
+ bb_verror_msg(s, p, NULL);
va_end(p);
- putc('\n', stderr);
}
va_list p;
va_start(p, s);
- bb_verror_msg(s, p);
+ bb_verror_msg(s, p, NULL);
va_end(p);
- putc('\n', stderr);
exit(bb_default_error_retval);
}
/* Grmpf. -FvK */
if (sin6->sin6_family != AF_INET6) {
#ifdef DEBUG
- bb_error_msg(_("rresolve: unsupport address family %d !\n"),
+ bb_error_msg(_("rresolve: unsupport address family %d!"),
sin6->sin6_family);
#endif
errno = EAFNOSUPPORT;
* Some systems default to HOME=/
*/
if ( chdir ( pw-> pw_dir )) {
- if ( chdir ( "/" )) {
- 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 /" );
- }
+ xchdir ( "/" );
fputs ( "warning: cannot change to home directory\n", stderr );
}
#include <errno.h>
#include <string.h>
#include <stdlib.h>
+#include <syslog.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);
- fprintf(stderr, "%s: ", bb_applet_name);
- vfprintf(stderr, s, p);
+ /* va_copy is used because it is not portable
+ * to use va_list p twice */
+ 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);
}
void bb_vherror_msg(const char *s, va_list p)
{
- if(s == 0)
- s = "";
- bb_verror_msg(s, p);
- if (*s)
- fputs(": ", stderr);
- herror("");
+ bb_verror_msg(s, p, hstrerror(h_errno));
}
void bb_vperror_msg(const char *s, va_list p)
{
- int err=errno;
- if(s == 0) s = "";
- bb_verror_msg(s, p);
- if (*s) s = ": ";
- fprintf(stderr, "%s%s\n", s, strerror(err));
+ bb_verror_msg(s, p, strerror(errno));
}
va_end(p);
#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;
}
#endif
setsid();
- openlog(bb_applet_name, 0, LOG_DAEMON);
return 1;
}
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(&G, 0, sizeof(G));
FD_ZERO(&G.readfds);
if (s < 0) {
if (errno != EINTR) /* EINTR */
- syslog(LOG_ERR, "accept: %s", strerror(errno));
+ bb_perror_msg("accept");
} else {
if (G.conncnt == MAXCONNS)
i = closeOldest();
config->rmt_ip & 0xff);
config->port = ntohs(fromAddr.sin_port);
#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);
#endif
#endif /* CONFIG_FEATURE_HTTPD_CGI */
static char line[1024];
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 (cp);
- syslog (LOG_ERR, "strdup: %m");
- exit (1);
+ return xstrdup (cp ? cp : "");
}
static int setconfig (void)
{
free (defhost);
- defhost = newstr ("*");
+ defhost = xstrdup ("*");
if (fconfig != NULL) {
fseek (fconfig, 0L, SEEK_SET);
return (1);
socklen_t size;
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;
}
size = sizeof ir_sin;
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);
return;
}
for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
(void) pmap_unset (sep->se_rpcprog, n);
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_rpcprog, n, pp->p_proto, ntohs (ir_sin.sin_port));
}
for (n = sep->se_rpcversl; n <= sep->se_rpcversh; 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 */
struct rlimit rl;
if (getrlimit (RLIMIT_NOFILE, &rl) < 0) {
- syslog (LOG_ERR, "getrlimit: %m");
+ bb_perror_msg ("getrlimit");
return -1;
}
rl.rlim_cur = MIN (rl.rlim_max, rl.rlim_cur + FD_CHUNK);
rl.rlim_cur = MIN (FD_SETSIZE, rl.rlim_cur + FD_CHUNK);
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);
return -1;
}
if (setrlimit (RLIMIT_NOFILE, &rl) < 0) {
- syslog (LOG_ERR, "setrlimit: %m");
+ bb_perror_msg ("setrlimit");
return -1;
}
int r;
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;
}
#define turnon(fd, opt) \
setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
if (turnon (sep->se_fd, SO_REUSEADDR) < 0)
- syslog (LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
+ bb_perror_msg ("setsockopt (SO_REUSEADDR)");
#undef turnon
#ifdef CONFIG_FEATURE_INETD_RPC
#endif
r = bind (sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size);
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);
close (sep->se_fd);
sep->se_fd = -1;
/* erp: */
if (*cpp == NULL) {
/* if (report) */
- /* syslog(LOG_ERR, "syntax error in inetd config file"); */
+ /* bb_error_msg ("syntax error in inetd config file"); */
return (NULL);
}
static servtab_t *new_servtab(void)
{
- servtab_t *sep;
-
- sep = (servtab_t *) malloc (sizeof (servtab_t));
- if (sep == NULL) {
- syslog (LOG_ERR, bb_msg_memory_exhausted);
- exit (1);
- }
- return sep;
+ return xmalloc (sizeof (servtab_t));
}
static servtab_t *dupconfig (servtab_t *sep)
newtab = new_servtab();
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_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
newtab->se_rpcprog = sep->se_rpcprog;
newtab->se_rpcversl = sep->se_rpcversl;
newtab->se_rpcversh = sep->se_rpcversh;
#endif
newtab->se_wait = sep->se_wait;
- newtab->se_user = sep->se_user ? newstr (sep->se_user) : NULL;
- newtab->se_group = sep->se_group ? newstr (sep->se_group) : NULL;
+ newtab->se_user = xstrdup (sep->se_user);
+ newtab->se_group = xstrdup (sep->se_group);
#ifdef INETD_FEATURE_ENABLED
newtab->se_bi = sep->se_bi;
#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++)
- newtab->se_argv[argc] = sep->se_argv[argc] ?
- newstr (sep->se_argv[argc]) : NULL;
+ newtab->se_argv[argc] = xstrdup (sep->se_argv[argc]);
newtab->se_max = sep->se_max;
return (newtab);
hostdelim = strrchr (arg, ':');
if (hostdelim) {
*hostdelim = '\0';
- sep->se_hostaddr = newstr (arg);
+ sep->se_hostaddr = xstrdup (arg);
arg = hostdelim + 1;
/*
* If the line is of the form `host:', then just change the
}
}
} else
- sep->se_hostaddr = newstr (defhost);
+ sep->se_hostaddr = xxstrdup (defhost);
- sep->se_service = newstr (arg);
+ sep->se_service = xxstrdup (arg);
arg = skip (&cp);
if (strcmp (arg, "stream") == 0)
else
sep->se_socktype = -1;
- sep->se_proto = newstr (skip (&cp));
+ sep->se_proto = xxstrdup (skip (&cp));
if (strcmp (sep->se_proto, "unix") == 0) {
sep->se_family = AF_UNIX;
#ifdef CONFIG_FEATURE_IPV6
sep->se_family = AF_INET6;
#else
- syslog (LOG_ERR, "%s: IPV6 not supported", sep->se_proto);
+ bb_error_msg ("%s: IPV6 not supported", sep->se_proto);
#endif
if (strncmp (sep->se_proto, "rpc/", 4) == 0) {
#ifdef CONFIG_FEATURE_INETD_RPC
p = strchr (sep->se_service, '/');
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;
}
*p++ = '\0';
l = strtol (p, &ccp, 0);
if (ccp == p || l < 0 || l > INT_MAX) {
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;
}
sep->se_rpcversl = sep->se_rpcversh = l;
} else if (*ccp != '\0')
goto badafterall;
#else
- syslog (LOG_ERR, "%s: rpc services not supported", sep->se_service);
+ bb_error_msg ("%s: rpc services not supported", sep->se_service);
#endif
}
}
sep->se_wait = strcmp (arg, "wait") == 0;
/* if ((arg = skip(&cp, 1)) == NULL) */
/* goto more; */
- sep->se_user = newstr (skip (&cp));
+ sep->se_user = xxstrdup (skip (&cp));
arg = strchr (sep->se_user, '.');
if (arg == NULL)
arg = strchr (sep->se_user, ':');
if (arg) {
*arg++ = '\0';
- sep->se_group = newstr (arg);
+ sep->se_group = xstrdup (arg);
}
/* if ((arg = skip(&cp, 1)) == NULL) */
/* goto more; */
- sep->se_server = newstr (skip (&cp));
+ sep->se_server = xxstrdup (skip (&cp));
if (strcmp (sep->se_server, "internal") == 0) {
#ifdef INETD_FEATURE_ENABLED
const struct builtin *bi;
strcmp (bi->bi_service, sep->se_service) == 0)
break;
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;
}
sep->se_bi = bi;
sep->se_wait = bi->bi_wait;
#else
- syslog (LOG_ERR, "internal service %s unknown", sep->se_service);
+ bb_perror_msg ("internal service %s unknown", sep->se_service);
goto more;
#endif
}
argc = 0;
for (arg = skip (&cp); cp; arg = skip (&cp)) {
if (argc < MAXARGV)
- sep->se_argv[argc++] = newstr (arg);
+ sep->se_argv[argc++] = xxstrdup (arg);
}
while (argc <= MAXARGV)
sep->se_argv[argc++] = NULL;
* and make a dup for the new entry.
*/
*hostdelim++ = '\0';
- nsep->se_hostaddr = newstr (hostdelim);
+ nsep->se_hostaddr = xstrdup (hostdelim);
nsep->se_next = sep->se_next;
sep->se_next = nsep;
hp = gethostbyname (nsep->se_hostaddr);
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;
goto skip;
} else if (hp->h_addrtype != AF_INET) {
- syslog (LOG_ERR,
- "%s: address isn't an Internet "
+ bb_error_msg ("%s: address isn't an Internet "
"address", nsep->se_hostaddr);
nsep->se_checked = 0;
goto skip;
hp->h_addr_list[0], sizeof (struct in_addr));
while (hp->h_addr_list[i] != NULL) {
psep = dupconfig (nsep);
- psep->se_hostaddr = newstr (nsep->se_hostaddr);
+ psep->se_hostaddr = xxstrdup (nsep->se_hostaddr);
psep->se_checked = 1;
memmove (&psep->se_ctrladdr_in.sin_addr,
hp->h_addr_list[i], sizeof (struct in_addr));
char protoname[10];
if (!setconfig ()) {
- syslog (LOG_ERR, "%s: %m", CONFIG);
+ bb_perror_msg ("%s", CONFIG);
return;
}
for (sep = servtab; sep; sep = sep->se_next)
if (sep->se_rpcprog == 0) {
rp = getrpcbyname (sep->se_service);
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;
}
sep->se_rpcprog = rp->r_number;
protoname[strlen (protoname) - 1] = '\0';
sp = getservbyname (sep->se_service, protoname);
if (sp == 0) {
- syslog (LOG_ERR,
- "%s/%s: unknown service", sep->se_service, sep->se_proto);
+ bb_error_msg ("%s/%s: unknown service",
+ sep->se_service, sep->se_proto);
goto serv_unknown;
}
port = sp->s_port;
if (sep->se_rpcprog == 0) {
rp = getrpcbyname (sep->se_service);
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;
}
sep->se_rpcprog = rp->r_number;
protoname[strlen (protoname) - 1] = '\0';
sp = getservbyname (sep->se_service, protoname);
if (sp == 0) {
- syslog (LOG_ERR,
- "%s/%s: unknown service", sep->se_service, sep->se_proto);
+ bb_error_msg ("%s/%s: unknown service",
+ sep->se_service, sep->se_proto);
goto serv_unknown;
}
port = sp->s_port;
for (sep = servtab; sep; sep = sep->se_next)
if (sep->se_wait == pid) {
if (WIFEXITED (status) && WEXITSTATUS (status))
- syslog (LOG_WARNING,
- "%s: exit status 0x%x",
+ bb_error_msg("%s: exit status 0x%x",
sep->se_server, WEXITSTATUS (status));
else if (WIFSIGNALED (status))
- syslog (LOG_WARNING,
- "%s: exit signal 0x%x", sep->se_server, WTERMSIG (status));
+ bb_error_msg("%s: exit signal 0x%x",
+ sep->se_server, WTERMSIG (status));
sep->se_wait = 1;
FD_SET (sep->se_fd, &allsock);
nsock++;
toomany = strtoul (stoomany, &e, 0);
if (!(toomany >= 0 && *e == '\0')) {
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;
} else {
setsid ();
}
+ logmode = LOGMODE_SYSLOG;
if (uid == 0) {
gid_t gid = getgid ();
}
if (getrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0) {
- syslog (LOG_ERR, "getrlimit: %m");
+ bb_perror_msg ("getrlimit");
} else {
rlim_ofile_cur = rlim_ofile.rlim_cur;
if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
readable = allsock;
if ((n = select (maxsock + 1, &readable, NULL, NULL, NULL)) <= 0) {
if (n < 0 && errno != EINTR) {
- syslog (LOG_WARNING, "select: %m");
+ bb_perror_msg("select");
sleep (1);
}
continue;
if (ctrl < 0) {
if (errno == EINTR)
continue;
- syslog (LOG_WARNING, "accept (for %s): %m", sep->se_service);
+ bb_perror_msg("accept (for %s)", sep->se_service);
continue;
}
if (sep->se_family == AF_INET && sep->se_socktype == SOCK_STREAM) {
socklen_t plen = sizeof (peer);
if (getpeername (ctrl, (struct sockaddr *) &peer, &plen) < 0) {
- syslog (LOG_WARNING, "could not getpeername");
+ bb_error_msg("could not getpeername");
close (ctrl);
continue;
}
--sep->se_count;
continue;
}
- syslog (LOG_ERR,
- "%s/%s server failing (looping), service terminated",
+ bb_error_msg ("%s/%s server failing (looping), service terminated",
sep->se_service, sep->se_proto);
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close (ctrl);
pid = fork ();
}
if (pid < 0) {
- syslog (LOG_ERR, "fork: %m");
+ bb_perror_msg ("fork");
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close (ctrl);
sigprocmask(SIG_UNBLOCK, &omask, NULL);
#endif
{
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)
recv (0, buf, sizeof (buf), 0);
_exit (1);
}
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) {
- 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)
recv (0, buf, sizeof (buf), 0);
_exit (1);
dup2 (0, 2);
if (rlim_ofile.rlim_cur != rlim_ofile_cur)
if (setrlimit (RLIMIT_NOFILE, &rlim_ofile) < 0)
- syslog (LOG_ERR, "setrlimit: %m");
+ bb_perror_msg ("setrlimit");
closelog ();
for (tmpint = rlim_ofile_cur - 1; --tmpint > 2;)
(void) close (tmpint);
execv (sep->se_server, sep->se_argv);
if (sep->se_socktype != SOCK_STREAM)
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);
}
}
unsigned int msk;
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));
}
netmask = 0;
if (argc == 2) {
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));
}
}
filter.flushed = 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;
}
if (filter.flushed == 0) {
struct ether_addr *mac;
} 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 */
static struct ether_addr *cc_macaddr(const char *str_macaddr)
{
lmac = ether_aton(str_macaddr);
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);
memcpy(mac, lmac, ETH_ALEN);
int if_index = 1;
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)
bb_show_usage();
char **a = argv + optind;
while (*a) {
-
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->ifname = xstrdup(*a++);
ch->mac = cc_macaddr(*a++);
ch = xzalloc(sizeof(mactable_t));
ch->ifname = xstrndup(line_ptr, name_length);
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 += strspn(line_ptr, " \t");
name_length = strspn(line_ptr, "0123456789ABCDEFabcdef:");
fclose(ifh);
}
- if ((ctl_sk = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
- serror("socket: %m");
+ ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
while (clist) {
struct ifreq ifr;
strcpy(ifr.ifr_newname, ch->ifname);
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);
/* Remove list entry of renamed interface */
pty = getpty(tty_name);
if (pty < 0) {
- syslog(LOG_ERR, "All terminals in use!");
+ bb_error_msg("all terminals in use");
return 0;
}
send_iac(ts, WILL, TELOPT_SGA);
if ((pid = fork()) < 0) {
- syslog(LOG_ERR, "Could not fork");
+ bb_perror_msg("fork");
}
if (pid == 0) {
/* In child, open the child's side of the tty. */
/* make new process group */
setsid();
- if (open(tty_name, O_RDWR /*| O_NOCTTY*/) < 0) {
- syslog(LOG_ERR, "Could not open tty");
- exit(1);
- }
+ xopen(tty_name, O_RDWR /*| O_NOCTTY*/);
dup(0);
dup(0);
execv(loginpath, (char *const *)argv_init);
/* NOT REACHED */
- syslog(LOG_ERR, "execv error");
- exit(1);
+ bb_perror_msg_and_die("execv");
}
ts->shell_pid = pid;
loginpath = DEFAULT_SHELL;
#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 (;;) {
c = getopt( argc, argv, options);
if (c == EOF) break;
}
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;
- openlog(bb_applet_name, 0, LOG_USER);
-
#ifdef CONFIG_FEATURE_TELNETD_INETD
maxfd = 1;
sessions = make_new_session();
} else
val = (int)strtol(str, &ep, 10);
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 (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
- 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)
- 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;
}
time_t prevTime;
- if ((s = socket (PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
- LOG(LOG_ERR, bb_msg_can_not_create_raw_socket);
+ if ((s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) {
+ bb_perror_msg(bb_msg_can_not_create_raw_socket);
return -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);
return -1;
}
FD_SET(s, &fdset);
tm.tv_sec = timeout;
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;
} else if (FD_ISSET(s, &fdset)) {
if (recv(s, &arp, sizeof(arp), 0) < 0 ) rv = 0;
if (arp.operation == htons(ARPOP_REPLY) &&
memcmp(arp.tHaddr, mac, 6) == 0 &&
*((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;
break;
}
prevTime = uptime();
}
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;
}
fd = open("/dev/urandom", 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);
}
if (fd >= 0) close(fd);
add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
add_requests(&packet);
- LOG(LOG_DEBUG, "Sending discover...");
+ bb_info_msg("Sending discover...");
return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
add_requests(&packet);
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,
SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
}
packet.ciaddr = ciaddr;
add_requests(&packet);
- LOG(LOG_DEBUG, "Sending renew...");
+ bb_info_msg("Sending renew...");
if (server)
ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
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);
}
memset(&packet, 0, sizeof(struct udp_dhcp_packet));
bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
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 */
return -1;
}
if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
- DEBUG(LOG_INFO, "message too short, ignoring");
+ DEBUG("Message too short, ignoring");
return -2;
}
if (bytes < ntohs(packet.ip.tot_len)) {
- DEBUG(LOG_INFO, "Truncated packet");
+ DEBUG("Truncated packet");
return -2;
}
packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
bytes > (int) sizeof(struct udp_dhcp_packet) ||
ntohs(packet.udp.len) != (uint16_t) (bytes - sizeof(packet.ip))) {
- DEBUG(LOG_INFO, "unrelated/bogus packet");
+ DEBUG("Unrelated/bogus packet");
return -2;
}
check = packet.ip.check;
packet.ip.check = 0;
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;
}
packet.ip.daddr = dest;
packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
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;
}
memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
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;
}
- DEBUG(LOG_INFO, "oooooh!!! got some!");
+ DEBUG("oooooh!!! got some!");
return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
}
int fd;
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) {
- DEBUG(LOG_ERR, "socket call failed: %m");
+ bb_perror_msg("socket");
return -1;
}
sock.sll_protocol = htons(ETH_P_IP);
sock.sll_ifindex = ifindex;
if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) {
- DEBUG(LOG_ERR, "bind call failed: %m");
+ bb_perror_msg("bind:");
close(fd);
return -1;
}
#include <paths.h>
#include <sys/socket.h>
#include <stdarg.h>
+#include <syslog.h>
#include "common.h"
#include "pidfile.h"
return info.uptime;
}
-
/*
* This function makes sure our first socket calls
* aren't going to fd 1 (printf badness...) and are
*/
static inline void sanitize_fds(void)
{
- int zero;
- if ((zero = open(bb_dev_null, O_RDWR, 0)) < 0)
+ int fd = open(bb_dev_null, O_RDWR, 0);
+ if (fd < 0)
return;
- while (zero < 3)
- zero = dup(zero);
- close(zero);
+ while (fd < 3)
+ fd = dup(fd);
+ close(fd);
}
void udhcp_background(const char *pidfile)
{
#ifdef __uClinux__
- LOG(LOG_ERR, "Cannot background in uclinux (yet)");
+ bb_error_msg("Cannot background in uclinux (yet)");
#else /* __uClinux__ */
int pid_fd;
/* hold lock during fork. */
pid_fd = pidfile_acquire(pidfile);
+ setsid();
xdaemon(0, 0);
daemonized++;
+ logmode &= ~LOGMODE_STDIO;
pidfile_write_release(pid_fd);
#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)
{
int pid_fd;
/* equivelent of doing a fflush after every \n */
setlinebuf(stdout);
- if (ENABLE_FEATURE_UDHCP_SYSLOG)
- openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
+ if (ENABLE_FEATURE_UDHCP_SYSLOG) {
+ 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);
}
#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);
-#define LOG(level, str, args...) udhcp_logging(level, str, ## args)
-
#if ENABLE_FEATURE_UDHCP_DEBUG
-# define DEBUG(level, str, args...) LOG(level, str, ## args)
+# define DEBUG(str, args...) bb_info_msg(str, ## args)
#else
-# define DEBUG(level, str, args...) do {;} while(0)
+# define DEBUG(str, args...) do {;} while(0)
#endif
#endif
/* just a little helper */
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");
if (fd >= 0) close(fd);
fd = -1;
/* perform a renew */
static void perform_renew(void)
{
- LOG(LOG_INFO, "Performing a DHCP renew");
+ bb_info_msg("Performing a DHCP renew");
switch (state) {
case BOUND:
change_mode(LISTEN_KERNEL);
temp_addr.s_addr = server_addr;
sprintf(buffer, "%s", inet_ntoa(temp_addr));
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);
send_release(server_addr, requested_ip); /* unicast */
udhcp_run_script(NULL, "deconfig");
}
- LOG(LOG_INFO, "Entering released state");
+ bb_info_msg("Entering released state");
change_mode(LISTEN_NONE);
state = RELEASED;
else
fd = raw_socket(client_config.ifindex);
if (fd < 0) {
- LOG(LOG_ERR, "FATAL: couldn't listen on socket, %m");
+ bb_perror_msg("FATAL: couldn't listen on socket");
return 0;
}
}
max_fd = udhcp_sp_fd_set(&rfds, fd);
if (tv.tv_sec > 0) {
- DEBUG(LOG_INFO, "Waiting on select...");
+ DEBUG("Waiting on select...");
retval = select(max_fd + 1, &rfds, NULL, NULL, &tv);
} else retval = 0; /* If we already timed out, fall through */
} else {
udhcp_run_script(NULL, "leasefail");
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();
} else if (client_config.abort_if_no_lease) {
- LOG(LOG_INFO, "No lease, failing.");
+ bb_info_msg("No lease, failing");
return 1;
}
/* wait to try again */
/* Lease is starting to run out, time to enter renewing state */
state = RENEWING;
change_mode(LISTEN_KERNEL);
- DEBUG(LOG_INFO, "Entering renew state");
+ DEBUG("Entering renew state");
/* fall right through */
case RENEWING:
/* Either set a new T1, or enter REBINDING state */
/* timed out, enter rebinding state */
state = REBINDING;
timeout = now + (t2 - t1);
- DEBUG(LOG_INFO, "Entering rebinding state");
+ DEBUG("Entering rebinding state");
} else {
/* send a request packet */
send_renew(xid, server_addr, requested_ip); /* unicast */
if ((lease - t2) <= (lease / 14400 + 1)) {
/* timed out, enter init state */
state = INIT_SELECTING;
- LOG(LOG_INFO, "Lease lost, entering init state");
+ bb_info_msg("Lease lost, entering init state");
udhcp_run_script(NULL, "deconfig");
timeout = now;
packet_num = 0;
else len = get_raw_packet(&packet, fd);
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 */
}
if (len < 0) continue;
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);
continue;
}
/* Ignore packets that aren't for us */
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;
}
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;
}
timeout = now;
packet_num = 0;
} else {
- DEBUG(LOG_ERR, "No server ID in message");
+ bb_error_msg("No server ID in message");
}
}
break;
case REBINDING:
if (*message == DHCPACK) {
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;
} else {
memcpy(&lease, temp, 4);
/* little fixed point for n * .875 */
t2 = (lease * 0x7) >> 3;
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);
start = now;
timeout = t1 + start;
} else if (*message == DHCPNAK) {
/* return to init state */
- LOG(LOG_INFO, "Received DHCP NAK");
+ bb_info_msg("Received DHCP NAK");
udhcp_run_script(&packet, "nak");
if (state != REQUESTING)
udhcp_run_script(NULL, "deconfig");
perform_release();
break;
case SIGTERM:
- LOG(LOG_INFO, "Received SIGTERM");
+ bb_info_msg("Received SIGTERM");
return 0;
}
} else if (retval == -1 && errno == EINTR) {
/* a signal was caught */
} else {
/* An error occured */
- DEBUG(LOG_ERR, "Error on select");
+ bb_perror_msg("select");
}
}
/* Sanity check */
num_ips = ntohl(server_config.end) - ntohl(server_config.start) + 1;
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",
server_config.max_leases, num_ips);
server_config.max_leases = num_ips;
if (server_socket < 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;
}
timeout_end = time(0) + server_config.auto_time;
continue;
} else if (retval < 0 && errno != EINTR) {
- DEBUG(LOG_INFO, "error on select");
+ DEBUG("error on select");
continue;
}
switch (udhcp_sp_read(&rfds)) {
case SIGUSR1:
- LOG(LOG_INFO, "Received a SIGUSR1");
+ bb_info_msg("Received a SIGUSR1");
write_leases();
/* why not just reset the timeout, eh */
timeout_end = time(0) + server_config.auto_time;
continue;
case SIGTERM:
- LOG(LOG_INFO, "Received a SIGTERM");
+ bb_info_msg("Received a SIGTERM");
return 0;
case 0: break; /* no signal */
default: continue; /* signal or error (probably EINTR) */
if ((bytes = udhcp_get_packet(&packet, server_socket)) < 0) { /* this waits for a packet - idle */
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);
server_socket = -1;
}
}
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;
}
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);
static_lease.yiaddr = static_lease_ip;
switch (state[0]) {
case DHCPDISCOVER:
- DEBUG(LOG_INFO,"received DISCOVER");
+ DEBUG("Received DISCOVER");
if (sendOffer(&packet) < 0) {
- LOG(LOG_ERR, "send OFFER failed");
+ bb_error_msg("Send OFFER failed");
}
break;
case DHCPREQUEST:
- DEBUG(LOG_INFO, "received REQUEST");
+ DEBUG("received REQUEST");
requested = get_option(&packet, DHCP_REQUESTED_IP);
server_id = get_option(&packet, DHCP_SERVER_ID);
if (lease) {
if (server_id) {
/* 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 &&
requested_align == lease->yiaddr) {
sendACK(&packet, lease->yiaddr);
}
break;
case DHCPDECLINE:
- DEBUG(LOG_INFO,"received DECLINE");
+ DEBUG("Received DECLINE");
if (lease) {
memset(lease->chaddr, 0, 16);
lease->expires = time(0) + server_config.decline_time;
}
break;
case DHCPRELEASE:
- DEBUG(LOG_INFO,"received RELEASE");
+ DEBUG("Received RELEASE");
if (lease) lease->expires = time(0);
break;
case DHCPINFORM:
- DEBUG(LOG_INFO,"received INFORM");
+ DEBUG("Received INFORM");
send_inform(&packet);
break;
default:
- LOG(LOG_WARNING, "unsupported DHCP message (%02x) -- ignoring", state[0]);
+ bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
}
}
/* add it to an existing option */
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 (existing->data[OPT_LEN] + length <= 255) {
existing->data = realloc(existing->data,
} /* else, ignore the data, we could put this in a second option in the future */
} /* else, ignore the new data */
} else {
- DEBUG(LOG_INFO, "Attaching option %s to list", option->name);
+ DEBUG("Attaching option %s to list", option->name);
/* make a new option */
new = xmalloc(sizeof(struct option_set));
keywords[i].handler(keywords[i].def, keywords[i].var);
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;
}
for (i = 0; keywords[i].keyword[0]; i++)
if (!strcasecmp(token, keywords[i].keyword))
if (!keywords[i].handler(line, keywords[i].var)) {
- LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
- DEBUG(LOG_ERR, "unable to parse '%s'", debug_orig);
+ bb_error_msg("Failure parsing line %d of %s", lm, file);
+ if (ENABLE_FEATURE_UDHCP_DEBUG)
+ bb_error_msg("unable to parse '%s'", debug_orig);
/* reset back to the default value */
keywords[i].handler(keywords[i].def, keywords[i].var);
}
unsigned long tmp_time;
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;
}
struct dhcpOfferedAddr lease;
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;
}
lease.expires = ntohl(lease.expires);
if (!server_config.remaining) lease.expires -= time(0);
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;
}
i++;
}
}
- DEBUG(LOG_INFO, "Read %d leases", i);
+ DEBUG("Read %d leases", i);
fclose(fp);
}
if (arpping(addr, server_config.server, server_config.arp, server_config.interface) == 0) {
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);
add_lease(blank_chaddr, addr, server_config.conflict_time);
return 1;
void udhcp_background(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);
length = 308;
while (!done) {
if (i >= length) {
- LOG(LOG_WARNING, "bogus packet, option fields too long.");
+ bb_error_msg("Bogus packet, option fields too long");
return NULL;
}
if (optionptr[i + OPT_CODE] == code) {
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 optionptr + i + 2;
break;
case DHCP_OPTION_OVER:
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;
}
over = optionptr[i + 3];
/* end position + string length + option code/length + end option */
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;
}
- 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);
optionptr[end + string[OPT_LEN] + 2] = DHCP_END;
return string[OPT_LEN] + 2;
}
}
- DEBUG(LOG_ERR, "Could not add option 0x%02x", code);
+ bb_error_msg("Could not add option 0x%02x", code);
return 0;
}
memset(packet, 0, sizeof(struct dhcpMessage));
bytes = read(fd, packet, sizeof(struct dhcpMessage));
if (bytes < 0) {
- DEBUG(LOG_INFO, "couldn't read on listening socket, ignoring");
+ DEBUG("couldn't read on listening socket, ignoring");
return -1;
}
if (ntohl(packet->cookie) != DHCP_MAGIC) {
- LOG(LOG_ERR, "received bogus message, ignoring");
+ bb_error_msg("Received bogus message, ignoring");
return -2;
}
- DEBUG(LOG_INFO, "Received a packet");
+ DEBUG("Received a packet");
if (packet->op == BOOTREQUEST && (vendor = get_option(packet, DHCP_VENDOR))) {
for (i = 0; broken_vendors[i][0]; i++) {
if (vendor[OPT_LEN - 2] == (uint8_t) strlen(broken_vendors[i]) &&
!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]);
packet->flags |= htons(BROADCAST_FLAG);
}
struct udp_dhcp_packet packet;
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;
}
dest.sll_halen = 6;
memcpy(dest.sll_addr, dest_arp, 6);
if (bind(fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_ll)) < 0) {
- DEBUG(LOG_ERR, "bind call failed: %m");
+ bb_perror_msg("bind");
close(fd);
return -1;
}
result = sendto(fd, &packet, sizeof(struct udp_dhcp_packet), 0, (struct sockaddr *) &dest, sizeof(dest));
if (result <= 0) {
- DEBUG(LOG_ERR, "write on socket failed: %m");
+ bb_perror_msg("sendto");
}
close(fd);
return result;
pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
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 {
lockf(pid_fd, F_LOCK, 0);
if (!saved_pidfile)
if (client_config.script == NULL)
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);
/* call script */
/* exec script */
execle(client_config.script, client_config.script,
name, NULL, envp);
- LOG(LOG_ERR, "script %s failed: %m", client_config.script);
+ bb_perror_msg("script %s failed", client_config.script);
exit(1);
}
}
/* send a packet to giaddr using the kernel ip stack */
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,
payload->giaddr, SERVER_PORT);
uint32_t ciaddr;
if (force_broadcast) {
- DEBUG(LOG_INFO, "broadcasting packet to client (NAK)");
+ DEBUG("broadcasting packet to client (NAK)");
ciaddr = INADDR_BROADCAST;
chaddr = MAC_BCAST_ADDR;
} else if (payload->ciaddr) {
- DEBUG(LOG_INFO, "unicasting packet to client ciaddr");
+ DEBUG("unicasting packet to client ciaddr");
ciaddr = payload->ciaddr;
chaddr = payload->chaddr;
} else if (ntohs(payload->flags) & BROADCAST_FLAG) {
- DEBUG(LOG_INFO, "broadcasting packet to client (requested)");
+ DEBUG("broadcasting packet to client (requested)");
ciaddr = INADDR_BROADCAST;
chaddr = MAC_BCAST_ADDR;
} else {
- DEBUG(LOG_INFO, "unicasting packet to client yiaddr");
+ DEBUG("unicasting packet to client yiaddr");
ciaddr = payload->yiaddr;
chaddr = payload->chaddr;
}
}
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;
}
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;
}
add_bootp_options(&packet);
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);
}
init_packet(&packet, oldpacket, DHCPNAK);
- DEBUG(LOG_INFO, "sending NAK");
+ DEBUG("Sending NAK");
return send_packet(&packet, 1);
}
add_bootp_options(&packet);
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)
return -1;
static void signal_handler(int sig)
{
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");
}
if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
our_ip = (struct sockaddr_in *) &ifr.ifr_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 {
- 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);
return -1;
}
}
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;
} else {
- LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m");
+ bb_perror_msg("SIOCGIFINDEX failed");
close(fd);
return -1;
}
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) {
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]);
} else {
- LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m");
+ bb_perror_msg("SIOCGIFHWADDR failed");
close(fd);
return -1;
}
} else {
- LOG(LOG_ERR, "socket failed!: %m");
+ bb_perror_msg("socket failed");
return -1;
}
close(fd);
struct sockaddr_in addr;
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) {
- DEBUG(LOG_ERR, "socket call failed: %m");
+ bb_perror_msg("socket");
return -1;
}
ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
} else {
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]);
p = argv[2];
// send it
if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
- if (FOREGROUND)
- perror("sendto");
- else
- syslog(LOG_ERR, "sendto: %s", strerror(errno));
+ bb_perror_msg("sendto");
//return -errno;
}
// Currently all callers ignore errors, that's why returns are
if (ip != NULL) {
char *addr = inet_ntoa(*ip);
setenv("ip", addr, 1);
- if (!FOREGROUND)
- syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
+ bb_info_msg("%s %s %s", arg, intf, addr);
}
pid = vfork();
goto bad;
} else if (pid == 0) { // child
execl(script, script, arg, NULL);
- if (FOREGROUND)
- perror("execl");
- else
- syslog(LOG_ERR, "execl: %s", strerror(errno));
+ bb_perror_msg("execl");
_exit(EXIT_FAILURE);
}
goto bad;
}
if (WEXITSTATUS(status) != 0) {
- if (FOREGROUND)
- bb_error_msg("script %s failed, exit=%d",
- script, WEXITSTATUS(status));
- else
- syslog(LOG_ERR, "script %s failed, exit=%d",
- script, WEXITSTATUS(status));
+ bb_error_msg("script %s failed, exit=%d",
+ script, WEXITSTATUS(status));
return -errno;
}
}
return 0;
bad:
status = -errno;
- if (FOREGROUND)
- bb_perror_msg("%s %s, %s",
- arg, intf, why);
- else
- syslog(LOG_ERR, "%s %s, %s: %s",
- arg, intf, why, strerror(errno));
+ bb_perror_msg("%s %s, %s", arg, intf, why);
return status;
}
char *r_opt;
bb_opt_complementally = "vv:vf"; // -v accumulates and implies -f
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 (inet_aton(r_opt, &ip) == 0
|| (ntohl(ip.s_addr) & IN_CLASSB_NET) != LINKLOCAL_ADDR) {
// daemonize now; don't delay system startup
if (!FOREGROUND) {
- xdaemon(0, verbose);
- openlog(bb_applet_name, 0, LOG_DAEMON);
- syslog(LOG_INFO, "start, interface %s", intf);
+ setsid();
+ xdaemon(0, 0);
+ bb_info_msg("start, interface %s", intf);
}
// run the dynamic address negotiation protocol,
} // switch poll
}
bad:
- if (FOREGROUND)
- perror(why);
- else
- syslog(LOG_ERR, "%s %s, %s error: %s",
- bb_applet_name, intf, why, strerror(errno));
+ bb_perror_msg("%s, %s", intf, why);
return EXIT_FAILURE;
}