tvarious bug fixes - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 2e965b3324b32be00a2193bf304dcb936f02824b
 (DIR) parent 4f48d1d4f72b1986ba6df3ccd9db62cfa1ef3df9
 (HTM) Author: rsc <devnull@localhost>
       Date:   Wed,  5 May 2004 04:22:16 +0000
       
       various bug fixes
       
       Diffstat:
         M acid/thread                         |      10 +++-------
         M src/cmd/acid/proc.c                 |      13 ++++++++++++-
         M src/lib9/_p9proc.c                  |       4 ++--
         M src/libhttpd/gethead.c              |      11 +++--------
         M src/libhttpd/hio.c                  |       6 +++---
         M src/libhttpd/parse.c                |       2 +-
         M src/libhttpd/parsereq.c             |       4 ++--
         M src/libmach/Linux.c                 |      49 ++++++++++++++++++++++++++-----
         M src/libmach/map.c                   |       2 +-
         M src/libthread/note.c                |       1 +
         M src/libventi/rpc.c                  |       1 +
         M src/libventi/srvhello.c             |      20 +++++++++++---------
       
       12 files changed, 82 insertions(+), 41 deletions(-)
       ---
 (DIR) diff --git a/acid/thread b/acid/thread
       t@@ -61,11 +61,6 @@ defn alt(A){
                print(altfmt(A), "\n");
        }
        
       -threadignsrc = {
       -        "plan9/src/libc",
       -        "plan9/src/libthread",
       -};
       -
        defn fnname(a){
                local sym, s;
        
       t@@ -100,7 +95,7 @@ defn threadstkline(T){
                        pc = frame[2];
                        pc0 = frame[0];
                        file = pcfile(pc);
       -                if !regexp("plan9/src/libc/", file)
       +                if !regexp("plan9/src/lib9/", file)
                        && !regexp("plan9/src/libthread/", file) 
                        && match(file, stkignore)==-1 then
                                stop = 1;
       t@@ -234,7 +229,8 @@ defn lproc(P){
        
        threadstkignore = {
                "plan9/src/libthread/",
       -        "plan9/src/libc/(386|arm|alpha|sparc|power|mips)/"
       +        "plan9/src/lib9/",
       +        "plan9/src/lib9/(fmt|utf)/",
        };
        defn threadstks(P){
                complex Proc P;
 (DIR) diff --git a/src/cmd/acid/proc.c b/src/cmd/acid/proc.c
       t@@ -30,7 +30,7 @@ sproc(int xpid)
                correg = nil;
        
                if(mapproc(xpid, cormap, &correg) < 0)
       -                error("setproc %d: %r", pid);
       +                error("setproc %d: %r", xpid);
        
                /* XXX check text file here? */
        
       t@@ -165,6 +165,17 @@ install(int pid)
                s->v->set = 1;
        }
        
       +static int
       +installed(int pid)
       +{
       +        int i;
       +
       +        for(i=0; i<Maxproc; i++)
       +                if(ptab[i].pid == pid)
       +                        return 1;
       +        return 0;
       +}
       +
        void
        deinstall(int pid)
        {
 (DIR) diff --git a/src/lib9/_p9proc.c b/src/lib9/_p9proc.c
       t@@ -54,7 +54,7 @@ _p9uproc(int inhandler)
                while((up = mallocz(sizeof(Uproc), 1)) == nil)
                        sleep(1000);
        
       -//fprint(2, "alloc uproc for pid %d\n", pid);
       +        /* fprint(2, "alloc uproc for pid %d\n", pid); */
                up->pid = pid;
                lock(&uproclock);
                h = pid%PIDHASH;
       t@@ -82,7 +82,7 @@ _p9uprocdie(void)
                int pid, i, h;
        
                pid = getpid();
       -fprint(2, "reap uproc for pid %d\n", pid);
       +        /* fprint(2, "reap uproc for pid %d\n", pid); */
                h = pid%PIDHASH;
                for(i=0; i<PIDHASH; i++){
                        up = alluproc[h];
 (DIR) diff --git a/src/libhttpd/gethead.c b/src/libhttpd/gethead.c
       t@@ -15,15 +15,11 @@ hgethead(HConnect *c, int many)
                int n;
        
                hin = &c->hin;
       -fprint(2, "hgethead top %p - %p\n", hin->pos, hin->stop);
                for(;;){
                        s = (char*)hin->pos;
                        pp = s;
       -fprint(2, "hgethead %p - %p\n", pp, hin->stop);
                        while(p = memchr(pp, '\n', (char*)hin->stop - pp)){
       -fprint(2, "hgethead %p - %p newline at %p %d\n", pp, hin->stop, p, *pp);
                                if(!many || p == pp || (p == pp + 1 && *pp == '\r')){
       -fprint(2, "breaking\n");
                                        pp = p + 1;
                                        break;
                                }
       t@@ -32,14 +28,13 @@ fprint(2, "breaking\n");
                        hin->pos = (uchar*)pp;
                        n = pp - s;
                        if(c->hstop + n > &c->header[HBufSize])
       -                        return 0;
       +                        return -1;
                        memmove(c->hstop, s, n);
                        c->hstop += n;
                        *c->hstop = '\0';
       -fprint(2, "p %p\n", p);
                        if(p != nil)
       -                        return 1;
       -                if(hreadbuf(hin, hin->pos) == nil || hin->state == Hend)
                                return 0;
       +                if(hreadbuf(hin, hin->pos) == nil || hin->state == Hend)
       +                        return -1;
                }
        }
 (DIR) diff --git a/src/libhttpd/hio.c b/src/libhttpd/hio.c
       t@@ -281,7 +281,7 @@ hload(Hio *h, char *buf)
        
                s = strchr(hstates, buf[0]);
                if(s == nil)
       -                return 0;
       +                return -1;
                h->state = s - hstates;
        
                s = strchr(hxfers, buf[1]);
       t@@ -300,13 +300,13 @@ hload(Hio *h, char *buf)
                        }
                        *t++ = c;
                        if(t >= stop)
       -                        return 0;
       +                        return -1;
                }
                *t = '\0';
                h->pos = h->start;
                h->stop = t;
                h->seek = 0;
       -        return 1;
       +        return 0;
        }
        
        void
 (DIR) diff --git a/src/libhttpd/parse.c b/src/libhttpd/parse.c
       t@@ -198,7 +198,7 @@ hparseheaders(HConnect *c, int timeout)
                memset(&h, 0, sizeof(h));
                h.c = c;
                alarm(timeout);
       -        if(!hgethead(c, 1))
       +        if(hgethead(c, 1) < 0)
                        return -1;
                alarm(0);
                h.hstart = c->hpos;
 (DIR) diff --git a/src/libhttpd/parsereq.c b/src/libhttpd/parsereq.c
       t@@ -41,8 +41,8 @@ hparsereq(HConnect *c, int timeout)
                 * only works for http/1.1 or later.
                 */
                alarm(timeout);
       -        if(!hgethead(c, 0))
       -                return 0;
       +        if(hgethead(c, 0) < 0)
       +                return -1;
                alarm(0);
                c->reqtime = time(nil);
                c->req.meth = getword(c);
 (DIR) diff --git a/src/libmach/Linux.c b/src/libmach/Linux.c
       t@@ -37,6 +37,9 @@ struct PtraceRegs
        static int ptracerw(Map*, Seg*, ulong, void*, uint, int);
        static int ptraceregrw(Regs*, char*, ulong*, int);
        
       +static int attachedpids[1000];
       +static int nattached;
       +
        void
        unmapproc(Map *map)
        {
       t@@ -55,21 +58,36 @@ unmapproc(Map *map)
        int
        mapproc(int pid, Map *map, Regs **rp)
        {
       +        int i;
                Seg s;
                PtraceRegs *r;
        
       -        if(ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)
       -        if(ptrace(PTRACE_PEEKUSER, pid, 0, 0) < 0)
       +        if(nattached==1 && attachedpids[0] == pid)
       +                goto already;
       +        if(nattached)
       +                detachproc(attachedpids[0]);
       +
       +        for(i=0; i<nattached; i++)
       +                if(attachedpids[i]==pid)
       +                        goto already;
       +        if(nattached == nelem(attachedpids)){
       +                werrstr("attached to too many processes");
       +                return -1;
       +        }
       +
                if(ptrace(PTRACE_ATTACH, pid, 0, 0) < 0){
                        werrstr("ptrace attach %d: %r", pid);
                        return -1;
                }
       -
       +        
                if(ctlproc(pid, "waitstop") < 0){
       +                fprint(2, "waitstop: %r");
                        ptrace(PTRACE_DETACH, pid, 0, 0);
                        return -1;
                }
       +        attachedpids[nattached++] = pid;
        
       +already:
                memset(&s, 0, sizeof s);
                s.base = 0;
                s.size = 0xFFFFFFFF;
       t@@ -78,11 +96,15 @@ mapproc(int pid, Map *map, Regs **rp)
                s.file = nil;
                s.rw = ptracerw;
                s.pid = pid;
       -        if(addseg(map, s) < 0)
       +        if(addseg(map, s) < 0){
       +                fprint(2, "addseg: %r\n");
                        return -1;
       +        }
        
       -        if((r = mallocz(sizeof(PtraceRegs), 1)) == nil)
       +        if((r = mallocz(sizeof(PtraceRegs), 1)) == nil){
       +                fprint(2, "mallocz: %r\n");
                        return -1;
       +        }
                r->r.rw = ptraceregrw;
                r->pid = pid;
                *rp = (Regs*)r;
       t@@ -92,6 +114,14 @@ mapproc(int pid, Map *map, Regs **rp)
        int
        detachproc(int pid)
        {
       +        int i;
       +
       +        for(i=0; i<nattached; i++){
       +                if(attachedpids[i] == pid){
       +                        attachedpids[i] = attachedpids[--nattached];
       +                        break;
       +                }
       +        }
                return ptrace(PTRACE_DETACH, pid, 0, 0);
        }
        
       t@@ -367,9 +397,14 @@ ctlproc(int pid, char *msg)
                        if(isstopped(pid))
                                return 0;
                        for(;;){
       -                        p = waitpid(pid, &status, WUNTRACED);
       -                        if(p <= 0)
       +                        p = waitpid(pid, &status, WUNTRACED|__WALL);
       +                        if(p <= 0){
       +                                if(errno == ECHILD){
       +                                        if(isstopped(pid))
       +                                                return 0;
       +                                }
                                        return -1;
       +                        }
                                if(WIFEXITED(status) || WIFSTOPPED(status))
                                        return 0;
                        }
 (DIR) diff --git a/src/libmach/map.c b/src/libmach/map.c
       t@@ -174,7 +174,7 @@ get8(Map *map, ulong addr, u64int *u)
        {
                u64int v;
        
       -        if(mrw(map, addr, &v, 4, 1) < 0)
       +        if(mrw(map, addr, &v, 8, 1) < 0)
                        return -1;
                *u = mach->swap8(v);
                return 8;
 (DIR) diff --git a/src/libthread/note.c b/src/libthread/note.c
       t@@ -65,6 +65,7 @@ delayednotes(Proc *p, void *v)
                                }
                                if(i==NFN){
                                        _threaddebug(DBGNOTE, "Unhandled note %s, proc %p\n", n->s, p);
       +                                fprint(2, "unhandled note %s, pid %d\n", n->s, p->pid);
                                        if(v != nil)
                                                noted(NDFLT);
                                        else if(strncmp(n->s, "sys:", 4)==0)
 (DIR) diff --git a/src/libventi/rpc.c b/src/libventi/rpc.c
       t@@ -75,6 +75,7 @@ vtrpc(VtConn *z, Packet *p)
                        while(!r->done){
                                qunlock(&z->lk);
                                if((p = vtrecv(z)) == nil){
       +                                werrstr("unexpected eof on venti connection");
                                        z->muxer = 0;
                                        return nil;
                                }
 (DIR) diff --git a/src/libventi/srvhello.c b/src/libventi/srvhello.c
       t@@ -8,29 +8,31 @@ vtsrvhello(VtConn *z)
                VtFcall tx, rx;
                Packet *p;
        
       -        if((p = vtrecv(z)) == nil)
       -                return 0;
       +        if((p = vtrecv(z)) == nil){
       +                werrstr("unexpected eof on venti connection");
       +                return -1;
       +        }
        
                if(vtfcallunpack(&tx, p) < 0){
                        packetfree(p);
       -                return 0;
       +                return -1;
                }
                packetfree(p);
        
                if(tx.type != VtThello){
                        vtfcallclear(&tx);
                        werrstr("bad packet type %d; want Thello %d", tx.type, VtThello);
       -                return 0;
       +                return -1;
                }
                if(tx.tag != 0){
                        vtfcallclear(&tx);
                        werrstr("bad tag in hello");
       -                return 0;
       +                return -1;
                }
                if(strcmp(tx.version, z->version) != 0){
                        vtfcallclear(&tx);
                        werrstr("bad version in hello");
       -                return 0;
       +                return -1;
                }
                vtfree(z->uid);
                z->uid = tx.uid;
       t@@ -42,9 +44,9 @@ vtsrvhello(VtConn *z)
                rx.tag = tx.tag;
                rx.sid = "anonymous";
                if((p = vtfcallpack(&rx)) == nil)
       -                return 0;
       +                return -1;
                if(vtsend(z, p) < 0)
       -                return 0;
       +                return -1;
        
       -        return 1;
       +        return 0;
        }