X-Git-Url: http://git.jankratochvil.net/?a=blobdiff_plain;f=src%2Funixpipe.c;fp=src%2Funixpipe.c;h=e03a8df52178fddcf08aff8710e7d39657e7d02b;hb=99fa422f22f367a97118216b705ea5448842b784;hp=0000000000000000000000000000000000000000;hpb=82e0aa5676ee5e3f4b4b06c850294c2ec04d51ec;p=nethome.git diff --git a/src/unixpipe.c b/src/unixpipe.c new file mode 100644 index 0000000..e03a8df --- /dev/null +++ b/src/unixpipe.c @@ -0,0 +1,112 @@ +/* $Id$ */ + +FIXME: Check and tidy it up first! + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +int main(int argc,char **argv) +{ +int fdunix,fdpipe; +struct sockaddr_un sockaddr_unix; +const char *pathnameunix,*pathnamepipe; +struct pollfd pollfds[2]; +int fdi,fdo; + + if (argc!=3) { + fprintf(stderr,"%s: \n",argv[0]); + exit(EXIT_FAILURE); + } + pathnameunix=argv[1]; + pathnamepipe=argv[2]; + if (unlink(pathnamepipe) && errno!=ENOENT) { + fprintf(stderr,"unlink(\"%s\"): %m\n",pathnamepipe); + exit(EXIT_FAILURE); + } + if (mkfifo(pathnamepipe,0600)) { + fprintf(stderr,"mkfifo(\"%s\",0600): %m\n",pathnamepipe); + exit(EXIT_FAILURE); + } + if (-1==(fdpipe=open(pathnamepipe,O_WRONLY))) { + fprintf(stderr,"open(\"%s\",O_WRONLY)=%d: %m\n",pathnamepipe,fdpipe); + exit(EXIT_FAILURE); + } + if (-1==(fdunix=socket(PF_UNIX,SOCK_STREAM,0))) { + fprintf(stderr,"socket(PF_UNIX,SOCK_STREAM,0)=%d: %m\n",fdunix); + exit(EXIT_FAILURE); + } + memset(&sockaddr_unix,0,sizeof(sockaddr_unix)); + sockaddr_unix.sun_family=AF_UNIX; + strncpy(sockaddr_unix.sun_path,pathnameunix,sizeof(sockaddr_unix.sun_path)); + if (connect(fdunix,(const struct sockaddr *)&sockaddr_unix,sizeof(sockaddr_unix))) { + fprintf(stderr,"connect(UNIX socket,\"%s\"): %m\n",pathnameunix); + exit(EXIT_FAILURE); + } + for (;;) { + pollfds[0].fd=fdunix; + pollfds[0].events=POLLIN; + pollfds[1].fd=fdpipe; + pollfds[1].events=POLLIN; + if (0>=poll(pollfds,sizeof(pollfds)/sizeof(*pollfds),-1)) { + fprintf(stderr,"poll(UNIX socket,PIPE socket): %m\n"); + exit(EXIT_FAILURE); + } + if (pollfds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) { + fprintf(stderr,"poll(UNIX socket): revents=0x%X\n",(unsigned)pollfds[0].revents); + exit(EXIT_FAILURE); + } + if (pollfds[1].revents & (POLLERR|POLLHUP|POLLNVAL)) { + fprintf(stderr,"poll(PIPE socket): revents=0x%X\n",(unsigned)pollfds[1].revents); + exit(EXIT_FAILURE); + } + for (fdi=0;fdi<1 /*sizeof(pollfds)/sizeof(*pollfds)*/;fdi++) { + for (;;) { + ssize_t got; + char buf[0x1000]; + + if (fcntl(pollfds[fdi].fd,F_SETFL,O_NONBLOCK)) { + fprintf(stderr,"fcntl(%s socket,F_SETFL,O_NONBLOCK): %m\n", + (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : ""))); + exit(EXIT_FAILURE); + } + got=read(pollfds[fdi].fd,buf,sizeof(buf)); + if (got<0) { + if (errno==EAGAIN) + break; + fprintf(stderr,"read(%s socket): %m\n", + (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : ""))); + exit(EXIT_FAILURE); + } + if (got==0) + exit(EXIT_SUCCESS); +write(1,buf,got); + for (fdo=0;fdo"))); + exit(EXIT_FAILURE); + } + if (got!=write(pollfds[fdo].fd,buf,got)) { + fprintf(stderr,"write(%s socket,%ld): %m\n", + (fdo==0 ? "UNIX" : (fdo==1 ? "PIPE" : "")), + (long)got); + exit(EXIT_FAILURE); + } + } + } + } + } + return EXIT_SUCCESS; +}