summaryrefslogtreecommitdiff
path: root/tests/daemon/testd.c
blob: 92dfc35cdce635c94fe1473b26a20654b4c11580 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "port/stdbool.h"	/* for bool */

#include <unistd.h>		/* for exit, unistd, getuid, getppid */
#include <stdlib.h>		/* for EXIT_FAILURE, EXIT_SUCCESS */
#include <string.h>		/* for memset */

#include "errors.h"		/* global error codes */
#include "log.h"		/* logging facility */
#include "daemon/daemon.h"	/* Unix daemonizing code */
#include "daemon/signals.h"	/* signal supension */

#include "port/unused.h"

#include "testd_cmdline.h"	/* for command line and option parsing (gengetopt) */

#define DEFAULT_CONFIG_FILE "/etc/" CMDLINE_PARSER_PACKAGE ".conf"

static int parse_options_and_arguments( int argc, char *argv[], struct gengetopt_args_info *args_info ) {
        cmdline_parser_init( args_info );

        if( cmdline_parser2( argc, argv, args_info, 1, 0, 1 ) != 0 ) {
        	cmdline_parser_free( args_info );
        	return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}

static int test_config( const char *filename ) {
	UNUSED( filename );
	return EXIT_SUCCESS;
}

static int read_config( const char *filename, struct gengetopt_args_info *args_info ) {
	char *config_filename = strdup( filename );
	if( cmdline_parser_configfile( config_filename, args_info, 1, 0, 1 ) != 0 ) {
		fprintf( stderr, "\n%s\n", gengetopt_args_info_usage );
		cmdline_parser_free( args_info );
		free( config_filename );
		return EXIT_FAILURE;
	}
	free( config_filename );
	
	return EXIT_SUCCESS;
}

int main( int argc, char *argv[] ) {
	struct gengetopt_args_info args_info;
	wolf_error_t error;
	daemon_params_t daemon_params;
	daemon_p demon = NULL;
	int sig = 0;

	if( parse_options_and_arguments( argc, argv, &args_info ) == EXIT_FAILURE ) {
		exit( EXIT_FAILURE );
	}

	if( read_config( args_info.config_file_given ?
		args_info.config_file_arg : DEFAULT_CONFIG_FILE, &args_info ) == EXIT_FAILURE ) {
		exit( EXIT_FAILURE );
	}
	
	if( args_info.test_given ) {
		cmdline_parser_free( &args_info );
		exit( test_config( args_info.config_file_given ?
			args_info.config_file_arg : DEFAULT_CONFIG_FILE ) );
	}

	openlogtostderr( log_DEBUG - 1 + (int)args_info.debug_given );
	if( args_info.logfile_given )
		openlogtofile( args_info.logfile_arg,
		args_info.logfile_level_given ?
		log_str_to_level( args_info.logfile_level_arg ) : log_NOTICE );
	
	if( !args_info.foreground_given ) {
		openlogtosyslog( CMDLINE_PARSER_PACKAGE,
			args_info.syslog_facility_given ?
			log_str_to_syslog_facility( args_info.syslog_facility_arg ) : log_DAEMON,
			args_info.syslog_level_given ?
			log_str_to_level( args_info.syslog_level_arg ) : log_NOTICE );

		memset( &daemon_params, 0, sizeof( daemon_params ) );
		daemon_params.daemon_name = CMDLINE_PARSER_PACKAGE;
		daemon_params.pid_filename = args_info.pidfile_given ?
			args_info.pidfile_arg : NULL;
		daemon_params.group_name = args_info.group_given ?
			args_info.group_arg : NULL;
		daemon_params.user_name = args_info.user_given ?
			args_info.user_arg : NULL;
			
		demon = daemon_new( daemon_params, &error );
		if( demon == NULL ) {
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
	
		if( ( error = daemon_start( demon ) ) != WOLF_OK ) {
			cmdline_parser_free( &args_info );
			daemon_exit( demon );
		}
	} else {
		if( ( error = signal_initialize( ) ) != WOLF_OK ) {
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
		if( ( error = signal_install_handlers_daemon( ) ) != WOLF_OK ) {
			signal_terminate( );
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
	}

	LOG( log_NOTICE, "Started %s daemon", CMDLINE_PARSER_PACKAGE );
	while( ( sig != SIGTERM ) && ( sig != SIGINT ) && ( sig != -1 ) ) {
		sig = signal_suspend( 60, &error );
		switch( sig ) {
			case 0: /* timeout */
				break;

			case -1: /* internal error */
				break;

			case SIGHUP:
				LOG( log_NOTICE, "Rereading configuration" );
				break;
				
			case SIGTERM:
			case SIGINT:
				LOG( log_NOTICE, "Got termination signal, shutting down the daemon" );
				break;
			
			case SIGUSR1:
				break;
			
			case SIGUSR2:
				break;
			
			default:
				LOG( log_ERR, "Unexpected signal '%s' (%s, %d)",
					signal_get_long_name( sig ),
					signal_get_short_name( sig ),
					sig );
		}
	}
	LOG( log_NOTICE, "Stopped %s daemon", CMDLINE_PARSER_PACKAGE );

	if( !args_info.foreground_given ) {
		cmdline_parser_free( &args_info );
		daemon_exit( demon );
	} else {
		cmdline_parser_free( &args_info );
		signal_terminate( );
		exit( EXIT_SUCCESS );
	}

	exit( EXIT_FAILURE );
}