• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <signal.h>
6 #include "test.h"
7 
8 #define TEST(r, f, x, m) ( \
9 	((r) = (f)) == (x) || (t_error("%s failed (" m ")\n", #f, r, x), 0) )
10 
11 #define TEST_E(f) ( \
12 	(errno = 0), \
13 	(f) || (t_error("%s failed (errno = %d)\n", #f, errno), 0) )
14 
15 #define TEST_S(s, x, m) ( \
16 	!strcmp((s),(x)) || \
17 		(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
18 
19 static sig_atomic_t got_sig;
20 
handler(int sig)21 static void handler(int sig) {
22 	got_sig = 1;
23 }
24 
main(void)25 int main(void)
26 {
27 	int i;
28 	char foo[6];
29 	char cmd[64];
30 	FILE *f;
31 
32 	TEST_E(f = popen("echo hello", "r"));
33 	if (f) {
34 		TEST_E(fgets(foo, sizeof foo, f));
35 		TEST_S(foo, "hello", "child process did not say hello");
36 		TEST(i, pclose(f), 0, "exit status %04x != %04x");
37 	}
38 
39 	signal(SIGUSR1, handler);
40 	snprintf(cmd, sizeof cmd, "read a ; test \"x$a\" = xhello && kill -USR1 %d", getpid());
41 	TEST_E(f = popen(cmd, "w"));
42 	if (f) {
43 		TEST_E(fputs("hello", f) >= 0);
44 		TEST(i, pclose(f), 0, "exit status %04x != %04x");
45 		TEST(i, got_sig, 1, "child process did not send signal");
46 	}
47 	signal(SIGUSR1, SIG_DFL);
48 	return t_status;
49 }
50