reboot.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. void
  12. reboot(void)
  13. {
  14. int fd;
  15. fd = open("/dev/reboot", OWRITE);
  16. if(fd >= 0)
  17. write(fd, "reboot", 6);
  18. exits(0);
  19. }
  20. char*
  21. readenv(char *name, char *buf, int n)
  22. {
  23. char *ans;
  24. int f;
  25. char ename[200];
  26. ans = buf;
  27. ename[0] = 0;
  28. strcat(ename, "/env/");
  29. strcat(ename, name);
  30. f = open(ename, OREAD);
  31. if(f < 0)
  32. return 0;
  33. n = read(f, ans, n-1);
  34. if(n < 0)
  35. ans = 0;
  36. else
  37. ans[n] = 0;
  38. close(f);
  39. return ans;
  40. }
  41. int alarmed;
  42. void
  43. ding(void *v, char*msg)
  44. {
  45. if(strstr(msg, "alarm")){
  46. alarmed = 1;
  47. noted(NCONT);
  48. }
  49. noted(NDFLT);
  50. }
  51. void
  52. main(int argc, char **argv)
  53. {
  54. int fd;
  55. char buf[256];
  56. char file[128];
  57. char *p;
  58. Dir *d;
  59. if(argc > 1)
  60. strecpy(file, file+sizeof file, argv[1]);
  61. else{
  62. p = readenv("cputype", buf, sizeof buf);
  63. if(p == 0)
  64. exits(0);
  65. file[0] = 0;
  66. strcat(file, "/");
  67. strcat(file, p);
  68. strcat(file, "/lib");
  69. }
  70. if (access(file, AREAD) < 0)
  71. sysfatal("%s not readable: %r", file);
  72. switch(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFCFDG)){
  73. case 0:
  74. break;
  75. default:
  76. exits(0);
  77. }
  78. notify(ding);
  79. fd = open(file, OREAD);
  80. if (fd < 0)
  81. exits("no file");
  82. // the logic here is to make a request every 5 minutes.
  83. // If the request alarms out, that's OK, the file server
  84. // may just be busy. If the request fails for any other
  85. // reason, it's probably because the connection went
  86. // away so reboot.
  87. for(;;){
  88. alarm(1000*60);
  89. alarmed = 0;
  90. d = dirfstat(fd);
  91. free(d);
  92. if(d == nil)
  93. if(!alarmed)
  94. reboot();
  95. alarm(0);
  96. sleep(60*1000*5);
  97. }
  98. }