hpfall.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Disk protection for HP machines.
  2. *
  3. * Copyright 2008 Eric Piel
  4. * Copyright 2009 Pavel Machek <pavel@suse.cz>
  5. *
  6. * GPLv2.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include <errno.h>
  17. #include <signal.h>
  18. void write_int(char *path, int i)
  19. {
  20. char buf[1024];
  21. int fd = open(path, O_RDWR);
  22. if (fd < 0) {
  23. perror("open");
  24. exit(1);
  25. }
  26. sprintf(buf, "%d", i);
  27. if (write(fd, buf, strlen(buf)) != strlen(buf)) {
  28. perror("write");
  29. exit(1);
  30. }
  31. close(fd);
  32. }
  33. void set_led(int on)
  34. {
  35. write_int("/sys/class/leds/hp::hddprotect/brightness", on);
  36. }
  37. void protect(int seconds)
  38. {
  39. write_int("/sys/block/sda/device/unload_heads", seconds*1000);
  40. }
  41. int on_ac(void)
  42. {
  43. // /sys/class/power_supply/AC0/online
  44. }
  45. int lid_open(void)
  46. {
  47. // /proc/acpi/button/lid/LID/state
  48. }
  49. void ignore_me(void)
  50. {
  51. protect(0);
  52. set_led(0);
  53. }
  54. int main(int argc, char* argv[])
  55. {
  56. int fd, ret;
  57. fd = open("/dev/freefall", O_RDONLY);
  58. if (fd < 0) {
  59. perror("open");
  60. return EXIT_FAILURE;
  61. }
  62. signal(SIGALRM, ignore_me);
  63. for (;;) {
  64. unsigned char count;
  65. ret = read(fd, &count, sizeof(count));
  66. alarm(0);
  67. if ((ret == -1) && (errno == EINTR)) {
  68. /* Alarm expired, time to unpark the heads */
  69. continue;
  70. }
  71. if (ret != sizeof(count)) {
  72. perror("read");
  73. break;
  74. }
  75. protect(21);
  76. set_led(1);
  77. if (1 || on_ac() || lid_open()) {
  78. alarm(2);
  79. } else {
  80. alarm(20);
  81. }
  82. }
  83. close(fd);
  84. return EXIT_SUCCESS;
  85. }