Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 1 | /* Disk protection for HP/DELL machines. |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2008 Eric Piel |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 4 | * Copyright 2009 Pavel Machek <pavel@ucw.cz> |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 5 | * Copyright 2012 Sonal Santan |
| 6 | * Copyright 2014 Pali Rohár <pali.rohar@gmail.com> |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 7 | * |
| 8 | * GPLv2. |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <unistd.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <string.h> |
| 18 | #include <stdint.h> |
| 19 | #include <errno.h> |
| 20 | #include <signal.h> |
Christian Thaeter | 2bace8b | 2009-07-25 20:55:15 +0200 | [diff] [blame] | 21 | #include <sys/mman.h> |
| 22 | #include <sched.h> |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 23 | #include <syslog.h> |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 24 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 25 | static int noled; |
| 26 | static char unload_heads_path[64]; |
| 27 | static char device_path[32]; |
| 28 | static const char app_name[] = "FREE FALL"; |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 29 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 30 | static int set_unload_heads_path(char *device) |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 31 | { |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 32 | if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0) |
| 33 | return -EINVAL; |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 34 | strncpy(device_path, device, sizeof(device_path) - 1); |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 35 | |
Stefan Weil | 6e641c9 | 2013-12-30 12:35:43 +0100 | [diff] [blame] | 36 | snprintf(unload_heads_path, sizeof(unload_heads_path) - 1, |
Pavel Machek | d74aae4 | 2014-08-05 23:32:41 -0700 | [diff] [blame] | 37 | "/sys/block/%s/device/unload_heads", device+5); |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 38 | return 0; |
| 39 | } |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 40 | |
| 41 | static int valid_disk(void) |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 42 | { |
| 43 | int fd = open(unload_heads_path, O_RDONLY); |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 44 | |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 45 | if (fd < 0) { |
| 46 | perror(unload_heads_path); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | close(fd); |
| 51 | return 1; |
| 52 | } |
| 53 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 54 | static void write_int(char *path, int i) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 55 | { |
| 56 | char buf[1024]; |
| 57 | int fd = open(path, O_RDWR); |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 58 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 59 | if (fd < 0) { |
| 60 | perror("open"); |
| 61 | exit(1); |
| 62 | } |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 63 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 64 | sprintf(buf, "%d", i); |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 65 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 66 | if (write(fd, buf, strlen(buf)) != strlen(buf)) { |
| 67 | perror("write"); |
| 68 | exit(1); |
| 69 | } |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 70 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 71 | close(fd); |
| 72 | } |
| 73 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 74 | static void set_led(int on) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 75 | { |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 76 | if (noled) |
| 77 | return; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 78 | write_int("/sys/class/leds/hp::hddprotect/brightness", on); |
| 79 | } |
| 80 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 81 | static void protect(int seconds) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 82 | { |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 83 | const char *str = (seconds == 0) ? "Unparked" : "Parked"; |
| 84 | |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 85 | write_int(unload_heads_path, seconds*1000); |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 86 | syslog(LOG_INFO, "%s %s disk head\n", str, device_path); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 89 | static int on_ac(void) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 90 | { |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 91 | /* /sys/class/power_supply/AC0/online */ |
| 92 | return 1; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 95 | static int lid_open(void) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 96 | { |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 97 | /* /proc/acpi/button/lid/LID/state */ |
| 98 | return 1; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 101 | static void ignore_me(int signum) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 102 | { |
| 103 | protect(0); |
| 104 | set_led(0); |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 107 | int main(int argc, char **argv) |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 108 | { |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 109 | int fd, ret; |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 110 | struct stat st; |
Christian Thaeter | 2bace8b | 2009-07-25 20:55:15 +0200 | [diff] [blame] | 111 | struct sched_param param; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 112 | |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 113 | if (argc == 1) |
| 114 | ret = set_unload_heads_path("/dev/sda"); |
| 115 | else if (argc == 2) |
| 116 | ret = set_unload_heads_path(argv[1]); |
| 117 | else |
| 118 | ret = -EINVAL; |
| 119 | |
| 120 | if (ret || !valid_disk()) { |
| 121 | fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n", |
| 122 | argv[0]); |
| 123 | exit(1); |
| 124 | } |
| 125 | |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 126 | fd = open("/dev/freefall", O_RDONLY); |
| 127 | if (fd < 0) { |
Frans Pop | be3990b | 2009-07-25 21:00:12 +0200 | [diff] [blame] | 128 | perror("/dev/freefall"); |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 129 | return EXIT_FAILURE; |
| 130 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 131 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 132 | if (stat("/sys/class/leds/hp::hddprotect/brightness", &st)) |
| 133 | noled = 1; |
| 134 | |
| 135 | if (daemon(0, 0) != 0) { |
| 136 | perror("daemon"); |
| 137 | return EXIT_FAILURE; |
| 138 | } |
| 139 | |
| 140 | openlog(app_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); |
| 141 | |
Christian Thaeter | 2bace8b | 2009-07-25 20:55:15 +0200 | [diff] [blame] | 142 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); |
| 143 | sched_setscheduler(0, SCHED_FIFO, ¶m); |
| 144 | mlockall(MCL_CURRENT|MCL_FUTURE); |
| 145 | |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 146 | signal(SIGALRM, ignore_me); |
| 147 | |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 148 | for (;;) { |
| 149 | unsigned char count; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 150 | |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 151 | ret = read(fd, &count, sizeof(count)); |
| 152 | alarm(0); |
| 153 | if ((ret == -1) && (errno == EINTR)) { |
| 154 | /* Alarm expired, time to unpark the heads */ |
| 155 | continue; |
| 156 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 157 | |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 158 | if (ret != sizeof(count)) { |
| 159 | perror("read"); |
| 160 | break; |
| 161 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 162 | |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 163 | protect(21); |
| 164 | set_led(1); |
| 165 | if (1 || on_ac() || lid_open()) |
| 166 | alarm(2); |
| 167 | else |
| 168 | alarm(20); |
| 169 | } |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 170 | |
Pali Rohár | 3a57cc5 | 2014-07-12 09:56:22 -0700 | [diff] [blame] | 171 | closelog(); |
Frans Pop | b519c15 | 2009-06-22 11:08:36 +0200 | [diff] [blame] | 172 | close(fd); |
| 173 | return EXIT_SUCCESS; |
Pavel Machek | ef2cfc7 | 2009-02-18 14:48:23 -0800 | [diff] [blame] | 174 | } |