xref: /minix/minix/tests/test86.c (revision 0a6a1f1d)
1 #include <stdlib.h>
2 #include <string.h>
3 #include <limits.h>
4 #include <fcntl.h>
5 #include <sys/wait.h>
6 
7 #include "common.h"
8 
9 /*
10  * Test for dynamic executables with no read permissions.  This test relies on
11  * being linked dynamically.
12  */
13 int
14 main(int argc, char ** argv)
15 {
16 	char *executable, cp_cmd[PATH_MAX + 9];
17 	int status;
18 
19 	if (strcmp(argv[0], "DO CHECK") == 0)
20 		exit(EXIT_SUCCESS);
21 
22 	start(86);
23 
24 	/* Make a copy of this binary which is executable-only. */
25 	executable = argv[0];
26 
27 	snprintf(cp_cmd, sizeof(cp_cmd), "cp ../%s .", executable);
28 	status = system(cp_cmd);
29 	if (status < 0 || !WIFEXITED(status) ||
30 	    WEXITSTATUS(status) != EXIT_SUCCESS) e(0);
31 
32 	if (chmod(executable, S_IXUSR) != 0) e(0);
33 
34 	/* Invoke the changed binary in a child process. */
35 	switch (fork()) {
36 	case -1:
37 		e(0);
38 	case 0:
39 		execl(executable, "DO CHECK", NULL);
40 
41 		exit(EXIT_FAILURE);
42 	default:
43 		if (wait(&status) <= 0) e(0);
44 		if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SUCCESS)
45 			e(0);
46 	}
47 
48 	quit();
49 	/* NOTREACHED */
50 }
51