示例#1
0
int main(int argc, char *argv[])
{
    char *conffile = NULL;
    int request_kill = 0, show_message = 0;
    
    if (!read_parameters(argc, argv, &conffile, &request_kill, &show_message)) {
	print_usage(argv[0]);
	return 1;
    }
    
    /* read the config file; conffile = NULL means use the default. */
    if (!read_config(conffile))
	return 1; /* error reading the config file */
    setup_defaults();
    
    if (request_kill) {
	kill_server();
	return 0;
    }
    
    if (show_message) {
	signal_message();
	return 0;
    }
    
    setup_signals();
    
    /* Init files and directories.
     * Start logging last, so that the log is only created if startup succeeds. */
    if (!init_workdir() ||
	!init_database() ||
	!check_database() ||
	!begin_logging())
	return 1;
    
    if (!settings.nodaemon) {
    	if (daemon(0, 0) == -1) {
	    settings.nodaemon = TRUE;
	    log_msg("could not detach from terminal: %s", strerror(errno));
	    return 1;
	}
	if (!create_pidfile())
	    return 1;
    }

    /* give this process and its children their own process group id for kill() */
    setpgid(0, 0);
    report_startup();
    
    runserver();
        
    /* shutdown */
    remove_pidfile();
    end_logging();
    close_database();
    remove_unix_socket();
    free_config();
    
    return 0;
}
示例#2
0
static int
setup_server_sockets(int *ipv4fd, int *ipv6fd, int *unixfd, int epfd)
{
    struct epoll_event ev;

    ev.data.ptr = NULL;
    ev.events = EPOLLIN | EPOLLET;      /* epoll_wait waits for EPOLLERR and
                                           EPOLLHUP as well */

    if (!settings.disable_ipv6) {
        settings.bind_addr_6.sin6_port = htons((unsigned short)settings.port);
        *ipv6fd = init_server_socket((struct sockaddr *)&settings.bind_addr_6);
        ev.data.fd = *ipv6fd;
        if (*ipv6fd != -1)
            epoll_ctl(epfd, EPOLL_CTL_ADD, *ipv6fd, &ev);
    } else
        *ipv6fd = -1;

    if (!settings.disable_ipv4) {
        settings.bind_addr_4.sin_port = htons((unsigned short)settings.port);
        *ipv4fd = init_server_socket((struct sockaddr *)&settings.bind_addr_4);
        ev.data.fd = *ipv4fd;
        if (*ipv4fd != -1)
            epoll_ctl(epfd, EPOLL_CTL_ADD, *ipv4fd, &ev);
    } else
        *ipv4fd = -1;

    if (settings.bind_addr_unix.sun_family && remove_unix_socket()) {
        int prevmask = umask(0);

        *unixfd =
            init_server_socket((struct sockaddr *)&settings.bind_addr_unix);
        ev.data.fd = *unixfd;
        if (*unixfd != -1)
            epoll_ctl(epfd, EPOLL_CTL_ADD, *unixfd, &ev);
        umask(prevmask);
    }

    if (*ipv4fd == -1 && *ipv6fd == -1) {
        log_msg
            ("Failed to create any listening socket. Nothing to do except shut down.");
        return FALSE;
    }

    return TRUE;
}
示例#3
0
gint
accept_close_servers (ACCEPT_POOL ap, GError **err)
{
	int *pSrv = NULL; 
	int max=0, nb_errors=0;

	if (!ap) {
		GSETERROR(err,"Invalid parameter");
		return 0;
	}

	g_rec_mutex_lock (&(ap->mut));
	if (ap->srv) {
		pSrv = ap->srv;
		max = ap->count;
	}
	ap->srv = NULL;
	ap->count = 0;
	g_rec_mutex_unlock (&(ap->mut));

	if (pSrv) {
		int i;
		for (i=0; i<max ;i++) {
			int fd = pSrv[ i ];
			if (fd>=0) {
				remove_unix_socket( fd );
				errno=0;
				metautils_pclose(&fd);
			}
			pSrv[ i ] = -1;
		}
	}

	g_free(pSrv);
	return nb_errors;
}