--- /dev/null
+/* $Id$ */
+
+FIXME: Check and tidy it up first!
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/poll.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+
+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: <pathname UNIX socket> <pathname PIPE>\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" : "<UNKNOWN>")));
+ 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" : "<UNKNOWN>")));
+ exit(EXIT_FAILURE);
+ }
+ if (got==0)
+ exit(EXIT_SUCCESS);
+write(1,buf,got);
+ for (fdo=0;fdo<sizeof(pollfds)/sizeof(*pollfds);fdo++) {
+ if (fdi==fdo)
+ continue;
+ if (fcntl(pollfds[fdo].fd,F_SETFL,0 /* !O_NONBLOCK */)) {
+ fprintf(stderr,"fcntl(%s socket,F_SETFL,0 /* !O_NONBLOCK */): %m\n",
+ (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : "<UNKNOWN>")));
+ 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" : "<UNKNOWN>")),
+ (long)got);
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+ }
+ }
+ return EXIT_SUCCESS;
+}