From: Zoran V. <vas...@us...> - 2005-11-03 07:15:17
|
Update of /cvsroot/naviserver/naviserver/nsd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20519/nsd Modified Files: binder.c Log Message: Untabify, reindent Index: binder.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/binder.c,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** binder.c 2 Nov 2005 23:07:15 -0000 1.15 --- binder.c 3 Nov 2005 07:15:09 -0000 1.16 *************** *** 51,58 **** typedef struct CMsg { ! unsigned int len; ! int level; ! int type; ! int fds[1]; } CMsg; --- 51,58 ---- typedef struct CMsg { ! unsigned int len; ! int level; ! int type; ! int fds[1]; } CMsg; *************** *** 66,70 **** */ ! static Ns_Mutex lock; static Tcl_HashTable preboundTcp; static Tcl_HashTable preboundUdp; --- 66,70 ---- */ ! static Ns_Mutex lock; static Tcl_HashTable preboundTcp; static Tcl_HashTable preboundUdp; *************** *** 106,110 **** Ns_SockListenEx(char *address, int port, int backlog) { ! int sock = -1; struct sockaddr_in sa; --- 106,110 ---- Ns_SockListenEx(char *address, int port, int backlog) { ! int sock = -1; struct sockaddr_in sa; *************** *** 155,159 **** Ns_SockListenUdp(char *address, int port) { ! int sock = -1; struct sockaddr_in sa; --- 155,159 ---- Ns_SockListenUdp(char *address, int port) { ! int sock = -1; struct sockaddr_in sa; *************** *** 196,201 **** Ns_SockListenRaw(int proto) { ! int sock = -1; ! Tcl_HashEntry *hPtr; Tcl_HashSearch search; --- 196,201 ---- Ns_SockListenRaw(int proto) { ! int sock = -1; ! Tcl_HashEntry *hPtr; Tcl_HashSearch search; *************** *** 239,244 **** Ns_SockListenUnix(char *path, int backlog) { ! int sock = -1; ! Tcl_HashEntry *hPtr; Tcl_HashSearch search; --- 239,244 ---- Ns_SockListenUnix(char *path, int backlog) { ! int sock = -1; ! Tcl_HashEntry *hPtr; Tcl_HashSearch search; *************** *** 292,309 **** Ns_SockBindUdp(struct sockaddr_in *saPtr) { ! int sock = -1, n = 1; ! sock = socket(AF_INET,SOCK_DGRAM, 0); ! if (sock == -1 ! || setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&n,sizeof(n)) == -1 ! || bind(sock,(struct sockaddr*)saPtr,sizeof(struct sockaddr_in)) == -1) { ! int err = errno; ! close(sock); ! sock = -1; ! Ns_SetSockErrno(err); ! } ! return (SOCKET)sock; } --- 292,309 ---- Ns_SockBindUdp(struct sockaddr_in *saPtr) { ! int sock = -1, n = 1; ! sock = socket(AF_INET,SOCK_DGRAM, 0); ! if (sock == -1 ! || setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&n,sizeof(n)) == -1 ! || bind(sock,(struct sockaddr*)saPtr,sizeof(struct sockaddr_in)) == -1) { ! int err = errno; ! close(sock); ! sock = -1; ! Ns_SetSockErrno(err); ! } ! return (SOCKET)sock; } *************** *** 329,333 **** Ns_SockBindUnix(char *path) { ! int sock = -1; struct sockaddr_un addr; --- 329,333 ---- Ns_SockBindUnix(char *path) { ! int sock = -1; struct sockaddr_un addr; *************** *** 374,388 **** Ns_SockBindRaw(int proto) { ! int sock = -1; ! sock = socket(AF_INET,SOCK_RAW, proto); ! if (sock == -1) { ! int err = errno; ! close(sock); ! Ns_SetSockErrno(err); ! } ! return (SOCKET)sock; } --- 374,388 ---- Ns_SockBindRaw(int proto) { ! int sock = -1; ! sock = socket(AF_INET,SOCK_RAW, proto); ! if (sock == -1) { ! int err = errno; ! close(sock); ! Ns_SetSockErrno(err); ! } ! return (SOCKET)sock; } *************** *** 481,488 **** return; #else ! Tcl_HashEntry *hPtr; ! Tcl_HashSearch search; ! char *addr; ! int port, sock; struct sockaddr_in *saPtr; --- 481,488 ---- return; #else ! Tcl_HashEntry *hPtr; ! Tcl_HashSearch search; ! char *addr; ! int port, sock; struct sockaddr_in *saPtr; *************** *** 594,601 **** return; #else ! Tcl_HashEntry *hPtr; ! int new, sock, port; struct sockaddr_in sa; - char *next, *str, *addr, *proto; for (;line != NULL; line = next) { --- 594,601 ---- return; #else ! Tcl_HashEntry *hPtr; ! int new, sock, port; ! char *next, *str, *addr, *proto; struct sockaddr_in sa; for (;line != NULL; line = next) { *************** *** 712,722 **** } /* *---------------------------------------------------------------------- * ! * Ns_SockBinder -- * ! * Create a new TCP/UDP/Unix socket bound to the specified port and ! * listening for new connections. * * The following types are defined: --- 712,723 ---- } + /* *---------------------------------------------------------------------- * ! * Ns_SockBinderListen -- * ! * Create a new TCP/UDP/Unix socket bound to the specified port ! * and listening for new connections. * * The following types are defined: *************** *** 727,734 **** * * Results: ! * Socket descriptor or -1 on error. * * Side effects: ! * None. * *---------------------------------------------------------------------- --- 728,735 ---- * * Results: ! * Socket descriptor or -1 on error. * * Side effects: ! * None. * *---------------------------------------------------------------------- *************** *** 738,746 **** Ns_SockBinderListen(int type, char *address, int port, int options) { ! int err; ! SOCKET sock; ! char data[64]; struct msghdr msg; ! struct iovec iov[4]; #ifdef HAVE_CMMSG CMsg cm; --- 739,748 ---- Ns_SockBinderListen(int type, char *address, int port, int options) { ! int err; ! SOCKET sock; ! char data[64]; struct msghdr msg; ! struct iovec iov[4]; ! #ifdef HAVE_CMMSG CMsg cm; *************** *** 764,768 **** msg.msg_iovlen = 4; if (sendmsg(binderRequest[1], (struct msghdr *) &msg, 0) != REQUEST_SIZE) { ! Ns_Log(Error, "Ns_SockBinderListen: sendmsg() failed: '%s'", strerror(errno)); return -1; } --- 766,771 ---- msg.msg_iovlen = 4; if (sendmsg(binderRequest[1], (struct msghdr *) &msg, 0) != REQUEST_SIZE) { ! Ns_Log(Error, "Ns_SockBinderListen: sendmsg() failed: '%s'", ! strerror(errno)); return -1; } *************** *** 785,791 **** #endif if (recvmsg(binderResponse[0], (struct msghdr *) &msg, 0) != RESPONSE_SIZE) { ! Ns_Log(Error, "Ns_SockBinderListen: recvmsg() failed: '%s'", strerror(errno)); return -1; } #ifdef HAVE_CMMSG sock = cm.fds[0]; --- 788,796 ---- #endif if (recvmsg(binderResponse[0], (struct msghdr *) &msg, 0) != RESPONSE_SIZE) { ! Ns_Log(Error, "Ns_SockBinderListen: recvmsg() failed: '%s'", ! strerror(errno)); return -1; } + #ifdef HAVE_CMMSG sock = cm.fds[0]; *************** *** 806,819 **** } if (err == 0) { ! Ns_Log(Notice, "Ns_SockBinderListen: listen(%s,%d) = %d", address, port, sock); } else { Ns_SetSockErrno(err); sock = -1; Ns_Log(Error, "Ns_SockBinderListen: listen(%s,%d) failed: '%s'", ! address, port, ns_sockstrerror(ns_sockerrno)); } return sock; } /* *---------------------------------------------------------------------- --- 811,827 ---- } if (err == 0) { ! Ns_Log(Notice, "Ns_SockBinderListen: listen(%s,%d) = %d", ! address, port, sock); } else { Ns_SetSockErrno(err); sock = -1; Ns_Log(Error, "Ns_SockBinderListen: listen(%s,%d) failed: '%s'", ! address, port, ns_sockstrerror(ns_sockerrno)); } + return sock; } + /* *---------------------------------------------------------------------- *************** *** 821,833 **** * NsForkBinder -- * ! * Fork of the slave bind/listen process. This routine is called ! * by main() when the server starts as root. * * Results: ! * None. * * Side effects: ! * The binderRunning, binderRequest, binderResponse static variables ! * are updated. * *---------------------------------------------------------------------- --- 829,841 ---- * NsForkBinder -- * ! * Fork of the slave bind/listen process. This routine is called ! * by main() when the server starts as root. * * Results: ! * None. * * Side effects: ! * The binderRunning, binderRequest, binderResponse static variables ! * are updated. * *---------------------------------------------------------------------- *************** *** 845,849 **** if (ns_sockpair(binderRequest) != 0 || ns_sockpair(binderResponse) != 0) { ! Ns_Fatal("binder: ns_sockpair() failed: '%s'", strerror(errno)); } --- 853,857 ---- if (ns_sockpair(binderRequest) != 0 || ns_sockpair(binderResponse) != 0) { ! Ns_Fatal("binder: ns_sockpair() failed: '%s'", strerror(errno)); } *************** *** 858,879 **** pid = ns_fork(); if (pid < 0) { ! Ns_Fatal("binder: fork() failed: '%s'", strerror(errno)); } else if (pid == 0) { ! pid = ns_fork(); ! if (pid < 0) { ! Ns_Fatal("binder: fork() failed: '%s'", strerror(errno)); ! } else if (pid == 0) { ! close(binderRequest[1]); ! close(binderResponse[0]); ! Binder(); ! } ! exit(0); } if (Ns_WaitForProcess(pid, &status) != NS_OK) { ! Ns_Fatal("binder: Ns_WaitForProcess(%d) failed: '%s'", ! pid, strerror(errno)); } else if (status != 0) { ! Ns_Fatal("binder: process %d exited with non-zero status: %d", ! pid, status); } binderRunning = 1; --- 866,887 ---- pid = ns_fork(); if (pid < 0) { ! Ns_Fatal("binder: fork() failed: '%s'", strerror(errno)); } else if (pid == 0) { ! pid = ns_fork(); ! if (pid < 0) { ! Ns_Fatal("binder: fork() failed: '%s'", strerror(errno)); ! } else if (pid == 0) { ! close(binderRequest[1]); ! close(binderResponse[0]); ! Binder(); ! } ! exit(0); } if (Ns_WaitForProcess(pid, &status) != NS_OK) { ! Ns_Fatal("binder: Ns_WaitForProcess(%d) failed: '%s'", ! pid, strerror(errno)); } else if (status != 0) { ! Ns_Fatal("binder: process %d exited with non-zero status: %d", ! pid, status); } binderRunning = 1; *************** *** 886,898 **** * NsStopBinder -- * ! * Close the socket to the binder after startup. This is done ! * to avoid a possible security risk of binding to privileged ! * ports after startup. * * Results: ! * None. * * Side effects: ! * Binder process will exit. * *---------------------------------------------------------------------- --- 894,906 ---- * NsStopBinder -- * ! * Close the socket to the binder after startup. This is done ! * to avoid a possible security risk of binding to privileged ! * ports after startup. * * Results: ! * None. * * Side effects: ! * Binder process will exit. * *---------------------------------------------------------------------- *************** *** 903,914 **** { if (binderRunning) { ! close(binderRequest[1]); ! close(binderResponse[0]); ! close(binderRequest[0]); ! close(binderResponse[1]); ! binderRunning = 0; } } /* *---------------------------------------------------------------------- --- 911,923 ---- { if (binderRunning) { ! close(binderRequest[1]); ! close(binderResponse[0]); ! close(binderRequest[0]); ! close(binderResponse[1]); ! binderRunning = 0; } } + /* *---------------------------------------------------------------------- *************** *** 916,926 **** * Binder -- * ! * Slave process bind/listen loop. * * Results: ! * None. * * Side effects: ! * Sockets are created and sent to the parent on request. * *---------------------------------------------------------------------- --- 925,935 ---- * Binder -- * ! * Slave process bind/listen loop. * * Results: ! * None. * * Side effects: ! * Sockets are created and sent to the parent on request. * *---------------------------------------------------------------------- *************** *** 930,938 **** Binder(void) { ! int n, err, fd; ! char address[64]; struct msghdr msg; ! struct iovec iov[4]; ! int options, type, port; #ifdef HAVE_CMMSG CMsg cm; --- 939,947 ---- Binder(void) { ! int options, type, port, n, err, fd; ! char address[64]; struct msghdr msg; ! struct iovec iov[4]; ! #ifdef HAVE_CMMSG CMsg cm; *************** *** 952,956 **** iov[3].iov_base = (caddr_t) address; iov[3].iov_len = sizeof(address); ! memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 4; --- 961,965 ---- iov[3].iov_base = (caddr_t) address; iov[3].iov_len = sizeof(address); ! memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 4; *************** *** 961,1007 **** } while (n == -1 && errno == EINTR); if (n == 0) { ! break; ! } if (n != REQUEST_SIZE) { Ns_Fatal("binder: recvmsg() failed: '%s'", strerror(errno)); } ! /* ! * NB: Due to a bug in Solaris the slave process must ! * call both bind() and listen() before returning the ! * socket. All other Unix versions would actually allow ! * just performing the bind() in the slave and allowing ! * the parent to perform the listen(). ! */ switch (type) { ! case 'U': fd = Ns_SockListenUdp(address, port); break; ! case 'D': fd = Ns_SockListenUnix(address, options); break; ! case 'R': fd = Ns_SockListenRaw(options); break; ! case 'T': ! default: fd = Ns_SockListenEx(address, port, options); } ! if (fd < 0) err = errno; iov[0].iov_base = (caddr_t) &err; iov[0].iov_len = sizeof(err); ! memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 1; if (fd != -1) { #ifdef HAVE_CMMSG ! cm.len = sizeof(cm); ! cm.level = SOL_SOCKET; ! cm.type = SCM_RIGHTS; ! cm.fds[0] = fd; ! msg.msg_control = (void *) &cm; ! msg.msg_controllen = cm.len; ! msg.msg_flags = 0; #else msg.msg_accrights = (caddr_t) &fd; --- 970,1019 ---- } while (n == -1 && errno == EINTR); if (n == 0) { ! break; ! } if (n != REQUEST_SIZE) { Ns_Fatal("binder: recvmsg() failed: '%s'", strerror(errno)); } ! /* ! * NB: Due to a bug in Solaris the slave process must ! * call both bind() and listen() before returning the ! * socket. All other Unix versions would actually allow ! * just performing the bind() in the slave and allowing ! * the parent to perform the listen(). ! */ switch (type) { ! case 'U': fd = Ns_SockListenUdp(address, port); break; ! case 'D': fd = Ns_SockListenUnix(address, options); break; ! case 'R': fd = Ns_SockListenRaw(options); break; ! case 'T': ! default: fd = Ns_SockListenEx(address, port, options); } ! ! if (fd < 0) { ! err = errno; ! } iov[0].iov_base = (caddr_t) &err; iov[0].iov_len = sizeof(err); ! memset(&msg, 0, sizeof(msg)); msg.msg_iov = iov; msg.msg_iovlen = 1; if (fd != -1) { #ifdef HAVE_CMMSG ! cm.len = sizeof(cm); ! cm.level = SOL_SOCKET; ! cm.type = SCM_RIGHTS; ! cm.fds[0] = fd; ! msg.msg_control = (void *) &cm; ! msg.msg_controllen = cm.len; ! msg.msg_flags = 0; #else msg.msg_accrights = (caddr_t) &fd; *************** *** 1016,1023 **** } if (fd != -1) { ! ! /* ! * Close the socket as it won't be needed in the slave. ! */ close(fd); --- 1028,1035 ---- } if (fd != -1) { ! ! /* ! * Close the socket as it won't be needed in the slave. ! */ close(fd); |