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