comparison src/dnsbl.cpp @ 65:1ab70970c8c8

need to cleanup zombie children
author carl
date Sun, 16 Jan 2005 17:40:22 -0800
parents 6add229288a8
children e6a2d0be7c5e
comparison
equal deleted inserted replaced
64:6add229288a8 65:1ab70970c8c8
68 // misc stuff needed here 68 // misc stuff needed here
69 #include <ctype.h> 69 #include <ctype.h>
70 #include <fstream> 70 #include <fstream>
71 #include <syslog.h> 71 #include <syslog.h>
72 #include <pwd.h> 72 #include <pwd.h>
73 #include <sys/wait.h> /* header for waitpid() and various macros */
74 #include <signal.h> /* header for signal functions */
73 75
74 static char* dnsbl_version="$Id$"; 76 static char* dnsbl_version="$Id$";
75 77
76 #define DEFAULT "default" 78 #define DEFAULT "default"
77 #define WHITE "white" 79 #define WHITE "white"
95 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); 97 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
96 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len); 98 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len);
97 sfsistat mlfi_eom(SMFICTX *ctx); 99 sfsistat mlfi_eom(SMFICTX *ctx);
98 sfsistat mlfi_abort(SMFICTX *ctx); 100 sfsistat mlfi_abort(SMFICTX *ctx);
99 sfsistat mlfi_close(SMFICTX *ctx); 101 sfsistat mlfi_close(SMFICTX *ctx);
102 void sig_chld(int signo);
100 } 103 }
101 104
102 struct ltstr { 105 struct ltstr {
103 bool operator()(char* s1, char* s2) const { 106 bool operator()(char* s1, char* s2) const {
104 return strcmp(s1, s2) < 0; 107 return strcmp(s1, s2) < 0;
1651 // bind(s, (sockaddr*)&addr, sizeof(addr)); 1654 // bind(s, (sockaddr*)&addr, sizeof(addr));
1652 // close(s); 1655 // close(s);
1653 } 1656 }
1654 1657
1655 1658
1659 /*
1660 * The signal handler function -- only gets called when a SIGCHLD
1661 * is received, ie when a child terminates
1662 */
1663 void sig_chld(int signo)
1664 {
1665 int status;
1666 /* Wait for any child without blocking */
1667 while (waitpid(-1, &status, WNOHANG) > 0) {
1668 // ignore child exit status, we only do this to cleanup zombies
1669 }
1670 }
1671
1672
1656 int main(int argc, char**argv) 1673 int main(int argc, char**argv)
1657 { 1674 {
1658 bool check = false; 1675 bool check = false;
1659 bool setconn = false; 1676 bool setconn = false;
1660 bool setreso = false; 1677 bool setreso = false;
1797 } 1814 }
1798 sockaddr_un server; 1815 sockaddr_un server;
1799 memset(&server, '\0', sizeof(server)); 1816 memset(&server, '\0', sizeof(server));
1800 server.sun_family = AF_UNIX; 1817 server.sun_family = AF_UNIX;
1801 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); 1818 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1);
1802 // set the socket options
1803 ///int reuse_addr = 1;
1804 ///linger linger_opt;
1805 ///linger_opt.l_onoff = 0; // off
1806 ///linger_opt.l_linger = 0;
1807 ///setsockopt(resolver_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&reuse_addr), sizeof(reuse_addr));
1808 ///setsockopt(resolver_socket, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&linger_opt), sizeof(linger_opt));
1809 ///// set nonblocking mode
1810 ///int dummy = 0;
1811 ///int flags = fcntl(resolver_socket, F_GETFL, dummy);
1812 ///if (flags >= 0) fcntl(resolver_socket, F_SETFL, flags | O_NONBLOCK);
1813 //try to bind the address to the socket. 1819 //try to bind the address to the socket.
1814 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) { 1820 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) {
1815 // bind failed 1821 // bind failed
1816 shutdown(resolver_socket, SHUT_RDWR); 1822 shutdown(resolver_socket, SHUT_RDWR);
1817 close(resolver_socket); 1823 close(resolver_socket);
1818 my_syslog("child failed to bind resolver socket"); 1824 my_syslog("child failed to bind resolver socket");
1819 exit(0); // failed 1825 exit(0); // failed
1820 } 1826 }
1821
1822 //listen on the socket. 1827 //listen on the socket.
1823 if (listen(resolver_socket, 10) < 0) { 1828 if (listen(resolver_socket, 10) < 0) {
1824 // listen failed 1829 // listen failed
1825 shutdown(resolver_socket, SHUT_RDWR); 1830 shutdown(resolver_socket, SHUT_RDWR);
1826 close(resolver_socket); 1831 close(resolver_socket);
1827 my_syslog("child failed to listen to the resolver socket"); 1832 my_syslog("child failed to listen to resolver socket");
1833 exit(0); // failed
1834 }
1835 // setup sigchld handler to prevent zombies
1836 struct sigaction act;
1837 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler
1838 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example
1839 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated
1840 if (sigaction(SIGCHLD, &act, NULL) < 0) {
1841 my_syslog("child failed to setup SIGCHLD handler");
1828 exit(0); // failed 1842 exit(0); // failed
1829 } 1843 }
1830 while (true) { 1844 while (true) {
1831 sockaddr_un client; 1845 sockaddr_un client;
1832 socklen_t clientlen = sizeof(client); 1846 socklen_t clientlen = sizeof(client);