示例#1
0
文件: listener.c 项目: pepa65/weborf
int main(int argc, char *argv[]) {
    int s, s1; // Socket descriptors

    init_logger();
    init_thread_info();

    configuration_load(argc,argv);

    if (weborf_conf.is_inetd) inetd();

    print_start_disclaimer(argc,argv);

    s=net_create_server_socket();
    net_bind_and_listen(s);

    set_new_gid(weborf_conf.gid);
    set_new_uid(weborf_conf.uid);

    // init the queue for opened sockets
    if (q_init(&queue, MAXTHREAD + 1)!=0)
        exit(NOMEM);

    // Starts the 1st group of threads
    init_thread_attr();
    init_threads(INITIALTHREAD);
    init_thread_shaping();
    init_signals();

    // Infinite cycle, accept connections
    while (1) {
        s1=accept(s, NULL,NULL);

        if (s1 >= 0 && q_put(&queue, s1)!=0) { // Adds s1 to the queue
#ifdef REQUESTDBG
            syslog(LOG_ERR,"Not enough resources, dropping connection...");
#endif
            close(s1);
        }

        // Start new thread if needed
        if (thread_info.free <= LOWTHREAD && thread_info.free<MAXTHREAD) {
            // Need to start new thread
            if (thread_info.count + INITIALTHREAD < MAXTHREAD) {
                // Starts a group of threads
                init_threads(INITIALTHREAD);
            } else { // Can't start a group because the limit is close, starting less than a whole group
                init_threads(MAXTHREAD - thread_info.count);
            }
        }

    }
    return 0;

}
/**
 * init_cpu_drc_info
 *
 * @returns pointer to drc_info on success, NULL otherwise
 */
int
init_cpu_drc_info(struct dr_info *dr_info)
{
	struct dr_node *cpu;
	struct thread *t;
	int rc;

	memset(dr_info, 0, sizeof(*dr_info));

	rc = init_thread_info(dr_info);
	if (rc) {
		return -1;
	}

	rc = init_cpu_info(dr_info);
	if (rc) {
		free_cpu_drc_info(dr_info);
		return -1;
	}

	rc = init_cache_info(dr_info);
	if (rc) {
		free_cpu_drc_info(dr_info);
		return -1;
	}
	
	say(DEBUG, "Start CPU List.\n");
	for (cpu = dr_info->all_cpus; cpu; cpu = cpu->next) {
		say(DEBUG, "%x : %s\n", cpu->drc_index, cpu->drc_name);
		for (t = cpu->cpu_threads; t; t = t->sibling)
			say(DEBUG, "\tthread: %d: %s\n", t->phys_id, t->path);
	}
	say(DEBUG, "Done.\n");

	return 0;
}