hlswmaster/src/main.c

130 lines
3.6 KiB
C
Raw Normal View History

2006-02-02 16:25:55 +01:00
/***************************************************************************
* Copyright (C) 03/2005 by Olaf Rempel *
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2006-02-02 16:24:06 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <getopt.h>
#include <sys/stat.h>
#include <pwd.h>
2006-02-02 16:24:06 +01:00
#include "hlswmaster.h"
#include "configfile.h"
2006-02-02 16:24:06 +01:00
static struct option opts[] = {
{"config", 1, 0, 'c'},
{"user", 1, 0, 'u'},
{"debug", 0, 0, 'd'},
2006-02-02 16:24:06 +01:00
{"help", 0, 0, 'h'},
{0, 0, 0, 0}
};
int main(int argc, char *argv[]) {
pthread_t thread1, thread2, thread3;
int arg = 0, code = 0, debug = 0;
char *config = NULL, *user = NULL, *logfile;
2006-02-02 16:24:06 +01:00
while (code != -1) {
code = getopt_long(argc, argv, "c:u:dh", opts, &arg);
2006-02-02 16:24:06 +01:00
switch (code) {
case 'c': /* config */
config = optarg;
2006-02-02 16:24:06 +01:00
break;
case 'u': /* user */
user = optarg;
2006-02-02 16:24:06 +01:00
break;
case 'd': /* debug */
debug = 1;
2006-02-02 16:24:06 +01:00
break;
case 'h': /* help */
printf("Usage: hlsw-master [options]\n"
"Options: \n"
" --config -c configfile use this configfile\n"
" --user -u username change uid to username\n"
" --debug -d do not fork and log to stderr\n"
2006-02-02 16:24:06 +01:00
" --help -h this help\n"
"\n");
exit(0);
break;
case '?': /* error */
exit(-1);
break;
default: /* unknown / all options parsed */
break;
}
}
/* userwechsel */
if (user) {
struct passwd *pwl;
if (!(pwl = getpwnam(user))) {
log_print("unknown user: %s", user);
exit(-1);
}
if (setgid(pwl->pw_gid) || setuid(pwl->pw_uid)) {
log_print("setgid/setuid");
exit(-1);
}
}
/* parse config file */
if (!config_parse(config))
exit(-1);
2006-02-02 16:24:06 +01:00
/* check logfile */
logfile = config_get_string("global", "logfile", NULL);
if (logfile && !debug) {
/* start logging */
if (!log_init(logfile))
exit(-1);
/* zum daemon mutieren */
daemon(-1, 0);
}
log_print("hlswmaster started (user: %s, pid: %d)", getpwuid(getuid())->pw_name, getpid());
/* init scan engine */
if (!scan_init())
exit(-1);
2006-02-02 16:24:06 +01:00
/* load plugins */
plugin_load_all();
/* startup threads */
pthread_create(&thread1, NULL, (void *)&scan_control, NULL);
2006-02-02 16:27:19 +01:00
pthread_create(&thread2, NULL, (void *)&scan_receive, NULL);
pthread_create(&thread3, NULL, (void *)&client_handler, NULL);
2006-02-02 16:24:06 +01:00
/* wait untill d00msday */
while (1)
sleep(3600);
2006-02-02 16:24:06 +01:00
return 0;
}