CGI-SpeedyCGI

 view release on metacpan or  search on metacpan

speedy/t/chdir.t  view on Meta::CPAN

# it should restart the next time from the original directory.

# Test #3 same as #1, but cd to /tmp in between runs.  The backend
# shoudl track the chdir

# Tests 4&5, similar to 1&3, but start from a path where the parent
# is unreabable, meaning getcwd will fail on some oses.  The backend
# may not be able to get to the right dir in 4, so don't check that.

# Tests 6&7, same as 4&5, but with current directory mode 0, which makes
# stat(".") fail.

print "1..7\n";

# Test 1
my $scr = 't/scripts/chdir';

use strict;
use vars qw($start);
$start = `pwd`;
chop $start;

speedy_dump/SpeedyFile.pl  view on Meta::CPAN

}

sub fname { my $self = shift;
    $self->{fname} ||=
	sprintf("%s.${FILEREV}.%x.F", ($ENV{SPEEDY_TMPBASE} || '/tmp/speedy'), $>);
}

sub data { my $self = shift;
    if (!$self->{data}) {
	open(F, $self->fname) || die $self->fname . ": $!\n";
	my $sz = (stat(F))[7];
	my $data;
	read(F, $data, $sz);
	$self->{data} = $data;
	close(F);
    }
    return $self->{data};
}

sub get_struct { my($self, $type, $offset) = @_;
    if ($type !~ /^_/) {

src/mod_speedycgi.c  view on Meta::CPAN

			       "attempt to include NPH CGI script");

#if defined(OS2) || defined(WIN32)
    /* Allow for cgi files without the .EXE extension on them under OS/2 */
    if (r->finfo.st_mode == 0) {
	struct stat statbuf;
	char *newfile;

	newfile = ap_pstrcat(r->pool, r->filename, ".EXE", NULL);

	if ((stat(newfile, &statbuf) != 0) || (!S_ISREG(statbuf.st_mode))) {
	    return log_scripterror(r, NOT_FOUND, 0,
				   "script not found or unable to stat");
	} else {
	    r->filename = newfile;
	}
    }
#else
    if (r->finfo.st_mode == 0)
	return log_scripterror(r, NOT_FOUND, APLOG_NOERRNO,
			       "script not found or unable to stat");

src/speedy_file.c  view on Meta::CPAN

#ifdef SPEEDY_BACKEND
SPEEDY_INLINE void speedy_file_fd_is_suspect(void) {
    fd_is_suspect = 1;
}

static void fix_suspect_fd(void) {
    if (fd_is_suspect) {
	if (file_fd != -1) {
	    struct stat stbuf;

	    if (fstat(file_fd, &stbuf) == -1 ||
		stbuf.st_dev != file_stat.st_dev ||
		stbuf.st_ino != file_stat.st_ino)
	    {
		file_unmap();
		file_fd = -1;
	    }
	}
	fd_is_suspect = 0;
    }
}
#endif


#define get_stat() \
    if (fstat(file_fd, &file_stat) == -1) speedy_util_die("fstat")

static void remove_file(int is_corrupt) {
#ifdef SPEEDY_DEBUG
    if (is_corrupt) {
	/* Keep the file for debugging */
	char newname[200];
	struct timeval tv;

	gettimeofday(&tv, NULL);
	sprintf(newname, "%s.corrupt.%d.%06d.%d",

src/speedy_file.c  view on Meta::CPAN

	    if (file_fd == -1) speedy_util_die("open temp file");
	    fcntl(file_fd, F_SETFD, FD_CLOEXEC);
	}

	/* Lock the file */
	fillin_fl(fl);
	fl.l_type = F_WRLCK;
	if (fcntl(file_fd, F_SETLKW, &fl) == -1) speedy_util_die("lock file");

	/* Fstat the file, now that it's locked down */
	get_stat();

	/* Map into memory */
	file_map(file_stat.st_size);

	/* If file is too small (0 or below MIN_SLOTS_FREE), extend it */
	if (file_stat.st_size < sizeof(file_head_t) ||
	    file_stat.st_size < sizeof(file_head_t) +
		sizeof(slot_t) * (FILE_HEAD.slots_alloced + MIN_SLOTS_FREE))
	{
	    if (ftruncate(file_fd, file_stat.st_size + FILE_ALLOC_CHUNK) == -1)
		speedy_util_die("ftruncate");
	    get_stat();
	    file_map(file_stat.st_size);
	}

	/* Initialize file's create time if necessary */
	if (!FILE_HEAD.create_time.tv_sec)
	    speedy_util_gettimeofday(&(FILE_HEAD.create_time));
	
	/* Initialize our copy of the create-time if necessary */
	if (!file_create_time.tv_sec || cur_state < FS_HAVESLOTS) {
	    file_create_time = FILE_HEAD.create_time;

src/speedy_frontend.c  view on Meta::CPAN

#endif

    /* Add env and argv */
    add_strings(sb, envp);
    add_strings(sb, scr_argv+1);

    /* Put script filename into buffer */
    add_string(sb, script_fname, strlen(script_fname));

    /* Put script device/inode into buffer */
    ADD_DEVINO(sb, speedy_script_getstat());

    /* Handle passing over cwd */
    if (script_has_cwd) {
	ADDCHAR(sb, SPEEDY_CWD_IN_SCRIPT);
    }
    else if (stat(".", &dir_stat) != -1) {
	ADDCHAR(sb, SPEEDY_CWD_DEVINO);
	ADD_DEVINO(sb, &dir_stat);
    } else {
	ADDCHAR(sb, SPEEDY_CWD_UNKNOWN);
    }
}

void speedy_frontend_proto2(int err_sock, int first_byte) {
    int n, cwd_len, buflen;
    char *bp, *cwd;

src/speedy_ipc.c  view on Meta::CPAN


    listener = -1;
    if (PREF_FD_LISTENER != -1) {
	int namelen = sizeof(sa);
	char *fname = get_fname(slotnum, 0);
	struct stat stbuf;

	if (getsockname(PREF_FD_LISTENER, (struct sockaddr *)&sa, &namelen) != -1 &&
	    sa.sun_family == AF_UNIX &&
	    strcmp(sa.sun_path, fname) == 0 &&
	    stat(fname, &stbuf) != -1 &&
	    stbuf.st_uid == speedy_util_geteuid())
	{
	    listener = PREF_FD_LISTENER;
	}
	speedy_free(fname);
    }
    if (listener == -1) {
	mode_t saved_umask = umask(077);
	listener = make_sock();
	make_sockname(slotnum, &sa, 1);
	if (bind(listener, (struct sockaddr*)&sa, sizeof(sa)) == -1)
	    speedy_util_die("cannot bind socket");
	umask(saved_umask);
    }
    if (listen(listener, LISTEN_BACKLOG) == -1)
	speedy_util_die("cannot listen on socket");
    fstat(listener, &listener_stbuf);
    listener = speedy_util_pref_fd(listener, PREF_FD_LISTENER);
    fcntl(listener, F_SETFD, FD_CLOEXEC);
    speedy_poll_init(&listener_pi, listener);
}

static void ipc_unlisten(void) {
    close(listener);
    listener = -1;
    speedy_poll_free(&listener_pi);
}

void speedy_ipc_listen_fixfd(slotnum_t slotnum) {
    struct stat stbuf;

    /* Odd compiler bug - Solaris 2.7 plus gcc 2.95.2, can't put all of
     * this into one big "if" statment - returns false constantly.  Probably
     * has something to do with 64-bit values in st_dev/st_ino
     * 2.7 bug was found on sparc. Bug does not exist on Solaris-8/intel.
     */
#ifdef WANT_SOLARIS_BUG
    if ((fstat(listener, &stbuf) == -1) ||
	(stbuf.st_dev != listener_stbuf.st_dev) ||
	(stbuf.st_ino != listener_stbuf.st_ino))
#else
    int status, test1, test2;
    status = fstat(listener, &stbuf);
    test1 = stbuf.st_dev != listener_stbuf.st_dev;
    test2 = stbuf.st_ino != listener_stbuf.st_ino;
    if (status == -1 || test1 || test2)
#endif
    {
	ipc_unlisten();
	speedy_ipc_listen(slotnum);
    }
}

src/speedy_main.c  view on Meta::CPAN

    in_is_tty = fd_open(0) ? isatty(0) : 0;

    /* Initialize options */
    DO_OPT_INIT(argv, (const char * const *)environ);

#   ifdef IAMSUID
	if (speedy_util_geteuid() == 0) {
	    int new_uid;

	    /* Set group-id */
	    if (speedy_script_getstat()->st_mode & S_ISGID) {
		if (setegid(speedy_script_getstat()->st_gid) == -1)
		    speedy_util_die("setegid");
	    }

	    /* Must set euid to something - either the script owner
	     * or the real-uid
	     */
	    if (speedy_script_getstat()->st_mode & S_ISUID) {
		new_uid = speedy_script_getstat()->st_uid;
	    } else {
		new_uid = speedy_util_getuid();
	    }
	    if (speedy_util_seteuid(new_uid) == -1)
		speedy_util_die("seteuid");
	}
#   endif

    /* Create buffer with env/argv data to send */
    speedy_frontend_mkenv(

src/speedy_perl.c  view on Meta::CPAN

	hv_delete_ent(cwd_hash, cwd->path, G_DISCARD, 0);
	SvREFCNT_dec(cwd->path);
	speedy_free(cwd);
    }
}

static int stat_cwd_fd(SpeedyDevIno *devino) {
    struct stat stbuf;

    if (cwd_fd != -1) {
	if (fstat(cwd_fd, &stbuf) != -1) {
	    *devino = speedy_util_stat_devino(&stbuf);
	    return 1;
	}
	close(cwd_fd);
	cwd_fd = -1;
    }
    return 0;
}

static int chdir_path(const char *path, SpeedyDevIno *devino) {

src/speedy_perl.c  view on Meta::CPAN

	fcntl(cwd_fd, F_SETFD, FD_CLOEXEC);

    /* TEST - simulate unreadable "." directory */
    /* close(cwd_fd); cwd_fd = -1; */
    
    /* Get device/inode for "." */
    if (retval != -1 && devino && !stat_cwd_fd(devino)) {
	struct stat stbuf;

	if (cwd_fd == -1) {
	    if (stat(".", &stbuf) == -1) {
		devino->d = 0;
		devino->i = 0;
	    }
	} else {
	    *devino = speedy_util_stat_devino(&stbuf);
	}
    }
    return retval;
}

src/speedy_perl.c  view on Meta::CPAN

	sprintf(pkg, PACKAGE_FMT, hex_str);
    }

    /* Create phony package in sv with the script code in the handler func */
    {
	struct stat stbuf;
	SpeedyMapInfo *mi = NULL;
	int fd;

	/* Grab the contents of the file */
	if ((fd = speedy_util_open_stat(scr_path, &stbuf)) != -1) {
	    mi = speedy_util_mapin(fd, -1, stbuf.st_size);
	    close(fd);
	}
	if (fd == -1 || mi == NULL)
	    speedy_util_die(scr_path);

	/* Create sv to eval */
        sv = newSVpvn(PACKAGE1, sizeof(PACKAGE1)-1);
        sv_catpv (sv, pkg);
        sv_catpvn(sv, PACKAGE2, sizeof(PACKAGE2)-1);

src/speedy_perl.c  view on Meta::CPAN

    const char *temp_script_name;
    int use_devfd, is_new;
    char dev_fd_name[sizeof(DEVFD)+10];
    SpeedyScript *scr;
    int single_script = DOING_SINGLE_SCRIPT;

    /* If we're exec'ing a setuid script then we must use a temporary
     * script name of /dev/fd/N 
     */
    use_devfd = single_script &&
                speedy_script_getstat()->st_mode & (S_ISUID|S_ISGID);

    if (single_script) {
	if (use_devfd) {
	    sprintf(dev_fd_name, DEVFD, speedy_script_open());
	    temp_script_name = dev_fd_name;
	} else {
	    temp_script_name = NULL;
	}
    } else {
	temp_script_name = "-e&{$" SPEEDY_PKG("_sub") "}(@ARGV);";

src/speedy_perl.c  view on Meta::CPAN

    cleanup_after_perl();

    /* If we had to use /dev/fd/N, perl will close the file for us, so
     * make sure our code knows it's closed.  If we need it from here on out
     * it'll have to be re-opened.
     */
    if (use_devfd)
	speedy_script_close();

    /* Create a SpeedyScript entry for the standard script */
    scr = find_scr(speedy_util_stat_devino(speedy_script_getstat()), &is_new);

    /* If using groups, try pre-loading the script to save time later */
    if (!single_script && !speedy_script_open_failure()) {
	load_script(
	    speedy_util_stat_devino(speedy_script_getstat()),
	    scr, speedy_opt_script_fname()
	);
	cleanup_after_perl();
    }

    /* Time to close stderr */
    close(2);
}

void speedy_perl_run(slotnum_t gslotnum, slotnum_t bslotnum) {

src/speedy_script.c  view on Meta::CPAN

    time_t now = speedy_util_time();
    const char *fname;

    if (!last_open || now - last_open > OPTVAL_RESTATTIMEOUT) {

	speedy_script_close();

	if (!(fname = speedy_opt_script_fname()))
	    return 1;

	if ((script_fd = speedy_util_open_stat(fname, &script_stat)) == -1)
	    return 2;

	last_open = now;
    }
    return 0;
}

int speedy_script_open(void) {
    switch (speedy_script_open_failure()) {
	case 1:

src/speedy_script.c  view on Meta::CPAN

	return 0;
    stbuf = script_stat;
    (void) speedy_script_open();
    return
	stbuf.st_mtime != script_stat.st_mtime ||
	stbuf.st_ino != script_stat.st_ino ||
	stbuf.st_dev != script_stat.st_dev;
}
#endif

const struct stat *speedy_script_getstat(void) {
    speedy_script_open();
    return &script_stat;
}

slotnum_t speedy_script_find(void) {
    slotnum_t gslotnum, next, name_match = 0;
    int single_script = DOING_SINGLE_SCRIPT;
    
    (void) speedy_script_getstat();

    /* Find the slot for this script in the file */
    for (gslotnum = FILE_HEAD.group_head; gslotnum; gslotnum = next) {
	gr_slot_t *gslot = &FILE_SLOT(gr_slot, gslotnum);
	slotnum_t sslotnum = 0;
	next = speedy_slot_next(gslotnum);

	/* The end of the list contains only invalid groups */
	if (!speedy_group_isvalid(gslotnum)) {
	    gslotnum = 0;

src/speedy_script.c  view on Meta::CPAN

void speedy_script_munmap(void) {
    if (script_mapinfo) {
	speedy_util_mapout(script_mapinfo);
	script_mapinfo = NULL;
    }
}

SpeedyMapInfo *speedy_script_mmap(int max_size) {
    speedy_script_munmap();
    script_mapinfo = speedy_util_mapin(
	speedy_script_open(), max_size, speedy_script_getstat()->st_size
    );
    return script_mapinfo;
}

src/speedy_script.h  view on Meta::CPAN

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

slotnum_t speedy_script_find(void);
int  speedy_script_changed(void);
void speedy_script_close(void);
int  speedy_script_open(void);
const struct stat *speedy_script_getstat(void);
int  speedy_script_open_failure(void);
void speedy_script_munmap(void);
SpeedyMapInfo *speedy_script_mmap(int max_size);
void speedy_script_missing(void);

src/speedy_util.c  view on Meta::CPAN

    return mi;
}

SPEEDY_INLINE SpeedyDevIno speedy_util_stat_devino(const struct stat *stbuf) {
    SpeedyDevIno retval;
    retval.d = stbuf->st_dev;
    retval.i = stbuf->st_ino;
    return retval;
}

SPEEDY_INLINE int speedy_util_open_stat(const char *path, struct stat *stbuf)
{
    int fd = open(path, O_RDONLY);
    if (fd != -1 && fstat(fd, stbuf) == -1) {
       close(fd);
       fd = -1;
    }
    return fd;
}

void speedy_util_exit(int status, int underbar_exit) {

#   ifdef SPEEDY_PROFILING
	end_profiling(underbar_exit);

src/speedy_util.h  view on Meta::CPAN

int speedy_util_execvp(const char *filename, const char *const *argv);
char *speedy_util_strndup(const char *s, int len);
SPEEDY_INLINE int speedy_util_time(void);
SPEEDY_INLINE void speedy_util_gettimeofday(struct timeval *tv);
void speedy_util_time_invalidate(void);
char *speedy_util_fname(int num, char type);
char *speedy_util_getcwd(void);
SpeedyMapInfo *speedy_util_mapin(int fd, int max_size, int file_size);
void speedy_util_mapout(SpeedyMapInfo *mi);
SPEEDY_INLINE SpeedyDevIno speedy_util_stat_devino(const struct stat *stbuf);
SPEEDY_INLINE int speedy_util_open_stat(const char *path, struct stat *stbuf);
void speedy_util_exit(int status, int underbar_exit);
int speedy_util_kill(pid_t pid, int sig);

#define speedy_util_strdup(s) speedy_util_strndup(s, strlen(s))

#define PREF_FD_DONTCARE	-1

/* Preferred file descriptors */

#ifdef SPEEDY_BACKEND



( run in 0.665 second using v1.01-cache-2.11-cpan-49f99fa48dc )