socat/xio-pipe.c

179 lines
5.2 KiB
C
Raw Normal View History

2008-01-28 21:37:16 +00:00
/* source: xio-pipe.c */
/* Copyright Gerhard Rieger and contributors (see file CHANGES) */
2008-01-27 12:00:08 +00:00
/* Published under the GNU General Public License V.2, see file COPYING */
/* this file contains the source for opening addresses of pipe type */
#include "xiosysincludes.h"
#include "xioopen.h"
#include "xio-named.h"
#include "xio-pipe.h"
2008-01-27 12:00:08 +00:00
#if WITH_PIPE
static int xioopen_fifo(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, groups_t groups, int dummy1, int dummy2, int dummy3);
2008-01-27 12:00:08 +00:00
static int xioopen_fifo_unnamed(xiofile_t *sock, struct opt *opts);
const struct addrdesc xioaddr_pipe = { "PIPE", 3, xioopen_fifo, GROUP_FD|GROUP_NAMED|GROUP_OPEN|GROUP_FIFO, 0, 0, 0 HELP("[:<filename>]") };
2008-01-27 12:00:08 +00:00
/* process an unnamed bidirectional "pipe" or "fifo" or "echo" argument with
options */
static int xioopen_fifo_unnamed(xiofile_t *sock, struct opt *opts) {
2023-07-13 07:06:08 +00:00
struct single *sfd = &sock->stream;
2008-01-27 12:00:08 +00:00
struct opt *opts2;
int filedes[2];
int numleft;
int result;
2023-07-13 07:06:08 +00:00
if (applyopts_single(sfd, opts, PH_INIT) < 0) return -1;
applyopts(sfd, -1, opts, PH_INIT);
2008-01-27 12:00:08 +00:00
if (Pipe(filedes) != 0) {
Error2("pipe(%p): %s", filedes, strerror(errno));
return -1;
}
/*0 Info2("pipe({%d,%d})", filedes[0], filedes[1]);*/
sock->common.tag = XIO_TAG_RDWR;
2023-07-13 07:06:08 +00:00
sfd->dtype = XIODATA_PIPE;
sfd->fd = filedes[0];
sfd->para.bipipe.fdout = filedes[1];
sfd->para.bipipe.socktype = SOCK_STREAM; /* due to socketpair reuse */
applyopts_cloexec(sfd->fd, opts);
applyopts_cloexec(sfd->para.bipipe.fdout, opts);
2008-01-27 12:00:08 +00:00
/* one-time and input-direction options, no second application */
2023-07-13 07:06:08 +00:00
retropt_bool(opts, OPT_IGNOREEOF, &sfd->ignoreeof);
2008-01-27 12:00:08 +00:00
/* here we copy opts! */
if ((opts2 = copyopts(opts, GROUP_FIFO)) == NULL) {
return STAT_NORETRY;
}
/* apply options to first FD */
2023-07-13 07:06:08 +00:00
if ((result = applyopts(sfd, -1, opts, PH_ALL)) < 0) {
2008-01-27 12:00:08 +00:00
return result;
}
2023-07-13 07:06:08 +00:00
if ((result = applyopts_single(sfd, opts, PH_ALL)) < 0) {
2008-01-27 12:00:08 +00:00
return result;
}
/* apply options to second FD */
2023-07-13 07:06:08 +00:00
if (applyopts(&sock->stream, sfd->para.bipipe.fdout, opts2, PH_ALL) < 0)
return -1;
2008-01-27 12:00:08 +00:00
if ((numleft = leftopts(opts)) > 0) {
showleft(opts);
Error1("INTERNAL: %d option(s) remained unused", numleft);
2008-01-27 12:00:08 +00:00
}
Notice("writing to and reading from unnamed pipe");
return 0;
}
2008-01-29 06:59:12 +00:00
/* open a named or unnamed pipe/fifo */
2023-07-13 07:06:08 +00:00
static int xioopen_fifo(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, groups_t groups, int dummy1, int dummy2, int dummy3) {
struct single *sfd = &xfd->stream;
2008-01-27 12:00:08 +00:00
const char *pipename = argv[1];
int rw = (xioflags & XIO_ACCMODE);
#if HAVE_STAT64
struct stat64 pipstat;
#else
struct stat pipstat;
#endif /* !HAVE_STAT64 */
bool opt_unlink_early = false;
bool opt_unlink_close = true;
mode_t mode = 0666;
int result;
if (argc == 1) {
2023-07-13 07:06:08 +00:00
return xioopen_fifo_unnamed(xfd, sfd->opts);
2008-01-27 12:00:08 +00:00
}
if (argc != 2) {
Error2("%s: wrong number of parameters (%d instead of 1)", argv[0], argc-1);
}
2023-07-13 07:06:08 +00:00
if (applyopts_single(sfd, opts, PH_INIT) < 0)
return -1;
applyopts(sfd, -1, opts, PH_INIT);
2008-01-27 12:00:08 +00:00
retropt_bool(opts, OPT_UNLINK_EARLY, &opt_unlink_early);
applyopts_named(pipename, opts, PH_EARLY); /* umask! */
2023-07-13 07:06:08 +00:00
applyopts(sfd, -1, opts, PH_EARLY);
2008-01-27 12:00:08 +00:00
if (opt_unlink_early) {
if (Unlink(pipename) < 0) {
return STAT_RETRYLATER;
2008-01-27 12:00:08 +00:00
}
}
retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
retropt_modet(opts, OPT_PERM, &mode);
if (applyopts_named(pipename, opts, PH_EARLY) < 0) {
return STAT_RETRYLATER;
}
if (applyopts_named(pipename, opts, PH_PREOPEN) < 0) {
return STAT_RETRYLATER;
}
if (
#if HAVE_STAT64
Stat64(pipename, &pipstat) < 0
#else
Stat(pipename, &pipstat) < 0
#endif /* !HAVE_STAT64 */
) {
if (errno != ENOENT) {
Error3("stat(\"%s\", %p): %s", pipename, &pipstat, strerror(errno));
} else {
Debug1("xioopen_fifo(\"%s\"): does not exist, creating fifo", pipename);
#if 0
result = Mknod(pipename, S_IFIFO|mode, 0);
if (result < 0) {
Error3("mknod(%s, %d, 0): %s", pipename, mode, strerror(errno));
return STAT_RETRYLATER;
}
#else
result = Mkfifo(pipename, mode);
if (result < 0) {
Error3("mkfifo(%s, %d): %s", pipename, mode, strerror(errno));
return STAT_RETRYLATER;
}
#endif
Notice2("created named pipe \"%s\" for %s", pipename, ddirection[rw]);
applyopts_named(pipename, opts, PH_ALL);
2008-01-27 12:00:08 +00:00
}
if (opt_unlink_close) {
2023-07-13 07:06:08 +00:00
if ((sfd->unlink_close = strdup(pipename)) == NULL) {
2008-01-27 12:00:08 +00:00
Error1("strdup(\"%s\"): out of memory", pipename);
}
2023-07-13 07:06:08 +00:00
sfd->opt_unlink_close = true;
2008-01-27 12:00:08 +00:00
}
} else {
/* exists */
Info1("xioopen_fifo(\"%s\"): already exist, opening it", pipename);
2008-01-27 12:00:08 +00:00
Notice3("opening %s \"%s\" for %s",
filetypenames[(pipstat.st_mode&S_IFMT)>>12],
pipename, ddirection[rw]);
applyopts_named(pipename, opts, PH_EARLY);
}
if ((result = _xioopen_open(pipename, rw, opts)) < 0) {
return result;
}
2023-07-13 07:06:08 +00:00
sfd->fd = result;
2008-01-27 12:00:08 +00:00
applyopts_named(pipename, opts, PH_FD);
2023-07-13 07:06:08 +00:00
applyopts(sfd, -1, opts, PH_FD);
applyopts_cloexec(sfd->fd, opts);
return _xio_openlate(sfd, opts);
2008-01-27 12:00:08 +00:00
}
#endif /* WITH_PIPE */