xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/exec/recursion-depth.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com>
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, and distribute this software for any
5*4882a593Smuzhiyun  * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun  * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11*4882a593Smuzhiyun  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13*4882a593Smuzhiyun  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14*4882a593Smuzhiyun  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun /* Test that pointing #! script interpreter to self doesn't recurse. */
17*4882a593Smuzhiyun #include <errno.h>
18*4882a593Smuzhiyun #include <sched.h>
19*4882a593Smuzhiyun #include <stdio.h>
20*4882a593Smuzhiyun #include <string.h>
21*4882a593Smuzhiyun #include <sys/types.h>
22*4882a593Smuzhiyun #include <sys/stat.h>
23*4882a593Smuzhiyun #include <fcntl.h>
24*4882a593Smuzhiyun #include <sys/mount.h>
25*4882a593Smuzhiyun #include <unistd.h>
26*4882a593Smuzhiyun 
main(void)27*4882a593Smuzhiyun int main(void)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	if (unshare(CLONE_NEWNS) == -1) {
30*4882a593Smuzhiyun 		if (errno == ENOSYS || errno == EPERM) {
31*4882a593Smuzhiyun 			fprintf(stderr, "error: unshare, errno %d\n", errno);
32*4882a593Smuzhiyun 			return 4;
33*4882a593Smuzhiyun 		}
34*4882a593Smuzhiyun 		fprintf(stderr, "error: unshare, errno %d\n", errno);
35*4882a593Smuzhiyun 		return 1;
36*4882a593Smuzhiyun 	}
37*4882a593Smuzhiyun 	if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
38*4882a593Smuzhiyun 		fprintf(stderr, "error: mount '/', errno %d\n", errno);
39*4882a593Smuzhiyun 		return 1;
40*4882a593Smuzhiyun 	}
41*4882a593Smuzhiyun 	/* Require "exec" filesystem. */
42*4882a593Smuzhiyun 	if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1) {
43*4882a593Smuzhiyun 		fprintf(stderr, "error: mount ramfs, errno %d\n", errno);
44*4882a593Smuzhiyun 		return 1;
45*4882a593Smuzhiyun 	}
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #define FILENAME "/tmp/1"
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	int fd = creat(FILENAME, 0700);
50*4882a593Smuzhiyun 	if (fd == -1) {
51*4882a593Smuzhiyun 		fprintf(stderr, "error: creat, errno %d\n", errno);
52*4882a593Smuzhiyun 		return 1;
53*4882a593Smuzhiyun 	}
54*4882a593Smuzhiyun #define S "#!" FILENAME "\n"
55*4882a593Smuzhiyun 	if (write(fd, S, strlen(S)) != strlen(S)) {
56*4882a593Smuzhiyun 		fprintf(stderr, "error: write, errno %d\n", errno);
57*4882a593Smuzhiyun 		return 1;
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 	close(fd);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	int rv = execve(FILENAME, NULL, NULL);
62*4882a593Smuzhiyun 	if (rv == -1 && errno == ELOOP) {
63*4882a593Smuzhiyun 		return 0;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 	fprintf(stderr, "error: execve, rv %d, errno %d\n", rv, errno);
66*4882a593Smuzhiyun 	return 1;
67*4882a593Smuzhiyun }
68