Esempio n. 1
0
File: rub.c Progetto: github188/Ptl
 /*Program entrance*/
int main(int argc, char** argv)
{

	key_t ipckey = ftok("/",999);
	int shmid = shmget(ipckey,sizeof(struct tag),IPC_CREAT |0660);
	strTag = (struct tag *)shmat(shmid,NULL,0);
	strTag->str = (char *)malloc(10);
	printf("malloc : %lx\n",(unsigned long)strTag->str);
	pid_t pid = fork();

	if(pid == 0)
	{
		create_base();
	}
	if(pid > 0 )
	{
		signal(SIGCHLD,sig_chld);
		struct event_base *base = event_base_new();
		struct timeval tv = {1,0};
		struct event sig;
		struct event sig1;
//		struct event timeout;
//		evtimer_assign(&timeout,base,timerout,&timeout);
//		evtimer_add(&timeout,&tv);
		evsignal_assign(&sig,base,SIGINT,sig_act,(void *)&sig);
		evsignal_add(&sig,NULL);
		evsignal_assign(&sig1,base,SIGUSR1,sig_act2,(void *)base);
		evsignal_add(&sig1,NULL);
		event_base_dispatch(base);
		event_base_free(base);
		shmdt(strTag);
		shmctl(shmid,IPC_RMID,NULL);
	}
	return 0;
}
Esempio n. 2
0
int add_signal(int signum, struct event *event)
{
	if (!event) {
		event = evsignal_new(base, signum, cb_signal, NULL);
		if (!event) {
			log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: evsignal_new failed[%d]", __FUNCTION__, __LINE__, errno);					
			return (-1);
		}
		event->ev_arg = event;
	} else {
		evsignal_assign(event, base, signum, event->ev_callback, NULL);
	}

	return evsignal_add(event, NULL);
}
Esempio n. 3
0
File: rub.c Progetto: github188/Ptl
static void create_base()
{
	signal(SIGINT,sig_int);
//	strTag->pid = getpid();
	struct event_base *base = event_base_new();
	struct timeval tv = {5,0};
	struct event sig;
	struct event timeout;
	evtimer_assign(&timeout,base,timerout1,&timeout);
	evtimer_add(&timeout,&tv);
	evsignal_assign(&sig,base,SIGUSR2,sig_act1,&sig);
	evsignal_add(&sig,NULL);
	event_base_dispatch(base);
	printf("child break loopbase!!\n");
	event_base_free(base);
	sleep(5);
}
Esempio n. 4
0
int main(int argc, char **argv)
{
	int error;
	app_subsys **ss;
	int exit_signals[2] = {SIGTERM, SIGINT};
	struct event terminators[2];
    struct event dumper;
	bool conftest = false;
	int opt;
	int i;

	red_srand();
	while ((opt = getopt(argc, argv, "h?vtc:p:")) != -1) {
		switch (opt) {
		case 't':
			conftest = true;
			break;
		case 'c':
			confname = optarg;
			break;
		case 'p':
			pidfile = optarg;
			break;
		case 'v':
			puts(redsocks_version);
			return EXIT_SUCCESS;
		default:
			printf(
				"Usage: %s [-?hvt] [-c config] [-p pidfile]\n"
				"  -h, -?       this message\n"
				"  -v           print version\n"
				"  -t           test config syntax\n"
				"  -p           write pid to pidfile\n",
				argv[0]);
			return (opt == '?' || opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE;
		}
	}


	FILE *f = fopen(confname, "r");
	if (!f) {
		perror("Unable to open config file");
		return EXIT_FAILURE;
	}

	parser_context* parser = parser_start(f, NULL);
	if (!parser) {
		perror("Not enough memory for parser");
		return EXIT_FAILURE;
	}

	FOREACH(ss, subsystems)
		if ((*ss)->conf_section)
			parser_add_section(parser, (*ss)->conf_section);
	error = parser_run(parser);
	parser_stop(parser);
	fclose(f);

	if (error)
		return EXIT_FAILURE;

	if (conftest)
		return EXIT_SUCCESS;

	// Initialize global event base
	g_event_base = event_base_new();
	if (!g_event_base)
		return EXIT_FAILURE;
		
	memset(&dumper, 0, sizeof(dumper));
	memset(terminators, 0, sizeof(terminators));

	FOREACH(ss, subsystems) {
		if ((*ss)->init) {
			error = (*ss)->init();
			if (error)
				goto shutdown;
		}
	}

	if (pidfile) {
		f = fopen(pidfile, "w");
		if (!f) {
			perror("Unable to open pidfile for write");
			return EXIT_FAILURE;
		}
		fprintf(f, "%d\n", getpid());
		fclose(f);
	}

	assert(SIZEOF_ARRAY(exit_signals) == SIZEOF_ARRAY(terminators));
	for (i = 0; i < SIZEOF_ARRAY(exit_signals); i++) {
		evsignal_assign(&terminators[i], get_event_base(), exit_signals[i], terminate, NULL);
		if (evsignal_add(&terminators[i], NULL) != 0) {
			log_errno(LOG_ERR, "signal_add");
			goto shutdown;
		}
	}

    evsignal_assign(&dumper, get_event_base(), SIGUSR1, dump_handler, NULL);
    if (evsignal_add(&dumper, NULL) != 0) {
        log_errno(LOG_ERR, "evsignal_add");
        goto shutdown;
    }

	log_error(LOG_NOTICE, "redsocks started");

	event_base_dispatch(g_event_base);

	log_error(LOG_NOTICE, "redsocks goes down");

shutdown:
    if (evsignal_initialized(&dumper)) {
        if (evsignal_del(&dumper) != 0)
		    log_errno(LOG_WARNING, "signal_del");
        memset(&dumper, 0, sizeof(dumper));
    }

	for (i = 0; i < SIZEOF_ARRAY(exit_signals); i++) {
		if (evsignal_initialized(&terminators[i])) {
			if (evsignal_del(&terminators[i]) != 0)
				log_errno(LOG_WARNING, "signal_del");
			memset(&terminators[i], 0, sizeof(terminators[i]));
		}
	}

	for (--ss; ss >= subsystems; ss--)
		if ((*ss)->fini)
			(*ss)->fini();

	if (g_event_base)
		event_base_free(g_event_base);
	
	return !error ? EXIT_SUCCESS : EXIT_FAILURE;
}
Esempio n. 5
0
bool SignalEventWatcher::DoInit()
{
    assert(signo_);
    evsignal_assign(&event_, event_base_, signo_, SignalEventWatcher::HandlerFn, this);
    return true;
}