aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: fbb8ee959dd31823fa424cfc930b79ae1a8c76f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * main.c
 * (c) 2019, Jonas Gunz, jonasgunz.de
 * <Description>
 * License: MIT
*/

#include "main.h"

int main(int argc, char* argv[])
{
	log_init_stdout(_LOG_DEBUG);

	signal(SIGCHLD,SIG_IGN); //Ignore sigchld
	struct prog_params params = parse_args(argc, argv);

	//Fork and write PID to pidfile
	if(params.fork)
	{
		FILE* pidfile = fopen(params.pidfile, "w");

		if(!pidfile)
		{
			LOGPRINTF(_LOG_ERROR,"Unable to open pidfile\n");
			exit(1);
		}
		pid_t pid = fork();

		if(pid < 0)
		{
			LOGPRINTF(_LOG_ERROR,"fork failed\n");
			exit(1);
		}
		else if(pid > 0)
		{
			fprintf(pidfile, "%i", pid);
			LOGPRINTF(_LOG_INFO,"Forked with PID %i\n\n", pid);
			fclose (pidfile);
			exit(0);
		}

		fclose (pidfile);

		//Close STDIO
		close (STDIN_FILENO);
		close (STDOUT_FILENO);
		close (STDERR_FILENO);
	}//if params.fork

	LOGPRINTF(_LOG_DEBUG, "%s, %i\n", params.run_argv[0], params.run_argc);

	if ( params.serial )
		dialup_server(params);
	else if (params.telnet)
		telnet_server(params);

	return 0;
}