hpfall.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Disk protection for HP machines.
  2. *
  3. * Copyright 2008 Eric Piel
  4. * Copyright 2009 Pavel Machek <pavel@ucw.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. char unload_heads_path[64];
  21. int set_unload_heads_path(char *device)
  22. {
  23. char devname[64];
  24. if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
  25. return -EINVAL;
  26. strncpy(devname, device + 5, sizeof(devname));
  27. snprintf(unload_heads_path, sizeof(unload_heads_path),
  28. "/sys/block/%s/device/unload_heads", devname);
  29. return 0;
  30. }
  31. int valid_disk(void)
  32. {
  33. int fd = open(unload_heads_path, O_RDONLY);
  34. if (fd < 0) {
  35. perror(unload_heads_path);
  36. return 0;
  37. }
  38. close(fd);
  39. return 1;
  40. }
  41. void write_int(char *path, int i)
  42. {
  43. char buf[1024];
  44. int fd = open(path, O_RDWR);
  45. if (fd < 0) {
  46. perror("open");
  47. exit(1);
  48. }
  49. sprintf(buf, "%d", i);
  50. if (write(fd, buf, strlen(buf)) != strlen(buf)) {
  51. perror("write");
  52. exit(1);
  53. }
  54. close(fd);
  55. }
  56. void set_led(int on)
  57. {
  58. write_int("/sys/class/leds/hp::hddprotect/brightness", on);
  59. }
  60. void protect(int seconds)
  61. {
  62. write_int(unload_heads_path, seconds*1000);
  63. }
  64. int on_ac(void)
  65. {
  66. // /sys/class/power_supply/AC0/online
  67. }
  68. int lid_open(void)
  69. {
  70. // /proc/acpi/button/lid/LID/state
  71. }
  72. void ignore_me(void)
  73. {
  74. protect(0);
  75. set_led(0);
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. int fd, ret;
  80. struct sched_param param;
  81. if (argc == 1)
  82. ret = set_unload_heads_path("/dev/sda");
  83. else if (argc == 2)
  84. ret = set_unload_heads_path(argv[1]);
  85. else
  86. ret = -EINVAL;
  87. if (ret || !valid_disk()) {
  88. fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n",
  89. argv[0]);
  90. exit(1);
  91. }
  92. fd = open("/dev/freefall", O_RDONLY);
  93. if (fd < 0) {
  94. perror("/dev/freefall");
  95. return EXIT_FAILURE;
  96. }
  97. daemon(0, 0);
  98. param.sched_priority = sched_get_priority_max(SCHED_FIFO);
  99. sched_setscheduler(0, SCHED_FIFO, &param);
  100. mlockall(MCL_CURRENT|MCL_FUTURE);
  101. signal(SIGALRM, ignore_me);
  102. for (;;) {
  103. unsigned char count;
  104. ret = read(fd, &count, sizeof(count));
  105. alarm(0);
  106. if ((ret == -1) && (errno == EINTR)) {
  107. /* Alarm expired, time to unpark the heads */
  108. continue;
  109. }
  110. if (ret != sizeof(count)) {
  111. perror("read");
  112. break;
  113. }
  114. protect(21);
  115. set_led(1);
  116. if (1 || on_ac() || lid_open())
  117. alarm(2);
  118. else
  119. alarm(20);
  120. }
  121. close(fd);
  122. return EXIT_SUCCESS;
  123. }