watchdog.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. static int wdog;
  12. int
  13. procctl(int pid)
  14. {
  15. int ctlfd;
  16. char *ctl;
  17. ctl = smprint("/proc/%d/ctl", pid);
  18. ctlfd = open(ctl, OWRITE);
  19. if (ctlfd < 0)
  20. sysfatal("open %s: %r", ctl);
  21. free(ctl);
  22. return ctlfd;
  23. }
  24. void
  25. main(int argc, char **argv)
  26. {
  27. int ctl;
  28. wdog = open("#w/wdctl", ORDWR);
  29. if (wdog < 0)
  30. sysfatal("open #w/wdctl: %r");
  31. switch(rfork(RFPROC|RFNOWAIT|RFFDG)){
  32. case 0:
  33. break;
  34. default:
  35. exits(0);
  36. }
  37. ctl = procctl(getpid());
  38. fprint(ctl, "pri 18");
  39. close(ctl);
  40. if (fprint(wdog, "enable") < 0)
  41. sysfatal("write #w/wdctl enable: %r");
  42. for(;;){
  43. sleep(300); /* allows 4.2GHz CPU, with some slop */
  44. seek(wdog, 0, 0);
  45. if (fprint(wdog, "restart") < 0)
  46. sysfatal("write #w/wdctl restart: %r");
  47. }
  48. }