/*****************************************************************************************
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;
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 t = 0x0;
time_t seconds = 0;
struct timeval timeout = {30,0};
/* Get our start time */
startTime = time(NULL);
while ((ch = getopt(argc,argv,"sl:nd:")) != -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;
default:
usage();
}
}
argc -= optind;
argv += optind;
parseConfig();
/* Prepare log file */
if (logFile == 0x0) {
sprintf(logFileName,"./logs/tuved.%i.log",time(NULL));
logFile = fopen(logFileName,"a");
}
/* Fork into background unless specified not to do so */
if (doFork == 1)
if (fork() != 0x0)
exit(0x1);
writeLog(0,"Podz 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[t], NULL, tuveCMD_Thread, (void *)t);
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(); */