/***************************************************************************************** Copyright (c) 2002-2004, 2005, 2007 Ubix Technology Group All rights reserved. $Id: main.c,v 1.21 2008/06/30 18:55:10 reddawg Exp $ *****************************************************************************************/ #include <stdio.h> #include <stdarg.h> #include <sys/select.h> #include <time.h> #include "tuved.h" FILE *logFile = 0x0; FILE *pidFile = 0x0; int logLevel = 1; pthread_mutex_t chanMutex = PTHREAD_MUTEX_INITIALIZER; time_t startTime; int userCount = 0; int channelCount = 0; int songCount = 0; int playCount = 0; char logFileName[64]; void usage(); int main(int argc,char **argv) { fd_set readset; int ch; int readSocks = 0x0; int doFork = 0x1; int rc = 0x0; int *param = (int *)malloc(2 * sizeof(int)); time_t seconds = 0; struct timeval timeout = {30,0}; /* Get our start time */ startTime = time(NULL); while ((ch = getopt(argc,argv,"sl:nd:c:")) != -1) { switch (ch) { case 's': logFile = stdout; break; case 'l': logFile = fopen(optarg,"a"); break; case 'n': doFork = 0; break; case 'd': logLevel = atoi(optarg); if (logLevel < 1) logLevel = 1; break; case 'c': break; default: usage(); } } argc -= optind; argv += optind; parseConfig(); /* Prepare log file */ if (logFile == 0x0) { // XXX MrOlsen: Do I want to use time any more? - sprintf(logFileName,"/var/log/tuved.%i.log", time(NULL)); sprintf(logFileName,"/var/log/tuved.log"); logFile = fopen(logFileName,"a"); } /* Fork into background unless specified not to do so */ if (doFork == 1) if (fork() != 0x0) exit(0x0); /* Create PID file */ pidFile = fopen("/var/run/tuved.pid", "w"); fprintf(pidFile, "%d", getpid()); fclose(pidFile); writeLog(0,"TUve Daemon Starting: %i\n",startTime); if (dbInit() == 0x1) exit(0x1); /* Reset Server Sessions */ dbQuery("DELETE FROM active",0); srandom(time(NULL)); pthread_mutex_init(&chanMutex, NULL); pthread_t threads[NUM_THREADS]; rc = pthread_create(&threads[0], NULL, tuveCMD_Thread, param); sStartListener(); while (1) { /* Get Socket Set For Readable Connections */ if (sGetConnections(&readset) == 0x0) { writeLog(0,"Error: sGetConnections Failed\nShutting Down\n"); exit(0x1); } readSocks = select(highSock+1,&readset,0x0,0x0,&timeout); if (readSocks < 0) { writeLog(0,"Error: select Failed\nShutting Down\n"); exit(0x1); } else if (readSocks > 0) { sProcessConnections(&readset); } if ((seconds + PING_INTERVAL) < 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: tuved -n -s [-l logfile] [-d debuglevel]"); exit(1); } int writeLog(int level,char const * __restrict fmt, ...) { int ret; va_list ap; if (level > logLevel) return(0x0); va_start(ap, fmt); ret = vfprintf(logFile, fmt, ap); va_end(ap); return(ret); } /* End writeLog(); */