xpipe: introduce (saves ~170 bytes)

udhcp/signalpipe.c: use pipe instead of socketpair.
This commit is contained in:
Denis Vlasenko 2007-05-26 16:44:20 +00:00
parent 6239b1f50a
commit 5a6aeddfa7
9 changed files with 35 additions and 36 deletions

View File

@ -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) {

View File

@ -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 */

View File

@ -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);

View File

@ -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)
{

View File

@ -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;

View File

@ -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]);
}

View File

@ -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;
}

View File

@ -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

View File

@ -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];