1 /* Disk protection for HP/DELL machines.
3 * Copyright 2008 Eric Piel
4 * Copyright 2009 Pavel Machek <pavel@ucw.cz>
5 * Copyright 2012 Sonal Santan
6 * Copyright 2014 Pali Rohár <pali.rohar@gmail.com>
16 #include <sys/types.h>
26 static char unload_heads_path[64];
27 static char device_path[32];
28 static const char app_name[] = "FREE FALL";
30 static int set_unload_heads_path(char *device)
34 if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
36 strncpy(devname, device + 5, sizeof(devname) - 1);
37 strncpy(device_path, device, sizeof(device_path) - 1);
39 snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
40 "/sys/block/%s/device/unload_heads", devname);
44 static int valid_disk(void)
46 int fd = open(unload_heads_path, O_RDONLY);
49 perror(unload_heads_path);
57 static void write_int(char *path, int i)
60 int fd = open(path, O_RDWR);
67 sprintf(buf, "%d", i);
69 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
77 static void set_led(int on)
81 write_int("/sys/class/leds/hp::hddprotect/brightness", on);
84 static void protect(int seconds)
86 const char *str = (seconds == 0) ? "Unparked" : "Parked";
88 write_int(unload_heads_path, seconds*1000);
89 syslog(LOG_INFO, "%s %s disk head\n", str, device_path);
92 static int on_ac(void)
94 /* /sys/class/power_supply/AC0/online */
98 static int lid_open(void)
100 /* /proc/acpi/button/lid/LID/state */
104 static void ignore_me(int signum)
110 int main(int argc, char **argv)
114 struct sched_param param;
117 ret = set_unload_heads_path("/dev/sda");
119 ret = set_unload_heads_path(argv[1]);
123 if (ret || !valid_disk()) {
124 fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n",
129 fd = open("/dev/freefall", O_RDONLY);
131 perror("/dev/freefall");
135 if (stat("/sys/class/leds/hp::hddprotect/brightness", &st))
138 if (daemon(0, 0) != 0) {
143 openlog(app_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
145 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
146 sched_setscheduler(0, SCHED_FIFO, ¶m);
147 mlockall(MCL_CURRENT|MCL_FUTURE);
149 signal(SIGALRM, ignore_me);
154 ret = read(fd, &count, sizeof(count));
156 if ((ret == -1) && (errno == EINTR)) {
157 /* Alarm expired, time to unpark the heads */
161 if (ret != sizeof(count)) {
168 if (1 || on_ac() || lid_open())