bug-link-o-tmpfile.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 open(O_TMPFILE), linkat() doesn't screw accounting. */
  17. #include <errno.h>
  18. #include <sched.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <sys/mount.h>
  24. #include <unistd.h>
  25. #include "kselftest.h"
  26. int main(void)
  27. {
  28. int fd;
  29. // Setting up kselftest framework
  30. ksft_print_header();
  31. ksft_set_plan(1);
  32. // Check if test is run as root
  33. if (geteuid()) {
  34. ksft_exit_skip("This test needs root to run!\n");
  35. return 1;
  36. }
  37. if (unshare(CLONE_NEWNS) == -1) {
  38. if (errno == ENOSYS || errno == EPERM) {
  39. ksft_exit_skip("unshare() error: unshare, errno %d\n", errno);
  40. } else {
  41. ksft_exit_fail_msg("unshare() error: unshare, errno %d\n", errno);
  42. }
  43. }
  44. if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) == -1) {
  45. ksft_exit_fail_msg("mount() error: Root filesystem private mount: Fail %d\n", errno);
  46. }
  47. /* Our heroes: 1 root inode, 1 O_TMPFILE inode, 1 permanent inode. */
  48. if (mount(NULL, "/tmp", "tmpfs", 0, "nr_inodes=3") == -1) {
  49. ksft_exit_fail_msg("mount() error: Mounting tmpfs on /tmp: Fail %d\n", errno);
  50. }
  51. fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
  52. if (fd == -1) {
  53. ksft_exit_fail_msg("openat() error: Open first temporary file: Fail %d\n", errno);
  54. }
  55. if (linkat(fd, "", AT_FDCWD, "/tmp/1", AT_EMPTY_PATH) == -1) {
  56. ksft_exit_fail_msg("linkat() error: Linking the temporary file: Fail %d\n", errno);
  57. /* Ensure fd is closed on failure */
  58. close(fd);
  59. }
  60. close(fd);
  61. fd = openat(AT_FDCWD, "/tmp", O_WRONLY|O_TMPFILE, 0600);
  62. if (fd == -1) {
  63. ksft_exit_fail_msg("openat() error: Opening the second temporary file: Fail %d\n", errno);
  64. }
  65. ksft_test_result_pass(" ");
  66. ksft_exit_pass();
  67. return 0;
  68. }