recursion-depth.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2019 Alexey Dobriyan <adobriyan@gmail.com>
  3. *
  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.
  7. *
  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.
  15. */
  16. /* Test that pointing #! script interpreter to self doesn't recurse. */
  17. #include <errno.h>
  18. #include <sched.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <sys/mount.h>
  25. #include <unistd.h>
  26. #include "kselftest.h"
  27. int main(void)
  28. {
  29. int fd, rv;
  30. ksft_print_header();
  31. ksft_set_plan(1);
  32. if (unshare(CLONE_NEWNS) == -1) {
  33. if (errno == ENOSYS || errno == EPERM) {
  34. ksft_test_result_skip("error: unshare, errno %d\n", errno);
  35. ksft_finished();
  36. }
  37. ksft_exit_fail_perror("error: unshare");
  38. }
  39. if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1)
  40. ksft_exit_fail_perror("error: mount '/'");
  41. /* Require "exec" filesystem. */
  42. if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1)
  43. ksft_exit_fail_perror("error: mount ramfs");
  44. #define FILENAME "/tmp/1"
  45. fd = creat(FILENAME, 0700);
  46. if (fd == -1)
  47. ksft_exit_fail_perror("error: creat");
  48. #define S "#!" FILENAME "\n"
  49. if (write(fd, S, strlen(S)) != strlen(S))
  50. ksft_exit_fail_perror("error: write");
  51. close(fd);
  52. rv = execve(FILENAME, NULL, NULL);
  53. ksft_test_result(rv == -1 && errno == ELOOP,
  54. "execve failed as expected (ret %d, errno %d)\n", rv, errno);
  55. ksft_finished();
  56. }