Newer
Older
tuved / main.c
@reddawg reddawg on 3 Oct 2007 1 KB Changed some files around
/*
  Podz Server

  $Id$
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/select.h>
#include <unistd.h>
#include <time.h>

#include "tuved.h"

FILE *logFile = 0x0;

void usage();

int main(int argc,char **argv) {
  fd_set readset;
  int ch;
  int readSocks = 0x0;
  int doFork    = 0x1;
  time_t seconds;
  struct timeval timeout = {30,0};

  /* Get our start time */
  seconds = time(NULL);

  while ((ch = getopt(argc,argv,"sl:n")) != -1) {
    switch (ch) {
      case 's':
        logFile = stdout;
        break;
      case 'l':
        logFile = fopen(optarg,"a");
        break;
      case 'n':
        doFork = 0;
        break;
      default:
        usage();
      }
    }
  argc -= optind;
  argv += optind;


  /* Prepare log file */
  if (logFile == 0x0)
    logFile = fopen(LOG_FILE,"a");

  /* Fork into background unless specified not to do so */
  if (doFork == 1)
    if (fork() != 0x0)
      exit(1);

  writeLog("Podz Daemon Starting: %i\n",seconds);

  sStartListener();

  while (1) {
    sGetConnections(&readset);

    readSocks = select(highSock+1,&readset,0x0,0x0,&timeout);

    #ifdef DEBUG
    writeLog("readSocks: [%i]\n",readSocks);
    #endif

    if (readSocks < 0) {
      perror("select failed");
      exit(0x1);
      }
    else if (readSocks > 0) {
      sProcessConnections(&readset);
      }
  
    if ((seconds + 30) < time(NULL)) {
      sSendPing(seconds);
      seconds = time(NULL);
      }
      
    /* Clean up old connections */
    sCleanConnections();
    
    /* Flush the log file */
    fflush(logFile);
    }
    
  return(0);
  }

void usage() {
  printf("%s\n","usage: podzd -n [-l logfile]");
  exit(1);
  }


int writeLog(char const * __restrict fmt, ...) {
  int ret;
  va_list ap;

  va_start(ap, fmt);
  ret = vfprintf(logFile, fmt, ap);
  va_end(ap);
  
  return(ret);
  }