Subversion Repositories Mokou

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 nishi 1
/* $Id: main.c 7 2024-09-07 09:14:35Z nishi $ */
2
 
3
#include <stdio.h>
4
#include <unistd.h>
3 nishi 5
#include <stdlib.h>
4 nishi 6
#include <string.h>
7
#include <stdbool.h>
2 nishi 8
 
9
#include "mk_service.h"
7 nishi 10
#include "mk_server.h"
3 nishi 11
#include "mk_log.h"
12
#include "mk_util.h"
13
#include "mk_version.h"
2 nishi 14
 
4 nishi 15
extern bool mk_syslog;
16
 
2 nishi 17
int main(int argc, char** argv){
4 nishi 18
	int i;
7 nishi 19
	bool daemon = true;
4 nishi 20
	for(i = 1; i < argc; i++){
21
		if(argv[i][0] == '-'){
22
			if(strcmp(argv[i], "--stderr") == 0 || strcmp(argv[i], "-S") == 0){
23
				mk_syslog = false;
7 nishi 24
			}else if(strcmp(argv[i], "-D") == 0){
25
				daemon = false;
4 nishi 26
			}else{
27
				fprintf(stderr, "%s: %s: unknown flag\n", argv[0], argv[i]);
28
				return 1;
29
			}
30
		}
31
	}
2 nishi 32
	if(getuid() != 0){
33
		fprintf(stderr, "Run me as root.\n");
34
		return 1;
35
	}
3 nishi 36
	char* log = mk_strcat3("Mokou version ", mk_get_version(), " starting up");
37
	mk_log(log);
38
	free(log);
2 nishi 39
	mk_service_scan();
5 nishi 40
	mk_start_services();
41
	mk_log("Mokou is up, creating the server socket");
7 nishi 42
	if(mk_server_init() != 0){
43
		mk_log("Could not initialize the server");
44
		return 1;
45
	}
46
	unsigned long long pid = 0;
47
	if(daemon){
48
		mk_log("Spawning daemon");
49
		pid = fork();
50
	}
51
	if(pid == 0){
52
		if(daemon){
53
			mk_log("Hello from daemon");
54
		}
55
		mk_log("Entering server loop");
56
		mk_server_loop();
57
		return 0;
58
	}else if(daemon){
59
		FILE* f = fopen("/var/run/mokou.pid", "w");
60
		fprintf(f, "%llu", pid);
61
		fclose(f);
62
	}
2 nishi 63
}