Exemplo n.º 1
0
int uwsgi_spooler_read_header(char *task, int spool_fd, struct uwsgi_header *uh) {

	// check if the file is locked by another process
	if (uwsgi_fcntl_is_locked(spool_fd)) {
		uwsgi_protected_close(spool_fd);
		return -1;
	}

	// unlink() can destroy the lock !!!
	if (access(task, R_OK|W_OK)) {
		uwsgi_protected_close(spool_fd);
		return -1;
	}

	ssize_t rlen = uwsgi_protected_read(spool_fd, uh, 4);

	if (rlen != 4) {
		// it could be here for broken file or just opened one
		if (rlen < 0)
			uwsgi_error("spooler_manage_task()/read()");
		uwsgi_protected_close(spool_fd);
		return -1;
	}

#ifdef __BIG_ENDIAN__
	uh->_pktsize = uwsgi_swap16(uh->_pktsize);
#endif

	return 0;
}
Exemplo n.º 2
0
void spooler_manage_task(struct uwsgi_spooler *uspool, char *dir, char *task) {

    int i, ret;

    char spool_buf[0xffff];
    struct uwsgi_header uh;
    char *body = NULL;
    size_t body_len = 0;

    int spool_fd;

    if (!dir) dir = uspool->dir;

    if (!strncmp("uwsgi_spoolfile_on_", task, 19) || (uwsgi.spooler_ordered && is_a_number(task))) {
        struct stat sf_lstat;
        if (lstat(task, &sf_lstat)) {
            return;
        }

        // a spool request for the future
        if (sf_lstat.st_mtime > uwsgi_now()) {
            return;
        }

#ifdef __linux__
        if (S_ISDIR(sf_lstat.st_mode) && uwsgi.spooler_ordered) {
            if (chdir(task)) {
                uwsgi_error("chdir()");
                return;
            }
            char *prio_path = realpath(".", NULL);
            spooler_scandir(uspool, prio_path);
            free(prio_path);
            if (chdir(dir)) {
                uwsgi_error("chdir()");
            }
            return;
        }
#endif
        if (!S_ISREG(sf_lstat.st_mode)) {
            return;
        }
        if (!access(task, R_OK | W_OK)) {

            spool_fd = open(task, O_RDWR);

            if (spool_fd < 0) {
                if (errno != ENOENT)
                    uwsgi_error_open(task);
                return;
            }

            // check if the file is locked by another process
            if (uwsgi_fcntl_is_locked(spool_fd)) {
                uwsgi_protected_close(spool_fd);
                return;
            }

            // unlink() can destroy the lock !!!
            if (access(task, R_OK | W_OK)) {
                uwsgi_protected_close(spool_fd);
                return;
            }

            ssize_t rlen = uwsgi_protected_read(spool_fd, &uh, 4);

            if (rlen != 4) {
                // it could be here for broken file or just opened one
                if (rlen < 0)
                    uwsgi_error("read()");
                uwsgi_protected_close(spool_fd);
                return;
            }

#ifdef __BIG_ENDIAN__
            uh.pktsize = uwsgi_swap16(uh.pktsize);
#endif

            if (uwsgi_protected_read(spool_fd, spool_buf, uh.pktsize) != uh.pktsize) {
                uwsgi_error("read()");
                destroy_spool(dir, task);
                uwsgi_protected_close(spool_fd);
                return;
            }

            // body available ?
            if (sf_lstat.st_size > (uh.pktsize+4)) {
                body_len = sf_lstat.st_size - (uh.pktsize+4);
                body = uwsgi_malloc(body_len);
                if ((size_t)uwsgi_protected_read(spool_fd, body, body_len) != body_len) {
                    uwsgi_error("read()");
                    destroy_spool(dir, task);
                    uwsgi_protected_close(spool_fd);
                    free(body);
                    return;
                }
            }

            // now the task is running and should not be waken up
            uspool->running = 1;

            if (!uwsgi.spooler_quiet)
                uwsgi_log("[spooler %s pid: %d] managing request %s ...\n", uspool->dir, (int) uwsgi.mypid, task);


            // chdir before running the task (if requested)
            if (uwsgi.spooler_chdir) {
                if (chdir(uwsgi.spooler_chdir)) {
                    uwsgi_error("chdir()");
                }
            }

            int callable_found = 0;
            for(i=0; i<256; i++) {
                if (uwsgi.p[i]->spooler) {
                    time_t now = uwsgi_now();
                    if(uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
                        set_spooler_harakiri(uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI]);
                    }
                    ret = uwsgi.p[i]->spooler(task, spool_buf, uh.pktsize, body, body_len);
                    if(uwsgi.shared->options[UWSGI_OPTION_SPOOLER_HARAKIRI] > 0) {
                        set_spooler_harakiri(0);
                    }
                    if (ret == 0) continue;
                    callable_found = 1;
                    // increase task counter
                    uspool->tasks++;
                    if (ret == -2) {
                        if (!uwsgi.spooler_quiet)
                            uwsgi_log("[spooler %s pid: %d] done with task %s after %d seconds\n", uspool->dir, (int) uwsgi.mypid, task, uwsgi_now()-now);
                        destroy_spool(dir, task);
                    }
                    // re-spool it
                    break;
                }
            }

            if (body)
                free(body);

            // here we free and unlock the task
            uwsgi_protected_close(spool_fd);
            uspool->running = 0;

            if (chdir(dir)) {
                uwsgi_error("chdir()");
                uwsgi_log("[spooler] something horrible happened to the spooler. Better to kill it.\n");
                exit(1);
            }

            if (!callable_found) {
                uwsgi_log("unable to find the spooler function, have you loaded it into the spooler process ?\n");
            }

        }
    }
}