| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#include <stddef.h> |
|---|
| 25 |
#include <string.h> |
|---|
| 26 |
#include <stdio.h> |
|---|
| 27 |
#include <unistd.h> |
|---|
| 28 |
#include <sys/types.h> |
|---|
| 29 |
#include <sys/stat.h> |
|---|
| 30 |
#include <fcntl.h> |
|---|
| 31 |
#include <signal.h> |
|---|
| 32 |
#include <sys/time.h> |
|---|
| 33 |
|
|---|
| 34 |
#define PROC_FILE "/proc/sys/button" |
|---|
| 35 |
|
|---|
| 36 |
int proc_fd; |
|---|
| 37 |
int pipe_fds[2]; |
|---|
| 38 |
|
|---|
| 39 |
void alrm_handler(int signo) |
|---|
| 40 |
{ |
|---|
| 41 |
static char last; |
|---|
| 42 |
char c; |
|---|
| 43 |
lseek(proc_fd, 0, SEEK_SET); |
|---|
| 44 |
if (read(proc_fd, &c, 1) == 1) { |
|---|
| 45 |
if ((last == '1') && (c == '0')) { |
|---|
| 46 |
write(pipe_fds[1], "1", 1); |
|---|
| 47 |
} |
|---|
| 48 |
last = c; |
|---|
| 49 |
} |
|---|
| 50 |
else { |
|---|
| 51 |
write(pipe_fds[1], "q", 1); |
|---|
| 52 |
} |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
int main(void) |
|---|
| 56 |
{ |
|---|
| 57 |
struct sigaction sa; |
|---|
| 58 |
struct itimerval itv, itv0; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
if (pipe(pipe_fds) < 0) { |
|---|
| 64 |
perror("pipe"); |
|---|
| 65 |
return 1; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
proc_fd = open(PROC_FILE, O_RDONLY); |
|---|
| 72 |
if (proc_fd < 0) { |
|---|
| 73 |
perror(PROC_FILE); |
|---|
| 74 |
return 1; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
memset(&sa, 0, sizeof(sa)); |
|---|
| 81 |
sigemptyset(&sa.sa_mask); |
|---|
| 82 |
sa.sa_handler = alrm_handler; |
|---|
| 83 |
sa.sa_flags = SA_RESTART; |
|---|
| 84 |
if (sigaction(SIGALRM, &sa, NULL) < 0) { |
|---|
| 85 |
perror("sigaction"); |
|---|
| 86 |
return 1; |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
itv.it_interval.tv_sec = itv.it_value.tv_sec = 0; |
|---|
| 93 |
itv.it_interval.tv_usec = itv.it_value.tv_usec = 100000; |
|---|
| 94 |
itv0.it_interval.tv_sec = itv0.it_value.tv_sec = 0; |
|---|
| 95 |
itv0.it_interval.tv_usec = itv0.it_value.tv_usec = 0; |
|---|
| 96 |
if (setitimer(ITIMER_REAL, &itv, NULL) < 0) { |
|---|
| 97 |
perror("setitimer"); |
|---|
| 98 |
return 1; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
while (1) { |
|---|
| 102 |
char c = 0; |
|---|
| 103 |
if (read(pipe_fds[0], &c, 1) == 1) { |
|---|
| 104 |
if (c == '1') { |
|---|
| 105 |
char *cmd = "/sbin/hotplug button"; |
|---|
| 106 |
|
|---|
| 107 |
setitimer(ITIMER_REAL, &itv0, NULL); |
|---|
| 108 |
system(cmd); |
|---|
| 109 |
|
|---|
| 110 |
setitimer(ITIMER_REAL, &itv, NULL); |
|---|
| 111 |
} |
|---|
| 112 |
else if (c == 'q') { |
|---|
| 113 |
return 0; |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|