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
)
32 if (strlen(device
) <= 5 || strncmp(device
, "/dev/", 5) != 0)
34 strncpy(device_path
, device
, sizeof(device_path
) - 1);
36 snprintf(unload_heads_path
, sizeof(unload_heads_path
) - 1,
37 "/sys/block/%s/device/unload_heads", device
+5);
41 static int valid_disk(void)
43 int fd
= open(unload_heads_path
, O_RDONLY
);
46 perror(unload_heads_path
);
54 static void write_int(char *path
, int i
)
57 int fd
= open(path
, O_RDWR
);
64 sprintf(buf
, "%d", i
);
66 if (write(fd
, buf
, strlen(buf
)) != strlen(buf
)) {
74 static void set_led(int on
)
78 write_int("/sys/class/leds/hp::hddprotect/brightness", on
);
81 static void protect(int seconds
)
83 const char *str
= (seconds
== 0) ? "Unparked" : "Parked";
85 write_int(unload_heads_path
, seconds
*1000);
86 syslog(LOG_INFO
, "%s %s disk head\n", str
, device_path
);
89 static int on_ac(void)
91 /* /sys/class/power_supply/AC0/online */
95 static int lid_open(void)
97 /* /proc/acpi/button/lid/LID/state */
101 static void ignore_me(int signum
)
107 int main(int argc
, char **argv
)
111 struct sched_param param
;
114 ret
= set_unload_heads_path("/dev/sda");
116 ret
= set_unload_heads_path(argv
[1]);
120 if (ret
|| !valid_disk()) {
121 fprintf(stderr
, "usage: %s <device> (default: /dev/sda)\n",
126 fd
= open("/dev/freefall", O_RDONLY
);
128 perror("/dev/freefall");
132 if (stat("/sys/class/leds/hp::hddprotect/brightness", &st
))
135 if (daemon(0, 0) != 0) {
140 openlog(app_name
, LOG_CONS
| LOG_PID
| LOG_NDELAY
, LOG_LOCAL1
);
142 param
.sched_priority
= sched_get_priority_max(SCHED_FIFO
);
143 sched_setscheduler(0, SCHED_FIFO
, ¶m
);
144 mlockall(MCL_CURRENT
|MCL_FUTURE
);
146 signal(SIGALRM
, ignore_me
);
151 ret
= read(fd
, &count
, sizeof(count
));
153 if ((ret
== -1) && (errno
== EINTR
)) {
154 /* Alarm expired, time to unpark the heads */
158 if (ret
!= sizeof(count
)) {
165 if (1 || on_ac() || lid_open())