forked from floriandejonckheere/thinkalert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththinkalert.c
196 lines (164 loc) · 6.09 KB
/
thinkalert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*---------------------------------------------------------------------------
* ThinkAlert - A small program to manipulate the ThinkLight.
* Based on a "stupid little hack to blink the ThinkLight"
* http://paste.lisp.org/display/37500
*
* New features:
* - Interval argument is optional
* - Turn the light on or off (and leave it that way)
* - Specify separate values for on/off periods
* - Restores the light back to its initial state before terminating
* - Drops privileges after opening ThinkLight interface
*
*-------------------------------------------------------------------------*/
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
void blink(int, int, int);
void dropPrivs();
void lightOn();
void lightOff();
void restoreState();
void saveState();
void terminate(int);
const char filename[] = "/proc/acpi/ibm/light";
int initialState = 0; // Initial state of the ThinkLight
FILE *thinklight; // Handle to the ThinkLight proc interface.
#define DEFAULT_ON_PERIOD 250000
#define DEFAULT_OFF_PERIOD 250000
// ThinkAlert
int main(int argc, char *argv[]) {
int noBlink = 0; // Whether light should be blinked or switched
int onPeriod; // Duration of a blink (microseconds)
int offPeriod; // Interval between blinks (microseconds)
int switchOn = 0; // Whether light should be switched on or off
int times; // Number of times to blink
// Attempt to open the ThinkLight interface.
thinklight = fopen(filename, "r+");
if (!thinklight) {
perror("Unable to open the ThinkLight interface");
return errno;
}
dropPrivs();
switch (argc - 1) {
case 1:
if (!strcmp(argv[1], "on")) {
noBlink = 1;
switchOn = 1;
} else if (!strcmp(argv[1], "off")) {
noBlink = 1;
} else if (!strcmp(argv[1], "toggle")) {
saveState();
noBlink = 1;
if (initialState) {
switchOn = 0;
} else switchOn = 1;
}
else {
times = atoi(argv[1]);
onPeriod = DEFAULT_ON_PERIOD;
offPeriod = DEFAULT_OFF_PERIOD;
}
break;
case 2:
times = atoi(argv[1]);
onPeriod = offPeriod = atoi(argv[2]);
break;
case 3:
times = atoi(argv[1]);
onPeriod = atoi(argv[2]);
offPeriod = atoi(argv[3]);
break;
default:
printf("thinkalert <on|off|toggle>\n");
printf("thinkalert <times> [interval (microseconds)]\n");
printf("thinkalert <times> <on period (microseconds)> "
"<off period (microseconds)>\n");
fclose(thinklight);
exit(0);
}
// Just turn the light on or off and leave it that way.
if (noBlink) {
if (switchOn) lightOn();
else lightOff();
fclose(thinklight);
}
// Blink the light.
else {
saveState();
signal(SIGINT, terminate);
blink(times, onPeriod, offPeriod);
terminate(0);
}
return 0;
}
// Blink the light a number of times.
void blink(int times, int onPeriod, int offPeriod) {
if (!times) return; // Blink zero times.
int t = times;
while (t--) {
// Only shine the first time if the light was initially off.
if ((t < (times - 1)) || !initialState) {
lightOn();
usleep(onPeriod);
}
// Only shade the last time if the light was initially on.
if (t || initialState) {
lightOff();
usleep(offPeriod);
}
}
}
// Drop root privileges. This code is Linux specific. Adapted from Secure
// Programming Cookbook for C and C++.
void dropPrivs() {
gid_t newgid = getgid(), oldgid = getegid();
uid_t newuid = getuid(), olduid = geteuid();
// Drop ancillary group memberships.
if (!olduid) setgroups(1, &newgid);
// Set the effective gid to the real gid.
if ((newgid != oldgid) && (-1 == setregid(newgid, newgid))) abort();
// Set the effective uid to the real uid.
if ((newuid != olduid) && (-1 == setreuid(newuid, newuid))) abort();
// Verify that the changes were successful.
if (newgid != oldgid && (-1 != setegid(oldgid) || getegid() != newgid))
abort();
if (newuid != olduid && (-1 != seteuid(olduid) || geteuid() != newuid))
abort();
}
// Turn the light on.
void lightOn() {
rewind(thinklight);
fprintf(thinklight, "on");
fflush(thinklight);
}
// Turn the light off.
void lightOff() {
rewind(thinklight);
fprintf(thinklight, "off");
fflush(thinklight);
}
// Restore the initial state of the ThinkLight.
void restoreState() {
if (initialState) lightOn();
else lightOff();
}
// Get the initial state of the ThinkLight.
void saveState() {
char status;
fseek(thinklight, 10, SEEK_SET);
fread(&status, 1, 1, thinklight);
if ('n' == status) initialState = 1;
}
// Terminate the program.
void terminate(int sig) {
restoreState();
fclose(thinklight);
exit(sig);
}