2 * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 /* Test that pointing #! script interpreter to self doesn't recurse. */
21 #include <sys/types.h>
24 #include <sys/mount.h>
29 if (unshare(CLONE_NEWNS
) == -1) {
30 if (errno
== ENOSYS
|| errno
== EPERM
) {
31 fprintf(stderr
, "error: unshare, errno %d\n", errno
);
34 fprintf(stderr
, "error: unshare, errno %d\n", errno
);
37 if (mount(NULL
, "/", NULL
, MS_PRIVATE
|MS_REC
, NULL
) == -1) {
38 fprintf(stderr
, "error: mount '/', errno %d\n", errno
);
41 /* Require "exec" filesystem. */
42 if (mount(NULL
, "/tmp", "ramfs", 0, NULL
) == -1) {
43 fprintf(stderr
, "error: mount ramfs, errno %d\n", errno
);
47 #define FILENAME "/tmp/1"
49 int fd
= creat(FILENAME
, 0700);
51 fprintf(stderr
, "error: creat, errno %d\n", errno
);
54 #define S "#!" FILENAME "\n"
55 if (write(fd
, S
, strlen(S
)) != strlen(S
)) {
56 fprintf(stderr
, "error: write, errno %d\n", errno
);
61 int rv
= execve(FILENAME
, NULL
, NULL
);
62 if (rv
== -1 && errno
== ELOOP
) {
65 fprintf(stderr
, "error: execve, rv %d, errno %d\n", rv
, errno
);