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 was genuinely broken at the time (not related to our build system): scripts/config/mkconfigs.c (2005-08-01 - 2006-10-05 arasi) uretilen bbconfigopts.h icine .config satirlarini kacissiz, ham newline'larla gömüyordu - gecerli C degildi, busybox'un kendi bb_mkdep araci bile parse edemiyordu (7d219aab7 ile duzeltildi, mkconfigs shell scripte cevrildi)
No binary (build failed)
—
(log yok)
| @@ -112,6 +112,14 @@ extern void *llist_pop(llist_t **elm); | |
| 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))); | |
| @@ -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(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); | |
| @@ -2092,7 +2092,7 @@ USE_FEATURE_MDEV_CONFIG( \ | |
| #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" \ | |
| @@ -28,6 +28,7 @@ LIBBB-y:= \ | |
| 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 \ | |
| @@ -220,7 +220,7 @@ static void rewrite(FS * fs) | |
| } | |
| } 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); |
| } | |
| /* | |
| @@ -253,7 +253,7 @@ static void rewrite(FS * fs) | |
| /* 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."); |
| } | |
| } | |
| /* | |
| @@ -18,7 +18,6 @@ void bb_error_msg(const char *s, ...) | |
| va_list p; | |
| va_start(p, s); | |
| - | bb_verror_msg(s, p); |
| + | bb_verror_msg(s, p, NULL); |
| va_end(p); | |
| - | putc('\n', stderr); |
| } | |
| @@ -18,8 +18,7 @@ void bb_error_msg_and_die(const char *s, ...) | |
| 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); | |
| } | |
| @@ -205,7 +205,7 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, | |
| /* 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; | |
| @@ -60,10 +60,7 @@ void setup_environment ( const char *shell, int loginshell, int changeenv, const | |
| * 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 ); | |
| } | |
| @@ -11,11 +11,37 @@ | |
| #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); |
| } | |
| @@ -16,10 +16,5 @@ | |
| 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)); |
| } | |
| @@ -15,9 +15,5 @@ | |
| 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)); |
| } | |
| @@ -397,7 +397,7 @@ char *xasprintf(const char *format, ...) | |
| 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 | |
| @@ -157,7 +157,6 @@ static int godaemon(void) | |
| setsid(); | |
| - | openlog(bb_applet_name, 0, LOG_DAEMON); |
| return 1; | |
| } | |
| @@ -219,6 +218,10 @@ static int checkInput(char *buf, int len, int l) | |
| 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); | |
| @@ -286,7 +289,7 @@ deleteconn: | |
| 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(); | |
| @@ -1809,7 +1809,7 @@ static int miniHttpd(int server) | |
| 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 */ | |
| @@ -311,18 +311,19 @@ static FILE *fconfig; | |
| 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); | |
| @@ -350,12 +351,12 @@ static void register_rpc (servtab_t *sep) | |
| 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; | |
| } | |
| @@ -363,7 +364,7 @@ static void register_rpc (servtab_t *sep) | |
| 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)); | |
| } | |
| @@ -375,7 +376,7 @@ static void unregister_rpc (servtab_t *sep) | |
| 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 */ | |
| @@ -401,19 +402,19 @@ static int bump_nofile (void) | |
| 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; | |
| } | |
| @@ -427,13 +428,13 @@ static void setup (servtab_t *sep) | |
| 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 | |
| @@ -467,7 +468,7 @@ setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on)) | |
| #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; | |
| @@ -510,7 +511,7 @@ static char *skip (char **cpp) /* int report; */ | |
| /* 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); | |
| } | |
| @@ -543,14 +544,7 @@ again: | |
| 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) | |
| @@ -560,26 +554,25 @@ 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); | |
| @@ -617,7 +610,7 @@ more: | |
| 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 | |
| @@ -632,9 +625,9 @@ more: | |
| } | |
| } | |
| } 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) | |
| @@ -650,7 +643,7 @@ more: | |
| 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; | |
| @@ -660,7 +653,7 @@ more: | |
| #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 | |
| @@ -669,14 +662,14 @@ more: | |
| 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; | |
| @@ -689,7 +682,7 @@ more: | |
| } 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 | |
| } | |
| } | |
| @@ -708,18 +701,18 @@ more: | |
| 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; | |
| @@ -729,13 +722,13 @@ more: | |
| 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 | |
| } | |
| @@ -746,7 +739,7 @@ more: | |
| 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; | |
| @@ -764,7 +757,7 @@ more: | |
| * 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; | |
| @@ -781,12 +774,11 @@ more: | |
| 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; | |
| @@ -797,7 +789,7 @@ more: | |
| 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)); | |
| @@ -913,7 +905,7 @@ static void config (int sig ATTRIBUTE_UNUSED) | |
| 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) | |
| @@ -989,7 +981,7 @@ static void config (int sig ATTRIBUTE_UNUSED) | |
| 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; | |
| @@ -1009,8 +1001,8 @@ static void config (int sig ATTRIBUTE_UNUSED) | |
| 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; | |
| @@ -1042,7 +1034,7 @@ static void config (int sig ATTRIBUTE_UNUSED) | |
| 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; | |
| @@ -1062,8 +1054,8 @@ static void config (int sig ATTRIBUTE_UNUSED) | |
| 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; | |
| @@ -1137,12 +1129,11 @@ static void reapchild (int sig ATTRIBUTE_UNUSED) | |
| 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++; | |
| @@ -1271,7 +1262,7 @@ inetd_main (int argc, char *argv[]) | |
| 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; | |
| @@ -1295,6 +1286,7 @@ inetd_main (int argc, char *argv[]) | |
| } else { | |
| setsid (); | |
| } | |
| + | logmode = LOGMODE_SYSLOG; |
| if (uid == 0) { | |
| gid_t gid = getgid (); | |
| @@ -1313,7 +1305,7 @@ inetd_main (int argc, char *argv[]) | |
| } | |
| 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) /* ! */ | |
| @@ -1365,7 +1357,7 @@ inetd_main (int argc, char *argv[]) | |
| 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; | |
| @@ -1378,7 +1370,7 @@ inetd_main (int argc, char *argv[]) | |
| 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) { | |
| @@ -1386,7 +1378,7 @@ inetd_main (int argc, char *argv[]) | |
| 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; | |
| } | |
| @@ -1426,8 +1418,7 @@ inetd_main (int argc, char *argv[]) | |
| --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); | |
| @@ -1447,7 +1438,7 @@ inetd_main (int argc, char *argv[]) | |
| 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); | |
| @@ -1468,15 +1459,15 @@ inetd_main (int argc, char *argv[]) | |
| #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); | |
| @@ -1502,7 +1493,7 @@ inetd_main (int argc, char *argv[]) | |
| 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); | |
| @@ -1510,7 +1501,7 @@ inetd_main (int argc, char *argv[]) | |
| 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); | |
| } | |
| } | |
| @@ -119,7 +119,7 @@ int ipcalc_main(int argc, char **argv) | |
| 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; | |
| @@ -149,7 +149,7 @@ int ipcalc_main(int argc, char **argv) | |
| 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)); | |
| } | |
| @@ -608,7 +608,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush) | |
| } | |
| 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) { | |
| @@ -41,29 +41,6 @@ typedef struct mactable_s { | |
| 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) | |
| { | |
| @@ -71,7 +48,7 @@ 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); | |
| @@ -88,7 +65,10 @@ int nameif_main(int argc, char **argv) | |
| 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(); | |
| @@ -97,9 +77,9 @@ int nameif_main(int argc, char **argv) | |
| 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++); | |
| @@ -124,7 +104,8 @@ int nameif_main(int argc, char **argv) | |
| 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:"); | |
| @@ -139,8 +120,7 @@ int nameif_main(int argc, char **argv) | |
| 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; | |
| @@ -168,7 +148,7 @@ int nameif_main(int argc, char **argv) | |
| 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 */ | |
| @@ -263,7 +263,7 @@ make_new_session(int sockfd) | |
| pty = getpty(tty_name); | |
| if (pty < 0) { | |
| - | syslog(LOG_ERR, "All terminals in use!"); |
| + | bb_error_msg("all terminals in use"); |
| return 0; | |
| } | |
| @@ -285,7 +285,7 @@ make_new_session(int sockfd) | |
| 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. */ | |
| @@ -296,10 +296,7 @@ make_new_session(int sockfd) | |
| /* 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); | |
| @@ -323,8 +320,7 @@ make_new_session(int sockfd) | |
| 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; | |
| @@ -390,6 +386,14 @@ telnetd_main(int argc, char **argv) | |
| 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; | |
| @@ -415,13 +419,11 @@ telnetd_main(int argc, char **argv) | |
| } | |
| 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(); | |
| @@ -515,16 +515,16 @@ str2val(const char *str, const char *what, int mi, int ma) | |
| } 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; | |
| } | |
| @@ -43,13 +43,13 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) | |
| 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; | |
| } | |
| @@ -81,14 +81,14 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) | |
| 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; | |
| } | |
| @@ -97,6 +97,6 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) | |
| 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; | |
| } | |
| @@ -44,7 +44,8 @@ unsigned long random_xid(void) | |
| 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); | |
| @@ -97,7 +98,7 @@ int send_discover(unsigned long xid, unsigned long requested) | |
| 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); | |
| } | |
| @@ -117,7 +118,7 @@ int send_selecting(unsigned long xid, unsigned long server, unsigned long reques | |
| 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); | |
| } | |
| @@ -134,7 +135,7 @@ int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr) | |
| 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, | |
| @@ -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_SERVER_ID, server); | |
| - | LOG(LOG_DEBUG, "Sending release..."); |
| + | bb_info_msg("Sending release..."); |
| 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)); | |
| 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; | |
| } | |
| @@ -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) || | |
| 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; | |
| } | |
| @@ -202,7 +203,7 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |
| 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; | |
| } | |
| @@ -218,17 +219,17 @@ int get_raw_packet(struct dhcpMessage *payload, int fd) | |
| 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)); | |
| } | |
| @@ -44,9 +44,9 @@ int raw_socket(int ifindex) | |
| 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; | |
| } | |
| @@ -54,7 +54,7 @@ int raw_socket(int ifindex) | |
| 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; | |
| } | |
| @@ -19,6 +19,7 @@ | |
| #include <paths.h> | |
| #include <sys/socket.h> | |
| #include <stdarg.h> | |
| + | #include <syslog.h> |
| #include "common.h" | |
| #include "pidfile.h" | |
| @@ -33,7 +34,6 @@ long uptime(void) | |
| return info.uptime; | |
| } | |
| - | |
| /* | |
| * This function makes sure our first socket calls | |
| * aren't going to fd 1 (printf badness...) and are | |
| @@ -41,77 +41,32 @@ long uptime(void) | |
| */ | |
| 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; | |
| @@ -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 */ | |
| 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); |
| } | |
| @@ -12,26 +12,12 @@ | |
| #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 | |
| @@ -65,7 +65,7 @@ struct client_config_t client_config = { | |
| /* 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; | |
| @@ -76,7 +76,7 @@ static void change_mode(int new_mode) | |
| /* 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); | |
| @@ -114,12 +114,12 @@ static void perform_release(void) | |
| 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; | |
| @@ -310,14 +310,14 @@ int udhcpc_main(int argc, char *argv[]) | |
| 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 */ | |
| @@ -338,10 +338,10 @@ int udhcpc_main(int argc, char *argv[]) | |
| } 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 */ | |
| @@ -372,7 +372,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| /* 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 */ | |
| @@ -380,7 +380,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| /* 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 */ | |
| @@ -394,7 +394,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| 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; | |
| @@ -420,25 +420,25 @@ int udhcpc_main(int argc, char *argv[]) | |
| 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; | |
| } | |
| @@ -456,7 +456,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| timeout = now; | |
| packet_num = 0; | |
| } else { | |
| - | DEBUG(LOG_ERR, "No server ID in message"); |
| + | bb_error_msg("No server ID in message"); |
| } | |
| } | |
| break; | |
| @@ -466,7 +466,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| 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); | |
| @@ -479,7 +479,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| /* 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; | |
| @@ -496,7 +496,7 @@ int udhcpc_main(int argc, char *argv[]) | |
| } 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"); | |
| @@ -519,14 +519,14 @@ int udhcpc_main(int argc, char *argv[]) | |
| 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"); |
| } | |
| } | |
| @@ -66,7 +66,7 @@ int udhcpd_main(int argc, char *argv[]) | |
| /* 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; | |
| @@ -90,7 +90,7 @@ int udhcpd_main(int argc, char *argv[]) | |
| 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; | |
| } | |
| @@ -109,19 +109,19 @@ int udhcpd_main(int argc, char *argv[]) | |
| 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) */ | |
| @@ -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 == -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; | |
| } | |
| @@ -137,7 +137,7 @@ int udhcpd_main(int argc, char *argv[]) | |
| } | |
| 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; | |
| } | |
| @@ -146,7 +146,7 @@ int udhcpd_main(int argc, char *argv[]) | |
| 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; | |
| @@ -162,14 +162,14 @@ int udhcpd_main(int argc, char *argv[]) | |
| 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); | |
| @@ -180,7 +180,7 @@ int udhcpd_main(int argc, char *argv[]) | |
| 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); | |
| @@ -224,22 +224,22 @@ int udhcpd_main(int argc, char *argv[]) | |
| } | |
| 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]); |
| } | |
| } | |
| @@ -112,7 +112,7 @@ static void attach_option(struct option_set **opt_list, struct dhcp_option *opti | |
| /* 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, | |
| @@ -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 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)); | |
| @@ -286,7 +286,7 @@ int read_config(const char *file) | |
| 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; | |
| } | |
| @@ -310,8 +310,9 @@ int read_config(const char *file) | |
| 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); | |
| } | |
| @@ -330,7 +331,7 @@ void write_leases(void) | |
| 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; | |
| } | |
| @@ -368,7 +369,7 @@ void read_leases(const char *file) | |
| 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; | |
| } | |
| @@ -378,12 +379,12 @@ void read_leases(const char *file) | |
| 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); | |
| } | |
| @@ -113,7 +113,7 @@ static int check_ip(uint32_t addr) | |
| 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; | |
| @@ -22,7 +22,6 @@ | |
| 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); | |
| @@ -73,12 +73,12 @@ uint8_t *get_option(struct dhcpMessage *packet, int code) | |
| 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; | |
| @@ -89,7 +89,7 @@ uint8_t *get_option(struct dhcpMessage *packet, int code) | |
| 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]; | |
| @@ -137,10 +137,11 @@ int add_option_string(uint8_t *optionptr, uint8_t *string) | |
| /* 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; | |
| @@ -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; | |
| } | |
| @@ -58,21 +58,21 @@ int udhcp_get_packet(struct dhcpMessage *packet, int fd) | |
| 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); | |
| } | |
| @@ -123,7 +123,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source | |
| 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; | |
| } | |
| @@ -136,7 +136,7 @@ int udhcp_raw_packet(struct dhcpMessage *payload, uint32_t source_ip, int source | |
| 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; | |
| } | |
| @@ -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)); | |
| if (result <= 0) { | |
| - | DEBUG(LOG_ERR, "write on socket failed: %m"); |
| + | bb_perror_msg("sendto"); |
| } | |
| close(fd); | |
| return result; | |
| @@ -45,7 +45,7 @@ int pidfile_acquire(const char *pidfile) | |
| 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) | |
| @@ -200,7 +200,7 @@ void udhcp_run_script(struct dhcpMessage *packet, const char *name) | |
| 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 */ | |
| @@ -216,7 +216,7 @@ void udhcp_run_script(struct dhcpMessage *packet, const char *name) | |
| /* 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); | |
| } | |
| } | |
| @@ -35,7 +35,7 @@ | |
| /* 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); | |
| @@ -49,19 +49,19 @@ static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcas | |
| 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; | |
| } | |
| @@ -158,12 +158,12 @@ int sendOffer(struct dhcpMessage *oldpacket) | |
| } | |
| 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; | |
| } | |
| @@ -197,7 +197,7 @@ int sendOffer(struct dhcpMessage *oldpacket) | |
| 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); | |
| } | |
| @@ -208,7 +208,7 @@ int sendNAK(struct dhcpMessage *oldpacket) | |
| init_packet(&packet, oldpacket, DHCPNAK); | |
| - | DEBUG(LOG_INFO, "sending NAK"); |
| + | DEBUG("Sending NAK"); |
| return send_packet(&packet, 1); | |
| } | |
| @@ -245,7 +245,7 @@ int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr) | |
| 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; | |
| @@ -36,7 +36,7 @@ static int signal_pipe[2]; | |
| 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"); |
| } | |
| @@ -60,33 +60,33 @@ int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) | |
| 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); | |
| @@ -101,9 +101,9 @@ int listen_socket(uint32_t ip, int port, char *inf) | |
| 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; | |
| } | |
| @@ -133,7 +133,7 @@ int vconfig_main(int argc, char **argv) | |
| 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]; | |
| @@ -124,10 +124,7 @@ static void arp(int fd, struct sockaddr *saddr, int op, | |
| // 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 | |
| @@ -148,8 +145,7 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip) | |
| 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(); | |
| @@ -158,10 +154,7 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip) | |
| 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); | |
| } | |
| @@ -170,24 +163,15 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip) | |
| 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; | |
| } | |
| @@ -235,6 +219,11 @@ int zcip_main(int argc, char *argv[]) | |
| 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) { | |
| @@ -285,9 +274,9 @@ int zcip_main(int argc, char *argv[]) | |
| // 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, | |
| @@ -557,10 +546,6 @@ int zcip_main(int argc, char *argv[]) | |
| } // 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; | |
| } | |