1% -*- mode: slang; mode: fold -*-
2
3() = evalfile ("./test.sl");
4
5require ("fork");
6
7private define test_fork ()
8{
9   variable _ppid = getpid ();
10   variable pid = fork ();
11
12   if (pid == 0)
13     {
14	% child
15	if (_ppid == getpid ())
16	  {
17	     () = fprintf (stderr, "fork did not change pid\n");
18	     _exit (1);
19	  }
20
21#ifexists getppid
22	if (_ppid != getppid ())
23	  {
24	     () = fprintf (stderr, "getppid failed in child\n");
25	     _exit (1);
26	  }
27#endif
28	_exit(123);
29     }
30
31   variable w = waitpid (pid, 0);
32   if (w.exited)
33     {
34	if (w.exit_status != 123)
35	  failed ("child exited with unexpected status of %d", w.exit_status);
36     }
37}
38
39private define test_pipe ()
40{
41   variable fdr, fdw, buf, n;
42   (fdr, fdw) = pipe ();
43
44   while (-1 == write (fdw, "hello"))
45     {
46	if (errno == EINTR)
47	  continue;
48	failed ("write to pipe failed: %S", errno_string);
49     }
50   n = read (fdr, &buf, 10);
51   if ((buf != "hello") || (n != 5))
52     {
53	failed ("pipe failed");
54     }
55}
56
57define slsh_main ()
58{
59   testing_module ("fork");
60   test_fork ();
61   test_pipe ();
62   %test_exec ();
63   end_test ();
64}
65