Hello,

I have been playing the following program.
I tried on official plan9, 9front, and 9atom.
none of them showed messages that come from:
                sysfatal("fork: %r”);
                sysfatal("exec: %r");
I suspect that
        fork()
does not return -1 even if it failed in creating new process.

Be ware this program may cause system panic.

#include <u.h>
#include <libc.h>
#define ERRLEN 256

static int
waitfor(int pid, char *msg)
{
        Waitmsg *w;
        while((w = wait()) != nil){
                if(w->pid == pid){
                        strncpy(msg, w->msg, ERRMAX);
                        free(w);
                        return 0;
                }
                free(w);
        }
        return -1;
}

int
run(char *path, char *cmd)
{
        int pid;
        int status;
        int n;
        char *args[32];
        char msg[ERRLEN];

        n = tokenize(cmd, args, 32);
        args[n] = nil;

        switch(pid = fork()) {/* assign = */
        case -1:
                sysfatal("fork: %r");
        case 0:
                close(0);
                exec(path, args);
                sysfatal("exec: %r");
        default:
                break;
        }
        status = waitfor(pid, msg);
        if(status < 0){
                werrstr("waitfor: %r");
                return -1;
        }
        return 0;
}

void main(int argc, char *argv[])
{       char *e;
        int n;
        int m = 100;
        char buf[32];
        ARGBEGIN{
        case 'm':       m = atoi(ARGF());
                break;
        default: sysfatal("usage");
        }ARGEND

        if(argv[0])
                n = atoi(argv[0]);
        else
                n = 0;
        if(n == m)
                sysfatal("stop");

        print("%d\n",n);
        snprint(buf,sizeof(buf),"8.out -m %d %d",m,n + 1);
        run("./8.out", buf);
        exits(nil);
}


Reply via email to