aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 1f6f58390601642aa7d063948ff7f62371d43ecc (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
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * main.c
 * (c) 2019, Jonas Gunz, jonasgunz.de
 * <Description>
 * License: MIT
*/

#include "main.h"

int main(int argc, char* argv[])
{
	struct prog_params params = parse_args(argc, argv);
	
	log_init_stdout( _LOG_DEBUG );

	signal( SIGCHLD, SIG_IGN ); //Ignore sigchld
	signal( SIGKILL, terminate);
	signal( SIGINT, terminate);

	//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_NOTE, "Forked with PID %i\n\n", pid );
			
			if( log_fd == STDIN_FILENO )
				LOGPRINTF( _LOG_WARNING, "STDIO will be closed but no logfile is set. No logging possible\n" );
			
			fclose ( pidfile );
			exit( 0 );
		}

		fclose (pidfile);

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

	LOGPRINTF(_LOG_DEBUG, "command: %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;
}

void terminate()
{
	log_close();
	exit ( 1 );
}