setns-dcache.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright © 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. /*
  17. * Test that setns(CLONE_NEWNET) points to new /proc/net content even
  18. * if old one is in dcache.
  19. *
  20. * FIXME /proc/net/unix is under CONFIG_UNIX which can be disabled.
  21. */
  22. #undef NDEBUG
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <sched.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <fcntl.h>
  34. #include <sys/socket.h>
  35. static pid_t pid = -1;
  36. static void f(void)
  37. {
  38. if (pid > 0) {
  39. kill(pid, SIGTERM);
  40. }
  41. }
  42. int main(void)
  43. {
  44. int fd[2];
  45. char _ = 0;
  46. int nsfd;
  47. atexit(f);
  48. /* Check for priviledges and syscall availability straight away. */
  49. if (unshare(CLONE_NEWNET) == -1) {
  50. if (errno == ENOSYS || errno == EPERM) {
  51. return 4;
  52. }
  53. return 1;
  54. }
  55. /* Distinguisher between two otherwise empty net namespaces. */
  56. if (socket(AF_UNIX, SOCK_STREAM, 0) == -1) {
  57. return 1;
  58. }
  59. if (pipe(fd) == -1) {
  60. return 1;
  61. }
  62. pid = fork();
  63. if (pid == -1) {
  64. return 1;
  65. }
  66. if (pid == 0) {
  67. if (unshare(CLONE_NEWNET) == -1) {
  68. return 1;
  69. }
  70. if (write(fd[1], &_, 1) != 1) {
  71. return 1;
  72. }
  73. pause();
  74. return 0;
  75. }
  76. if (read(fd[0], &_, 1) != 1) {
  77. return 1;
  78. }
  79. {
  80. char buf[64];
  81. snprintf(buf, sizeof(buf), "/proc/%u/ns/net", pid);
  82. nsfd = open(buf, O_RDONLY);
  83. if (nsfd == -1) {
  84. return 1;
  85. }
  86. }
  87. /* Reliably pin dentry into dcache. */
  88. (void)open("/proc/net/unix", O_RDONLY);
  89. if (setns(nsfd, CLONE_NEWNET) == -1) {
  90. return 1;
  91. }
  92. kill(pid, SIGTERM);
  93. pid = 0;
  94. {
  95. char buf[4096];
  96. ssize_t rv;
  97. int fd;
  98. fd = open("/proc/net/unix", O_RDONLY);
  99. if (fd == -1) {
  100. return 1;
  101. }
  102. #define S "Num RefCount Protocol Flags Type St Inode Path\n"
  103. rv = read(fd, buf, sizeof(buf));
  104. assert(rv == strlen(S));
  105. assert(memcmp(buf, S, strlen(S)) == 0);
  106. }
  107. return 0;
  108. }