#include #include #include #include #include #include #include #include #include #include extern char **argvec; #include "utils.h" #include "config.h" #include "strings.h" #include "hostname.h" #if defined(HOSTNAMES) && !defined(SYNC_HOSTNAMES) int hostname_fd; void fork_hostname_process(void) { int p[2]; int pid; int npend; int pend[1024]; int i; int j; fd_set rfds; socketpair(AF_UNIX,SOCK_STREAM,0,&p[0]); pid = fork(); if (pid) { hostname_fd = p[0]; close(p[1]); return; } signal(SIGINT,SIG_IGN); hostname_fd = p[1]; close(p[0]); for (i=getdtablesize()-1;i>=0;i--) { if ((i != hostname_fd) && (i != 2)) close(i); } npend = 0; while (1) { while (1) { pid = wait3(0,WNOHANG,0); if (pid <= 0) break; } FD_ZERO(&rfds); FD_SET(hostname_fd,&rfds); j = 0; for (i=0;i= 0) { FD_SET(pend[i],&rfds); if (j != i) pend[j] = pend[i]; j ++; } } npend = j; i = select(FD_SETSIZE,&rfds,0,0,0); if (i < 0) { if (errno == EINTR) continue; fprintf(stderr,"%s: select: %s\n",argvec[0],strerror(errno)); exit(0); } if (FD_ISSET(hostname_fd,&rfds)) { struct in_addr ia; if (Read(hostname_fd,&ia,sizeof(ia)) < 0) exit(0); socketpair(AF_UNIX,SOCK_STREAM,0,&p[0]); pend[npend++] = p[0]; pid = fork(); if (pid == 0) { struct hostent *hp; close(hostname_fd); for (i=npend-1;i>=0;i--) close(pend[i]); hp = gethostbyaddr((void *)&ia,sizeof(ia),AF_INET); write(p[1],&ia,sizeof(ia)); if (hp) { unsigned char len; unsigned int ilen; ilen = strlen(hp->h_name); len = (ilen > 255) ? 255 : ilen; write(p[1],&len,1); if (len) write(p[1],hp->h_name,len); } else { write(p[1],"",1); } exit(0); } close(p[1]); continue; } for (i=npend-1;i>=0;i--) { if (FD_ISSET(pend[i],&rfds)) #define IAS sizeof(struct in_addr) { unsigned char buf[IAS+1+255]; #define SETERR(s) (buf[IAS]=strlen(strcpy(&buf[IAS+1],(s)))) if (Read(pend[i],&buf[0],IAS) == 0) { if (Read(pend[i],&buf[IAS],1) == 0) { if (Read(pend[i],&buf[IAS+1],buf[IAS]) == 0) { } else { SETERR("(error A)"); } } else { SETERR("(error B)"); } } else { SETERR("(error C)"); } write(hostname_fd,&buf[0],IAS+1+buf[IAS]); #undef SETERR #undef IAS close(pend[i]); pend[i] = -1; } } } } #endif