/
proc.c
222 lines (199 loc) · 4.76 KB
/
proc.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* proc.c - ACPI daemon proc filesystem interface
*
* Portions Copyright (C) 2000 Andrew Henroid
* Portions Copyright (C) 2001 Sun Microsystems
* Portions Copyright (C) 2004 Tim Hockin (thockin@hockin.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "acpid.h"
#include "log.h"
#include "event.h"
#include "connection_list.h"
#include "libc_compat.h"
#include "proc.h"
const char *eventfile = ACPID_EVENTFILE;
static char *read_line(int fd);
static void
process_proc(int fd)
{
char *event;
/* read an event */
event = read_line(fd);
/* if we're locked, don't process the event */
if (locked()) {
if (logevents && event != NULL) {
acpid_log(LOG_INFO,
"lockfile present, not processing "
"event \"%s\"", event);
}
return;
}
/* handle the event */
if (event) {
if (logevents) {
acpid_log(LOG_INFO,
"procfs received event \"%s\"", event);
}
acpid_handle_event(event);
if (logevents) {
acpid_log(LOG_INFO,
"procfs completed event \"%s\"", event);
}
} else if (errno == EPIPE) {
acpid_log(LOG_WARNING,
"events file connection closed");
exit(EXIT_FAILURE);
} else {
static int nerrs;
if (++nerrs >= ACPID_MAX_ERRS) {
acpid_log(LOG_ERR,
"too many errors reading "
"events file - aborting");
exit(EXIT_FAILURE);
}
}
}
int
open_proc()
{
int fd;
struct connection c;
/* O_CLOEXEC: Make sure scripts we exec() (in event.c) don't get our file
descriptors. */
fd = open(eventfile, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
if (errno == ENOENT) {
acpid_log(LOG_DEBUG, "Deprecated %s was not found. "
"Trying netlink and the input layer...", eventfile);
} else {
acpid_log(LOG_ERR, "can't open %s: %s (%d)", eventfile,
strerror(errno), errno);
}
return -1;
}
acpid_log(LOG_DEBUG, "proc fs opened successfully");
/* add a connection to the list */
c.fd = fd;
c.process = process_proc;
c.pathname = NULL;
c.kybd = 0;
if (add_connection(&c) < 0) {
close(fd);
acpid_log(LOG_ERR, "can't add connection for %s", eventfile);
return -1;
}
return 0;
}
/*
* This depends on fixes in linux ACPI after 2.4.8
*/
#define BUFLEN 1024
static char *
read_line(int fd)
{
static char buf[BUFLEN];
int i = 0;
int r;
int searching = 1;
while (searching) {
memset(buf+i, 0, BUFLEN-i);
/* only go to BUFLEN-1 so there will always be a 0 at the end */
while (i < BUFLEN-1) {
r = TEMP_FAILURE_RETRY(read(fd, buf+i, 1));
if (r < 0) {
/* we should do something with the data */
acpid_log(LOG_ERR, "read(): %s",
strerror(errno));
return NULL;
} else if (r == 0) {
/* signal this in an almost standard way */
errno = EPIPE;
return NULL;
} else if (r == 1) {
/* scan for a newline */
if (buf[i] == '\n') {
searching = 0;
buf[i] = '\0';
break;
}
i++;
}
}
if (i >= BUFLEN - 1)
break;
}
return buf;
}
#if 0
/* This version leaks memory. The above version is simpler and leak-free. */
/* Downside is that the above version always uses 1k of RAM. */
/*
* This depends on fixes in linux ACPI after 2.4.8
*/
#define MAX_BUFLEN 1024
static char *
read_line(int fd)
{
static char *buf;
int buflen = 64;
int i = 0;
int r;
int searching = 1;
while (searching) {
/* ??? This memory is leaked since it is never freed */
buf = realloc(buf, buflen);
if (!buf) {
acpid_log(LOG_ERR, "malloc(%d): %s",
buflen, strerror(errno));
return NULL;
}
memset(buf+i, 0, buflen-i);
while (i < buflen) {
r = read(fd, buf+i, 1);
if (r < 0 && errno != EINTR) {
/* we should do something with the data */
acpid_log(LOG_ERR, "read(): %s",
strerror(errno));
return NULL;
} else if (r == 0) {
/* signal this in an almost standard way */
errno = EPIPE;
return NULL;
} else if (r == 1) {
/* scan for a newline */
if (buf[i] == '\n') {
searching = 0;
buf[i] = '\0';
break;
}
i++;
}
}
if (buflen >= MAX_BUFLEN) {
break;
}
buflen *= 2;
}
return buf;
}
#endif