[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>
This commit is contained in:
Fumitoshi UKAI
2003-01-17 16:57:17 +00:00
parent 4e7b05a901
commit 439d2745a5
8 changed files with 153 additions and 188 deletions
+35 -1
View File
@@ -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.
@@ -6519,4 +6553,4 @@ a * [w3m-dev 03276] compile error on EWS4800
* release-0-2-1
* import w3m-0.2.1
$Id: ChangeLog,v 1.685 2003/01/15 17:13:21 ukai Exp $
$Id: ChangeLog,v 1.686 2003/01/17 16:57:17 ukai Exp $
+25 -15
View File
@@ -1,4 +1,4 @@
/* $Id: etc.c,v 1.49 2003/01/15 17:13:21 ukai Exp $ */
/* $Id: etc.c,v 1.50 2003/01/17 16:57:18 ukai Exp $ */
#include "fm.h"
#include <pwd.h>
#include "myctype.h"
@@ -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
setup_child(int child, int i, int f)
{
reset_signals();
signal(SIGINT, SIG_IGN);
if (!child)
SETPGRP();
close_tty();
close_all_fds_except(i, f);
QuietMessage = TRUE;
fmInitialized = FALSE;
}
void
myExec(char *command)
{
reset_signals();
SETPGRP();
close_tty();
close_all_fds(0);
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);
+9 -28
View File
@@ -1,4 +1,4 @@
/* $Id: file.c,v 1.187 2003/01/15 17:13:21 ukai Exp $ */
/* $Id: file.c,v 1.188 2003/01/17 16:57:19 ukai Exp $ */
#include "fm.h"
#include <sys/types.h>
#include "myctype.h"
@@ -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);
+8 -19
View File
@@ -1,4 +1,4 @@
/* $Id: image.c,v 1.25 2003/01/15 16:11:43 ukai Exp $ */
/* $Id: image.c,v 1.26 2003/01/17 16:57:19 ukai Exp $ */
#include "fm.h"
#include <sys/types.h>
@@ -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;
+67 -108
View File
@@ -1,4 +1,4 @@
/* $Id: local.c,v 1.21 2003/01/15 17:13:22 ukai Exp $ */
/* $Id: local.c,v 1.22 2003/01/17 16:57:20 ukai Exp $ */
#include "fm.h"
#include <string.h>
#include <stdio.h>
@@ -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,49 +414,46 @@ 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) {
else if (pid) {
if (fdw > 0) {
write(fdw, request->body, request->length);
close(fdw);
}
return fdopen(fdr, "r");
}
if (qstr == NULL)
set_cgi_environ(uri, file, uri);
}
else {
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
@@ -466,41 +463,3 @@ localcgi_post(char *uri, char *qstr, FormList *request, char *referer)
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__? */
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;
}
+3 -7
View File
@@ -1,4 +1,4 @@
/* $Id: proto.h,v 1.79 2003/01/15 17:13:22 ukai Exp $ */
/* $Id: proto.h,v 1.80 2003/01/17 16:57:20 ukai Exp $ */
/*
* This file was automatically generated by version 1.7 of cextract.
* Manual editing not recommended.
@@ -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);
+4 -8
View File
@@ -1,4 +1,4 @@
/* $Id: search.c,v 1.23 2002/12/18 16:33:19 ukai Exp $ */
/* $Id: search.c,v 1.24 2003/01/17 16:57:20 ukai Exp $ */
#include "fm.h"
#include "regex.h"
#include <signal.h>
@@ -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]);
+2 -2
View File
@@ -1,4 +1,4 @@
/* $Id: w3mimgdisplay.c,v 1.11 2002/12/25 16:14:45 ukai Exp $ */
/* $Id: w3mimgdisplay.c,v 1.12 2003/01/17 16:57:21 ukai Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
@@ -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
/*