From 5a6aeddfa7262e41802c77f70c9ef88e9c2c2476 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 26 May 2007 16:44:20 +0000 Subject: [PATCH] xpipe: introduce (saves ~170 bytes) udhcp/signalpipe.c: use pipe instead of socketpair. --- archival/libunarchive/open_transformer.c | 4 +-- archival/tar.c | 4 +-- include/libbb.h | 2 +- libbb/xfuncs.c | 32 +++++++++++++----------- networking/udhcp/signalpipe.c | 13 +++++----- runit/runsv.c | 5 ++-- runit/runsvdir.c | 2 +- shell/hush.c | 6 ++--- shell/lash.c | 3 +-- 9 files changed, 35 insertions(+), 36 deletions(-) diff --git a/archival/libunarchive/open_transformer.c b/archival/libunarchive/open_transformer.c index 456d3e986..58a89b918 100644 --- a/archival/libunarchive/open_transformer.c +++ b/archival/libunarchive/open_transformer.c @@ -17,9 +17,7 @@ int open_transformer(int src_fd, 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) { diff --git a/archival/tar.c b/archival/tar.c index 79979b05f..e634cc670 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -507,8 +507,8 @@ static int writeTarFile(const int tar_fd, const int verboseFlag, 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 */ diff --git a/include/libbb.h b/include/libbb.h index 1859a3f09..4561ef6a5 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -261,10 +261,10 @@ int xopen(const char *pathname, int flags); 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); diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index a85a046cf..4eb4737c0 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -106,7 +106,7 @@ FILE *xfopen(const char *path, const char *mode) { 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; } @@ -117,7 +117,7 @@ int xopen3(const char *pathname, int flags, int mode) 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; } @@ -135,7 +135,7 @@ int open3_or_warn(const char *pathname, int flags, int mode) 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; } @@ -146,21 +146,27 @@ int open_or_warn(const char *pathname, int flags) 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 @@ -169,7 +175,7 @@ void xmove_fd(int from, int to) 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); } @@ -199,7 +205,7 @@ off_t xlseek(int fd, off_t offset, int whence) 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); } } @@ -520,7 +526,7 @@ DIR *warn_opendir(const char *path) dp = opendir(path); if (!dp) - bb_perror_msg("cannot open '%s'", path); + bb_perror_msg("can't open '%s'", path); return dp; } @@ -531,7 +537,7 @@ DIR *xopendir(const char *path) 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; } @@ -568,10 +574,8 @@ void xlisten(int s, int backlog) 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) { diff --git a/networking/udhcp/signalpipe.c b/networking/udhcp/signalpipe.c index d52a931a9..9c7ead965 100644 --- a/networking/udhcp/signalpipe.c +++ b/networking/udhcp/signalpipe.c @@ -27,7 +27,8 @@ static int signal_pipe[2]; 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"); } @@ -36,11 +37,11 @@ static void signal_handler(int sig) * 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); @@ -67,12 +68,12 @@ int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) * 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; diff --git a/runit/runsv.c b/runit/runsv.c index bd4a81eee..d5bfd4e89 100644 --- a/runit/runsv.c +++ b/runit/runsv.c @@ -420,7 +420,7 @@ int runsv_main(int argc, char **argv) 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]); @@ -456,8 +456,7 @@ int runsv_main(int argc, char **argv) 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]); } diff --git a/runit/runsvdir.c b/runit/runsvdir.c index 39929fc49..4b94aa211 100644 --- a/runit/runsvdir.c +++ b/runit/runsvdir.c @@ -184,7 +184,7 @@ static int setup_log(void) 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; } diff --git a/shell/hush.c b/shell/hush.c index 800b0f970..2e6f286dc 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -1800,8 +1800,7 @@ static int run_pipe_real(struct pipe *pi) /* 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; @@ -3134,8 +3133,7 @@ static FILE *generate_stream_from_list(struct pipe *head) FILE *pf; int pid, channel[2]; - if (pipe(channel) < 0) - bb_perror_msg_and_die("pipe"); + xpipe(channel); #if BB_MMU pid = fork(); #else diff --git a/shell/lash.c b/shell/lash.c index 24e48c337..28449b791 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -1222,8 +1222,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2]) 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];