示例#1
0
static ape_global *ape_init()
{
    ape_global *ape;
    struct _fdevent *fdev;

    if ((ape = malloc(sizeof(*ape))) == NULL) return NULL;

	inc_rlimit(64000);

    signal(SIGPIPE, SIG_IGN);
	signal(SIGINT, &signal_handler);
	signal(SIGTERM, &signal_handler);
	
    fdev = &ape->events;
    fdev->handler = EVENT_UNKNOWN;
    #ifdef USE_EPOLL_HANDLER
    fdev->handler = EVENT_EPOLL;
    #endif
    #ifdef USE_KQUEUE_HANDLER
    fdev->handler = EVENT_KQUEUE;
    #endif

    ape->basemem    = APE_BASEMEM;
    ape->is_running = 1;
    ape->timers.ntimers = 0;
    ape->timers.timers  = NULL;
	
	ape_ssl_init();
	
    if (ape_dns_init(ape) != 0) {
        goto error;
    }
    events_init(ape);

    ape->seed = _ape_seed = time(NULL) ^ (getpid() << 16);

    ape->hashs.servers    = hashtbl_init(APE_HASH_STR);
    ape->hashs.cmds       = hashtbl_init(APE_HASH_STR);
    ape->hashs.pipes.pub  = hashtbl_init(APE_HASH_INT);
    ape->hashs.pipes.priv = hashtbl_init(APE_HASH_INT);

    if ((ape->conf = ape_read_config("../../etc/ape.conf", ape)) == NULL) {
        goto error;
    }
    
    ape_cmd_init_core(ape);

    ape->extend = ape_array_new(8);
	
    return ape;

error:

    free(ape);

    return NULL;
}
示例#2
0
int main(void) {
  vars = hashtbl_init(free, NULL, 0);
  /*fprintf(stderr, "%.15Lg\n", evaluate(argv[1]));*/
  check_operators();
  check_functions();
  check_precedence();
  check_longer();
  hashtbl_destroy(vars);
  return 0;
}
示例#3
0
static ape_global *ape_init()
{
    ape_global *ape;
    struct _fdevent *fdev;

    if ((ape = malloc(sizeof(*ape))) == NULL) return NULL;

    signal(SIGPIPE, SIG_IGN);

    fdev = &ape->events;
    fdev->handler = EVENT_UNKNOWN;
    #ifdef USE_EPOLL_HANDLER
    fdev->handler = EVENT_EPOLL;
    #endif
    #ifdef USE_KQUEUE_HANDLER
    fdev->handler = EVENT_ KQUEUE;
    #endif

    ape->basemem    = APE_BASEMEM;
    ape->is_running = 1;

    if (ape_dns_init(ape) != 0) {
        goto error;
    }
    events_init(ape);

    ape->seed = _ape_seed = time(NULL) ^ (getpid() << 16);

    ape->hashs.servers = hashtbl_init();

    if ((ape->conf = ape_read_config("../../etc/ape.conf", ape)) == NULL) {
        goto error;
    }

    return ape;

error:

    free(ape);

    return NULL;
}
示例#4
0
void options_init(integrit_t *it)
{
    it->conffile	 = NULL;
    it->knowndbname	 = NULL;
    memset(&it->knowndb, 0, sizeof(it->knowndb));
    it->currdbname	 = NULL;
    memset(&it->currdb, 0, sizeof(it->currdb));
    it->root		 = NULL;
    if (! (it->ruleset = malloc(sizeof(hashtbl_t))) )
      DIE("malloc hashtbl");
    if (hashtbl_init(it->ruleset, 20) == -1)
      DIE("initializing hashtbl");
    it->verbose		 = 1;
    it->stop_on_err	 = 1;
    it->do_check	 = 0;
    it->do_update	 = 0;
    it->default_flags	 = RULE_SUM | RULE_INODE | RULE_PERMS | 
            RULE_TYPE | RULE_DEVICETYPE | RULE_NLINK | RULE_UID | RULE_GID | 
            RULE_MTIME | RULE_CTIME;
    it->output		 = OUTPUT_LINES; /* human-readable output */
}
示例#5
0
int main(int argc, char **argv) 
{
	apeconfig *srv;
	
	int random, im_r00t = 0, pidfd = 0, serverfd;
	unsigned int getrandom = 0, ct_id;
	const char *pidfile = NULL;
	char *confs_path = NULL;
	
	struct _fdevent fdev;
	
	char cfgfile[513] = APE_CONFIG_FILE;
	
	acetables *g_ape;
	
	if (argc > 1 && strcmp(argv[1], "--version") == 0) {
		printf("\n   AJAX Push Engine Server %s - (C) Anthony Catel <*****@*****.**>\n   http://www.ape-project.org/\n\n", _VERSION);
		return 0;
	}
	if (argc > 1 && strcmp(argv[1], "--help") == 0) {
		printf("\n   AJAX Push Engine Server %s - (C) Anthony Catel <*****@*****.**>\n   http://www.ape-project.org/\n", _VERSION);
		printf("\n   usage: aped [options]\n\n");
		printf("   Options:\n     --help             : Display this help\n     --version          : Show version number\n     --cfg <config path>: Load a specific config file (default is %s)\n\n", cfgfile);
		return 0;
	} else if (argc > 2 && strcmp(argv[1], "--cfg") == 0) {
		memset(cfgfile, 0, 513);
		strncpy(cfgfile, argv[2], 512);
		confs_path = get_path(cfgfile);
	} else if (argc > 1) {
		printf("\n   AJAX Push Engine Server %s - (C) Anthony Catel <*****@*****.**>\n   http://www.ape-project.org/\n\n", _VERSION);
		printf("   Unknown parameters - check \"aped --help\"\n\n");
		return 0;
	}
	if (NULL == (srv = ape_config_load(cfgfile))) {
		printf("\nExited...\n\n");
		exit(1);
	}
	
	if (getuid() == 0) {
		im_r00t = 1;
	}

	signal(SIGINT, &signal_handler);
	signal(SIGTERM, &signal_handler);
	
	if (VTICKS_RATE < 1) {
		printf("[ERR] TICKS_RATE cant be less than 1\n");
		return 0;
	}
	
	random = open("/dev/urandom", O_RDONLY);
	if (!random) {
		printf("Cannot open /dev/urandom... exiting\n");
		return 0;
	}
	read(random, &getrandom, 3);
	srand(getrandom);
	close(random);

	g_ape = xmalloc(sizeof(*g_ape));
	g_ape->basemem = 1; // set 1 for testing if growup works
	g_ape->srv = srv;
	g_ape->confs_path = confs_path;
	g_ape->is_daemon = 0;
	
	ape_log_init(g_ape);
	
	fdev.handler = EVENT_UNKNOWN;

	#ifdef USE_EPOLL_HANDLER
	fdev.handler = EVENT_EPOLL;
	#endif
	#ifdef USE_KQUEUE_HANDLER
	fdev.handler = EVENT_KQUEUE;
	#endif

	g_ape->co = xmalloc(sizeof(*g_ape->co) * g_ape->basemem);
	memset(g_ape->co, 0, sizeof(*g_ape->co) * g_ape->basemem);
	
	g_ape->bad_cmd_callbacks = NULL;
	g_ape->bufout = xmalloc(sizeof(struct _socks_bufout) * g_ape->basemem);
	
	g_ape->timers.timers = NULL;
	g_ape->timers.ntimers = 0;
	g_ape->events = &fdev;
	if (events_init(g_ape, &g_ape->basemem) == -1) {
		printf("Fatal error: APE compiled without an event handler... exiting\n");
		return 0;
	};
	
	serverfd = servers_init(g_ape);
	
	ape_log(APE_INFO, __FILE__, __LINE__, g_ape, 
		"APE starting up - pid : %i", getpid());
	
	if (strcmp(CONFIG_VAL(Server, daemon, srv), "yes") == 0 && (pidfile = CONFIG_VAL(Server, pid_file, srv)) != NULL) {
		if ((pidfd = open(pidfile, O_TRUNC | O_WRONLY | O_CREAT, 0655)) == -1) {
			ape_log(APE_WARN, __FILE__, __LINE__, g_ape, 
				"Cant open pid file : %s", CONFIG_VAL(Server, pid_file, srv));
		}
	}
	
	if (im_r00t) {
		struct group *grp = NULL;
		struct passwd *pwd = NULL;
		
		if (inc_rlimit(atoi(CONFIG_VAL(Server, rlimit_nofile, srv))) == -1) {
			ape_log(APE_WARN, __FILE__, __LINE__, g_ape, 
				"Cannot set the max filedescriptos limit (setrlimit) %s", strerror(errno));
		}
		
		/* Set uid when uid section exists */
		if (ape_config_get_section(srv, "uid")) {

			/* Get the user information (uid section) */
			if ((pwd = getpwnam(CONFIG_VAL(uid, user, srv))) == NULL) {
				ape_log(APE_ERR, __FILE__, __LINE__, g_ape, 
					"Can\'t find username %s", CONFIG_VAL(uid, user, srv));
				return -1;
			}
			if (pwd->pw_uid == 0) {
				ape_log(APE_ERR, __FILE__, __LINE__, g_ape, 
					"%s uid can\'t be 0", CONFIG_VAL(uid, user, srv));
				return -1;			
			}
			
			/* Get the group information (uid section) */
			if ((grp = getgrnam(CONFIG_VAL(uid, group, srv))) == NULL) {
				printf("[ERR] Can\'t find group %s\n", CONFIG_VAL(uid, group, srv));
				ape_log(APE_ERR, __FILE__, __LINE__, g_ape, 
					"Can\'t find group %s", CONFIG_VAL(uid, group, srv));
				return -1;
			}
			
			if (grp->gr_gid == 0) {
				ape_log(APE_ERR, __FILE__, __LINE__, g_ape, 
				"%s gid can\'t be 0", CONFIG_VAL(uid, group, srv));
			return -1;
			}
		
			setgid(grp->gr_gid);
			setgroups(0, NULL);

			initgroups(CONFIG_VAL(uid, user, srv), grp->gr_gid);
		
			setuid(pwd->pw_uid);
		}

	} else {
		printf("[WARN] You have to run \'aped\' as root to increase r_limit\n");
		ape_log(APE_WARN, __FILE__, __LINE__, g_ape, 
			"You have to run \'aped\' as root to increase r_limit");
	}
	
	if (strcmp(CONFIG_VAL(Server, daemon, srv), "yes") == 0) {
		ape_log(APE_INFO, __FILE__, __LINE__, g_ape, 
			"Starting daemon");
		ape_daemon(pidfd, g_ape);

		events_reload(g_ape->events);
		events_add(g_ape->events, serverfd, EVENT_READ);
	}
	
	if (!g_ape->is_daemon) {	
		printf("   _   ___ ___ \n");
		printf("  /_\\ | _ \\ __|\n");
		printf(" / _ \\|  _/ _| \n");
		printf("/_/ \\_\\_| |___|\nAJAX Push Engine\n\n");

		printf("Bind on port %i\n\n", atoi(CONFIG_VAL(Server, port, srv)));
		printf("Version : %s\n", _VERSION);
		printf("Build   : %s %s\n", __DATE__, __TIME__);
		printf("Author  : Weelya ([email protected])\n\n");		
	}
	signal(SIGPIPE, SIG_IGN);
	
	ape_dns_init(g_ape);
	
	g_ape->cmd_hook.head = NULL;
	g_ape->cmd_hook.foot = NULL;
	
	g_ape->hSessid = hashtbl_init();

	g_ape->hChannel = hashtbl_init();
	g_ape->hPubid = hashtbl_init();
	
	g_ape->proxy.list = NULL;
	g_ape->proxy.hosts = NULL;
	
	g_ape->hCallback = hashtbl_init();

	g_ape->uHead = NULL;
	g_ape->cHead = NULL;
	
	g_ape->nConnected = 0;
	g_ape->plugins = NULL;
	
	g_ape->properties = NULL;

	ct_id = add_ticked(check_timeout, g_ape)->identifier;
	
	do_register(g_ape);
	
	transport_start(g_ape);	
	
	findandloadplugin(g_ape);

	server_is_running = 1;
	server_is_shutdowning = 0;

	/* Starting Up */
	sockroutine(g_ape); /* loop */
	/* Shutdown */	
	
	if (pidfile != NULL) {
		unlink(pidfile);
	}
	
	free(confs_path);

	ape_dns_free(g_ape);

	del_timer_identifier(ct_id, g_ape);

	events_free(g_ape);

	transport_free(g_ape);

	hashtbl_free(g_ape->hSessid, 0);
	hashtbl_free(g_ape->hChannel, 0);
	hashtbl_free(g_ape->hPubid, 0);
	
	do_unregister(g_ape);
	hashtbl_free(g_ape->hCallback, 1);
	
	ape_config_free(srv);

	int i;
	for (i = 0; i < g_ape->basemem; i++) {
		if (g_ape->co[i] != NULL) {
			close_socket(i, g_ape);
			free(g_ape->co[i]);
		}
	}
	free(g_ape->co);

	free(g_ape->bufout);

	free_all_hook_cmd(g_ape);
	free_all_plugins(g_ape);

	free(g_ape);
	
	return 0;
}
示例#6
0
int parser_eval(const expr_t *e, long double *r, hashtbl_t *vars) {
    if (!e || !r) {
        fprintf(stderr, "eval error: null expression or result var\n");
        return 1;
    }

    /* load known functions */
    static hashtbl_t *functions = NULL;
    if (unlikely(functions == NULL)) {
        functions = hashtbl_init(NULL, NULL);
        register_functions(functions);
    }
    /* stash constants into whatever symtab we get */
    if (unlikely(vars && !hashtbl_get(vars, "_stashed"))) {
        register_constants(vars);
    }

    const list_t *l = (const list_t*)e;
    const list_node_t *n = list_last(l);
    list_t *args = list_init(free, NULL);
    const symbol_t *s;

    while (n && (s = (const symbol_t*)list_data(n))) {
        long double *d = NULL, *v = NULL;
        long double (*f)(list_t*, size_t);

        switch (s->type) {
        case stNumber:
            d = (long double*)zmalloc(sizeof(long double));
            *d = s->number;
            list_push(args, d);
            break;

        case stVariable:
            if (!vars) {
                fprintf(stderr, "eval error: no symbol table\n");
                list_destroy(args);
                return 1;
            }
            if (!(v = (long double*)hashtbl_get(vars, s->variable))) {
                fprintf(stderr, "eval error: uninitialized variable [%s]\n", s->variable);
                list_destroy(args);
                return 1;
            }
            d = (long double*)zmalloc(sizeof(long double));
            *d = *v;
            list_push(args, d);
            break;

        case stBinOperator:
            /* rhs operand */
            if (!(v = (long double*)list_pop(args))) {
                fprintf(stderr, "eval error: missing rhs operand\n");
                list_destroy(args);
                return 1;
            }
        case stUniOperator:
            /* lhs operand, don't pop it... use it to store the result too */
            if (!(d = (long double*)list_peek_head(args))) {
                fprintf(stderr, "eval error: missing lhs operand\n");
                list_destroy(args);
                return 1;
            }
            *d = semanter_operator(s->operator, *d, s->type == stBinOperator ? *v : 0.0);
            free(v);
            break;

        case stFunction:
            if (!(f = (long double(*)(list_t*, size_t))hashtbl_get(functions, s->func.name))) {
                fprintf(stderr, "eval error: unknown function [%s]\n", s->func.name);
                list_destroy(args);
                return 1;
            }
            d = (long double*)zmalloc(sizeof(long double));
            *d = f(args, s->func.nargs);
            list_push(args, d);
            break;
        }
        n = list_prev(n);
    }

    if (list_size(args) != 1) {
        fprintf(stderr, "eval error: corrupt args stack\n");
        list_destroy(args);
        return 1;
    }

    long double *d = (long double*)list_peek_head(args);
    *r = *d;
    list_destroy(args);
    return 0;
}