From 3238906f14511128dfbf6512e8bbaa2332ff9f36 Mon Sep 17 00:00:00 2001 From: KyoungSoo Park Date: Fri, 27 Apr 2007 15:43:52 +0000 Subject: [PATCH] ready for RPM building --- codemux.c | 127 +++++++++++++++++++++++++++++++++++++++------ codemux.conf | 4 ++ codemux.initscript | 1 + codemux.spec | 32 ++++++------ 4 files changed, 134 insertions(+), 30 deletions(-) diff --git a/codemux.c b/codemux.c index c554cd0..3ca9ba0 100644 --- a/codemux.c +++ b/codemux.c @@ -17,7 +17,7 @@ #define CONF_FILE "/etc/codemux/codemux.conf" #define DEMUX_PORT 80 -#define REAL_WEBSERVER_CONFLINE "* root 1080" +#define PIDFILE "/var/run/codemux.pid" #define TARG_SETSIZE 4096 /* set aside some small number of fds for us, allow the rest for @@ -86,8 +86,6 @@ static int numNeedingHeaders; /* how many conns waiting on headers? */ static int numForks; -//HANDLE hdebugLog; - #ifndef SO_SETXID #define SO_SETXID SO_PEERCRED #endif @@ -281,14 +279,9 @@ ReadConfFile(void) int port; if (line != NULL) free(line); - - /* on the first pass, put in a fake entry for apache */ - if (num == 0) - line = strdup(REAL_WEBSERVER_CONFLINE); - else { - if ((line = GetNextLine(f)) == NULL) - break; - } + + if ((line = GetNextLine(f)) == NULL) + break; memset(&serv, 0, sizeof(serv)); if (WordCount(line) != 3) { @@ -303,6 +296,13 @@ ReadConfFile(void) serv.ss_host = GetWord(line, 0); serv.ss_slice = GetWord(line, 1); + + if (num == 0 && /* the first row must be an entry for apache */ + (strcmp(serv.ss_host, "*") != 0 || + strcmp(serv.ss_slice, "root") != 0)) { + fprintf(stderr, "first row has to be for webserver\n"); + exit(-1); + } if (num >= numAlloc) { numAlloc = MAX(numAlloc * 2, 8); servs = realloc(servs, numAlloc * sizeof(ServiceSig)); @@ -725,10 +725,10 @@ SocketReadyToRead(int fd) return; } - printf("trying to find service\n"); + // printf("trying to find service\n"); if (FindService(fb, &whichService, si->si_cliAddr) != SUCCESS) return; - printf("found service %d\n", whichService); + // printf("found service %d\n", whichService); slice = ServiceToSlice(whichService); /* no service can have more than some absolute max number of @@ -743,10 +743,15 @@ SocketReadyToRead(int fd) } if (slice->si_xid > 0) { + static int first = 1; setsockopt(fd, SOL_SOCKET, SO_SETXID, &slice->si_xid, sizeof(slice->si_xid)); - fprintf(stderr, "setsockopt() with XID = %d name = %s\n", - slice->si_xid, slice->si_sliceName); + if (first) { + /* just to log it for once */ + fprintf(stderr, "setsockopt() with XID = %d name = %s\n", + slice->si_xid, slice->si_sliceName); + first = 0; + } } si->si_needsHeaderSince = 0; @@ -917,17 +922,109 @@ MainLoop(int lisSock) } } /*-----------------------------------------------------------------*/ +static int +InitDaemon(void) +{ + pid_t pid; + FILE *pidfile; + + pidfile = fopen(PIDFILE, "w"); + if (pidfile == NULL) { + fprintf(stderr, "%s creation failed\n", PIDFILE); + return(-1); + } + + if ((pid = fork()) < 0) { + fclose(pidfile); + return(-1); + } + else if (pid != 0) { + /* i'm the parent, writing down the child pid */ + fprintf(pidfile, "%u\n", pid); + fclose(pidfile); + exit(0); + } + + /* close the pid file */ + fclose(pidfile); + + /* routines for any daemon process + 1. create a new session + 2. change directory to the root + 3. change the file creation permission + */ + setsid(); + chdir("/"); + umask(0); + + return(0); +} +/*-----------------------------------------------------------------*/ +static int +OpenLogFile(void) +{ + static const char* logfile = "/var/log/codemux.log"; + static const char* oldlogfile = "/var/log/codemux.log.old"; + int logfd; + + /* if the previous log file exists, + rename it to the oldlogfile */ + if (access(logfile, F_OK) == 0) { + if (rename(logfile, oldlogfile) < 0) { + fprintf(stderr, "cannot rotate the logfile err=%s\n", + strerror(errno)); + exit(-1); + } + } + + logfd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0600); + if (logfd < 0) { + fprintf(stderr, "cannot open the logfile err=%s\n", + strerror(errno)); + exit(-1); + } + + /* duplicate logfile to stderr */ + if (dup2(logfd, STDERR_FILENO) != STDERR_FILENO) { + fprintf(stderr, "cannot open the logfile err=%s\n", + strerror(errno)); + exit(-1); + } + + /* set the close-on-exec flag */ + if (fcntl(STDERR_FILENO, F_SETFD, 1) != 0) { + fprintf(stderr, "fcntl to set the close-on-exec flag failed err=%s\n", + strerror(errno)); + exit(-1); + } + + return logfd; +} +/*-----------------------------------------------------------------*/ int main(int argc, char *argv[]) { int lisSock; + int logFd; + /* do the daemon stuff */ + if (argc <= 1 || strcmp(argv[1], "-d") != 0) { + if (InitDaemon() < 0) { + fprintf(stderr, "codemux daemon_init() failed\n"); + exit(-1); + } + } + + /* create the accept socket */ if ((lisSock = CreatePrivateAcceptSocket(DEMUX_PORT, TRUE)) < 0) { fprintf(stderr, "failed creating accept socket\n"); exit(-1); } SetFd(lisSock, &masterReadSet); + /* open the log file */ + logFd = OpenLogFile(); + while (1) { numForks++; if (fork()) { diff --git a/codemux.conf b/codemux.conf index b5c86fb..f027947 100644 --- a/codemux.conf +++ b/codemux.conf @@ -1,3 +1,7 @@ +# regular option: +# format is "domain_name" "slice_name" "port + +* root 1080 # this is for the Apache webserver coblitz.codeen.org princeton_coblitz 3125 cdn.rd.tp.pl princeton_coblitz 3125 nyud.net nyu_d 8080 diff --git a/codemux.initscript b/codemux.initscript index 5a69a70..2780e67 100644 --- a/codemux.initscript +++ b/codemux.initscript @@ -45,6 +45,7 @@ case "$1" in stop) echo -n "shutting down $PROC: " killproc $PROC + killproc $PROC RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROC diff --git a/codemux.spec b/codemux.spec index b212ece..337de1c 100644 --- a/codemux.spec +++ b/codemux.spec @@ -1,12 +1,16 @@ -Summary: CoDemux - HTTP port demultiplexer +%define name proper +%define version 0.1 +%define release 3%{?pldistro:.%{pldistro}}%{?date:.%{date}} + +Summary: CoDemux - HTTP port DeMux Name: codemux -Version: 0.4 -Release: 1 +Version: %{version} +Release: %{release} License: Private Group: System Environment/Base URL: http://codeen.cs.princeton.edu/ Source0: %{name}-%{version}.tar.gz -BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root +BuildRoot: %{_tmppath}/%{name}-buildroot %description @@ -21,13 +25,13 @@ make %install rm -rf $RPM_BUILD_ROOT -make install +make INSTALL_ROOT=$RPM_BUILD_ROOT install %clean rm -rf $RPM_BUILD_ROOT %files -%defattr(0644,root,root,) +%defattr(0644,root,root) %attr(0755,root,root) %{_initrddir}/codemux %config /etc/codemux/codemux.conf %attr(0755,root,root) /usr/local/planetlab/sbin/codemux @@ -36,22 +40,20 @@ rm -rf $RPM_BUILD_ROOT chkconfig codemux reset if [ -z "$PL_BOOTCD" ]; then - /sbin/ldconfig /etc/init.d/codemux restart fi %preun -if [ "$1" = 0 ]; then +#if [ "$1" = 0 ]; then # erase, not upgrade - chkconfig --del codemux +# chkconfig --del codemux # stop daemon if its currently running - if [ "`/etc/init.d/codemux status`" = "running" ]; then - /etc/init.d/codemux stop - fi -fi - -%doc +# if [ "`/etc/init.d/codemux status`" = "running" ]; then +# /etc/init.d/codemux stop +# fi +#fi +# %doc %changelog * Sun Apr 22 2007 KYOUNGSOO PARK - -- 2.43.0