w3m

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/w3m.git/
Log | Files | Refs | README

commit 257d273ecbaf9d6999c8b79d15891084baf88d6a
parent 5015829919a6a5855024b189620c613a1d5e8f83
Author: ukai <ukai>
Date:   Fri, 17 Jan 2003 16:57:17 +0000

[w3m-dev 03646] setup child process, local CGI
* etc.c (reset_signals): static
			don't ignore SIGUSR1
	(close_all_fds_except): static
			DEV_NULL_PATH
	(setup_child): added
	(myExec): rewrite
	(mySystem): rewrite
* file.c (readHeader): check image_source
	(loadGeneralFile): check image_source
	(doExternal): use setup_child
	(_doFileCopy): use setup_child
	(doFileSave): use setup_child
	(uncompress_stream): check image_source
			use setup_child
* image.c (getCharSize): no need stderr redirect
	(openImgdisplay): use setup_child
	(loadImage): use setup_child
	(getImageSize): no need stderr redirect
* local.c (writeLocalCookie): check Local_cookie_file
	(localcgi_popen_rw): added
	(localcgi_popen_r): deleted
	(localcgi_post): rewrite
	(localcgi_get): deleted
* proto.h (localcgi_get): defined by localcgi_post
	(reset_signals): deleted
	(close_all_fds_except): deleted
	(close_all_fds): deleted
	(setup_child): added
* search.c (open_migemo): use setup_child, myExec
* w3mimgdisplay.c (main): use DEV_NULL_PATH
From: Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>

Diffstat:
MChangeLog | 34++++++++++++++++++++++++++++++++++
Metc.c | 36+++++++++++++++++++++++-------------
Mfile.c | 35++++++++---------------------------
Mimage.c | 25+++++++------------------
Mlocal.c | 173++++++++++++++++++++++++++++++-------------------------------------------------
Mproto.h | 8++------
Msearch.c | 10+++-------
Mw3mimgdisplay.c | 2+-
8 files changed, 144 insertions(+), 179 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,3 +1,37 @@ +2003-01-18 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp> + + * [w3m-dev 03646] setup child process, local CGI + * etc.c (reset_signals): static + don't ignore SIGUSR1 + (close_all_fds_except): static + DEV_NULL_PATH + (setup_child): added + (myExec): rewrite + (mySystem): rewrite + * file.c (readHeader): check image_source + (loadGeneralFile): check image_source + (doExternal): use setup_child + (_doFileCopy): use setup_child + (doFileSave): use setup_child + (uncompress_stream): check image_source + use setup_child + * image.c (getCharSize): no need stderr redirect + (openImgdisplay): use setup_child + (loadImage): use setup_child + (getImageSize): no need stderr redirect + * local.c (writeLocalCookie): check Local_cookie_file + (localcgi_popen_rw): added + (localcgi_popen_r): deleted + (localcgi_post): rewrite + (localcgi_get): deleted + * proto.h (localcgi_get): defined by localcgi_post + (reset_signals): deleted + (close_all_fds_except): deleted + (close_all_fds): deleted + (setup_child): added + * search.c (open_migemo): use setup_child, myExec + * w3mimgdisplay.c (main): use DEV_NULL_PATH + 2003-01-16 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp> * [w3m-dev 03644] Re: Other user can see local cookie. diff --git a/etc.c b/etc.c @@ -7,6 +7,7 @@ #include "hash.h" #include "terms.h" +#include <fcntl.h> #include <sys/types.h> #include <time.h> #if defined(HAVE_WAITPID) || defined(HAVE_WAIT3) @@ -1279,7 +1280,7 @@ romanAlphabet(int n) #define SIGIOT SIGABRT #endif /* not SIGIOT */ -void +static void reset_signals(void) { signal(SIGHUP, SIG_DFL); /* terminate process */ @@ -1298,23 +1299,22 @@ reset_signals(void) #ifdef SIGPIPE signal(SIGPIPE, SIG_IGN); #endif - signal(SIGUSR1, SIG_IGN); } #ifndef FOPEN_MAX #define FOPEN_MAX 1024 /* XXX */ #endif -void +static void close_all_fds_except(int i, int f) { switch (i) { /* fall through */ case 0: - dup2(open("/dev/null", O_RDONLY), 0); + dup2(open(DEV_NULL_PATH, O_RDONLY), 0); case 1: - dup2(open("/dev/null", O_WRONLY), 1); + dup2(open(DEV_NULL_PATH, O_WRONLY), 1); case 2: - dup2(open("/dev/null", O_WRONLY), 2); + dup2(open(DEV_NULL_PATH, O_WRONLY), 2); } /* close all other file descriptors (socket, ...) */ for (i = 3; i < FOPEN_MAX; i++) { @@ -1323,27 +1323,37 @@ close_all_fds_except(int i, int f) } } -#ifdef HAVE_SETPGRP void -myExec(char *command) +setup_child(int child, int i, int f) { reset_signals(); - SETPGRP(); + signal(SIGINT, SIG_IGN); + if (!child) + SETPGRP(); close_tty(); - close_all_fds(0); + close_all_fds_except(i, f); + QuietMessage = TRUE; + fmInitialized = FALSE; +} + +void +myExec(char *command) +{ + signal(SIGINT, SIG_DFL); execl("/bin/sh", "sh", "-c", command, NULL); exit(127); } -#endif void mySystem(char *command, int background) { if (background) { -#ifdef HAVE_SETPGRP +#ifndef __EMX__ flush_tty(); - if (!fork()) + if (!fork()) { + setup_child(FALSE, 0, -1); myExec(command); + } #else Str cmd = Strnew_charp("start /f "); Strcat_charp(cmd, command); diff --git a/file.c b/file.c @@ -594,7 +594,7 @@ readHeader(URLFile *uf, Buffer *newBuf, int thru, ParsedURL *pu) else http_response_code = 0; - if (thru && !newBuf->header_source) { + if (thru && !newBuf->header_source && !image_source) { Str tmpf = tmpfname(TMPF_DFL, NULL); src = fopen(tmpf->ptr, "w"); if (src) @@ -1885,6 +1885,8 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer, page_loaded: if (page) { FILE *src; + if (image_source) + return NULL; tmp = tmpfname(TMPF_SRC, ".html"); src = fopen(tmp->ptr, "w"); if (src) { @@ -7315,11 +7317,7 @@ doExternal(URLFile uf, char *path, char *type, Buffer **bufp, !(mcap->flags & MAILCAP_NEEDSTERMINAL) && BackgroundExtViewer) { flush_tty(); if (!fork()) { - reset_signals(); - signal(SIGINT, SIG_IGN); - close_tty(); - QuietMessage = TRUE; - fmInitialized = FALSE; + setup_child(FALSE, 0, UFfileno(&uf)); if (save2tmp(uf, tmpf->ptr) < 0) exit(1); UFclose(&uf); @@ -7484,13 +7482,7 @@ _doFileCopy(char *tmpf, char *defstr, int download) flush_tty(); pid = fork(); if (!pid) { - reset_signals(); - signal(SIGINT, SIG_IGN); - SETPGRP(); - close_tty(); - close_all_fds(2); - QuietMessage = TRUE; - fmInitialized = FALSE; + setup_child(FALSE, 0, -1); if (!_MoveFile(tmpf, p) && PreserveTimestamp && !is_pipe && !stat(tmpf, &st)) setModtime(p, st.st_mtime); @@ -7591,13 +7583,7 @@ doFileSave(URLFile uf, char *defstr) flush_tty(); pid = fork(); if (!pid) { - reset_signals(); - signal(SIGINT, SIG_IGN); - SETPGRP(); - close_tty(); - close_all_fds_except(2, UFfileno(&uf)); - QuietMessage = TRUE; - fmInitialized = FALSE; + setup_child(FALSE, 0, UFfileno(&uf)); if (!save2tmp(uf, p) && PreserveTimestamp && uf.modtime != -1) setModtime(p, uf.modtime); UFclose(&uf); @@ -7733,7 +7719,7 @@ uncompress_stream(URLFile *uf, char **src) return; } - if (uf->scheme != SCM_LOCAL) { + if (uf->scheme != SCM_LOCAL && !image_source) { tmpf = tmpfname(TMPF_DFL, ext)->ptr; if (save2tmp(*uf, tmpf) < 0) { UFclose(uf); @@ -7752,15 +7738,10 @@ uncompress_stream(URLFile *uf, char **src) flush_tty(); /* fd1[0]: read, fd1[1]: write */ if ((pid1 = fork()) == 0) { - reset_signals(); - signal(SIGINT, SIG_IGN); - close_tty(); - QuietMessage = TRUE; - fmInitialized = FALSE; close(fd1[0]); dup2(fd1[1], 1); dup2(fd1[1], 2); - close_all_fds_except(-1, UFfileno(uf)); + setup_child(TRUE, -1, UFfileno(uf)); if (tmpf) { #ifdef USE_BINMODE_STREAM int tmpfd = open(tmpf, O_RDONLY | O_BINARY); diff --git a/image.c b/image.c @@ -54,7 +54,7 @@ getCharSize() tmp = Strnew(); if (!strchr(Imgdisplay, '/')) Strcat_m_charp(tmp, w3m_auxbin_dir(), "/", NULL); - Strcat_m_charp(tmp, Imgdisplay, " -test 2> /dev/null", NULL); + Strcat_m_charp(tmp, Imgdisplay, " -test", NULL); f = popen(tmp->ptr, "r"); if (!f) return FALSE; @@ -103,23 +103,18 @@ openImgdisplay() goto err2; if (Imgdisplay_pid == 0) { /* child */ - reset_signals(); - signal(SIGINT, SIG_IGN); - set_environ("W3M_TTY", ttyname_tty()); - SETPGRP(); - close_tty(); close(fdr[0]); close(fdw[1]); dup2(fdw[0], 0); dup2(fdr[1], 1); - close_all_fds(2); + setup_child(FALSE, 2, -1); + set_environ("W3M_TTY", ttyname_tty()); if (!strchr(Imgdisplay, '/')) cmd = Strnew_m_charp(w3m_auxbin_dir(), "/", Imgdisplay, NULL)->ptr; else cmd = Imgdisplay; - execl("/bin/sh", "sh", "-c", cmd, NULL); - /* XXX: ifndef HAVE_SETPGRP, use start /f ? */ - exit(1); + myExec(cmd); + /* XXX: ifdef __EMX__, use start /f ? */ } close(fdr[1]); close(fdw[0]); @@ -453,12 +448,7 @@ loadImage(int flag) flush_tty(); if ((cache->pid = fork()) == 0) { Buffer *b; - reset_signals(); - signal(SIGINT, SIG_IGN); - close_tty(); - close_all_fds(2); - QuietMessage = TRUE; - fmInitialized = FALSE; + setup_child(TRUE, 0, -1); image_source = cache->file; b = loadGeneralFile(cache->url, cache->current, NULL, 0, NULL); if (!b || !b->real_type || strncasecmp(b->real_type, "image/", 6)) @@ -554,8 +544,7 @@ getImageSize(ImageCache * cache) tmp = Strnew(); if (!strchr(Imgdisplay, '/')) Strcat_m_charp(tmp, w3m_auxbin_dir(), "/", NULL); - Strcat_m_charp(tmp, Imgdisplay, " -size ", shell_quote(cache->file), - " 2> /dev/null", NULL); + Strcat_m_charp(tmp, Imgdisplay, " -size ", shell_quote(cache->file), NULL); f = popen(tmp->ptr, "r"); if (!f) return FALSE; diff --git a/local.c b/local.c @@ -32,6 +32,8 @@ writeLocalCookie() if (no_rc_dir) return; + if (Local_cookie_file) + return; Local_cookie_file = tmpfname(TMPF_COOKIE, NULL)->ptr; set_environ("LOCAL_COOKIE_FILE", Local_cookie_file); f = fopen(Local_cookie_file, "wb"); @@ -351,61 +353,59 @@ cgi_filename(char *fn, int *status) } static pid_t -localcgi_popen_r(FILE ** p_fp) +localcgi_popen_rw(int *p_fdr, int *p_fdw) { - int fd[2]; - FILE *fp; + int fdr[2], fdw[2]; pid_t pid; Str emsg; - if (pipe(fd) < 0) { - emsg = Sprintf("localcgi_popen_r: pipe: %s", strerror(errno)); - disp_err_message(emsg->ptr, FALSE); - return (pid_t) - 1; + if (pipe(fdr) < 0) { + emsg = Sprintf("localcgi_popen_rw: pipe: %s", strerror(errno)); + goto pipe_err0; + } + if (p_fdw && pipe(fdw) < 0) { + emsg = Sprintf("localcgi_popen_rw: pipe: %s", strerror(errno)); + goto pipe_err1; } flush_tty(); if ((pid = fork()) < 0) { - emsg = Sprintf("localcgi_popen_r: fork: %s", strerror(errno)); - disp_err_message(emsg->ptr, FALSE); - close(fd[0]); - close(fd[1]); - return (pid_t) - 1; + emsg = Sprintf("localcgi_popen_rw: fork: %s", strerror(errno)); + goto pipe_err2; } else if (!pid) { - close_tty(); - dup2(fd[1], 1); - - if (fd[1] > 1) - close(fd[1]); - - close(fd[0]); - close_all_fds(2); + close(fdr[0]); + dup2(fdr[1], 1); + if (p_fdw) { + close(fdw[1]); + dup2(fdw[0], 0); + } + setup_child(TRUE, 2, -1); } else { - close(fd[1]); - - if (!(fp = fdopen(fd[0], "r"))) { - emsg = - Sprintf("localcgi_popen_r: fdopen(%d, \"r\"): %s", fd[0], - strerror(errno)); - disp_err_message(emsg->ptr, FALSE); - kill(pid, SIGTERM); - close(fd[0]); - return (pid_t) - 1; + close(fdr[1]); + *p_fdr = fdr[0]; + if (p_fdw) { + close(fdw[0]); + *p_fdw = fdw[1]; } - - *p_fp = fp; } - return pid; + pipe_err2: + close(fdw[0]); + close(fdw[1]); + pipe_err1: + close(fdr[0]); + close(fdr[1]); + pipe_err0: + disp_err_message(emsg->ptr, FALSE); + return (pid_t) - 1; } FILE * localcgi_post(char *uri, char *qstr, FormList *request, char *referer) { - FILE *f, *f1; - Str tmp1; + int fdr, fdw = -1; int status; pid_t pid; char *file; @@ -414,88 +414,47 @@ localcgi_post(char *uri, char *qstr, FormList *request, char *referer) if (check_local_cgi(file, status) < 0) return NULL; writeLocalCookie(); - tmp1 = tmpfname(TMPF_DFL, NULL); - if ((pid = localcgi_popen_r(&f)) < 0) + if (request && request->enctype != FORM_ENCTYPE_MULTIPART) + pid = localcgi_popen_rw(&fdr, &fdw); + else + pid = localcgi_popen_rw(&fdr, NULL); + if (pid < 0) return NULL; - else if (pid) - return f; - f1 = fopen(tmp1->ptr, "w"); - if (f1 == NULL) - exit(1); - if (qstr == NULL) { - set_cgi_environ(uri, file, uri); + else if (pid) { + if (fdw > 0) { + write(fdw, request->body, request->length); + close(fdw); + } + return fdopen(fdr, "r"); } - else { + + if (qstr == NULL) + set_cgi_environ(uri, file, uri); + else set_cgi_environ(uri, file, Strnew_m_charp(uri, "?", qstr, NULL)->ptr); - } - set_environ("REQUEST_METHOD", "POST"); - if (qstr) - set_environ("QUERY_STRING", qstr); - set_environ("CONTENT_LENGTH", Sprintf("%d", request->length)->ptr); if (referer && referer != NO_REFERER) set_environ("HTTP_REFERER", referer); - if (request->enctype == FORM_ENCTYPE_MULTIPART) { - set_environ("CONTENT_TYPE", - Sprintf("multipart/form-data; boundary=%s", - request->boundary)->ptr); - } - else { - set_environ("CONTENT_TYPE", "application/x-www-form-urlencoded"); - } - if (request->enctype == FORM_ENCTYPE_MULTIPART) { - FILE *fd; - int c; - fd = fopen(request->body, "r"); - if (fd != NULL) { - while ((c = fgetc(fd)) != EOF) - fputc(c, f1); - fclose(fd); + if (request) { + set_environ("REQUEST_METHOD", "POST"); + if (qstr) + set_environ("QUERY_STRING", qstr); + set_environ("CONTENT_LENGTH", Sprintf("%d", request->length)->ptr); + if (request->enctype == FORM_ENCTYPE_MULTIPART) { + set_environ("CONTENT_TYPE", + Sprintf("multipart/form-data; boundary=%s", + request->boundary)->ptr); + freopen(request->body, "r", stdin); } + else + set_environ("CONTENT_TYPE", "application/x-www-form-urlencoded"); } else { - fputs(request->body, f1); + set_environ("REQUEST_METHOD", "GET"); + set_environ("QUERY_STRING", qstr ? qstr : ""); + freopen(DEV_NULL_PATH, "r", stdin); } - fclose(f1); - freopen(tmp1->ptr, "r", stdin); -#ifdef HAVE_CHDIR /* ifndef __EMX__ ? */ - chdir(mydirname(file)); -#endif - execl(file, mybasename(file), NULL); - fprintf(stderr, "execl(\"%s\", \"%s\", NULL): %s\n", - file, mybasename(file), strerror(errno)); - exit(1); - return NULL; -} - -FILE * -localcgi_get(char *uri, char *request, char *referer) -{ - FILE *f; - int status; - pid_t pid; - char *file; - file = cgi_filename(uri, &status); - if (check_local_cgi(file, status) < 0) - return NULL; - writeLocalCookie(); - if ((pid = localcgi_popen_r(&f)) < 0) - return NULL; - else if (pid) - return f; - if (request == NULL) { - set_cgi_environ(Strnew_charp(uri)->ptr, file, Strnew_charp(uri)->ptr); - } - else { - set_cgi_environ(Strnew_charp(uri)->ptr, file, - Strnew_m_charp(uri, "?", request, NULL)->ptr); - } - if (referer && referer != NO_REFERER) - set_environ("HTTP_REFERER", referer); - set_environ("REQUEST_METHOD", "GET"); - set_environ("QUERY_STRING", request ? request : ""); - freopen(DEV_NULL_PATH, "r", stdin); -#ifdef HAVE_CHDIR /* ifndef __EMX__? */ +#ifdef HAVE_CHDIR /* ifndef __EMX__ ? */ chdir(mydirname(file)); #endif execl(file, mybasename(file), NULL); diff --git a/proto.h b/proto.h @@ -574,7 +574,7 @@ extern void setLocalCookie(void); extern Str loadLocalDir(char *dirname); extern void set_environ(char *var, char *value); extern FILE *localcgi_post(char *, char *, FormList *, char *); -extern FILE *localcgi_get(char *, char *, char *); +#define localcgi_get(u, q, r) localcgi_post((u), (q), NULL, (r)) extern FILE *openSecretFile(char *fname); extern void loadPasswd(void); extern void loadPreForm(void); @@ -586,12 +586,8 @@ extern void add_auth_cookie(char *host, int port, char *file, char *realm, extern char *last_modified(Buffer *buf); extern Str romanNumeral(int n); extern Str romanAlphabet(int n); -extern void reset_signals(void); -extern void close_all_fds_except(int i, int f); -#define close_all_fds(i) close_all_fds_except(i, -1) -#ifdef HAVE_SETPGRP +extern void setup_child(int child, int i, int f); extern void myExec(char *command); -#endif extern void mySystem(char *command, int background); extern Str myExtCommand(char *cmd, char *arg, int redirect); extern Str myEditor(char *cmd, char *file, int line); diff --git a/search.c b/search.c @@ -50,17 +50,13 @@ open_migemo(char *migemo_command) goto err2; if (migemo_pid == 0) { /* child */ - reset_signals(); - SETPGRP(); - close_tty(); close(fdr[0]); close(fdw[1]); dup2(fdw[0], 0); dup2(fdr[1], 1); - close_all_fds(2); - execl("/bin/sh", "sh", "-c", migemo_command, NULL); - /* XXX: ifndef HAVE_SETPGRP, use "start /f"? */ - exit(1); + setup_child(FALSE, 2, -1); + myExec(migemo_command); + /* XXX: ifdef __EMX__, use start /f ? */ } close(fdr[1]); close(fdw[0]); diff --git a/w3mimgdisplay.c b/w3mimgdisplay.c @@ -37,7 +37,7 @@ main(int argc, char **argv) #endif GetOption(argc, argv); if (!defined_debug) - freopen("/dev/null", "w", stderr); + freopen(DEV_NULL_PATH, "w", stderr); #ifdef W3MIMGDISPLAY_SETUID /*