reboot.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if(argc > 1)
  52. strecpy(file, file+sizeof file, argv[1]);
  53. else{
  54. p = readenv("cputype", buf, sizeof buf);
  55. if(p == 0)
  56. exits(0);
  57. file[0] = 0;
  58. strcat(file, "/");
  59. strcat(file, p);
  60. strcat(file, "/lib");
  61. }
  62. if (access(file, AREAD) < 0)
  63. sysfatal("%s not readable: %r", file);
  64. switch(rfork(RFPROC|RFNOWAIT|RFNOTEG|RFCFDG)){
  65. case 0:
  66. break;
  67. default:
  68. exits(0);
  69. }
  70. notify(ding);
  71. fd = open(file, OREAD);
  72. if (fd < 0)
  73. exits("no file");
  74. // the logic here is to make a request every 5 minutes.
  75. // If the request alarms out, that's OK, the file server
  76. // may just be busy. If the request fails for any other
  77. // reason, it's probably because the connection went
  78. // away so reboot.
  79. for(;;){
  80. alarm(1000*60);
  81. alarmed = 0;
  82. d = dirfstat(fd);
  83. free(d);
  84. if(d == nil)
  85. if(!alarmed)
  86. reboot();
  87. alarm(0);
  88. sleep(60*1000*5);
  89. }
  90. }