nsclone.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. int sync[2];
  21. void
  22. setup_ns(void)
  23. {
  24. int fd;
  25. if(sys_bind("#|", "/", MREPL) < 0){
  26. print("FAIL: sys_rfork\n");
  27. jehanne_write(sync[0], "FAIL", 5);
  28. return;
  29. }
  30. fd = sys_open("/data", OWRITE);
  31. if(fd < 0){
  32. print("FAIL: sys_open(/data)\n");
  33. jehanne_write(sync[0], "FAIL", 5);
  34. return;
  35. }
  36. jehanne_write(fd, "hi!", 4);
  37. sys_close(fd);
  38. jehanne_write(sync[0], "DONE", 5);
  39. sleep(10);
  40. }
  41. void
  42. main(void)
  43. {
  44. int child, fd;
  45. char buf[64];
  46. sys_rfork(RFNOTEG|RFNAMEG);
  47. pipe(sync);
  48. switch(child = sys_rfork(RFPROC|RFCNAMEG|RFNOWAIT)){
  49. case -1:
  50. print("FAIL: sys_rfork\n");
  51. exits("FAIL");
  52. case 0:
  53. setup_ns();
  54. exits(nil);
  55. default:
  56. break;
  57. }
  58. jehanne_read(sync[1], buf, sizeof(buf));
  59. if(strcmp("DONE", buf) != 0)
  60. exits("FAIL");
  61. snprint(buf, sizeof(buf), "/proc/%d/ns", child);
  62. fd = sys_open(buf, OWRITE);
  63. if(fd < 0){
  64. print("FAIL: sys_open(%s)\n", buf);
  65. exits("FAIL");
  66. }
  67. jehanne_write(fd, "clone", 6);
  68. sys_close(fd);
  69. memset(buf, 0, sizeof(buf));
  70. fd = sys_open("/data1", OREAD);
  71. if(fd < 0){
  72. print("FAIL: sys_open(/data1)\n");
  73. exits("FAIL");
  74. }
  75. jehanne_read(fd, buf, sizeof(buf));
  76. sys_close(fd);
  77. if(strcmp("hi!", buf) == 0){
  78. print("PASS: read '%s' from /data1\n", buf);
  79. exits("PASS");
  80. }
  81. print("FAIL: unexpected string from %d: %s\n", child, buf);
  82. exits("FAIL");
  83. }