Index: Makefile =================================================================== RCS file: /usr/local/cvsroot/user/inetd/Makefile,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- Makefile 30 Aug 2001 04:13:52 -0000 1.1.1.1 +++ Makefile 30 Aug 2001 04:58:57 -0000 1.2 @@ -1,11 +1,14 @@ SERVICES = \"/etc/services\" INETD_CONF = \"/etc/inetd.conf\" +# HAVE_CFGREAD=y EXEC = inetd OBJS = inetd.o -ifeq ($(ARCHITECTURE),x86) -OBJS += config.o +ifeq ($(HAVE_CFGREAD),y) + CFLAGS += -DHAVE_CFGREAD +else + OBJS += config.o endif CFLAGS += -DSERVICES=$(SERVICES) -DINETD_CONF=$(INETD_CONF) Index: cfgfile.h =================================================================== RCS file: /usr/local/cvsroot/user/inetd/cfgfile.h,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- cfgfile.h 30 Aug 2001 04:13:52 -0000 1.1.1.1 +++ cfgfile.h 21 Apr 2002 04:18:42 -0000 1.2 @@ -5,4 +5,58 @@ char ** cfgread(FILE *fp); char ** cfgfind(FILE *fp, char *var); +/* +#define DEBUG_EXEC 1 +#define DEBUG_MALLOC 1 +*/ + +#if DEBUG_EXEC - 0 +# include +# define DEBUG_LOG(fmt, args...) do { \ + syslog(LOG_INFO, "%s (%d): " fmt "\n", \ + __FUNCTION__, __LINE__ , ##args); \ + } while (0) +# define D(x) x + +#else + +# define DEBUG_LOG(fmt, args...) do { } while (0) +# define D(x) +#endif + +#define DEBUG_MARK() DEBUG_LOG("debug mark") + +#if DEBUG_MALLOC - 0 +# define xmalloc(n) ( { \ + void *ptr; \ + ptr = malloc(n); \ + syslog(LOG_INFO, "%s (%d): malloc(%ld) %p\n", \ + __FUNCTION__, __LINE__ , (long)n, ptr); \ + ptr ; \ + } ) +# define xstrdup(s) ( { \ + char *ptr; \ + ptr = strdup(s); \ + syslog(LOG_INFO, "%s (%d): strdup(%s) %p\n", \ + __FUNCTION__, __LINE__ , s, ptr); \ + ptr ; \ + } ) +# define xfree(p) ( \ + syslog(LOG_INFO, "%s (%d): free %p\n", \ + __FUNCTION__, __LINE__ , p), \ + free(p), \ + p = NULL \ + ) +# define DEBUG_MALLOC_LOG(fmt, args...) do { \ + syslog(LOG_INFO, "%s (%d): " fmt "\n", \ + __FUNCTION__, __LINE__ , ##args); \ + } while (0) +#else +# define xmalloc(n) malloc(n) +# define xstrdup(s) strdup(s) +# define xfree(p) free(p) +# define DEBUG_MALLOC_LOG(fmt, args...) do { } while (0) +#endif + + #endif /* __CFGFILE_H__ */ Index: config.c =================================================================== RCS file: /usr/local/cvsroot/user/inetd/config.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -r1.1.1.1 -r1.2 --- config.c 30 Aug 2001 04:13:52 -0000 1.1.1.1 +++ config.c 30 Aug 2001 04:58:57 -0000 1.2 @@ -46,7 +46,6 @@ char *ebuf; char *p; int i; - int j; if (!fp) { errno = EIO; @@ -86,7 +85,7 @@ strncpy(search, var, sizeof(search)); fseek(fp, 0, SEEK_SET); - while (ret = cfgread(fp)) { + while ((ret = cfgread(fp)) != 0) { if (!strcmp(ret[0], search)) return ret; } return (void *)0; Index: inetd.c =================================================================== RCS file: /usr/local/cvsroot/user/inetd/inetd.c,v retrieving revision 1.1.1.1 retrieving revision 1.5 diff -u -r1.1.1.1 -r1.5 --- inetd.c 30 Aug 2001 04:13:52 -0000 1.1.1.1 +++ inetd.c 21 Apr 2002 04:33:19 -0000 1.5 @@ -35,8 +35,9 @@ #include #include #include +#include -#ifdef __UCLIBC__ +#ifdef HAVE_READCFG #include #else #include "cfgfile.h" @@ -46,16 +47,18 @@ * in a writable file system and that we should support reloading of it * via a HUP signal. */ -#undef MODIFY_CONFIG -#undef DEBUG +/* + #define MODIFY_CONFIG + #define DEBUG +*/ #define MAX_CONNECT 8 static void read_config(void); -#ifndef __UCLIBC__ -__sighandler_t -__signal (int sig, __sighandler_t handler, int flags) +typedef void (*my_sighandler_t)(int); + +my_sighandler_t signal_flags(int sig, my_sighandler_t handler, int flags) { int ret; struct sigaction action, oaction; @@ -65,18 +68,19 @@ ret = sigaction (sig, &action, &oaction); return (ret == -1) ? SIG_ERR : oaction.sa_handler; } -#endif static void close_on_exec(int f) { if (fcntl(f, F_SETFD, 1)) - perror("close on exec: "); + syslog(LOG_ERR, "close on exec: %m"); } static inline void close_all_fds(int keep_top) { int i, n; n = getdtablesize(); + if (n < 3) + n = 3; for(i=keep_top+1;imax) max = (x); for(p=servicelist; p!=NULL; p=p->next) { - if (!strlen(p->args[0]) || (p->current >= p->limit) || !p->enabled) + if (!strlen(p->args[0]) || (p->current >= p->limit) || !p->enabled || + (p->master_socket == -1)) continue; FD_MSET(p->master_socket, readmask); @@ -146,7 +150,7 @@ for(p=servicelist; p!=NULL; p=p->next) { int fd; - if (p->master_socket && FD_ISSET(p->master_socket, readmask)) { + if ((p->master_socket != -1) && FD_ISSET(p->master_socket, readmask)) { int j; for(j=0;jpid[j] == 0) @@ -157,7 +161,7 @@ int remotelen = sizeof(remote); fd = accept(p->master_socket, (struct sockaddr*)&remote, &remotelen); if (fd < 0) { - printf("accept failed\n"); + syslog(LOG_ERR, "accept failed"); break; } } else { @@ -188,7 +192,7 @@ p->current--; close(p->master_socket); p->enabled = 0; - p->master_socket = 0; + p->master_socket = -1; _exit(0); } @@ -207,14 +211,15 @@ for(p=servicelist; p!=NULL; p=p->next) { struct sockaddr_in server_sockaddr; - if (p->master_socket || !strlen(p->args[0])) + if ((p->master_socket != -1) || !strlen(p->args[0])) continue; if (p->tcp) { int true; if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { - printf("Unable to create socket\n"); + syslog(LOG_ERR, "Unable to create TCP socket: %m"); + continue; } close_on_exec(s); @@ -227,23 +232,34 @@ if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&true, sizeof(true))) == -1) { - perror("setsockopt: "); + syslog(LOG_ERR, "setsockopt: %m"); + close(s); + s = -1; + continue; } if(bind(s, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)) == -1) { - perror("Unable to bind server socket: "); + syslog(LOG_ERR, "Unable to bind server socket on TCP port %d: %m", + p->port); close(s); + s = -1; + continue; } if(listen(s, 1) == -1) { - printf("Unable to listen to socket\n"); + syslog(LOG_ERR, "Unable to listen to TCP socket on port %d: %m", + p->port); + close(s); + s = -1; + continue; } } else { if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { - printf("Unable to create socket\n"); + syslog(LOG_ERR, "Unable to create UDP socket: %m"); + continue; } close_on_exec(s); @@ -254,8 +270,10 @@ if(bind(s, (struct sockaddr *)&server_sockaddr, sizeof(server_sockaddr)) == -1) { - printf("Unable to bind socket\n"); + syslog(LOG_ERR, "Unable to bind socket on UDP port %d: %m", p->port); close(s); + s = -1; + continue; } } p->master_socket = s; @@ -265,14 +283,14 @@ } static inline void stop_services(void) { - int s; struct stService *p; struct server_sockaddr; for(p=servicelist; p!=NULL; p=p->next) { - if(p->master_socket) + if((p->master_socket != -1)) { close(p->master_socket); - p->master_socket = 0; + p->master_socket = -1; + } } } @@ -295,12 +313,12 @@ #endif volatile int got_cont; -static void cont_handler() +static void cont_handler(int signo) { got_cont = 1; } -static void stop_handler() +static void stop_handler(int signo) { got_cont = 0; /* To reduce memory usage & prevent callers from getting gummed up */ @@ -321,10 +339,9 @@ char * pstrdup(char * c) { char * _c = c; if (_c) - _c = strdup(_c); + _c = xstrdup(_c); return _c; } -#endif static inline void kill_changed_things(void) { struct stService *p; @@ -335,9 +352,9 @@ if (!p->changed || !p->reconfig) continue; - if (p->master_socket) { + if (p->master_socket != -1) { close(p->master_socket); - p->master_socket = 0; + p->master_socket = -1; } for (j=0;jchanged = p->reconfig = 0; } } +#endif static inline void run(void) { @@ -368,9 +386,11 @@ } #ifdef MODIFY_CONFIG if (got_hup) { + syslog(LOG_INFO, "Received HANGUP signal"); got_hup = 0; read_config(); - kill_changed_things(); + /* kill_changed_things() needs proper analysis in read_config() */ + /* kill_changed_things(); */ continue; } #endif @@ -400,21 +420,25 @@ // struct stService *q; + stop_services(); + // Trash the old service list for (p=servicelist; p!=NULL; p=q) { q = p->next; - free(p); + xfree(p); } servicelist = NULL; #endif + syslog(LOG_INFO, "Reading configuration file \"" INETD_CONF "\""); + if (!(cfp = fopen(INETD_CONF,"r"))) { - perror("Can't open " INETD_CONF); + syslog(LOG_ERR, "Can't open \"" INETD_CONF "\": %m"); exit(1); } if (!(sfp = fopen(SERVICES,"r"))) { - perror("Can't open " SERVICES); + syslog(LOG_ERR, "Can't open \"" SERVICES "\": %m"); fclose(cfp); exit(1); } @@ -424,7 +448,7 @@ for (j=0; (j<11) && (args[j]); j++); if (j<6) { - fprintf(stderr,"Bad line in config file %s...\n", + syslog(LOG_ERR, "Bad line in config file: \"%s\"", args[0] ? args[0] : ""); continue; } @@ -434,9 +458,9 @@ sz += strlen(args[k]) + 1; // Must remember the terminator } - p = malloc(sz); + p = xmalloc(sz); if (p == NULL) { - fprintf(stderr, "Malloc failed\n"); + syslog(LOG_ERR, "Malloc failed"); continue; } bzero(p, sz); @@ -457,14 +481,14 @@ p->tcp = !strcmp(args[2], "tcp") ? 1 : 0; if (!(args = cfgfind(sfp, args[0]))) { - fprintf(stderr, "can't find service\n"); - free(p); + syslog(LOG_ERR, "can't find service \"%s\"", args[0]); + xfree(p); continue; } p->port = atoi(args[1]); - p->master_socket = 0; + p->master_socket = -1; p->limit = MAX_CONNECT; p->changed = 1; p->enabled = 1; @@ -480,7 +504,7 @@ * get the list processing correct too. */ for (p=servicelist; p!=NULL; p=p->next) - printf("service %s port %d %s %s\n", + syslog(LOG_DEBUG, "service \"%s\" port %d \"%s\" \"%s\"", p->args[0], p->port, p->tcp ? "tcp" : "udp", @@ -488,31 +512,30 @@ #endif } +int main(int argc, char *argv[], char *env[]) { -#ifdef EMBED - __signal(SIGPIPE, SIG_IGN, 0); - __signal(SIGSTOP, stop_handler, 0); - __signal(SIGTSTP, stop_handler, 0); - __signal(SIGCONT, cont_handler, 0); - __signal(SIGCHLD, child_handler, SA_INTERRUPT); + signal_flags(SIGPIPE, SIG_IGN, 0); + signal_flags(SIGSTOP, stop_handler, 0); + signal_flags(SIGTSTP, stop_handler, 0); + signal_flags(SIGCONT, cont_handler, 0); + signal_flags(SIGCHLD, child_handler, SA_RESTART); #ifdef MODIFY_CONFIG // This only makes sense if we've got a modifyable filesystem. - __signal(SIGHUP, hup_handler, 0); + signal_flags(SIGHUP, hup_handler, 0); +#else + // Protect ourselves against the loss of controlling terminal + signal_flags(SIGHUP, SIG_IGN, 0); #endif - /* - * we must have an open FD 0 as we use 0 to signify no-fd :-( - */ - if (fcntl(0, F_GETFL) == -1) - open("/dev/null", O_RDWR); -#endif - /* Hack to fix problems with not dealing cleanly with fds of 0 */ - open("/dev/null", O_RDWR); + openlog("inetd", LOG_PERROR | LOG_PID | LOG_NDELAY, LOG_DAEMON); + + close_all_fds(-1); read_config(); run(); /* not reached */ + return 0; }