14e7301e6SAlexey Dobriyan /*
24e7301e6SAlexey Dobriyan * Copyright (c) 2019 Alexey Dobriyan <[email protected]>
34e7301e6SAlexey Dobriyan *
44e7301e6SAlexey Dobriyan * Permission to use, copy, modify, and distribute this software for any
54e7301e6SAlexey Dobriyan * purpose with or without fee is hereby granted, provided that the above
64e7301e6SAlexey Dobriyan * copyright notice and this permission notice appear in all copies.
74e7301e6SAlexey Dobriyan *
84e7301e6SAlexey Dobriyan * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
94e7301e6SAlexey Dobriyan * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
104e7301e6SAlexey Dobriyan * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
114e7301e6SAlexey Dobriyan * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
124e7301e6SAlexey Dobriyan * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
134e7301e6SAlexey Dobriyan * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
144e7301e6SAlexey Dobriyan * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
154e7301e6SAlexey Dobriyan */
164e7301e6SAlexey Dobriyan /* Test that pointing #! script interpreter to self doesn't recurse. */
174e7301e6SAlexey Dobriyan #include <errno.h>
184e7301e6SAlexey Dobriyan #include <sched.h>
194e7301e6SAlexey Dobriyan #include <stdio.h>
204e7301e6SAlexey Dobriyan #include <string.h>
214e7301e6SAlexey Dobriyan #include <sys/types.h>
224e7301e6SAlexey Dobriyan #include <sys/stat.h>
234e7301e6SAlexey Dobriyan #include <fcntl.h>
244e7301e6SAlexey Dobriyan #include <sys/mount.h>
254e7301e6SAlexey Dobriyan #include <unistd.h>
261d0e51b2SMuhammad Usama Anjum #include "../kselftest.h"
274e7301e6SAlexey Dobriyan
main(void)284e7301e6SAlexey Dobriyan int main(void)
294e7301e6SAlexey Dobriyan {
301d0e51b2SMuhammad Usama Anjum int fd, rv;
311d0e51b2SMuhammad Usama Anjum
321d0e51b2SMuhammad Usama Anjum ksft_print_header();
331d0e51b2SMuhammad Usama Anjum ksft_set_plan(1);
341d0e51b2SMuhammad Usama Anjum
354e7301e6SAlexey Dobriyan if (unshare(CLONE_NEWNS) == -1) {
364e7301e6SAlexey Dobriyan if (errno == ENOSYS || errno == EPERM) {
371d0e51b2SMuhammad Usama Anjum ksft_test_result_skip("error: unshare, errno %d\n", errno);
381d0e51b2SMuhammad Usama Anjum ksft_finished();
394e7301e6SAlexey Dobriyan }
40*9c84b890SMuhammad Usama Anjum ksft_exit_fail_perror("error: unshare");
414e7301e6SAlexey Dobriyan }
421d0e51b2SMuhammad Usama Anjum
431d0e51b2SMuhammad Usama Anjum if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1)
44*9c84b890SMuhammad Usama Anjum ksft_exit_fail_perror("error: mount '/'");
451d0e51b2SMuhammad Usama Anjum
464e7301e6SAlexey Dobriyan /* Require "exec" filesystem. */
471d0e51b2SMuhammad Usama Anjum if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1)
48*9c84b890SMuhammad Usama Anjum ksft_exit_fail_perror("error: mount ramfs");
494e7301e6SAlexey Dobriyan
504e7301e6SAlexey Dobriyan #define FILENAME "/tmp/1"
514e7301e6SAlexey Dobriyan
521d0e51b2SMuhammad Usama Anjum fd = creat(FILENAME, 0700);
531d0e51b2SMuhammad Usama Anjum if (fd == -1)
54*9c84b890SMuhammad Usama Anjum ksft_exit_fail_perror("error: creat");
551d0e51b2SMuhammad Usama Anjum
564e7301e6SAlexey Dobriyan #define S "#!" FILENAME "\n"
571d0e51b2SMuhammad Usama Anjum if (write(fd, S, strlen(S)) != strlen(S))
58*9c84b890SMuhammad Usama Anjum ksft_exit_fail_perror("error: write");
591d0e51b2SMuhammad Usama Anjum
604e7301e6SAlexey Dobriyan close(fd);
614e7301e6SAlexey Dobriyan
621d0e51b2SMuhammad Usama Anjum rv = execve(FILENAME, NULL, NULL);
631d0e51b2SMuhammad Usama Anjum ksft_test_result(rv == -1 && errno == ELOOP,
641d0e51b2SMuhammad Usama Anjum "execve failed as expected (ret %d, errno %d)\n", rv, errno);
651d0e51b2SMuhammad Usama Anjum ksft_finished();
664e7301e6SAlexey Dobriyan }
67