zcip: fix stdout/err versus syslog output.

Incomplete: xfunc() would not respect this. TODO.
This commit is contained in:
Denis Vlasenko 2006-09-03 12:28:32 +00:00
parent 65dbd8752b
commit a9abecd85e

View File

@ -76,6 +76,10 @@ enum {
do { } while (0) do { } while (0)
#define VDBG DBG #define VDBG DBG
static unsigned long opts;
#define FOREGROUND (opts & 1)
#define QUIT (opts & 2)
/** /**
* Pick a random link local IP address on 169.254/16, except that * Pick a random link local IP address on 169.254/16, except that
* the first and last 256 addresses are reserved. * the first and last 256 addresses are reserved.
@ -119,7 +123,10 @@ static int arp(int fd, struct sockaddr *saddr, int op,
// send it // send it
if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) { if (sendto(fd, &p, sizeof (p), 0, saddr, sizeof (*saddr)) < 0) {
if (FOREGROUND)
perror("sendto"); perror("sendto");
else
syslog(LOG_ERR, "sendto: %s", strerror(errno));
return -errno; return -errno;
} }
return 0; return 0;
@ -127,18 +134,19 @@ static int arp(int fd, struct sockaddr *saddr, int op,
/** /**
* Run a script. * Run a script.
* TODO: sort out stderr/syslog reporting. * TODO: we need a flag to direct bb_[p]error_msg output to stderr.
*/ */
static int run(char *script, char *arg, char *intf, struct in_addr *ip) static int run(char *script, char *arg, char *intf, struct in_addr *ip)
{ {
int pid, status; int pid, status;
char *why; char *why;
if (script != NULL) { if(1) { //always true: if (script != NULL)
VDBG("%s run %s %s\n", intf, script, arg); VDBG("%s run %s %s\n", intf, script, arg);
if (ip != NULL) { if (ip != NULL) {
char *addr = inet_ntoa(*ip); char *addr = inet_ntoa(*ip);
setenv("ip", addr, 1); setenv("ip", addr, 1);
if (!FOREGROUND)
syslog(LOG_INFO, "%s %s %s", arg, intf, addr); syslog(LOG_INFO, "%s %s %s", arg, intf, addr);
} }
@ -148,7 +156,10 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip)
goto bad; goto bad;
} else if (pid == 0) { // child } else if (pid == 0) { // child
execl(script, script, arg, NULL); execl(script, script, arg, NULL);
if (FOREGROUND)
perror("execl"); perror("execl");
else
syslog(LOG_ERR, "execl: %s", strerror(errno));
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
@ -157,15 +168,23 @@ static int run(char *script, char *arg, char *intf, struct in_addr *ip)
goto bad; goto bad;
} }
if (WEXITSTATUS(status) != 0) { if (WEXITSTATUS(status) != 0) {
if (FOREGROUND)
bb_error_msg("script %s failed, exit=%d\n", bb_error_msg("script %s failed, exit=%d\n",
script, WEXITSTATUS(status)); script, WEXITSTATUS(status));
else
syslog(LOG_ERR, "script %s failed, exit=%d",
script, WEXITSTATUS(status));
return -errno; return -errno;
} }
} }
return 0; return 0;
bad: bad:
status = -errno; status = -errno;
syslog(LOG_ERR, "%s %s, %s error: %s", if (FOREGROUND)
bb_perror_msg("%s %s, %s",
arg, intf, why);
else
syslog(LOG_ERR, "%s %s, %s: %s",
arg, intf, why, strerror(errno)); arg, intf, why, strerror(errno));
return status; return status;
} }
@ -211,11 +230,7 @@ int zcip_main(int argc, char *argv[])
int fd; int fd;
// parse commandline: prog [options] ifname script // parse commandline: prog [options] ifname script
#define FOREGROUND (opts & 1)
#define QUIT (opts & 2)
char *r_opt; char *r_opt;
unsigned long opts;
bb_opt_complementally = "vv"; // -v options accumulate bb_opt_complementally = "vv"; // -v options accumulate
opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose); opts = bb_getopt_ulflags(argc, argv, "fqr:v", &r_opt, &verbose);
if (opts & 4) { if (opts & 4) {