summaryrefslogtreecommitdiff
path: root/src/biruda.c
blob: ff2f04601c6fff6e41ff3d720e1d52a1688dc9e1 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#else
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#endif

// command line parser
#include "biruda_cmdline.h"

// configuration parser

#include "confuse.h"

// daemonizing
#ifndef _WIN32
#include "libdaemon/daemon.h"
#endif

#include "system.h"
#include "master.h"
#include "worker.h"
#include "coordinator.h"

// for integrated webserver
#ifndef _WIN32
#include "webserver.h"
#endif

#include "cli.h"

#include "port.h"

#include "biruda_conf.c"

static const char *DEFAULT_CONFIG_FILE = "/etc/biruda/biruda.conf";
static const unsigned int DEFAULT_WEBSERVER_PORT = 8080;
static const char *DEFAULT_WEBSERVER_HOST = "localhost";
static const unsigned int DEFAULT_WEBSERVER_THREADS = 4;

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

	cmdline_parser_params_init( &params );

        cmdline_parser_init( args_info );

        if( cmdline_parser_ext( argc, argv, args_info, &params ) != 0 ) {
        	cmdline_parser_free( args_info );
        	return 1;
        }

        return 0;
}

static int test_config( const struct gengetopt_args_info *args_info )
{
	return 0;
}

static int conf_validate_worker_execution( cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result )
{
	if( strcmp( value, "disabled" ) == 0 ) {
		*(worker_execution_mode_t *)result = WORKER_EXECUTION_DISABLED;
	} else if( strcmp( value, "direct" ) == 0 ) {
		*(worker_execution_mode_t *)result = WORKER_EXECUTION_DIRECT;
	} else {
		cfg_error( cfg, "invalid value for worker execution mode option '%s': %s",
			cfg_opt_name( opt ), value );
		return -1;
	}
	
	return 0;
}

static int conf_validate_master( cfg_t *cfg, cfg_opt_t *opt )
{ 
	cfg_t *master = cfg_opt_getnsec( opt, cfg_opt_size( opt ) - 1 );
	
	if( cfg_size( master, "control" ) == 0 ) {
		cfg_error( cfg, "missing required option 'control' for master" );
		return -1;
	}

	if( cfg_size( master, "data" ) == 0 ) {
		cfg_error( cfg, "missing required option 'data' for master" );
		return -1;
	}
	
	return 0;
}

static int conf_validate_coordinator( cfg_t *cfg, cfg_opt_t *opt )
{ 
	cfg_t *coordinator = cfg_opt_getnsec( opt, cfg_opt_size( opt ) - 1 );
	
	if( cfg_size( coordinator, "control" ) == 0 ) {
		cfg_error( cfg, "missing required option 'control' for coordinator" );
		return -1;
	}
	
	return 0;
}

static int conf_validate_worker( cfg_t *cfg, cfg_opt_t *opt )
{ 
	cfg_t *worker = cfg_opt_getnsec( opt, cfg_opt_size( opt ) - 1 );

	if( cfg_size( worker, "control" ) == 0 ) {
		cfg_error( cfg, "missing required option 'control' for worker" );
		return -1;
	}

	if( cfg_size( worker, "data" ) == 0 ) {
		cfg_error( cfg, "missing required option 'data' for worker" );
		return -1;
	}

	worker_execution_mode_t mode = (worker_execution_mode_t)cfg_getint( worker, "execution" );

	switch( mode ) {
		case WORKER_EXECUTION_DISABLED:
			// skip, ok
			break;
		
		case WORKER_EXECUTION_DIRECT:		
			if( cfg_size( worker, "command" ) == 0 ) {
				cfg_error( cfg, "missing required option 'command' for worker '%s'",
					cfg_title( worker ) );
				return -1;
			}
	}
	
	return 0;
}

static int read_config( const char *filename, cfg_t **cfg )
{
	cfg_opt_t opts_master[] = {
		CFG_STR( "control", 0, CFGF_NODEFAULT ),
		CFG_STR( "data", 0, CFGF_NODEFAULT ),
		CFG_STR( "spool_dir", 0, CFGF_NODEFAULT ),
		CFG_END( )
	};

	cfg_opt_t opts_coordinator[] = {
		CFG_STR( "control", 0, CFGF_NODEFAULT ),
		CFG_END( )
	};

	cfg_opt_t opts_worker[] = {
		CFG_STR( "control", 0, CFGF_NODEFAULT ),
		CFG_STR( "data", 0, CFGF_NODEFAULT ),
		CFG_INT_CB( "execution", WORKER_EXECUTION_DISABLED, CFGF_NONE, &conf_validate_worker_execution ),
		CFG_STR( "command", NULL, CFGF_NODEFAULT ),
		CFG_END( )
	};
	 
	cfg_opt_t opts_webserver[] = {
		CFG_STR( "host", (char *)DEFAULT_WEBSERVER_HOST, CFGF_NONE ),
		CFG_INT( "port", DEFAULT_WEBSERVER_PORT, CFGF_NONE ),
		CFG_INT( "threads", DEFAULT_WEBSERVER_THREADS, CFGF_NONE ),
		CFG_END( )
	};
	
	cfg_opt_t opts[] = {
		CFG_SEC( "master", opts_master, CFGF_MULTI ),
		CFG_SEC( "coordinator", opts_coordinator, CFGF_MULTI ),
		CFG_SEC( "worker", opts_worker, CFGF_MULTI | CFGF_TITLE ),
		CFG_SEC( "webserver", opts_webserver, CFGF_MULTI ),
		CFG_END( )
	};
	
	*cfg = cfg_init( opts, CFGF_NONE );

	cfg_set_validate_func( *cfg, "master", &conf_validate_master );
	cfg_set_validate_func( *cfg, "coordinator", &conf_validate_coordinator );
	cfg_set_validate_func( *cfg, "worker", &conf_validate_worker );
	
	switch( cfg_parse( *cfg, filename ) ) {
		case CFG_FILE_ERROR:
			fprintf( stderr, "ERROR: configuration file '%s' could not be read: %s\n",
				filename, strerror( errno ) );
			return 1;

		case CFG_PARSE_ERROR:
			return 1;
			
		case CFG_SUCCESS:
			break;
	}
			
	return 0;
}

static void print_config( struct gengetopt_args_info *args_info, cfg_t *cfg )
{
	unsigned int nofCpus = system_available_cpus( );
	puts( "Environment:" );
	printf( "  Number of CPUs: %d\n", nofCpus );
	int physMem = system_phys_memory( );
	printf( "  Physically installed memory: %d kB\n", physMem );
	char machine_arch[100];
	system_arch( machine_arch, sizeof( machine_arch ) );
	printf( "  System architecture: %s\n", machine_arch );
	char os_name[100];
	system_os( os_name, sizeof( os_name ) );
	printf( "  Operating system: %s\n", os_name );
	puts( "" );
	
	cfg_t *master_cfg = cfg_getnsec( cfg, "master", 0 );
	unsigned int has_master = cfg_size( cfg, "master" );
	if( has_master ) {
		puts( "Master:" );
		printf( "  Control channel: %s\n", cfg_getstr( master_cfg, "control" ) );
		printf( "  Data channel: %s\n", cfg_getstr( master_cfg, "data" ) );
		printf( "  Spool directory: %s\n", cfg_getstr( master_cfg, "spool_dir" ) );
		puts( "" );
	}
	
	cfg_t *coordinator_cfg = cfg_getnsec( cfg, "coordinator", 0 );
	unsigned int has_coordinator = cfg_size( cfg, "coordinator" );
	if( has_coordinator ) {
		puts( "Coordinator:" );
		printf( "  Control channel: %s\n", cfg_getstr( coordinator_cfg, "control" ) );
		puts( "" );
	}
	
	unsigned int nof_workers = cfg_size( cfg, "worker" );
	if( nof_workers > 0 ) {
		puts( "Workers:\n" );
		for( unsigned int i = 0; i < nof_workers; i++ ) {
			cfg_t *worker_cfg = cfg_getnsec( cfg, "worker", i );
			
			printf( "  Worker %s:\n", cfg_title( worker_cfg ) );
			printf( "    Control channel: %s\n", cfg_getstr( worker_cfg, "control" ) );
			printf( "    Data channel: %s\n", cfg_getstr( worker_cfg, "data" ) );
			printf( "    Execution mode: %s\n", worker_exection_mode_str( (worker_execution_mode_t)cfg_getint( worker_cfg, "execution" ) ) );
			if( cfg_getint( worker_cfg, "execution" ) == WORKER_EXECUTION_DIRECT ) {
				char *command = cfg_getstr( worker_cfg, "command" );
				if( command != NULL ) {
					printf( "    Command: %s\n", cfg_getstr( worker_cfg, "command" ) );
				} else {
					printf( "    No command configured\n" );
				}
			}
			puts( "" );
		}
		puts( "" );
	}

	cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 );
	unsigned int has_webserver = cfg_size( cfg, "webserver" );	
	if( has_webserver > 0 ) {
#ifndef _WIN32	
		printf( "Webserver:\n" );
		printf( "  Host: %s\n", cfg_getstr( webserver_cfg, "host" ) );
		printf( "  Port: %ld\n", cfg_getint( webserver_cfg, "port" ) );
		printf( "  Number of threads: %ld\n", cfg_getint( webserver_cfg, "threads" ) );
		puts( "" );
#else
		fprintf( stderr, "WARNING: Currently no built-in webserver available for master on Windows!\n" );
#endif
	}
}

static int create_master( cfg_t *cfg )
{
	cfg_t *master_cfg = cfg_getnsec( cfg, "master", 0 );
	char *control = cfg_getstr( master_cfg, "control" );
	char *data = cfg_getstr( master_cfg, "data" );
	char *spool_dir = cfg_getstr( master_cfg, "spool_dir" );
	
	return master_init( control, data, spool_dir );
}

static int create_coordinator( cfg_t *cfg )
{
	cfg_t *master_cfg = cfg_getnsec( cfg, "coordinator", 0 );
	char *control = cfg_getstr( master_cfg, "control" );

	int ret = coordinator_init( control );

	unsigned int nof_workers = cfg_size( cfg, "worker" );
	if( nof_workers > 0 ) {
		for( unsigned int i = 0; i < nof_workers; i++ ) {
			cfg_t *worker_cfg = cfg_getnsec( cfg, "worker", i );
			coordinator_add_worker( cfg_title( worker_cfg ),
				(worker_execution_mode_t)cfg_getint( worker_cfg, "execution" ),
				cfg_getstr( worker_cfg, "command" ),
				cfg_getstr( worker_cfg, "control" ),
				cfg_getstr( worker_cfg, "data" ) );
		}
	}
	
	return ret;
}

#ifndef _WIN32
static int create_webserver( cfg_t *cfg )
{
	cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 );
	unsigned int port = cfg_getint( webserver_cfg, "port" );
	
	return webserver_init( port );
}
#endif

static volatile int got_terminate = 0;

#ifdef _WIN32
BOOL WINAPI terminate_foreground_func( DWORD ctrlType )
{
	switch( ctrlType ) {
		case CTRL_C_EVENT:
		case CTRL_BREAK_EVENT:
		case CTRL_CLOSE_EVENT:
		case CTRL_LOGOFF_EVENT:
		case CTRL_SHUTDOWN_EVENT:
			got_terminate = 1;
			return TRUE;
		default:
			return FALSE;
	}
}
#else
static void terminate_foreground_func( int sig ) 
{
	got_terminate = 1;
}
#endif

int main( int argc, char *argv[] )
{
	struct gengetopt_args_info args_info;
	cfg_t *cfg = NULL;
	
	if( parse_options_and_arguments( argc, argv, &args_info ) ) {
		exit( EXIT_FAILURE );
	}
	
	if( args_info.generate_config_given ) {
		printf( "%*s", biruda_conf_len, biruda_conf );
		exit( EXIT_SUCCESS );
	}
	
	if( args_info.guess_env_given ) {
		print_guessed_env( args_info.human_readable_given );
		exit( EXIT_SUCCESS );
	}

	if( !( args_info.cli_given || args_info.filename_given ) || args_info.config_file_given ) {
		if( read_config( args_info.config_file_given ?
			args_info.config_file_arg : DEFAULT_CONFIG_FILE, &cfg ) ) {
			if( cfg ) cfg_free( cfg );
			cmdline_parser_free( &args_info );
			return 1;
		}
	}

	if( args_info.test_given ) {
		if( cfg ) cfg_free( cfg );
		cmdline_parser_free( &args_info );
		return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
	}

	if( args_info.print_given ) {
		print_config( &args_info, cfg );
		if( cfg ) cfg_free( cfg );
		cmdline_parser_free( &args_info );
		return( ( test_config( &args_info ) == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
	}

	if( args_info.cli_given || args_info.filename_given ) {
		FILE *in = stdin;
		if( args_info.filename_given ) {
			in = fopen( args_info.filename_arg, "r" );
			if( in == NULL ) {
				fprintf( stderr, "ERROR: Unable to open command file '%s'\n", args_info.filename_arg );
				if( cfg ) cfg_free( cfg );
				cmdline_parser_free( &args_info );
				exit( EXIT_FAILURE );
			}
		}
		int ret;
		unsigned int port = DEFAULT_WEBSERVER_PORT;
		if( args_info.config_file_given ) {
			cfg_t *webserver_cfg = cfg_getnsec( cfg, "webserver", 0 );
			unsigned int has_webserver = cfg_size( cfg, "webserver" );	
			if( has_webserver > 0 ) {
				port = cfg_getint( webserver_cfg, "port" );
			}
		}
		
		const char *host = DEFAULT_WEBSERVER_HOST;
		if( args_info.inputs_num >= 1 ) {
			host = args_info.inputs[0];
		}
		if( args_info.inputs_num >= 2 ) {
			port = atoi( args_info.inputs[1] );
		}
		
#ifndef _WIN32
		ret = start_interactive( ( in == stdin ) ? !args_info.no_colors_given : false, host, port, in );
#else
		if( args_info.filename_given ) {
			ret = start_interactive( false, host, port, in );
		} else {
			fprintf( stderr, "FATAL: No CLI mode implemented for Windows currently!\n" );
			ret = -1;
		}
#endif
		if( cfg ) cfg_free( cfg );
		cmdline_parser_free( &args_info );
		if( in != stdin ) fclose( in );
		exit( ( ret == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
	}
	
	unsigned int has_master = cfg_size( cfg, "master" );
	if( has_master ) {
		if( create_master( cfg ) != 0 ) {
			fprintf( stderr, "FATAL: Unable to create master thread!\n" );
			if( cfg ) cfg_free( cfg );
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
	}

	unsigned int has_coordinator = cfg_size( cfg, "coordinator" );
	if( has_coordinator ) {
		if( create_coordinator( cfg ) != 0 ) {
			fprintf( stderr, "FATAL: Unable to create coordinator thread!\n" );
			if( cfg ) cfg_free( cfg );
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
	}
	
	unsigned int has_webserver = cfg_size( cfg, "webserver" );
	if( has_webserver ) {
#ifndef _WIN32
		if( create_webserver( cfg ) != 0 ) {
			fprintf( stderr, "FATAL: Unable to create webserver thread!\n" );
			if( cfg ) cfg_free( cfg );
			cmdline_parser_free( &args_info );
			exit( EXIT_FAILURE );
		}
#else
		fprintf( stderr, "WARNING: Currently no built-in webserver available for master on Windows!\n" );
#endif
	}

	if( args_info.foreground_given ) {
#ifdef _WIN32
		SetConsoleCtrlHandler( terminate_foreground_func, TRUE );
#else
		struct sigaction sa;

		memset( &sa, 0, sizeof( struct sigaction ) );
		sa.sa_handler = terminate_foreground_func;
		sa.sa_flags = SA_RESTART;
		
		(void)sigaction( SIGINT, &sa, NULL );
		(void)sigaction( SIGTERM, &sa, NULL );
#endif
		
		while( !got_terminate ) {
			sleep( 1 );
		}
	}
	
#ifndef _WIN32
	if( has_webserver ) {
		webserver_terminate( );
	}
#endif
	if( has_coordinator ) {
		coordinator_terminate( );
	}
	if( has_master ) {
		master_terminate( !has_coordinator );
	}
	
	if( has_coordinator ) {
		coordinator_free( );
	}
	if( has_master ) {
		master_free( );
	}
#ifndef _WIN32
	if( has_webserver ) {
		webserver_free( );
	}
#endif
	
	if( cfg ) cfg_free( cfg );
	cmdline_parser_free( &args_info );

	exit( EXIT_SUCCESS );
}