From 600422d4c70829782cde54cd6b27fc25d6a24871 Mon Sep 17 00:00:00 2001 From: in2 Date: Thu, 20 Oct 2005 20:03:13 +0000 Subject: single daemon for all ports git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3228 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/mbbsd.c | 46 ++++++++++++++++++++++++++++++---------------- util/bbsctl.c | 26 +++++--------------------- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index 15625061..38716ad0 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -1490,45 +1490,59 @@ daemon_login(int argc, char *argv[], char *envp[]) int msock, csock; /* socket for Master and Child */ FILE *fp; int listen_port = 23; - int len_of_sock_addr, overloading = 0; + int len_of_sock_addr, overloading = 0, i; char buf[256]; #if OVERLOADBLOCKFDS int blockfd[OVERLOADBLOCKFDS]; - int i, nblocked = 0; + int nblocked = 0; #endif struct sockaddr_in xsin; xsin.sin_family = AF_INET; /* setup standalone */ - start_daemon(); - signal_restart(SIGCHLD, reapchild); /* choose port */ - if (argc == 1) + if( argc < 2 ) listen_port = 3006; - else if (argc >= 2) + else{ +#ifdef NO_FORK listen_port = atoi(argv[1]); - - snprintf(margs, sizeof(margs), "%s %d ", argv[0], listen_port); +#else + for( i = 1 ; i < (argc - 1) ; ++i ) + switch( fork() ){ + case -1: + perror("fork()"); + break; + case 0: + goto out; + default: + break; + } + out: + listen_port = atoi(argv[i]); +#endif + } /* port binding */ - msock = bind_port(listen_port); - if (msock < 0) { + if( (msock = bind_port(listen_port)) < 0 ){ syslog(LOG_INFO, "mbbsd bind_port failed.\n"); exit(1); } - initsetproctitle(argc, argv, envp); -#ifndef VALGRIND - setproctitle("%s: listening ", margs); -#endif /* Give up root privileges: no way back from here */ setgid(BBSGID); setuid(BBSUID); chdir(BBSHOME); + /* proctitle */ + initsetproctitle(argc, argv, envp); +#ifndef VALGRIND + snprintf(margs, sizeof(margs), "%s %d ", argv[0], listen_port); + setproctitle("%s: listening ", margs); +#endif + /* It's better to do something before fork */ #ifdef CONVERT big2gb_init(NULL); @@ -1540,7 +1554,6 @@ daemon_login(int argc, char *argv[], char *envp[]) #ifndef NO_FORK #ifdef PRE_FORK if( listen_port == 23 ){ // only pre-fork in port 23 - int i; for( i = 0 ; i < PRE_FORK ; ++i ) if( fork() <= 0 ) break; @@ -1548,7 +1561,8 @@ daemon_login(int argc, char *argv[], char *envp[]) #endif #endif - snprintf(buf, sizeof(buf), "run/mbbsd.%d.%d.pid", listen_port, (int)getpid()); + snprintf(buf, sizeof(buf), + "run/mbbsd.%d.%d.pid", listen_port, (int)getpid()); if ((fp = fopen(buf, "w"))) { fprintf(fp, "%d\n", (int)getpid()); fclose(fp); diff --git a/util/bbsctl.c b/util/bbsctl.c index 079e7738..7710795c 100644 --- a/util/bbsctl.c +++ b/util/bbsctl.c @@ -48,31 +48,15 @@ int HaveBBSADM(void) int startbbs(int argc, char **argv) { - int i; - char *port[] = {"3000", "3001", "3002", "3003", "3004", "3005", - "3006", "3007", "3008", "3009", "3010", NULL}; - pid_t pid; - - for( i = 0 ; port[i] != NULL ; ++i ){ - if( (pid = fork()) < 0 ){ - perror("fork()"); - return 1; - } - else if( pid == 0 ){ - printf("starting mbbsd at port %s\n", port[i]); - execl(BBSHOME "/bin/mbbsd", "mbbsd", port[i], NULL); - printf("start port[%s] failed\n", port[i]); - return 1; - } - } - if( setuid(0) < 0 ){ perror("setuid(0)"); exit(1); } - printf("starting mbbsd at port %s\n", "23"); - execl(BBSHOME "/bin/mbbsd", "mbbsd", "23", NULL); - printf("start port[%s] failed\n", "23"); + printf("starting mbbsd at 23, 443, 3000-3010\n"); + execl(BBSHOME "/bin/mbbsd", "mbbsd", "23", "443", + "3000", "3001", "3002", "3003", "3004", "3005", + "3006", "3007", "3008", "3009", "3010", NULL); + printf("starting mbbsd failed\n"); return 1; } -- cgit v1.2.3