hpfall.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <sys/mman.h>
  19. #include <sched.h>
  20. void write_int(char *path, int i)
  21. {
  22. char buf[1024];
  23. int fd = open(path, O_RDWR);
  24. if (fd < 0) {
  25. perror("open");
  26. exit(1);
  27. }
  28. sprintf(buf, "%d", i);
  29. if (write(fd, buf, strlen(buf)) != strlen(buf)) {
  30. perror("write");
  31. exit(1);
  32. }
  33. close(fd);
  34. }
  35. void set_led(int on)
  36. {
  37. write_int("/sys/class/leds/hp::hddprotect/brightness", on);
  38. }
  39. void protect(int seconds)
  40. {
  41. write_int("/sys/block/sda/device/unload_heads", seconds*1000);
  42. }
  43. int on_ac(void)
  44. {
  45. // /sys/class/power_supply/AC0/online
  46. }
  47. int lid_open(void)
  48. {
  49. // /proc/acpi/button/lid/LID/state
  50. }
  51. void ignore_me(void)
  52. {
  53. protect(0);
  54. set_led(0);
  55. }
  56. int main(int argc, char *argv[])
  57. {
  58. int fd, ret;
  59. struct sched_param param;
  60. fd = open("/dev/freefall", O_RDONLY);
  61. if (fd < 0) {
  62. perror("open");
  63. return EXIT_FAILURE;
  64. }
  65. daemon(0, 0);
  66. param.sched_priority = sched_get_priority_max(SCHED_FIFO);
  67. sched_setscheduler(0, SCHED_FIFO, &param);
  68. mlockall(MCL_CURRENT|MCL_FUTURE);
  69. signal(SIGALRM, ignore_me);
  70. for (;;) {
  71. unsigned char count;
  72. ret = read(fd, &count, sizeof(count));
  73. alarm(0);
  74. if ((ret == -1) && (errno == EINTR)) {
  75. /* Alarm expired, time to unpark the heads */
  76. continue;
  77. }
  78. if (ret != sizeof(count)) {
  79. perror("read");
  80. break;
  81. }
  82. protect(21);
  83. set_led(1);
  84. if (1 || on_ac() || lid_open())
  85. alarm(2);
  86. else
  87. alarm(20);
  88. }
  89. close(fd);
  90. return EXIT_SUCCESS;
  91. }