Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[sfrench/cifs-2.6.git] / Documentation / laptops / freefall.c
1 /* Disk protection for HP/DELL machines.
2  *
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>
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>
21 #include <sys/mman.h>
22 #include <sched.h>
23 #include <syslog.h>
24
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";
29
30 static int set_unload_heads_path(char *device)
31 {
32         char devname[64];
33
34         if (strlen(device) <= 5 || strncmp(device, "/dev/", 5) != 0)
35                 return -EINVAL;
36         strncpy(devname, device + 5, sizeof(devname) - 1);
37         devname[sizeof(devname) - 1] = '\0';
38         strncpy(device_path, device, sizeof(device_path) - 1);
39
40         snprintf(unload_heads_path, sizeof(unload_heads_path) - 1,
41                                 "/sys/block/%s/device/unload_heads", devname);
42         return 0;
43 }
44
45 static int valid_disk(void)
46 {
47         int fd = open(unload_heads_path, O_RDONLY);
48
49         if (fd < 0) {
50                 perror(unload_heads_path);
51                 return 0;
52         }
53
54         close(fd);
55         return 1;
56 }
57
58 static void write_int(char *path, int i)
59 {
60         char buf[1024];
61         int fd = open(path, O_RDWR);
62
63         if (fd < 0) {
64                 perror("open");
65                 exit(1);
66         }
67
68         sprintf(buf, "%d", i);
69
70         if (write(fd, buf, strlen(buf)) != strlen(buf)) {
71                 perror("write");
72                 exit(1);
73         }
74
75         close(fd);
76 }
77
78 static void set_led(int on)
79 {
80         if (noled)
81                 return;
82         write_int("/sys/class/leds/hp::hddprotect/brightness", on);
83 }
84
85 static void protect(int seconds)
86 {
87         const char *str = (seconds == 0) ? "Unparked" : "Parked";
88
89         write_int(unload_heads_path, seconds*1000);
90         syslog(LOG_INFO, "%s %s disk head\n", str, device_path);
91 }
92
93 static int on_ac(void)
94 {
95         /* /sys/class/power_supply/AC0/online */
96         return 1;
97 }
98
99 static int lid_open(void)
100 {
101         /* /proc/acpi/button/lid/LID/state */
102         return 1;
103 }
104
105 static void ignore_me(int signum)
106 {
107         protect(0);
108         set_led(0);
109 }
110
111 int main(int argc, char **argv)
112 {
113         int fd, ret;
114         struct stat st;
115         struct sched_param param;
116
117         if (argc == 1)
118                 ret = set_unload_heads_path("/dev/sda");
119         else if (argc == 2)
120                 ret = set_unload_heads_path(argv[1]);
121         else
122                 ret = -EINVAL;
123
124         if (ret || !valid_disk()) {
125                 fprintf(stderr, "usage: %s <device> (default: /dev/sda)\n",
126                                 argv[0]);
127                 exit(1);
128         }
129
130         fd = open("/dev/freefall", O_RDONLY);
131         if (fd < 0) {
132                 perror("/dev/freefall");
133                 return EXIT_FAILURE;
134         }
135
136         if (stat("/sys/class/leds/hp::hddprotect/brightness", &st))
137                 noled = 1;
138
139         if (daemon(0, 0) != 0) {
140                 perror("daemon");
141                 return EXIT_FAILURE;
142         }
143
144         openlog(app_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
145
146         param.sched_priority = sched_get_priority_max(SCHED_FIFO);
147         sched_setscheduler(0, SCHED_FIFO, &param);
148         mlockall(MCL_CURRENT|MCL_FUTURE);
149
150         signal(SIGALRM, ignore_me);
151
152         for (;;) {
153                 unsigned char count;
154
155                 ret = read(fd, &count, sizeof(count));
156                 alarm(0);
157                 if ((ret == -1) && (errno == EINTR)) {
158                         /* Alarm expired, time to unpark the heads */
159                         continue;
160                 }
161
162                 if (ret != sizeof(count)) {
163                         perror("read");
164                         break;
165                 }
166
167                 protect(21);
168                 set_led(1);
169                 if (1 || on_ac() || lid_open())
170                         alarm(2);
171                 else
172                         alarm(20);
173         }
174
175         closelog();
176         close(fd);
177         return EXIT_SUCCESS;
178 }