reboot.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <u.h>
  2. #include <libc.h>
  3. void
  4. reboot(void)
  5. {
  6. int fd;
  7. fd = open("/dev/reboot", OWRITE);
  8. if(fd >= 0)
  9. write(fd, "reboot", 6);
  10. exits(0);
  11. }
  12. char*
  13. readenv(char *name, char *buf, int n)
  14. {
  15. char *ans;
  16. int f;
  17. char ename[200];
  18. ans = buf;
  19. ename[0] = 0;
  20. strcat(ename, "/env/");
  21. strcat(ename, name);
  22. f = open(ename, OREAD);
  23. if(f < 0)
  24. return 0;
  25. n = read(f, ans, n-1);
  26. if(n < 0)
  27. ans = 0;
  28. else
  29. ans[n] = 0;
  30. close(f);
  31. return ans;
  32. }
  33. int alarmed;
  34. void
  35. ding(void*, char*msg)
  36. {
  37. if(strstr(msg, "alarm")){
  38. alarmed = 1;
  39. noted(NCONT);
  40. }
  41. noted(NDFLT);
  42. }
  43. void
  44. main(int argc, char **argv)
  45. {
  46. int fd;
  47. char buf[256];
  48. char file[128];
  49. char *p;
  50. Dir *d;
  51. switch(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFCFDG)){
  52. case 0:
  53. break;
  54. default:
  55. exits(0);
  56. }
  57. notify(ding);
  58. if(argc > 1)
  59. strecpy(file, file+sizeof file, argv[1]);
  60. else{
  61. p = readenv("cputype", buf, sizeof buf);
  62. if(p == 0)
  63. exits(0);
  64. file[0] = 0;
  65. strcat(file, "/");
  66. strcat(file, p);
  67. strcat(file, "/lib");
  68. }
  69. fd = open(file, OREAD);
  70. // the logic here is to make a request every 5 minutes.
  71. // If the request alarms out, that's OK, the file server
  72. // may just be busy. If the request fails for any other
  73. // reason, it's probably because the connection went
  74. // away so reboot.
  75. for(;;){
  76. alarm(1000*60*5);
  77. alarmed = 0;
  78. d = dirfstat(fd);
  79. free(d);
  80. if(d == nil)
  81. if(!alarmed)
  82. reboot();
  83. alarm(0);
  84. sleep(60*1000);
  85. }
  86. }