3 FIXME: Check and tidy it up first!
9 #include <sys/socket.h>
18 int main(int argc,char **argv)
21 struct sockaddr_un sockaddr_unix;
22 const char *pathnameunix,*pathnamepipe;
23 struct pollfd pollfds[2];
27 fprintf(stderr,"%s: <pathname UNIX socket> <pathname PIPE>\n",argv[0]);
32 if (unlink(pathnamepipe) && errno!=ENOENT) {
33 fprintf(stderr,"unlink(\"%s\"): %m\n",pathnamepipe);
36 if (mkfifo(pathnamepipe,0600)) {
37 fprintf(stderr,"mkfifo(\"%s\",0600): %m\n",pathnamepipe);
40 if (-1==(fdpipe=open(pathnamepipe,O_WRONLY))) {
41 fprintf(stderr,"open(\"%s\",O_WRONLY)=%d: %m\n",pathnamepipe,fdpipe);
44 if (-1==(fdunix=socket(PF_UNIX,SOCK_STREAM,0))) {
45 fprintf(stderr,"socket(PF_UNIX,SOCK_STREAM,0)=%d: %m\n",fdunix);
48 memset(&sockaddr_unix,0,sizeof(sockaddr_unix));
49 sockaddr_unix.sun_family=AF_UNIX;
50 strncpy(sockaddr_unix.sun_path,pathnameunix,sizeof(sockaddr_unix.sun_path));
51 if (connect(fdunix,(const struct sockaddr *)&sockaddr_unix,sizeof(sockaddr_unix))) {
52 fprintf(stderr,"connect(UNIX socket,\"%s\"): %m\n",pathnameunix);
57 pollfds[0].events=POLLIN;
59 pollfds[1].events=POLLIN;
60 if (0>=poll(pollfds,sizeof(pollfds)/sizeof(*pollfds),-1)) {
61 fprintf(stderr,"poll(UNIX socket,PIPE socket): %m\n");
64 if (pollfds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) {
65 fprintf(stderr,"poll(UNIX socket): revents=0x%X\n",(unsigned)pollfds[0].revents);
68 if (pollfds[1].revents & (POLLERR|POLLHUP|POLLNVAL)) {
69 fprintf(stderr,"poll(PIPE socket): revents=0x%X\n",(unsigned)pollfds[1].revents);
72 for (fdi=0;fdi<1 /*sizeof(pollfds)/sizeof(*pollfds)*/;fdi++) {
77 if (fcntl(pollfds[fdi].fd,F_SETFL,O_NONBLOCK)) {
78 fprintf(stderr,"fcntl(%s socket,F_SETFL,O_NONBLOCK): %m\n",
79 (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : "<UNKNOWN>")));
82 got=read(pollfds[fdi].fd,buf,sizeof(buf));
86 fprintf(stderr,"read(%s socket): %m\n",
87 (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : "<UNKNOWN>")));
93 for (fdo=0;fdo<sizeof(pollfds)/sizeof(*pollfds);fdo++) {
96 if (fcntl(pollfds[fdo].fd,F_SETFL,0 /* !O_NONBLOCK */)) {
97 fprintf(stderr,"fcntl(%s socket,F_SETFL,0 /* !O_NONBLOCK */): %m\n",
98 (fdi==0 ? "UNIX" : (fdi==1 ? "PIPE" : "<UNKNOWN>")));
101 if (got!=write(pollfds[fdo].fd,buf,got)) {
102 fprintf(stderr,"write(%s socket,%ld): %m\n",
103 (fdo==0 ? "UNIX" : (fdo==1 ? "PIPE" : "<UNKNOWN>")),