예제 #1
0
파일: main.c 프로젝트: JoeDog/fido
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 **/
예제 #2
0
static map_conf_t *
read_conf()
{
	map_conf_t *conf;
	static FILE *fd;
	char buff[BUFSIZ];
	char *b;
	char *value;

	if ((conf = new_conf()) == NULL) 
		return NULL;

	if ((fd = fopen(MAIN_CONF_FILE, "r")) == NULL ) {
		free_conf(conf);
		return NULL;
	}

	if (fgets (buff, BUFSIZ, fd) == NULL) {
		fclose(fd);
		free_conf(conf);
		return NULL;
	}
	fclose(fd);

	/* start reading configuration file */
	b = buff;

	/* pw_name */
	value = b;

	while (*b != ':' && *b != '\0')
		b++;

	if (*b != ':') 
		goto format_error;

	*b = '\0';
	b++;

	conf->pw_name = strdup(value);

	/* NOT USED pw_passwd will be set equal to  x (we like shadows) */
	while (*b != ':' && *b != '\0')
		b++;

	if ( *b != ':' )
		goto format_error;

	b++;

	/* pw_uid */
	value = b;

	while (*b != ':' && *b != '\0')
		b++;

	if (*b != ':')
		goto format_error;

	b++;

	conf->pw_uid = atoi(value);

	if ( conf->pw_uid < MIN_UID_NUMBER )
		conf->pw_uid = MIN_UID_NUMBER;

	/* pw_gid */
	value = b;

	while (*b != ':' && *b != '\0')
		b++;

	if (*b != ':')
		goto format_error;

	b++;
	conf->pw_gid = atoi(value);
	if ( conf->pw_gid < MIN_GID_NUMBER )
		conf->pw_gid = MIN_GID_NUMBER;

	/* pw_gecos */  
	value = b;

	while (*b != ':' && *b != '\0')
		b++;

	if (*b != ':')
		goto format_error;

	*b = '\0';
	b++;

	conf->pw_gecos = strdup (value);

	/* pw_dir */  
	value = b;

	while (*b != ':' && *b != '\0')
		b++;

	if (*b != ':')
		goto format_error;

	*b = '\0';
	b++;
	
	conf->pw_dir = strdup (value);

	/* pw_shell takes the rest */  
	/* Kyler Laird suggested to strip end line */
	value = b;

	while (*b != '\n' && *b != '\0')
		b++;

	*b = '\0';

	conf->pw_shell = strdup(value);

	return conf;

format_error:
	free (conf);
	return NULL;
}