postnote.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2016 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 verbose = 0;
  21. int procNoteReceived;
  22. int groupNoteReceived;
  23. int
  24. printFromProc(void *v, char *s)
  25. {
  26. /* just not exit, please */
  27. if(strcmp(s, "proc") == 0){
  28. if(procNoteReceived)
  29. atnotify(printFromProc, 0);
  30. procNoteReceived++;
  31. if(verbose)
  32. fprint(2, "%d: noted: %s at %lld\n", getpid(), s, nsec());
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. int
  38. printFromGroup(void *v, char *s)
  39. {
  40. /* just not exit, please */
  41. if(strcmp(s, "group") == 0){
  42. if(groupNoteReceived)
  43. atnotify(printFromGroup, 0);
  44. groupNoteReceived++;
  45. if(verbose)
  46. fprint(2, "%d: noted: %s at %lld\n", getpid(), s, nsec());
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. int
  52. fail(void *v, char *s)
  53. {
  54. if(groupNoteReceived == 2 || procNoteReceived == 2){
  55. print("FAIL\n");
  56. exits("FAIL");
  57. }
  58. return 0;
  59. }
  60. void
  61. main(void)
  62. {
  63. int ppid;
  64. Waitmsg *w;
  65. if (!atnotify(printFromProc, 1) || !atnotify(printFromGroup, 1) || !atnotify(fail, 1)){
  66. fprint(2, "%r\n");
  67. exits("atnotify fails");
  68. }
  69. rfork(RFNOTEG|RFNAMEG);
  70. if(rfork(RFPROC) == 0){
  71. ppid = getppid(); // this comes from exec() syscall
  72. postnote(PNPROC, ppid, "proc");
  73. postnote(PNGROUP, getpid(), "group");
  74. rfork(RFNOMNT);
  75. postnote(PNPROC, ppid, "proc");
  76. postnote(PNGROUP, getpid(), "group");
  77. rfork(RFNOMNT|RFCNAMEG);
  78. if(postnote(PNPROC, ppid, "proc") != -1 ||
  79. postnote(PNGROUP, getpid(), "group") != -1){
  80. exits("FAIL");
  81. }
  82. exits(nil);
  83. } else {
  84. while((w = wait()) == nil)
  85. ;
  86. if(w->msg[0]){
  87. exits("FAIL");
  88. }
  89. if(procNoteReceived < 2 || groupNoteReceived < 2){
  90. fprint(2, "FAIL: not enough notes.\n");
  91. exits("FAIL");
  92. }
  93. print("PASS\n");
  94. exits("PASS");
  95. }
  96. }