Beispiel #1
0
/* Check and parse config file */
int get_bootinfo(struct cfgdata_t *cfgdata)
{
	struct stat sinfo;

	/* Clean cfgdata structure */
	init_cfgdata(cfgdata);

	/* Parse config file */
	if (0 == parse_cfgfile(BOOTCFG_PATH, cfgdata)) {	/* Found and parsed */
		log_msg(lg, "+ config file found");
		/* Check kernel presence
		 * FIXME: we should stat every kernel or shouldn't stat at all
		if (0 == stat(cfgdata->kernelpath, &sinfo)) return 0;

		log_msg(lg, "+ config file points to non-existent kernel");
		return -1;
		*/
		return 0;

	} else {	/* No config file found. Check kernels. */

#ifdef USE_MACHINE_KERNEL
		/* Check machine kernel if set */
		if (NULL != machine_kernel) {
			if (0 == stat(machine_kernel, &sinfo)) {
				cfgdata_add_kernel(cfgdata, machine_kernel);
				log_msg(lg, "+ found machine kernel '%s'", machine_kernel);
				return 0;
			}
		}
#endif

		/* Check default kernels */
		char **kp;
		for (kp = default_kernels; NULL != *kp; kp++) {
			if (0 == stat(*kp, &sinfo)) {
				cfgdata_add_kernel(cfgdata, *kp);
				log_msg(lg, "+ found default kernel '%s'", *kp);
				return 0;
			}
		}
	}

	/* We have no kernels */
	log_msg(lg, "+ no config file nor any kernels found");
	return -1;
}
Beispiel #2
0
/* this is the main body */
int main(int argc, char *argv[])
{

	if (cfg.verbose)
		printf("%s version %s\n", APPNAME, VERSION);

	/* fill the cfg struct with the default options */
	fill_default_options();
	/* parse options from config file */
	parse_cfgfile(cfg.cfgfile);
	/* parse the command line options */
	parse_opts(argc, argv);

	odbc_alloc();
	connect_db(cfg.dsn, cfg.dbuser, cfg.dbpass);
	get_embedded_infos();
	disconnect_db();

	/* if cfg.daemon option is set to 1 we go to background */
	if (cfg.daemon)
		daemon(0, 0);

	/* set signal hook function */
	signal(SIGHUP, &destroyall);
	signal(SIGINT, &destroyall);
	signal(SIGQUIT, &destroyall);
	signal(SIGTERM, &destroyall);
	signal(SIGABRT, &destroyall);

	/* open and configure the serial port */
	cfgport();
	/* enter the main loop */
	serial_mainloop();

	odbc_free();
	disconnect_db();
	free_cfg();
	free_emb_info();

	exit(0);
}
Beispiel #3
0
int
main(int argc, char *argv[])
{
  CREW    crew;
  int     i;
  int     count = 0;
  int     res;
  BOOLEAN result;
  char ** keys;
  void *  statusp;

  C = new_conf();
  parse_cmdline_cfg(C, argc, argv);
  parse_cfgfile(C);
  parse_cmdline(C, argc, argv);
  
  RUNNER R = new_runner(conf_get_user(C), conf_get_group(C));
  runas(R);
  runner_destroy(R);
  sigmasker();
  
  if (is_daemon(C)) {
    res = fork();
    if (res == -1 ){
      // ERRROR
      NOTIFY(FATAL, "%s: [error] unable to run in the background\n", program_name);
    } else if (res == 0) {
      // CHILD
      PID  P = new_pid(conf_get_pidfile(C));
      HASH H = conf_get_items(C);
      count  = conf_get_count(C);
      keys   = hash_get_keys_delim(H, ':');
      if ((crew = new_crew(count, count, FALSE)) == NULL) {
        NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count);
      }
      set_pid(P, getpid());
      pid_destroy(P);
      for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) {
        FIDO F = new_fido(C, keys[i]);
        result = crew_add(crew, (void*)start, F);
        if (result == FALSE) {
          NOTIFY(FATAL, "%s: [error] unable to spawn additional threads", program_name);
        }
      }
      crew_join(crew, TRUE, &statusp);
      conf_destroy(C);
    } else {
      // PARENT 
    }
  } else {
    HASH H = conf_get_items(C);
    count  = conf_get_count(C);
    keys   = hash_get_keys_delim(H, ':');

    if ((crew = new_crew(count, count, FALSE)) == NULL) {
      NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count);
    }
    for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) {
      FIDO F = new_fido(C, keys[i]);
      result = crew_add(crew, (void*)start, F);
    }
    crew_join(crew, TRUE, &statusp);
    conf_destroy(C);
  } 
  exit(EXIT_SUCCESS);
} /* end of int main **/
Beispiel #4
0
static error_t parse_opt(int k, char *arg, struct argp_state *state)
{
	char *p;
	long key;
	int rc;

	switch (k) {
	case 'v':
		debug++;
		break;
	case 't':
		test++;
		break;
	case 'c':
		clear++;
		break;
	case 'D':
		delay = atoi(arg);
		break;
	case 'P':
		period = atoi(arg);
		break;
	case 'd':
		devicename = arg;
		break;
	case 's':
		devclass = arg;
		break;
	case 'r':
		readtable++;
		break;
	case 'w': {
		char *name = NULL;

		rc = parse_keyfile(arg, &name);
		if (rc)
			goto err_inval;
		if (name)
			fprintf(stderr, "Read %s table\n", name);
		break;
	}
	case 'a': {
		rc = parse_cfgfile(arg);
		if (rc)
			goto err_inval;
		break;
	}
	case 'k':
		p = strtok(arg, ":=");
		do {
			if (!p)
				goto err_inval;
			nextkey->codes[0] = strtoul(p, NULL, 0);
			if (errno)
				goto err_inval;

			p = strtok(NULL, ",;");
			if (!p)
				goto err_inval;
			key = parse_code(p);
			if (key == -1) {
				key = strtol(p, NULL, 0);
				if (errno)
					goto err_inval;
			}
			nextkey->codes[1] = key;

			if (debug)
				fprintf(stderr, "scancode 0x%04x=%u\n",
					nextkey->codes[0], nextkey->codes[1]);

			nextkey->next = calloc(1, sizeof(keys));
			if (!nextkey->next) {
				perror("No memory!\n");
				return ENOMEM;
			}
			nextkey = nextkey->next;

			p = strtok(NULL, ":=");
		} while (p);
		break;
	case 'p':
		p = strtok(arg, ",;");
		do {
			if (!p)
				goto err_inval;
			if (!strcasecmp(p,"rc5") || !strcasecmp(p,"rc-5"))
				ch_proto |= RC_5;
			else if (!strcasecmp(p,"rc6") || !strcasecmp(p,"rc-6"))
				ch_proto |= RC_6;
			else if (!strcasecmp(p,"nec"))
				ch_proto |= NEC;
			else if (!strcasecmp(p,"jvc"))
				ch_proto |= JVC;
			else if (!strcasecmp(p,"sony"))
				ch_proto |= SONY;
			else if (!strcasecmp(p,"sanyo"))
				ch_proto |= SANYO;
			else if (!strcasecmp(p,"lirc"))
				ch_proto |= LIRC;
			else if (!strcasecmp(p,"rc-5-sz"))
				ch_proto |= RC_5_SZ;
			else if (!strcasecmp(p,"sharp"))
				ch_proto |= RC_5_SZ;
			else if (!strcasecmp(p,"mce-kbd"))
				ch_proto |= RC_5_SZ;
			else if (!strcasecmp(p,"xmp"))
				ch_proto |= XMP;
			else if (!strcasecmp(p,"all"))
				ch_proto |= ~0;
			else
				goto err_inval;
			p = strtok(NULL, ",;");
		} while (p);
		break;
	default:
		return ARGP_ERR_UNKNOWN;
	}
	return 0;

err_inval:
	fprintf(stderr, "Invalid parameter(s)\n");
	return ARGP_ERR_UNKNOWN;

}
Beispiel #5
0
/*
 * -- configure
 *
 * do a first pass of the command line parameters to find all
 * configuration files. the options in those files are processed
 * before any other command line parameter. command line will
 * overwrite any other configuration, as well as the last config
 * file will overwrite previous config files.
 *
 */
void
configure(struct _como * m, int argc, char ** argv)
{
    cli_args_t cli_args;
    int config_file_exists;
    int c, i, j;
    DIR *d;
    
    if (m->cli_args.done_flag == 0) {
	parse_cmdline(&cli_args, argc, argv);

	if (cli_args.logflags != -1) {
	    m->logflags = cli_args.logflags;
	    m->cli_args.logflags_set = 1;
	}
	if (cli_args.dbdir != NULL) {
	    safe_dup(&m->dbdir, cli_args.dbdir);
	    m->cli_args.dbdir_set = 1;
	}
	if (cli_args.libdir != NULL) {
	    safe_dup(&m->libdir, cli_args.libdir);
	    m->cli_args.libdir_set = 1;
	}
	if (cli_args.query_port != -1) {
	    m->node->query_port = cli_args.query_port;
	    m->cli_args.query_port_set = 1;
	}
	if (cli_args.mem_size != 0) {
	    m->mem_size = cli_args.mem_size;
	    m->cli_args.mem_size_set = 1;
	}
	m->exit_when_done = cli_args.exit_when_done;
    }
    
    m->runmode = cli_args.runmode;
    m->inline_fd = (m->runmode == RUNMODE_INLINE) ? 1 /* stdout */ : -1; 
    
    m->debug = cli_args.debug;

    /*
     * build list of config files
     */
    config_file_exists = 0;
    for (c = 0; c < cli_args.cfg_files_count; c++) {
	config_file_exists = 1;
	parse_cfgfile(m, cli_args.cfg_files[c]);
    }
    
    if (!config_file_exists && m->runmode != RUNMODE_INLINE) 
        parse_cfgfile(m, DEFAULT_CFGFILE);	/* add default config file */

    if (m->runmode == RUNMODE_INLINE) {
	char *conf_argv[2];
	
	m->exit_when_done = 1;
	
    	if (cli_args.sniffer != NULL) {
	    add_sniffer(m, cli_args.sniffer, NULL, NULL);
    	}
    	
	/* prepare the arguments for do_config() */
	conf_argv[0] = "module";
	conf_argv[1] = cli_args.module;
	do_config(m, 2, conf_argv);

	if (cli_args.module_args != NULL) {
	    conf_argv[0] = "args";
	    conf_argv[1] = cli_args.module_args;
	    do_config(m, 2, conf_argv);
	}
	
	if (cli_args.filter != NULL) {
	    conf_argv[0] = "filter";
	    conf_argv[1] = cli_args.filter;
	    do_config(m, 2, conf_argv);
	}
	
	conf_argv[0] = "end";
	do_config(m, 1, conf_argv);
    }

    /* 
     * now look into the virtual nodes and replicate
     * all modules that have been found in the config file(s)
     * 
     * these new modules will have the same name but will be 
     * running the additional filter associated with the virtual 
     * node and save data in the virtual node dbdir.  
     * 
     * XXX all virtual nodes will be running on demand and 
     *     the source is defined in the configuration (or assumed to 
     *     be a trace module). later there shouldn't be a need 
     *     for defining the source module anyway...
     *
     */
    for (i = 0, j = m->module_last; i <= j; i++) { 
	module_t * orig; 
	int node_id; 

	orig = &m->modules[i]; 
	for (node_id = 1; node_id < m->node_count; node_id++) { 
	    module_t * mdl; 
	    char * nm; 

	    /* create a new module and copy it from  new module */
	    mdl = copy_module(m, orig, node_id, -1, m->node[node_id].args);
	    mdl->running = RUNNING_ON_DEMAND; 
	    
	    /* append node id to module's output file */
	    asprintf(&nm, "%s-%d", mdl->output, mdl->node); 
	    safe_dup(&mdl->output, nm); 
	    free(nm); 
	    
	    /* add the node filter to the module filter */
	    if (m->node[node_id].filter_str) {
		char * flt;
		if (!strcmp(mdl->filter_str, "all"))
		    asprintf(&flt, "%s", m->node[node_id].filter_str);
		else 
		    asprintf(&flt,"(%s) and (%s)", 
			m->node[node_id].filter_str, mdl->filter_str);
		mdl->filter_str = flt; /* FIXME: possible leak */
	    } 

	    /* add the node arguments to the module arguments */ 
	    if (m->node[node_id].args) { 
		int k; 

	 	for (k = 0; m->node[node_id].args[k]; k++) {
		    /* 
		     * XXX we copy one argument at a time to avoid 
		     *     having to count them first. FIX THIS
		     */ 
		    mdl->args = 
			copy_args(mdl->args, &m->node[node_id].args[k], 1); 
		}
	    } 

            logmsg(LOGUI, "... module%s %s [%d][%d] ",
                   (mdl->running == RUNNING_ON_DEMAND) ? " on-demand" : "",
                   mdl->name, mdl->node, mdl->priority);
            logmsg(LOGUI, " filter %s; out %s (%uMB)\n",
                   mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024));
            if (mdl->description != NULL)
                logmsg(LOGUI, "    -- %s\n", mdl->description);
	}
    }

    /* 
     * open the dbdir for all nodes (virtual ones included) 
     */
    if (m->runmode == RUNMODE_NORMAL) {
	if (m->dbdir == NULL)
	    panicx("missing db-path");
	d = opendir(m->dbdir);
	if (d == NULL) 
	    createdir(m->dbdir); 
	else 
	    closedir(d);
    }

    /*
     * process the AS file
     */

    asn_readfile(m->asnfile);
}
Beispiel #6
0
/*
 * -- configure
 *
 * do a first pass of the command line parameters to find all
 * configuration files. the options in those files are processed
 * before any other command line parameter. command line will
 * overwrite any other configuration, as well as the last config
 * file will overwrite previous config files.
 *
 */
void
configure(struct _como * m, int argc, char ** argv)
{
    static cli_args_t cli_args;
    int config_file_exists;
    int c, i, j;
    DIR *d;
    
    if (cli_args.done == 0) {
	parse_cmdline(&cli_args, argc, argv);
    }
    
    m->running = cli_args.running;

    /*
     * build list of config files
     */
    config_file_exists = 0;
    for (c = 0; c < cli_args.cfg_files_count; c++) {
	config_file_exists = 1;
	parse_cfgfile(m, cli_args.cfg_files[c]);
    }
    
    if (!config_file_exists && m->running == NORMAL) 
        parse_cfgfile(m, DEFAULT_CFGFILE);	/* add default config file */

    /* use cli args to override cfg file settings */
    if (cli_args.basedir != NULL)
	safe_dup(&m->basedir, cli_args.basedir);
    if (cli_args.libdir != NULL)
	safe_dup(&m->libdir, cli_args.libdir);
    if (cli_args.query_port != -1)
	m->node->query_port = cli_args.query_port;
    if (cli_args.mem_size != 0)
	m->mem_size = cli_args.mem_size;
    if (cli_args.logflags != -1)
	m->logflags = cli_args.logflags;
    m->debug = cli_args.debug;
    
    if (m->running == INLINE) {
	char *conf_argv[2];
	
    	if (cli_args.sniffer != NULL) {
	    add_sniffer(m, cli_args.sniffer, NULL, NULL);
    	}
    	
	/* prepare the arguments for do_config() */
	conf_argv[0] = "module";
	conf_argv[1] = cli_args.module;
	do_config(m, 2, conf_argv);

	if (cli_args.module_args != NULL) {
	    conf_argv[0] = "args";
	    conf_argv[1] = cli_args.module_args;
	    do_config(m, 2, conf_argv);
	}
	
	if (cli_args.filter != NULL) {
	    conf_argv[0] = "filter";
	    conf_argv[1] = cli_args.filter;
	    do_config(m, 2, conf_argv);
	}
	
	conf_argv[0] = "end";
	do_config(m, 1, conf_argv);
    }

    /* 
     * now look into the virtual nodes and replicate
     * all modules that have been found in the config file(s)
     * 
     * these new modules will have the same name but will be 
     * running the additional filter associated with the virtual 
     * node and save data in the virtual node basedir.  
     */
    for (i = 0, j = m->module_last; i <= j; i++) { 
	node_t * node; 
	module_t * orig; 

	orig = &m->modules[i]; 
	for (node = m->node; node; node = node->next) { 
	    module_t * mdl; 
	    char * nm; 

	    if (node->id == 0) 
		break; 	/* master node. nothing to do */

	    /* create a new module and copy it from  new module */
	    mdl = copy_module(m, orig, node->id, -1, NULL);
	    
	    /* append node id to module's output file */
	    asprintf(&nm, "%s-%d", mdl->output, mdl->node); 
	    safe_dup(&mdl->output, nm); 
	    free(nm); 
	    
	    /* add the node filter to the module filter */
	    if (node->filter_str) {
		char * flt;
		if (!strcmp(mdl->filter_str, "all"))
		    asprintf(&flt, "%s", node->filter_str);
		else 
		    asprintf(&flt,"(%s) and (%s)", 
				node->filter_str, mdl->filter_str);
		mdl->filter_str = flt; /* FIXME: possible leak */
	    } 

            logmsg(LOGUI, "... module %s [%d][%d] ",
                   mdl->name, mdl->node, mdl->priority);
            logmsg(LOGUI, " filter %s; out %s (%uMB)\n",
                   mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024));
            if (mdl->description != NULL)
                logmsg(LOGUI, "    -- %s\n", mdl->description);
	}
    }

    /* 
     * open the basedir for all nodes (virtual ones included) 
     */
    if (m->basedir == NULL)
	panicx("missing basedir");
    d = opendir(m->basedir);
    if (d == NULL) 
	createdir(m->basedir); 
    else 
	closedir(d);
}