blob: 5e44b20b18481123aad30d32dd5113418d2e2919 [file] [log] [blame]
Pali Rohár3a57cc52014-07-12 09:56:22 -07001/* Disk protection for HP/DELL machines.
Pavel Machekef2cfc72009-02-18 14:48:23 -08002 *
3 * Copyright 2008 Eric Piel
Pavel Macheka2531292010-07-18 14:27:13 +02004 * Copyright 2009 Pavel Machek <pavel@ucw.cz>
Pali Rohár3a57cc52014-07-12 09:56:22 -07005 * Copyright 2012 Sonal Santan
6 * Copyright 2014 Pali Rohár <pali.rohar@gmail.com>
Pavel Machekef2cfc72009-02-18 14:48:23 -08007 *
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 Thaeter2bace8b2009-07-25 20:55:15 +020021#include <sys/mman.h>
22#include <sched.h>
Pali Rohár3a57cc52014-07-12 09:56:22 -070023#include <syslog.h>
Pavel Machekef2cfc72009-02-18 14:48:23 -080024
Pali Rohár3a57cc52014-07-12 09:56:22 -070025static int noled;
26static char unload_heads_path[64];
27static char device_path[32];
28static const char app_name[] = "FREE FALL";
Frans Popbe3990b2009-07-25 21:00:12 +020029
Pali Rohár3a57cc52014-07-12 09:56:22 -070030static int set_unload_heads_path(char *device)
Frans Popbe3990b2009-07-25 21:00:12 +020031{
Frans Popbe3990b2009-07-25 21:00:12 +020032 if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
33 return -EINVAL;
Pali Rohár3a57cc52014-07-12 09:56:22 -070034 strncpy(device_path, device, sizeof(device_path) - 1);
Frans Popbe3990b2009-07-25 21:00:12 +020035
Stefan Weil6e641c92013-12-30 12:35:43 +010036 snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
Pavel Machekd74aae42014-08-05 23:32:41 -070037 "/sys/block/%s/device/unload_heads", device+5);
Frans Popbe3990b2009-07-25 21:00:12 +020038 return 0;
39}
Pali Rohár3a57cc52014-07-12 09:56:22 -070040
41static int valid_disk(void)
Frans Popbe3990b2009-07-25 21:00:12 +020042{
43 int fd = open(unload_heads_path, O_RDONLY);
Pali Rohár3a57cc52014-07-12 09:56:22 -070044
Frans Popbe3990b2009-07-25 21:00:12 +020045 if (fd < 0) {
46 perror(unload_heads_path);
47 return 0;
48 }
49
50 close(fd);
51 return 1;
52}
53
Pali Rohár3a57cc52014-07-12 09:56:22 -070054static void write_int(char *path, int i)
Pavel Machekef2cfc72009-02-18 14:48:23 -080055{
56 char buf[1024];
57 int fd = open(path, O_RDWR);
Pali Rohár3a57cc52014-07-12 09:56:22 -070058
Pavel Machekef2cfc72009-02-18 14:48:23 -080059 if (fd < 0) {
60 perror("open");
61 exit(1);
62 }
Pali Rohár3a57cc52014-07-12 09:56:22 -070063
Pavel Machekef2cfc72009-02-18 14:48:23 -080064 sprintf(buf, "%d", i);
Pali Rohár3a57cc52014-07-12 09:56:22 -070065
Pavel Machekef2cfc72009-02-18 14:48:23 -080066 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
67 perror("write");
68 exit(1);
69 }
Pali Rohár3a57cc52014-07-12 09:56:22 -070070
Pavel Machekef2cfc72009-02-18 14:48:23 -080071 close(fd);
72}
73
Pali Rohár3a57cc52014-07-12 09:56:22 -070074static void set_led(int on)
Pavel Machekef2cfc72009-02-18 14:48:23 -080075{
Pali Rohár3a57cc52014-07-12 09:56:22 -070076 if (noled)
77 return;
Pavel Machekef2cfc72009-02-18 14:48:23 -080078 write_int("/sys/class/leds/hp::hddprotect/brightness", on);
79}
80
Pali Rohár3a57cc52014-07-12 09:56:22 -070081static void protect(int seconds)
Pavel Machekef2cfc72009-02-18 14:48:23 -080082{
Pali Rohár3a57cc52014-07-12 09:56:22 -070083 const char *str = (seconds == 0) ? "Unparked" : "Parked";
84
Frans Popbe3990b2009-07-25 21:00:12 +020085 write_int(unload_heads_path, seconds*1000);
Pali Rohár3a57cc52014-07-12 09:56:22 -070086 syslog(LOG_INFO, "%s %s disk head\n", str, device_path);
Pavel Machekef2cfc72009-02-18 14:48:23 -080087}
88
Pali Rohár3a57cc52014-07-12 09:56:22 -070089static int on_ac(void)
Pavel Machekef2cfc72009-02-18 14:48:23 -080090{
Pali Rohár3a57cc52014-07-12 09:56:22 -070091 /* /sys/class/power_supply/AC0/online */
92 return 1;
Pavel Machekef2cfc72009-02-18 14:48:23 -080093}
94
Pali Rohár3a57cc52014-07-12 09:56:22 -070095static int lid_open(void)
Pavel Machekef2cfc72009-02-18 14:48:23 -080096{
Pali Rohár3a57cc52014-07-12 09:56:22 -070097 /* /proc/acpi/button/lid/LID/state */
98 return 1;
Pavel Machekef2cfc72009-02-18 14:48:23 -080099}
100
Pali Rohár3a57cc52014-07-12 09:56:22 -0700101static void ignore_me(int signum)
Pavel Machekef2cfc72009-02-18 14:48:23 -0800102{
103 protect(0);
104 set_led(0);
Pavel Machekef2cfc72009-02-18 14:48:23 -0800105}
106
Frans Popbe3990b2009-07-25 21:00:12 +0200107int main(int argc, char **argv)
Pavel Machekef2cfc72009-02-18 14:48:23 -0800108{
Frans Popb519c152009-06-22 11:08:36 +0200109 int fd, ret;
Pali Rohár3a57cc52014-07-12 09:56:22 -0700110 struct stat st;
Christian Thaeter2bace8b2009-07-25 20:55:15 +0200111 struct sched_param param;
Pavel Machekef2cfc72009-02-18 14:48:23 -0800112
Frans Popbe3990b2009-07-25 21:00:12 +0200113 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 Popb519c152009-06-22 11:08:36 +0200126 fd = open("/dev/freefall", O_RDONLY);
127 if (fd < 0) {
Frans Popbe3990b2009-07-25 21:00:12 +0200128 perror("/dev/freefall");
Frans Popb519c152009-06-22 11:08:36 +0200129 return EXIT_FAILURE;
130 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800131
Pali Rohár3a57cc52014-07-12 09:56:22 -0700132 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 Thaeter2bace8b2009-07-25 20:55:15 +0200142 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
143 sched_setscheduler(0, SCHED_FIFO, &param);
144 mlockall(MCL_CURRENT|MCL_FUTURE);
145
Pavel Machekef2cfc72009-02-18 14:48:23 -0800146 signal(SIGALRM, ignore_me);
147
Frans Popb519c152009-06-22 11:08:36 +0200148 for (;;) {
149 unsigned char count;
Pavel Machekef2cfc72009-02-18 14:48:23 -0800150
Frans Popb519c152009-06-22 11:08:36 +0200151 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 Machekef2cfc72009-02-18 14:48:23 -0800157
Frans Popb519c152009-06-22 11:08:36 +0200158 if (ret != sizeof(count)) {
159 perror("read");
160 break;
161 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800162
Frans Popb519c152009-06-22 11:08:36 +0200163 protect(21);
164 set_led(1);
165 if (1 || on_ac() || lid_open())
166 alarm(2);
167 else
168 alarm(20);
169 }
Pavel Machekef2cfc72009-02-18 14:48:23 -0800170
Pali Rohár3a57cc52014-07-12 09:56:22 -0700171 closelog();
Frans Popb519c152009-06-22 11:08:36 +0200172 close(fd);
173 return EXIT_SUCCESS;
Pavel Machekef2cfc72009-02-18 14:48:23 -0800174}