CGI-SpeedyCGI

 view release on metacpan or  search on metacpan

src/speedy_backend_main.c  view on Meta::CPAN

    /* Don't spawn beyond maximum backends */
    if (!speedy_backend_below_maxbe(gslotnum))
	return;

    /* Create backend record */
    bslotnum = speedy_backend_create_slot(gslotnum);

    /* Fork */
    pid = NORMAL_RUN ?  speedy_perl_fork() : speedy_util_getpid();

    if (pid != 0) {
	/* PARENT */

	if (pid == -1) {
	    FILE_SLOT(gr_slot, gslotnum).be_starting = 0;
	    speedy_backend_dispose(gslotnum, bslotnum);
	} else {

	    /* List this be as starting */
	    FILE_SLOT(gr_slot, gslotnum).be_starting = pid;

	    /* Store pid */
	    FILE_SLOT(be_slot, bslotnum).pid = pid;
	}
    }

    if (!NORMAL_RUN || pid == 0) {
	/* CHILD */

	/* Cleanup after fork */
	speedy_util_pid_invalidate();
	if (NORMAL_RUN)
	    speedy_file_fork_child();
	speedy_file_set_state(FS_CLOSED);

	/* Restore signals */
	speedy_sig_free(sl);

	/* Do perl */
	speedy_perl_run(gslotnum, bslotnum);
	speedy_util_exit(0,0);
    }
}

static void do_cleanup(slotnum_t gslotnum) {

    /* Find the prev group, wrap around the end */
    {
	slotnum_t prev;

	if (!(prev = speedy_slot_prev(gslotnum)))
	    prev = FILE_HEAD.group_tail;

	/* Don't check ourself or slot-0 */
	if (!prev || prev == gslotnum)
	    return;

	gslotnum = prev;
    }

    /* Check the group to see if be_parent is alive.  If so, done */
    if (speedy_group_parent_sig(gslotnum, 0))
	return;

    /* Invalidate this group.  This should kill any waiting be's. */
    speedy_group_invalidate(gslotnum);

    /* Check for and clean up any dead bes */
    {
	slotnum_t next, bslotnum;

	for (bslotnum = FILE_SLOT(gr_slot, gslotnum).be_head;
	     bslotnum; bslotnum = next)
	{
	    next = speedy_slot_next(bslotnum);
	    if (speedy_backend_dead(bslotnum))
		speedy_backend_dispose(gslotnum, bslotnum);
	}
    }

    /* Try to delete this group altogether */
    speedy_group_cleanup(gslotnum);
}

int main(int argc, char **argv, char **_junk) {
    slotnum_t gslotnum;
    int i;
    SigList sl;

    speedy_util_unlimit_core();

    if (!(my_perl = perl_alloc()))
        DIE_QUIET("Cannot allocate perl");
    perl_construct(my_perl);

#ifdef SPEEDY_DEBUG
    dont_fork = getenv("SPEEDY_NOPARENT") != NULL;
#endif

    /*
     * Make sure fd's 0 and 1 are open.
     * Fix for bug where STDOUT couldn't be duped during perl init
     * Tested in begin_dup.t
     */
    if (open("/dev/null", O_RDONLY) == -1 || open("/dev/null", O_WRONLY) == -1)
	speedy_util_die("Cannot open /dev/null");

    /* Initialize options */
    speedy_opt_init((const char * const *)argv, (const char * const *)environ);
    
    /* Open/Stat the script - this could hang */
    speedy_opt_read_shbang();

    /* Initialize interpreter with this script */
    speedy_perl_init();

    /* Close off all I/O except for stderr (close it later) */
    for (i = 32; i >= 0; --i) {
	if (i != 2 && i != PREF_FD_LISTENER)
	    (void) close(i);
    }



( run in 0.852 second using v1.01-cache-2.11-cpan-df04353d9ac )