reverted have*fork.c to erik's unlimited allocation to work better together with werc posts

This commit is contained in:
Anselm R Garbe
2011-01-05 08:33:42 +00:00
parent 38702511c5
commit 0ad0bc437b
3 changed files with 57 additions and 53 deletions

View File

@@ -4,7 +4,7 @@
PREFIX = /usr/local/plan9 PREFIX = /usr/local/plan9
MANPREFIX = ${PREFIX}/share/man MANPREFIX = ${PREFIX}/share/man
VERSION = 6 VERSION = 7
OBJTYPE = 386 OBJTYPE = 386
#OBJTYPE = arm #OBJTYPE = arm
#OBJTYPE = x86_64 #OBJTYPE = x86_64

View File

@@ -1,9 +1,3 @@
#include <u.h>
#include <signal.h>
#if defined(PLAN9PORT) && defined(__sun__)
# define BSD_COMP /* sigh. for TIOCNOTTY */
#endif
#include <sys/ioctl.h>
#include "rc.h" #include "rc.h"
#include "getflags.h" #include "getflags.h"
#include "exec.h" #include "exec.h"
@@ -16,7 +10,6 @@ void
Xasync(void) Xasync(void)
{ {
int null = open("/dev/null", 0); int null = open("/dev/null", 0);
int tty;
int pid; int pid;
char npid[10]; char npid[10];
if(null<0){ if(null<0){
@@ -30,33 +23,7 @@ Xasync(void)
break; break;
case 0: case 0:
clearwaitpids(); clearwaitpids();
/* pushredir(ROPEN, null, 0);
* I don't know what the right thing to do here is,
* so this is all experimentally determined.
* If we just dup /dev/null onto 0, then running
* ssh foo & will reopen /dev/tty, try to read a password,
* get a signal, and repeat, in a tight loop, forever.
* Arguably this is a bug in ssh (it behaves the same
* way under bash as under rc) but I'm fixing it here
* anyway. If we dissociate the process from the tty,
* then it won't be able to open /dev/tty ever again.
* The SIG_IGN on SIGTTOU makes writing the tty
* (via fd 1 or 2, for example) succeed even though
* our pgrp is not the terminal's controlling pgrp.
*/
if((tty = open("/dev/tty", OREAD)) >= 0){
/*
* Should make reads of tty fail, writes succeed.
*/
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
ioctl(tty, TIOCNOTTY);
close(tty);
}
if(isatty(0))
pushredir(ROPEN, null, 0);
else
close(null);
start(runq->code, runq->pc+1, runq->local); start(runq->code, runq->pc+1, runq->local);
runq->ret = 0; runq->ret = 0;
break; break;
@@ -104,21 +71,31 @@ Xpipe(void)
} }
} }
char*
erealloc(char *p, long n)
{
p = realloc(p, n); /* botch, should be Realloc */
if(p==0)
panic("Can't realloc %d bytes\n", n);
return p;
}
/* /*
* Who should wait for the exit from the fork? * Who should wait for the exit from the fork?
*/ */
enum { Stralloc = 100, };
void void
Xbackq(void) Xbackq(void)
{ {
char wd[8193]; int c, l, pid;
int c; int pfd[2];
char *s, *ewd=&wd[8192], *stop; char *s, *wd, *ewd, *stop;
struct io *f; struct io *f;
var *ifs = vlook("ifs"); var *ifs = vlook("ifs");
word *v, *nextv; word *v, *nextv;
int pfd[2];
int pid; stop = ifs->val? ifs->val->word: "";
stop = ifs->val?ifs->val->word:"";
if(pipe(pfd)<0){ if(pipe(pfd)<0){
Xerror("can't make pipe"); Xerror("can't make pipe");
return; return;
@@ -139,10 +116,16 @@ Xbackq(void)
addwaitpid(pid); addwaitpid(pid);
close(pfd[PWR]); close(pfd[PWR]);
f = openfd(pfd[PRD]); f = openfd(pfd[PRD]);
s = wd; s = wd = ewd = 0;
v = 0; v = 0;
while((c = rchr(f))!=EOF){ while((c = rchr(f))!=EOF){
if(strchr(stop, c) || s==ewd){ if(s==ewd){
l = s-wd;
wd = erealloc(wd, l+Stralloc);
ewd = wd+l+Stralloc-1;
s = wd+l;
}
if(strchr(stop, c)){
if(s!=wd){ if(s!=wd){
*s='\0'; *s='\0';
v = newword(wd, v); v = newword(wd, v);
@@ -155,6 +138,8 @@ Xbackq(void)
*s='\0'; *s='\0';
v = newword(wd, v); v = newword(wd, v);
} }
if(wd)
efree(wd);
closeio(f); closeio(f);
Waitfor(pid, 0); Waitfor(pid, 0);
/* v points to reversed arglist -- reverse it onto argv */ /* v points to reversed arglist -- reverse it onto argv */
@@ -254,4 +239,4 @@ execforkexec(void)
} }
addwaitpid(pid); addwaitpid(pid);
return pid; return pid;
} }

View File

@@ -51,12 +51,23 @@ Xasync(void)
setvar("apid", newword(buf, (word *)0)); setvar("apid", newword(buf, (word *)0));
} }
char*
erealloc(char *p, long n)
{
p = realloc(p, n); /* botch, should be Realloc */
if(p==0)
panic("Can't realloc %d bytes\n", n);
return p;
}
enum { Stralloc = 100, };
void void
Xbackq(void) Xbackq(void)
{ {
char wd[8193], **argv; char **argv;
int c; int c, l;
char *s, *ewd=&wd[8192], *stop; char *s, *wd, *ewd, *stop;
struct io *f; struct io *f;
var *ifs = vlook("ifs"); var *ifs = vlook("ifs");
word *v, *nextv; word *v, *nextv;
@@ -84,14 +95,20 @@ Xbackq(void)
} }
f = openfd(pfd[0]); f = openfd(pfd[0]);
s = wd; s = wd = ewd = 0;
v = 0; v = 0;
while((c=rchr(f))!=EOF){ while((c=rchr(f))!=EOF){
if(strchr(stop, c) || s==ewd){ if(s==ewd){
l = s-wd;
wd = erealloc(wd, l+Stralloc);
ewd = wd+l+Stralloc-1;
s = wd+l;
}
if(strchr(stop, c)){
if(s!=wd){ if(s!=wd){
*s='\0'; *s='\0';
v=newword(wd, v); v = newword(wd, v);
s=wd; s = wd;
} }
} }
else *s++=c; else *s++=c;
@@ -100,6 +117,8 @@ Xbackq(void)
*s='\0'; *s='\0';
v=newword(wd, v); v=newword(wd, v);
} }
if(wd)
efree(wd);
closeio(f); closeio(f);
Waitfor(pid, 1); Waitfor(pid, 1);
/* v points to reversed arglist -- reverse it onto argv */ /* v points to reversed arglist -- reverse it onto argv */
@@ -190,7 +209,7 @@ execforkexec(void)
for(path = searchpath(runq->argv->words->word);path;path = path->next){ for(path = searchpath(runq->argv->words->word);path;path = path->next){
nc = strlen(path->word); nc = strlen(path->word);
if(nc<sizeof(file)){ if(nc < sizeof file - 1){ /* 1 for / */
strcpy(file, path->word); strcpy(file, path->word);
if(file[0]){ if(file[0]){
strcat(file, "/"); strcat(file, "/");
@@ -208,4 +227,4 @@ execforkexec(void)
} }
free(argv); free(argv);
return -1; return -1;
} }