raz / commit 693bcbb7

commit
693bcbb7cee158e8c9e369a100796e11f814c738
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
19f3570a
Sys: pipes, programs by Program, wait's Status, groups and signals

spawn takes a Program: env alone or added, dir, fds, group, foreground,
tty (0 for none); signals blocked across the fork, then all at their
defaults and unblocked. wait returns pid, Status and code, and sees
stopped programs. pipe, pid, setGroup, foreground, foregroundOf;
catch, caught and ignore: a signalfd on Linux, a self-pipe over C,
whose ready and readyOf retry on EINTR. Both Sys files, each tested.
Rat breaks: spawn changed.

patch · log

4 files changed, +489 -114

PLAN.md (history) modified +1 -1

@@ -33,7 +33,7 @@
3333 - [x] 2. `Sys`, files: `open` with a set of `Access`, `seek`, `Info`'s
3434 `mtime` and `mode`, `chmod`, `readLink`, `symlink`, `removeDir`,
3535 `setWorkDir`.
36-- [ ] 3. `Sys`, programs: `pipe`, `spawn` of a `Program`, `wait` of any
36+- [x] 3. `Sys`, programs: `pipe`, `spawn` of a `Program`, `wait` of any
3737 child with its `Status`, process groups and the terminal's
3838 foreground, `pid`; signals caught on a file descriptor. Rat
3939 breaks here: `spawn` changes.

lib/C.raz (history) modified +8 -0

@@ -55,6 +55,14 @@
5555 extern proc execve*(path: ^U8; argv, envp: ^^U8): I32;
5656 extern proc waitpid*(pid: I32; status: ^I32; options: I32): I32;
5757 extern proc kill*(pid, signal: I32): I32;
58+extern proc getpid*: I32;
59+extern proc getpgrp*: I32;
60+extern proc setpgid*(pid, group: I32): I32;
61+extern proc tcsetpgrp*(fd, group: I32): I32;
62+extern proc tcgetpgrp*(fd: I32): I32;
63+extern proc pipe2*(fds: ^I32; flags: I32): I32;
64+extern proc sigaction*(sig: I32; act, old: ^U8): I32;
65+extern proc sigprocmask*(how: I32; mask, old: ^U8): I32;
5866 extern proc printf*(format: ^U8; ...): I32;
5967 extern proc fflush*(f: ^U8): I32;
6068 extern var environ*: ^^U8;

lib/Sys.linux-x86_64.raz (history) modified +226 -53

@@ -6,6 +6,10 @@
66 stdin* = 0;
77 stdout* = 1;
88 stderr* = 2;
9+ (* signals, by their numbers on Linux *)
10+ sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
11+ sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
12+ sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28;
913
1014 type
1115 (* What open is asked for: reading, writing or both, and what else
@@ -29,6 +33,21 @@
2933 buf: [8192]U8;
3034 end;
3135
36+ (* A program for spawn to start. *)
37+ Program* = record
38+ path*: []U8;
39+ args*: [][]U8; (* the first is its name *)
40+ env*: [][]U8; (* NAME=value, each added to this program's environment *)
41+ onlyEnv*: BOOL; (* env is its whole environment *)
42+ dir*: []U8; (* its working directory; "" for this program's *)
43+ fds*: [3]I64; (* its standard input, output and error; 0 for this program's own *)
44+ group*: I64; (* the process group it joins: 0 for this program's, -1 a new one it leads *)
45+ foreground*: BOOL; (* its group takes the terminal on standard input *)
46+ tty*: I64; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
47+ end;
48+ (* How a program that wait waited for changed. *)
49+ Status* = (running, exited, killed, stopped);
50+
3251 (* struct termios as the kernel's TCGETS fills it: 36 bytes *)
3352 Termios = record
3453 iflag, oflag, cflag, lflag: U32;
@@ -48,6 +67,8 @@
4867 cwd: [4096]U8; (* what workDir gives *)
4968 linked: [4096]U8; (* what readLink gives *)
5069 saved: Termios; (* the terminal as rawMode found it *)
70+ caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *)
71+ sigFd: I64; (* the signalfd that gives them, once caughtSigs isn't empty *)
5172
5273 (* Writes s to the file descriptor fd; the number of bytes written, or
5374 minus an errno. *)
@@ -468,66 +489,177 @@
468489 return p, size;
469490 end;
470491
471-(* Starts the program at path with args, the first its name, and this
472- program's environment changed by env: its process id, or minus an
473- errno. If tty is a pseudo-terminal's fd, the program runs on that
474- terminal, as its controlling terminal and its standard input, output
475- and error. It gets no other open file. A program that can't be
476- started ends with status 127. *)
477-proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64;
478-var a: [4096]U8; name: [32]U8; p: ^^U8; size, pid, n, i, fd: I64;
492+(* A pipe: the fd to read and the fd to write, both closed in a program
493+ exec starts; or minus an errno and -1. *)
494+proc pipe*: (I64, I64);
495+var fds: [2]I32; r: I64;
479496 begin
480- if not cstr(path, a) then return -36; end;
481- if tty >= 0 then
497+ r := SYSCALL(293, ADR(fds), 80000H); (* pipe2, O_CLOEXEC *)
498+ if r < 0 then return r, -1; end;
499+ return I64(fds[0]), I64(fds[1]);
500+end;
501+
502+(* This program's process id. *)
503+proc pid*: I64;
504+begin
505+ return SYSCALL(39); (* getpid *)
506+end;
507+
508+(* Moves the program pid, or this one if pid is 0, to the process group
509+ group, or to a new one it leads if group is 0. *)
510+proc setGroup*(pid, group: I64): I64;
511+begin
512+ return SYSCALL(109, pid, group); (* setpgid *)
513+end;
514+
515+(* Gives the keys of the terminal on fd, and its signals, to the process
516+ group group. *)
517+proc foreground*(fd, group: I64): I64;
518+var g: I32;
519+begin
520+ g := I32(group);
521+ return SYSCALL(16, fd, 5410H, ADR(g)); (* TIOCSPGRP *)
522+end;
523+
524+(* The process group the terminal on fd gives its keys to, or minus an
525+ errno. *)
526+proc foregroundOf*(fd: I64): I64;
527+var g: I32; r: I64;
528+begin
529+ r := SYSCALL(16, fd, 540FH, ADR(g)); (* TIOCGPGRP *)
530+ if r < 0 then return r; end;
531+ return I64(g);
532+end;
533+
534+(* Starts the program p describes: its process id, or minus an errno.
535+ A program that can't be started ends with status 127. Every signal
536+ is blocked from the fork until the program has its own: its group,
537+ its terminal's foreground, and signals as they are at first. *)
538+proc spawn*(p: Program): I64;
539+var
540+ a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *)
541+ argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: I64;
542+begin
543+ if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
544+ if p.tty # 0 then
482545 (* its terminal: /dev/pts/ and the number TIOCGPTN gives *)
483546 n := 0;
484- if SYSCALL(16, tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *)
547+ if SYSCALL(16, p.tty, 80045430H, ADR(n)) # 0 then return -25; end; (* ENOTTY *)
485548 i := put("/dev/pts/", ADR(name[0])) - ADR(name[0]) - 1; (* at its zero *)
486549 size := n;
487550 repeat INC(i); size := size div 10; until size = 0;
488551 name[i] := 0;
489552 repeat DEC(i); name[i] := U8(48 + n mod 10); n := n div 10; until n = 0;
490553 end;
491- p, size := argsAndEnv(args, env, ARGV() + (ARGC() + 1));
492- if p = nil then return -12; end; (* ENOMEM *)
554+ none := nil;
555+ if p.onlyEnv then
556+ argv, size := argsAndEnv(p.args, p.env, ADR(none));
557+ else
558+ argv, size := argsAndEnv(p.args, p.env, ARGV() + (ARGC() + 1));
559+ end;
560+ if argv = nil then return -12; end; (* ENOMEM *)
561+ all := 0FFFFFFFFFFFFFFFFH;
562+ SYSCALL(14, 2, ADR(all), ADR(old), 8); (* rt_sigprocmask: SIG_SETMASK *)
493563 pid := SYSCALL(57); (* fork *)
494564 if pid = 0 then
495- if tty >= 0 then
565+ if p.tty # 0 then
496566 SYSCALL(112); (* setsid *)
497567 fd := SYSCALL(2, ADR(name), 2); (* open, O_RDWR *)
498568 if (fd < 0) or (SYSCALL(16, fd, 540EH, 0) # 0) then SYSCALL(231, 127); end; (* TIOCSCTTY; exit *)
499569 for i := 0 to 2 do SYSCALL(33, fd, i); end; (* dup2 *)
570+ else
571+ if p.group = -1 then SYSCALL(109, 0, 0); (* setpgid *)
572+ elsif p.group # 0 then SYSCALL(109, 0, p.group);
573+ end;
574+ if p.foreground then foreground(0, SYSCALL(111)); end; (* getpgrp *)
575+ for i := 0 to 2 do
576+ if p.fds[i] # 0 then SYSCALL(33, p.fds[i], i); end; (* dup2 *)
577+ end;
500578 end;
579+ if (LEN(p.dir) > 0) and (SYSCALL(80, ADR(dir)) # 0) then SYSCALL(231, 127); end; (* chdir *)
501580 SYSCALL(436, 3, 0FFFFFFFFH, 0); (* close_range: every fd from 3 *)
502- SYSCALL(59, ADR(a), p, p + (LEN(args) + 1)); (* execve *)
581+ for i := 1 to 64 do SYSCALL(13, i, ADR(dfl), 0, 8); end; (* rt_sigaction *)
582+ SYSCALL(14, 2, ADR(empty), 0, 8); (* no signal blocked *)
583+ SYSCALL(59, ADR(a), argv, argv + (LEN(p.args) + 1)); (* execve *)
503584 SYSCALL(231, 127); (* exit *)
504585 end;
505- munmap(^U8(p), size);
586+ if (pid > 0) and (p.tty = 0) then
587+ (* as the program does, for whichever of the two runs first *)
588+ g := p.group;
589+ if g = -1 then g := pid; elsif g = 0 then g := SYSCALL(111); end;
590+ if p.group # 0 then SYSCALL(109, pid, g); end;
591+ if p.foreground then foreground(0, g); end;
592+ end;
593+ SYSCALL(14, 2, ADR(old), 0, 8);
594+ munmap(^U8(argv), size);
506595 return pid;
507596 end;
508-
509-(* Whether the program pid has ended, waiting for it to if block: its
510- exit status, or 128 and the number of the signal that stopped it,
511- and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there
512- is no such program to wait for. *)
513-proc wait*(pid: I64; block: BOOL): (I64, I64);
597+(* Waits for the program pid, or any of this program's if pid is -1, to
598+ change, if block, and says which one changed and how: exited, with
599+ its exit status; killed or stopped, with the signal's number. Without
600+ block it doesn't wait: 0 and running if none has changed. Minus an
601+ errno, and running, if there is none to wait for. *)
602+proc wait*(pid: I64; block: BOOL): (I64, Status, I64);
514603 var status: I32; r, options: I64;
515604 begin
516- options := 1; (* WNOHANG *)
517- if block then options := 0; end;
605+ options := 3; (* WNOHANG, WUNTRACED *)
606+ if block then options := 2; end;
518607 r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *)
519- if r < 0 then return r, 1; end;
520- if r = 0 then return 0, 0; end;
521- if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end;
522- return 128 + I64(status) & 7FH, 1;
608+ if r <= 0 then return r, running, 0; end;
609+ if status & 7FH = 0 then return r, exited, I64(status) shr 8 & 0FFH; end;
610+ if status & 0FFH = 7FH then return r, stopped, I64(status) shr 8 & 0FFH; end;
611+ return r, killed, I64(status) & 7FH;
523612 end;
524613
525-(* Sends the program pid the signal: 0, or minus an errno. *)
614+(* Sends the program pid, or the group -pid, the signal: 0, or minus an
615+ errno. *)
526616 proc kill*(pid, signal: I64): I64;
527617 begin
528618 return SYSCALL(62, pid, signal);
529619 end;
530620
621+(* Catches the signals sigs: blocks them, so that they don't do what
622+ they would, and gives them on an fd, which a later catch adds to and
623+ gives again; or minus an errno. Only the thread that catches them
624+ blocks them, and threads it starts later. *)
625+proc catch*(sigs: []I64): I64;
626+var m: U64; i, fd: I64;
627+begin
628+ m := caughtSigs;
629+ for i := 0 to LEN(sigs) - 1 do
630+ if (sigs[i] < 1) or (sigs[i] > 64) or (sigs[i] = sigKill) or (sigs[i] = sigStop)
631+ or (sigs[i] = 8) or (sigs[i] = 11) then (* SIGFPE and SIGSEGV, Rt's *)
632+ return -22; (* EINVAL *)
633+ end;
634+ m := m | U64(1) shl (sigs[i] - 1);
635+ end;
636+ fd := -1;
637+ if caughtSigs # 0 then fd := sigFd; end;
638+ SYSCALL(14, 0, ADR(m), 0, 8); (* rt_sigprocmask: SIG_BLOCK *)
639+ fd := SYSCALL(289, fd, ADR(m), 8, 80800H); (* signalfd4: SFD_NONBLOCK, SFD_CLOEXEC *)
640+ if fd < 0 then return fd; end;
641+ caughtSigs := m;
642+ sigFd := fd;
643+ return fd;
644+end;
645+
646+(* The next signal catch's fd has, or 0 if none is waiting. *)
647+proc caught*(fd: I64): I64;
648+var info: [128]U8; (* struct signalfd_siginfo: the number first, little-endian, below 65 *)
649+begin
650+ if read(fd, info) # 128 then return 0; end;
651+ return I64(info[0]);
652+end;
653+
654+(* Makes the signal sig do nothing, or, if not yes, what it does at
655+ first: 0, or minus an errno. *)
656+proc ignore*(sig: I64; yes: BOOL): I64;
657+var act: [4]U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *)
658+begin
659+ if yes then act[0] := 1; end;
660+ return SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *)
661+end;
662+
531663 (* Gives the processor to another thread. *)
532664 proc yield*;
533665 begin
@@ -555,38 +687,79 @@
555687 return fd;
556688 end;
557689
558-(* The test of programs: one spawned on a pseudo-terminal of 100 by 30
559- prints its size, its arguments and a variable put in place, and ends
560- with status 3; one with no terminal is stopped by kill; one that
561- isn't there ends with 127. *)
690+(* The test of programs: one on a pseudo-terminal of 100 by 30 prints
691+ its arguments, a variable put in place and its size, and leads the
692+ terminal's foreground until it ends with status 3; one in a
693+ directory and an environment of its own writes to a pipe; one in a
694+ group of its own is stopped and killed through it; one that isn't
695+ there ends with 127. Then signals: caught as a program ends and as
696+ one is sent; and a program starts with them neither caught nor
697+ ignored. *)
562698 proc testPrograms;
563-var pty, pid, n, m, status, ended: I64; buf: [64]U8;
699+var pty, child, n, m, r, w, sigs, c: I64; st: Status; buf: [64]U8;
564700 begin
565701 pty := openPty();
566702 if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end;
567- pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty);
568- if pid <= 0 then exit(2); end;
703+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $0 $1 $HOME; stty size; read x; exit 3", "a", "b"],
704+ env: ["HOME=/h"], tty: pty});
705+ if child <= 0 then exit(2); end;
569706 n := 0;
707+ while n < 16 do
708+ if not ready(pty, 5000) then exit(2); end;
709+ m := read(pty, buf[n..LEN(buf)]);
710+ if m <= 0 then exit(2); end;
711+ INC(n, m);
712+ end;
713+ if (buf[0..n] # "a b /h\r\n30 100\r\n") or (foregroundOf(pty) # child) then exit(2); end;
714+ if write(pty, "\n") # 1 then exit(2); end;
570715 repeat
571716 if not ready(pty, 5000) then exit(2); end;
572- m := read(pty, buf[n..LEN(buf)]);
717+ until read(pty, buf) <= 0; (* -5, EIO, once the terminal has no program *)
718+ close(pty);
719+ r, st, c := wait(child, true);
720+ if (r # child) or (st # exited) or (c # 3) then exit(2); end;
721+
722+ r, w := pipe();
723+ if w < 0 then exit(2); end;
724+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $X $HOME; pwd"], env: ["X=x"], onlyEnv: true,
725+ dir: "/tmp", fds: [0, w, 0]});
726+ close(w);
727+ n := 0;
728+ repeat
729+ m := read(r, buf[n..LEN(buf)]);
573730 if m > 0 then INC(n, m); end;
574- until m <= 0; (* -5, EIO, once the terminal has no program *)
575- close(pty);
576- if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end;
577- status, ended := wait(pid, true);
578- if (status # 3) or (ended # 1) then exit(2); end;
579- pid := spawn("/bin/sleep", ["sleep", "10"], [], -1);
580- status, ended := wait(pid, false);
581- if ended # 0 then exit(2); end;
582- if kill(pid, 15) # 0 then exit(2); end;
583- status, ended := wait(pid, true);
584- if (status # 128 + 15) or (ended # 1) then exit(2); end;
585- status, ended := wait(pid, true);
586- if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *)
587- pid := spawn("/no/such/program", ["x"], [], -1);
588- status, ended := wait(pid, true);
589- if (status # 127) or (ended # 1) then exit(2); end;
731+ until m <= 0;
732+ close(r);
733+ if buf[0..n] # "x\n/tmp\n" then exit(2); end;
734+ r, st, c := wait(child, true);
735+ if (r # child) or (st # exited) or (c # 0) then exit(2); end;
736+
737+ child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
738+ r, st, c := wait(child, false);
739+ if (r # 0) or (st # running) or (kill(-child, sigStop) # 0) then exit(2); end;
740+ r, st, c := wait(child, true);
741+ if (r # child) or (st # stopped) or (c # sigStop) or (kill(-child, sigKill) # 0) then exit(2); end;
742+ r, st, c := wait(-1, true);
743+ if (r # child) or (st # killed) or (c # sigKill) then exit(2); end;
744+ r, st, c := wait(-1, true);
745+ if r # -10 then exit(2); end; (* ECHILD: none left *)
746+ child := spawn({path: "/no/such/program", args: ["x"]});
747+ r, st, c := wait(child, true);
748+ if (r # child) or (st # exited) or (c # 127) then exit(2); end;
749+
750+ sigs := catch([sigChld, sigHup]);
751+ if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end;
752+ if (caught(sigs) # 0) or (ignore(sigInt, true) # 0) then exit(2); end;
753+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -INT $$; exit 0"]});
754+ if not ready(sigs, 5000) or (caught(sigs) # sigChld) then exit(2); end;
755+ r, st, c := wait(child, true);
756+ if (st # killed) or (c # sigInt) then exit(2); end;
757+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -HUP $$; exit 0"]});
758+ r, st, c := wait(child, true);
759+ if (st # killed) or (c # sigHup) then exit(2); end;
760+ while caught(sigs) # 0 do end;
761+ if (kill(pid(), sigHup) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigHup) then exit(2); end;
762+ if ignore(sigInt, false) # 0 then exit(2); end;
590763 end;
591764
592765 (* The module's test: a file written, read back and closed; a

lib/Sys.raz (history) modified +254 -60

@@ -14,6 +14,10 @@
1414 stdin* = 0;
1515 stdout* = 1;
1616 stderr* = 2;
17+ (* signals, by their numbers on Linux; another target has its own *)
18+ sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13;
19+ sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19;
20+ sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28;
1721 DirentName = 19; (* the offset of d_name in glibc's struct dirent *)
1822 TIOCGWINSZ = 5413H; (* glibc's, as the two below *)
1923 TIOCSWINSZ = 5414H;
@@ -40,6 +44,30 @@
4044 d: ^U8;
4145 end;
4246
47+ (* A program for spawn to start. *)
48+ Program* = record
49+ path*: []U8;
50+ args*: [][]U8; (* the first is its name *)
51+ env*: [][]U8; (* NAME=value, each added to this program's environment *)
52+ onlyEnv*: BOOL; (* env is its whole environment *)
53+ dir*: []U8; (* its working directory; "" for this program's *)
54+ fds*: [3]I64; (* its standard input, output and error; 0 for this program's own *)
55+ group*: I64; (* the process group it joins: 0 for this program's, -1 a new one it leads *)
56+ foreground*: BOOL; (* its group takes the terminal on standard input *)
57+ tty*: I64; (* a pseudo-terminal's fd, to start a session on; 0 for none *)
58+ end;
59+ (* How a program that wait waited for changed. *)
60+ Status* = (running, exited, killed, stopped);
61+
62+ (* glibc's struct sigaction; a handler of 0 is SIG_DFL, 1 SIG_IGN *)
63+ Handler = proc(sig: I32);
64+ Sigaction = record
65+ handler: Handler;
66+ mask: [16]U64;
67+ flags: I32;
68+ restorer: ^U8;
69+ end;
70+
4371 Winsize = record rows, cols, xpixel, ypixel: U16; end;
4472 Pollfd = record fd: I32; events, revents: I16; end;
4573
@@ -53,6 +81,7 @@
5381 cwd: [4096]U8; (* what workDir gives *)
5482 linked: [4096]U8; (* what readLink gives *)
5583 saved: [256]U8; (* the terminal as rawMode found it: a struct termios, of the C library's size *)
84+ sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *)
5685
5786 (* minus errno, as a system call returns it *)
5887 proc failed: I64;
@@ -312,10 +341,11 @@
312341 (* Whether fd has something to read within ms milliseconds; ms < 0
313342 waits as long as it takes. *)
314343 proc ready*(fd, ms: I64): BOOL;
315-var p: Pollfd;
344+var p: Pollfd; r: I32;
316345 begin
317346 p := {fd: I32(fd), events: 1}; (* POLLIN *)
318- return C.poll(^U8(ADR(p)), 1, I32(ms)) > 0;
347+ repeat r := C.poll(^U8(ADR(p)), 1, I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR: a signal caught *)
348+ return r > 0;
319349 end;
320350
321351 (* A stream socket connected to the Unix socket at path: its file
@@ -352,12 +382,13 @@
352382 or that has reached its end or failed, so that a read won't wait; -1
353383 if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
354384 proc readyOf*(fds: []I64; ms: I64): I64;
355-var p: [64]Pollfd; n, i: I64;
385+var p: [64]Pollfd; n, i: I64; r: I32;
356386 begin
357387 n := LEN(fds);
358388 if n > LEN(p) then n := LEN(p); end;
359389 for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *)
360- if C.poll(^U8(ADR(p)), U64(n), I32(ms)) <= 0 then return -1; end;
390+ repeat r := C.poll(^U8(ADR(p)), U64(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *)
391+ if r <= 0 then return -1; end;
361392 for i := 0 to n - 1 do
362393 if p[i].revents # 0 then return fds[i]; end;
363394 end;
@@ -471,69 +502,191 @@
471502 return p, size;
472503 end;
473504
474-(* Starts the program at path with args, the first its name, and this
475- program's environment changed by env: its process id, or minus an
476- errno. If tty is a pseudo-terminal's fd, the program runs on that
477- terminal, as its controlling terminal and its standard input, output
478- and error. It gets no other open file. A program that can't be
479- started ends with status 127. *)
480-proc spawn*(path: []U8; args, env: [][]U8; tty: I64): I64;
481-var a, name: [4096]U8; p: ^^U8; s: ^U8; size, n, err: I64; pid, fd: I32;
505+(* A pipe: the fd to read and the fd to write, both closed in a program
506+ exec starts; or minus an errno and -1. *)
507+proc pipe*: (I64, I64);
508+var fds: [2]I32;
509+begin
510+ if C.pipe2(ADR(fds[0]), 80000H) # 0 then return failed(), -1; end; (* O_CLOEXEC, glibc's *)
511+ return I64(fds[0]), I64(fds[1]);
512+end;
513+
514+(* This program's process id. *)
515+proc pid*: I64;
516+begin
517+ return I64(C.getpid());
518+end;
519+
520+(* Moves the program pid, or this one if pid is 0, to the process group
521+ group, or to a new one it leads if group is 0. *)
522+proc setGroup*(pid, group: I64): I64;
523+begin
524+ if C.setpgid(I32(pid), I32(group)) # 0 then return failed(); end;
525+ return 0;
526+end;
527+
528+(* Gives the keys of the terminal on fd, and its signals, to the process
529+ group group. *)
530+proc foreground*(fd, group: I64): I64;
531+begin
532+ if C.tcsetpgrp(I32(fd), I32(group)) # 0 then return failed(); end;
533+ return 0;
534+end;
535+
536+(* The process group the terminal on fd gives its keys to, or minus an
537+ errno. *)
538+proc foregroundOf*(fd: I64): I64;
539+var g: I32;
540+begin
541+ g := C.tcgetpgrp(I32(fd));
542+ if g < 0 then return failed(); end;
543+ return I64(g);
544+end;
545+
546+(* Starts the program p describes: its process id, or minus an errno.
547+ A program that can't be started ends with status 127. Every signal
548+ is blocked from the fork until the program has its own: its group,
549+ its terminal's foreground, and signals as they are at first. *)
550+proc spawn*(p: Program): I64;
551+var
552+ a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: I64;
553+ all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *)
482554 begin
483- if not cstr(path, a) then return -36; end;
484- if tty >= 0 then
485- s := C.ptsname(I32(tty)); (* here, since it isn't safe after fork *)
555+ if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *)
556+ if p.tty # 0 then
557+ s := C.ptsname(I32(p.tty)); (* here, since it isn't safe after fork *)
486558 if s = nil then return failed(); end;
487559 n := 0;
488560 while s[n] # 0 do INC(n); end;
489561 if not cstr(s[0..n], name) then return -36; end;
490562 end;
491- p, size := argsAndEnv(args, env, C.environ);
492- if p = nil then return -12; end; (* ENOMEM *)
563+ none := nil;
564+ if p.onlyEnv then
565+ argv, size := argsAndEnv(p.args, p.env, ADR(none));
566+ else
567+ argv, size := argsAndEnv(p.args, p.env, C.environ);
568+ end;
569+ if argv = nil then return -12; end; (* ENOMEM *)
570+ for i := 0 to LEN(all) - 1 do all[i] := 0FFFFFFFFFFFFFFFFH; end;
571+ C.sigprocmask(2, ^U8(ADR(all)), ^U8(ADR(old))); (* SIG_SETMASK *)
493572 pid := C.fork();
494573 if pid = 0 then
495- if tty >= 0 then
574+ if p.tty # 0 then
496575 C.setsid();
497576 fd := C.open(ADR(name[0]), 2); (* O_RDWR *)
498577 if (fd < 0) or (C.ioctl(fd, TIOCSCTTY, 0) # 0) then C._exit(127); end;
499- C.dup2(fd, 0);
500- C.dup2(fd, 1);
501- C.dup2(fd, 2);
578+ for i := 0 to 2 do C.dup2(fd, I32(i)); end;
579+ else
580+ if p.group = -1 then C.setpgid(0, 0);
581+ elsif p.group # 0 then C.setpgid(0, I32(p.group));
582+ end;
583+ if p.foreground then C.tcsetpgrp(0, C.getpgrp()); end;
584+ for i := 0 to 2 do
585+ if p.fds[i] # 0 then C.dup2(I32(p.fds[i]), I32(i)); end;
586+ end;
502587 end;
588+ if (LEN(p.dir) > 0) and (C.chdir(ADR(dir[0])) # 0) then C._exit(127); end;
503589 C.close_range(3, 0FFFFFFFFH, 0); (* every fd from 3 *)
504- C.execve(ADR(a[0]), p, p + (LEN(args) + 1));
590+ for i := 1 to 64 do C.sigaction(I32(i), ^U8(ADR(dfl)), nil); end;
591+ C.sigprocmask(2, ^U8(ADR(empty)), nil);
592+ C.execve(ADR(a[0]), argv, argv + (LEN(p.args) + 1));
505593 C._exit(127);
506594 end;
507595 err := failed();
508- munmap(^U8(p), size);
596+ if (pid > 0) and (p.tty = 0) then
597+ (* as the program does, for whichever of the two runs first *)
598+ g := p.group;
599+ if g = -1 then g := I64(pid); elsif g = 0 then g := I64(C.getpgrp()); end;
600+ if p.group # 0 then C.setpgid(pid, I32(g)); end;
601+ if p.foreground then C.tcsetpgrp(0, I32(g)); end;
602+ end;
603+ C.sigprocmask(2, ^U8(ADR(old)), nil);
604+ munmap(^U8(argv), size);
509605 if pid < 0 then return err; end;
510606 return I64(pid);
511607 end;
512608
513-(* Whether the program pid has ended, waiting for it to if block: its
514- exit status, or 128 and the number of the signal that stopped it,
515- and 1; or 0 and 0 if it goes on; or minus an errno and 1, if there
516- is no such program to wait for. The status is read as glibc's macros
517- read it. *)
518-proc wait*(pid: I64; block: BOOL): (I64, I64);
609+(* Waits for the program pid, or any of this program's if pid is -1, to
610+ change, if block, and says which one changed and how: exited, with
611+ its exit status; killed or stopped, with the signal's number. Without
612+ block it doesn't wait: 0 and running if none has changed. Minus an
613+ errno, and running, if there is none to wait for. The status is read
614+ as glibc's macros read it. *)
615+proc wait*(pid: I64; block: BOOL): (I64, Status, I64);
519616 var status, options, r: I32;
520617 begin
521- options := 1; (* WNOHANG *)
522- if block then options := 0; end;
618+ options := 3; (* WNOHANG, WUNTRACED *)
619+ if block then options := 2; end;
523620 r := C.waitpid(I32(pid), ADR(status), options);
524- if r < 0 then return failed(), 1; end;
525- if r = 0 then return 0, 0; end;
526- if status & 7FH = 0 then return I64(status) shr 8 & 0FFH, 1; end;
527- return 128 + I64(status) & 7FH, 1;
621+ if r < 0 then return failed(), running, 0; end;
622+ if r = 0 then return 0, running, 0; end;
623+ if status & 7FH = 0 then return I64(r), exited, I64(status) shr 8 & 0FFH; end;
624+ if status & 0FFH = 7FH then return I64(r), stopped, I64(status) shr 8 & 0FFH; end;
625+ return I64(r), killed, I64(status) & 7FH;
528626 end;
529627
530-(* Sends the program pid the signal: 0, or minus an errno. *)
628+(* Sends the program pid, or the group -pid, the signal: 0, or minus an
629+ errno. *)
531630 proc kill*(pid, signal: I64): I64;
532631 begin
533632 if C.kill(I32(pid), I32(signal)) # 0 then return failed(); end;
534633 return 0;
535634 end;
536635
636+(* The handler of caught signals: their numbers go down the pipe, which
637+ catch gives the end of to read. *)
638+proc onSignal(sig: I32);
639+var e: ^I32; saved: I32; b: [1]U8;
640+begin
641+ e := C.__errno_location();
642+ saved := e^; (* as the code it stopped left it *)
643+ b[0] := U8(sig);
644+ C.write(sigWrite, ADR(b[0]), 1);
645+ e^ := saved;
646+end;
647+
648+(* Catches the signals sigs: they don't do what they would, but give
649+ their numbers on an fd, which a later catch adds to and gives again;
650+ or minus an errno. *)
651+proc catch*(sigs: []I64): I64;
652+var fds: [2]I32; act: Sigaction; i: I64;
653+begin
654+ for i := 0 to LEN(sigs) - 1 do
655+ if (sigs[i] < 1) or (sigs[i] > 64) or (sigs[i] = sigKill) or (sigs[i] = sigStop)
656+ or (sigs[i] = 8) or (sigs[i] = 11) then (* SIGFPE and SIGSEGV, Rt's *)
657+ return -22; (* EINVAL *)
658+ end;
659+ end;
660+ if sigWrite = 0 then
661+ if C.pipe2(ADR(fds[0]), 80800H) # 0 then return failed(); end; (* O_NONBLOCK, O_CLOEXEC *)
662+ sigRead := fds[0];
663+ sigWrite := fds[1];
664+ end;
665+ act := {handler: onSignal, flags: 10000000H}; (* SA_RESTART *)
666+ for i := 0 to LEN(sigs) - 1 do
667+ if C.sigaction(I32(sigs[i]), ^U8(ADR(act)), nil) # 0 then return failed(); end;
668+ end;
669+ return I64(sigRead);
670+end;
671+
672+(* The next signal catch's fd has, or 0 if none is waiting. *)
673+proc caught*(fd: I64): I64;
674+var b: [1]U8;
675+begin
676+ if read(fd, b) # 1 then return 0; end;
677+ return I64(b[0]);
678+end;
679+
680+(* Makes the signal sig do nothing, or, if not yes, what it does at
681+ first: 0, or minus an errno. *)
682+proc ignore*(sig: I64; yes: BOOL): I64;
683+var act: Sigaction;
684+begin
685+ if yes then act.handler := Handler(1); end; (* SIG_IGN *)
686+ if C.sigaction(I32(sig), ^U8(ADR(act)), nil) # 0 then return failed(); end;
687+ return 0;
688+end;
689+
537690 proc isTerminal*(fd: I64): BOOL;
538691 begin
539692 return C.isatty(I32(fd)) = 1;
@@ -564,38 +717,79 @@
564717 return I64(fd);
565718 end;
566719
567-(* The test of programs: one spawned on a pseudo-terminal of 100 by 30
568- prints its size, its arguments and a variable put in place, and ends
569- with status 3; one with no terminal is stopped by kill; one that
570- isn't there ends with 127. *)
720+(* The test of programs: one on a pseudo-terminal of 100 by 30 prints
721+ its arguments, a variable put in place and its size, and leads the
722+ terminal's foreground until it ends with status 3; one in a
723+ directory and an environment of its own writes to a pipe; one in a
724+ group of its own is stopped and killed through it; one that isn't
725+ there ends with 127. Then signals: caught as a program ends and as
726+ one is sent; and a program starts with them neither caught nor
727+ ignored. *)
571728 proc testPrograms;
572-var pty, pid, n, m, status, ended: I64; buf: [64]U8;
729+var pty, child, n, m, r, w, sigs, c: I64; st: Status; buf: [64]U8;
573730 begin
574731 pty := openPty();
575732 if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end;
576- pid := spawn("/bin/sh", ["sh", "-c", "stty size; echo $0 $1 $HOME; exit 3", "a", "b"], ["HOME=/h"], pty);
577- if pid <= 0 then exit(2); end;
733+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $0 $1 $HOME; stty size; read x; exit 3", "a", "b"],
734+ env: ["HOME=/h"], tty: pty});
735+ if child <= 0 then exit(2); end;
578736 n := 0;
579- repeat
737+ while n < 16 do
580738 if not ready(pty, 5000) then exit(2); end;
581739 m := read(pty, buf[n..LEN(buf)]);
582- if m > 0 then INC(n, m); end;
583- until m <= 0; (* -5, EIO, once the terminal has no program *)
740+ if m <= 0 then exit(2); end;
741+ INC(n, m);
742+ end;
743+ if (buf[0..n] # "a b /h\r\n30 100\r\n") or (foregroundOf(pty) # child) then exit(2); end;
744+ if write(pty, "\n") # 1 then exit(2); end;
745+ repeat
746+ if not ready(pty, 5000) then exit(2); end;
747+ until read(pty, buf) <= 0; (* -5, EIO, once the terminal has no program *)
584748 close(pty);
585- if buf[0..n] # "30 100\r\na b /h\r\n" then exit(2); end;
586- status, ended := wait(pid, true);
587- if (status # 3) or (ended # 1) then exit(2); end;
588- pid := spawn("/bin/sleep", ["sleep", "10"], [], -1);
589- status, ended := wait(pid, false);
590- if ended # 0 then exit(2); end;
591- if kill(pid, 15) # 0 then exit(2); end;
592- status, ended := wait(pid, true);
593- if (status # 128 + 15) or (ended # 1) then exit(2); end;
594- status, ended := wait(pid, true);
595- if (status # -10) or (ended # 1) then exit(2); end; (* ECHILD: waited for already *)
596- pid := spawn("/no/such/program", ["x"], [], -1);
597- status, ended := wait(pid, true);
598- if (status # 127) or (ended # 1) then exit(2); end;
749+ r, st, c := wait(child, true);
750+ if (r # child) or (st # exited) or (c # 3) then exit(2); end;
751+
752+ r, w := pipe();
753+ if w < 0 then exit(2); end;
754+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "echo $X $HOME; pwd"], env: ["X=x"], onlyEnv: true,
755+ dir: "/tmp", fds: [0, w, 0]});
756+ close(w);
757+ n := 0;
758+ repeat
759+ m := read(r, buf[n..LEN(buf)]);
760+ if m > 0 then INC(n, m); end;
761+ until m <= 0;
762+ close(r);
763+ if buf[0..n] # "x\n/tmp\n" then exit(2); end;
764+ r, st, c := wait(child, true);
765+ if (r # child) or (st # exited) or (c # 0) then exit(2); end;
766+
767+ child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
768+ r, st, c := wait(child, false);
769+ if (r # 0) or (st # running) or (kill(-child, sigStop) # 0) then exit(2); end;
770+ r, st, c := wait(child, true);
771+ if (r # child) or (st # stopped) or (c # sigStop) or (kill(-child, sigKill) # 0) then exit(2); end;
772+ r, st, c := wait(-1, true);
773+ if (r # child) or (st # killed) or (c # sigKill) then exit(2); end;
774+ r, st, c := wait(-1, true);
775+ if r # -10 then exit(2); end; (* ECHILD: none left *)
776+ child := spawn({path: "/no/such/program", args: ["x"]});
777+ r, st, c := wait(child, true);
778+ if (r # child) or (st # exited) or (c # 127) then exit(2); end;
779+
780+ sigs := catch([sigChld, sigHup]);
781+ if (sigs < 0) or (catch([sigWinch]) # sigs) or (catch([11]) # -22) or (catch([sigKill]) # -22) then exit(2); end;
782+ if (caught(sigs) # 0) or (ignore(sigInt, true) # 0) then exit(2); end;
783+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -INT $$; exit 0"]});
784+ if not ready(sigs, 5000) or (caught(sigs) # sigChld) then exit(2); end;
785+ r, st, c := wait(child, true);
786+ if (st # killed) or (c # sigInt) then exit(2); end;
787+ child := spawn({path: "/bin/sh", args: ["sh", "-c", "kill -HUP $$; exit 0"]});
788+ r, st, c := wait(child, true);
789+ if (st # killed) or (c # sigHup) then exit(2); end;
790+ while caught(sigs) # 0 do end;
791+ if (kill(pid(), sigHup) # 0) or not ready(sigs, 1000) or (caught(sigs) # sigHup) then exit(2); end;
792+ if ignore(sigInt, false) # 0 then exit(2); end;
599793 end;
600794
601795 (* The module's test, as Linux's Sys has it: a file written, read back