udhcp/signalpipe.c: use pipe instead of socketpair.
int fd_pipe[2];
int pid;
- if (pipe(fd_pipe) != 0) {
- bb_perror_msg_and_die("can't create pipe");
- }
+ xpipe(fd_pipe);
pid = fork();
if (pid == -1) {
volatile int vfork_exec_errno = 0;
const char *zip_exec = (gzip == 1) ? "gzip" : "bzip2";
- if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0)
- bb_perror_msg_and_die("pipe");
+ xpipe(gzipDataPipe);
+ xpipe(gzipStatusPipe);
signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
int xopen3(const char *pathname, int flags, int mode);
int open_or_warn(const char *pathname, int flags);
int open3_or_warn(const char *pathname, int flags, int mode);
+void xpipe(int filedes[2]);
off_t xlseek(int fd, off_t offset, int whence);
off_t fdlength(int fd);
-
int xsocket(int domain, int type, int protocol);
void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
void xlisten(int s, int backlog);
{
FILE *fp = fopen(path, mode);
if (fp == NULL)
- bb_perror_msg_and_die("cannot open '%s'", path);
+ bb_perror_msg_and_die("can't open '%s'", path);
return fp;
}
ret = open(pathname, flags, mode);
if (ret < 0) {
- bb_perror_msg_and_die("cannot open '%s'", pathname);
+ bb_perror_msg_and_die("can't open '%s'", pathname);
}
return ret;
}
ret = open(pathname, flags, mode);
if (ret < 0) {
- bb_perror_msg("cannot open '%s'", pathname);
+ bb_perror_msg("can't open '%s'", pathname);
}
return ret;
}
return open3_or_warn(pathname, flags, 0666);
}
+void xpipe(int filedes[2])
+{
+ if (pipe(filedes))
+ bb_perror_msg_and_die("can't create pipe");
+}
+
void xunlink(const char *pathname)
{
if (unlink(pathname))
- bb_perror_msg_and_die("cannot remove file '%s'", pathname);
+ bb_perror_msg_and_die("can't remove file '%s'", pathname);
}
// Turn on nonblocking I/O on a fd
int ndelay_on(int fd)
{
- return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NONBLOCK);
+ return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL,0) | O_NONBLOCK);
}
int ndelay_off(int fd)
{
- return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NONBLOCK);
+ return fcntl(fd, F_SETFL, fcntl(fd,F_GETFL,0) & ~O_NONBLOCK);
}
// "Renumber" opened fd
if (from == to)
return;
if (dup2(from, to) != to)
- bb_perror_msg_and_die("cannot duplicate file descriptor");
+ bb_perror_msg_and_die("can't duplicate file descriptor");
close(from);
}
void die_if_ferror(FILE *fp, const char *fn)
{
if (ferror(fp)) {
- /* doesn't set useful errno */
+ /* ferror doesn't set useful errno */
bb_error_msg_and_die("%s: I/O error", fn);
}
}
dp = opendir(path);
if (!dp)
- bb_perror_msg("cannot open '%s'", path);
+ bb_perror_msg("can't open '%s'", path);
return dp;
}
dp = opendir(path);
if (!dp)
- bb_perror_msg_and_die("cannot open '%s'", path);
+ bb_perror_msg_and_die("can't open '%s'", path);
return dp;
}
if (listen(s, backlog)) bb_perror_msg_and_die("listen");
}
-/* Die with an error message if we the sendto failed.
- * Return bytes sent otherwise
- */
-
+/* Die with an error message if sendto failed.
+ * Return bytes sent otherwise */
ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
socklen_t tolen)
{
static void signal_handler(int sig)
{
- if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0)
+ unsigned char ch = sig; /* use char, avoid dealing with partial writes */
+ if (write(signal_pipe[1], &ch, 1) != 1)
bb_perror_msg("cannot send signal");
}
* and installs the signal handler */
void udhcp_sp_setup(void)
{
-// BTW, why socketpair and not just pipe?
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe))
- bb_perror_msg_and_die("socketpair");
+ /* was socketpair, but it needs AF_UNIX in kernel */
+ xpipe(signal_pipe);
fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC);
fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC);
+ fcntl(signal_pipe[1], F_SETFL, O_NONBLOCK);
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
signal(SIGTERM, signal_handler);
* your signal on success */
int udhcp_sp_read(fd_set *rfds)
{
- int sig;
+ unsigned char sig;
if (!FD_ISSET(signal_pipe[0], rfds))
return 0;
- if (read(signal_pipe[0], &sig, sizeof(sig)) < 0)
+ if (read(signal_pipe[0], &sig, 1) != 1)
return -1;
return sig;
if (!argv[1] || argv[2]) usage();
dir = argv[1];
- if (pipe(selfpipe) == -1) fatal_cannot("create selfpipe");
+ xpipe(selfpipe);
coe(selfpipe[0]);
coe(selfpipe[1]);
ndelay_on(selfpipe[0]);
taia_now(&svd[1].start);
if (stat("log/down", &s) != -1)
svd[1].want = W_DOWN;
- if (pipe(logpipe) == -1)
- fatal_cannot("create log pipe");
+ xpipe(logpipe);
coe(logpipe[0]);
coe(logpipe[1]);
}
warnx("log must have at least seven characters");
return 0;
}
- if (pipe(logpipe) == -1) {
+ if (pipe(logpipe)) {
warnx("cannot create pipe for log");
return -1;
}
/* pipes are inserted between pairs of commands */
if ((i + 1) < pi->num_progs) {
- if (pipe(pipefds) < 0)
- bb_perror_msg_and_die("pipe");
+ pipe(pipefds);
nextout = pipefds[1];
} else {
nextout = 1;
FILE *pf;
int pid, channel[2];
- if (pipe(channel) < 0)
- bb_perror_msg_and_die("pipe");
+ xpipe(channel);
#if BB_MMU
pid = fork();
#else
nextout = 1;
if ((i + 1) < newjob->num_progs) {
- if (pipe(pipefds) < 0)
- bb_perror_msg_and_die("pipe");
+ xpipe(pipefds);
nextout = pipefds[1];
} else if (outpipe[1] != -1) {
nextout = outpipe[1];