示例#1
0
int main(int argc, char **argv)
{
#ifdef ANDROID_CHANGES
    int control = android_get_control_and_arguments(&argc, &argv);
    ENGINE *e;
    if (control != -1) {
        pname = "%p";
        monitor_fd(control, NULL);

        ENGINE_load_dynamic();
        e = ENGINE_by_id("keystore");
        if (!e || !ENGINE_init(e)) {
            do_plog(LLV_ERROR, "ipsec-tools: cannot load keystore engine");
            exit(1);
        }
    }
#endif

    do_plog(LLV_INFO, "ipsec-tools 0.7.3 (http://ipsec-tools.sf.net)\n");

    signal(SIGHUP, terminate);
    signal(SIGINT, terminate);
    signal(SIGTERM, terminate);
    signal(SIGPIPE, SIG_IGN);
    atexit(terminated);

    setup(argc, argv);

#ifdef ANDROID_CHANGES
    shutdown(control, SHUT_WR);
    setuid(AID_VPN);
#endif

    while (1) {
        struct timeval *tv = schedular();
        int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000 + 1;

        if (poll(pollfds, monitors, timeout) > 0) {
            int i;
            for (i = 0; i < monitors; ++i) {
                if (pollfds[i].revents & POLLHUP) {
                    do_plog(LLV_ERROR, "Connection is closed\n", pollfds[i].fd);
                    exit(1);
                }
                if (pollfds[i].revents & POLLIN) {
                    callbacks[i](pollfds[i].fd);
                }
            }
        }
    }
#ifdef ANDROID_CHANGES
    if (e) {
        ENGINE_finish(e);
        ENGINE_free(e);
    }
#endif
    return 0;
}
示例#2
0
文件: ans.c 项目: ccreimondo/os
int
main(int argc, char *argv[])
{
    int err, i;
    char buff[MAXLINE];

    sprintf(buff, "usage: %s [1|2|3|4]\n", argv[0]);
    sprintf(buff, "%s1 (default): first come first served\n", buff);
    sprintf(buff, "%s2 : shortest job first\n", buff);
    sprintf(buff, "%s3 : round robin\n", buff);
    sprintf(buff, "%s4 : priority\n", buff);

    if (argc != 2) {
        exit(1);
    }

    /* create the virtual PCBs */
    srand(time(NULL));
    for (i = 0; i < MAXTHREAD; ++i) {
        struct pcb *pcbp;

        /*
         * allocate memory for a new pcb struct
         * and it helps to make vars like mutex won't be removed
         */
        if ((pcbp = malloc(sizeof(struct pcb))) != NULL) {
            pcbp->idx = ++i;
            pcbp->state = 0;
            pcbp->acnt.burst_time = 0;

            err = pthread_mutex_init(&pcbp->mutex, NULL);
            if (err != 0) {
                free(pcbp);
                err_exit(err, "can't init mutex");
            }
        } else {
            err_sys("can't malloc memory for struct pcb");
        }

        /*
         * lock new thread's mutex in the main thread firstly
         * and it will be unlocked in the schedular
         */
        err = pthread_mutex_lock(&pcbp->mutex);
        if (err != 0)
            err_exit(err, "can't lock mutex in main thread");

        /* create a new thread */
        err = pthread_create(&pcbp->tid, NULL, thr_fn, &pcbp->mutex);
        if (err != 0)
            err_exit(err, "can't create thread");

        /* append new PCB to the ready queue */
        pcbp->state = 1;
        ready_queue[i] = pcbp;
    }

    /* call schedular to do thread scheduling */
    schedular((unsigned int)atoi(argv[1]));

    exit(0);
}
示例#3
0
int
session(void)
{
	fd_set rfds;
	struct timeval *timeout;
	int error;
	struct myaddrs *p;
	char pid_file[MAXPATHLEN];
	FILE *fp;
	pid_t racoon_pid = 0;
	int i;

	/* initialize schedular */
	sched_init();

	init_signal();

#ifdef ENABLE_ADMINPORT
	if (admin_init() < 0)
		exit(1);
#endif

	initmyaddr();

	if (isakmp_init() < 0)
		exit(1);

	initfds();

#ifdef ENABLE_NATT
	natt_keepalive_init ();
#endif

	if (privsep_init() != 0)
		exit(1);

	for (i = 0; i <= NSIG; i++)
		sigreq[i] = 0;

	/* write .pid file */
	racoon_pid = getpid();
	if (lcconf->pathinfo[LC_PATHTYPE_PIDFILE] == NULL) 
		strlcpy(pid_file, _PATH_VARRUN "racoon.pid", MAXPATHLEN);
	else if (lcconf->pathinfo[LC_PATHTYPE_PIDFILE][0] == '/') 
		strlcpy(pid_file, lcconf->pathinfo[LC_PATHTYPE_PIDFILE], MAXPATHLEN);
	else {
		strlcat(pid_file, _PATH_VARRUN, MAXPATHLEN);
		strlcat(pid_file, lcconf->pathinfo[LC_PATHTYPE_PIDFILE], MAXPATHLEN);
	} 
	fp = fopen(pid_file, "w");
	if (fp) {
		if (fchmod(fileno(fp),
			S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
			syslog(LOG_ERR, "%s", strerror(errno));
			fclose(fp);
			exit(1);
		}
		fprintf(fp, "%ld\n", (long)racoon_pid);
		fclose(fp);
	} else {
		plog(LLV_ERROR, LOCATION, NULL,
			"cannot open %s", pid_file);
	}

	while (1) {
		if (dying)
			rfds = maskdying;
		else
			rfds = mask0;

		/*
		 * asynchronous requests via signal.
		 * make sure to reset sigreq to 0.
		 */
		check_sigreq();

		/* scheduling */
		timeout = schedular();

		error = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, timeout);
		if (error < 0) {
			switch (errno) {
			case EINTR:
				continue;
			default:
				plog(LLV_ERROR, LOCATION, NULL,
					"failed to select (%s)\n",
					strerror(errno));
				return -1;
			}
			/*NOTREACHED*/
		}

#ifdef ENABLE_ADMINPORT
		if ((lcconf->sock_admin != -1) &&
		    (FD_ISSET(lcconf->sock_admin, &rfds)))
			admin_handler();
#endif

		for (p = lcconf->myaddrs; p; p = p->next) {
			if (!p->addr)
				continue;
			if (FD_ISSET(p->sock, &rfds))
				isakmp_handler(p->sock);
		}

		if (FD_ISSET(lcconf->sock_pfkey, &rfds))
			pfkey_handler();

		if (lcconf->rtsock >= 0 && FD_ISSET(lcconf->rtsock, &rfds)) {
			if (update_myaddrs() && lcconf->autograbaddr)
				sched_new(5, check_rtsock, NULL);
			initfds();
		}
	}
}