crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

main.c (3367B)


      1 /*************************************************************************
      2  * This file is part of Crosroads 1.23, a load balancer and fail over
      3  * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL.
      4  * Visit http://crossroads.e-tunity.com for information.
      5  *************************************************************************/
      6 
      7 #define EXTERN
      8 #include "crossroads.h"
      9 
     10 /* Explicit initializations. */
     11 
     12 int log_facility = LOG_DAEMON;
     13 
     14 char *state_to_string_map [] = {	/* backend states as strings */
     15     "available",
     16     "UNAVAILABLE",
     17     "DOWN",
     18     "waking",
     19     "intermediate update",
     20     "unknown",
     21     0,
     22 };
     23 
     24 int relevant_sigs[] = {			/* signals relevant to this app */
     25     SIGHUP,				/* either caught or ignored, */
     26     SIGINT,				/* depending on the stage */
     27     SIGQUIT,
     28     SIGABRT,
     29     SIGKILL,
     30     SIGPIPE,
     31     SIGTERM,
     32     0,
     33 };
     34 
     35 typedef struct {
     36     char *arg;				/* cmdline argument */
     37     int narg;				/* required remaining args */
     38     int needconf;			/* configuration file required? */
     39     int (*handler)(int ac, char **av);	/* action handler */
     40 } Handler;
     41 
     42 int main (int argc, char **argv) {
     43     int opt, i;    
     44     static Handler handler[] = {
     45 	// Argument	     Nr.args  Needconf  Handler function  
     46 	{ "services",		0,	1,	show_services	},
     47 	{ "status",		0,	1,	show_status	},
     48 	{ "stop",		0,	1,	stop_daemon	},
     49 	{ "start", 		0,	1,	serve		},
     50 	{ "restart",		0,	1,	restart 	},
     51 	{ "tell", 		3,	1,	tell_service 	},
     52 	{ "configtest",		0,	1,	configtest 	},
     53     };
     54 
     55     /* Remember original ac/av/ep */
     56     org_argc = argc;
     57     org_argv = argv;
     58 
     59     /* Parse options. */
     60     config_file = DEFAULT_CONF;
     61     while ( (opt = getopt (argc, argv, "?c:fhvi:Val:s")) > 0 )
     62 	switch (opt) {
     63 	case 'a':
     64 	    log_activity++;
     65 	    break;
     66 	case 'c':
     67 	    config_file = optarg;
     68 	    break;
     69 	case 'v':
     70 	    flag_verbose++;
     71 	    break;
     72 	case 'i':
     73 	    iflag_present++;
     74 	    break;
     75 	case 'l':
     76 	    switch (atoi (optarg)) {
     77 	    case 0:
     78 		log_facility = LOG_LOCAL0;
     79 		break;
     80 	    case 1:
     81 		log_facility = LOG_LOCAL1;
     82 		break;
     83 	    case 2:
     84 		log_facility = LOG_LOCAL2;
     85 		break;
     86 	    case 3:
     87 		log_facility = LOG_LOCAL3;
     88 		break;
     89 	    case 4:
     90 		log_facility = LOG_LOCAL4;
     91 		break;
     92 	    case 5:
     93 		log_facility = LOG_LOCAL5;
     94 		break;
     95 	    case 6:
     96 		log_facility = LOG_LOCAL6;
     97 		break;
     98 	    case 7:
     99 		log_facility = LOG_LOCAL7;
    100 		break;
    101 	    default:
    102 		usage();
    103 	    }
    104 	    break;
    105 	case 's':
    106 	    sloppyportbind++;
    107 	    break;
    108 	case 'V':
    109 	    puts (VER);
    110 	    exit (0);
    111 	case '?':
    112 	case 'h':
    113 	default:
    114 	    usage ();
    115 	}
    116 
    117     /* We need at last one argument: the action */
    118     if (optind >= argc)
    119 	usage();
    120 
    121     /* Act on the argument. */
    122     for (i = 0; i < sizeof(handler) / sizeof(Handler); i++) {
    123 	/* Match the handler action string with argv, and match the
    124 	 * required remaining arguments. If they match..
    125 	 * scan the configuration (if needed) and GO!
    126 	 */
    127 	if (!strcmp (argv[optind], handler[i].arg) &&
    128 	    handler[i].narg == argc - optind - 1) {
    129 	    if (handler[i].needconf) {
    130 		msg ("Parsing configuration %s", config_file);
    131 		if (! (yyin = fopen (config_file, "r")) )
    132 		    error ("Cannot read configuration %s: %s",
    133 			   config_file, strerror(errno));
    134 		yyparse();
    135 		fclose (yyin);
    136 	    }
    137 	    if (!handler[i].handler (argc - optind, argv + optind + 1))
    138 		error ("'%s' failed", argv[optind]);
    139 	    return (0);
    140 	}
    141     }
    142 
    143     usage();
    144     return (1);
    145 }