2 * proc.c - ACPI daemon proc filesystem interface
4 * Portions Copyright (C) 2000 Andrew Henroid
5 * Portions Copyright (C) 2001 Sun Microsystems
6 * Portions Copyright (C) 2004 Tim Hockin (thockin@hockin.org)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "connection_list.h"
35 const char *eventfile = ACPID_EVENTFILE;
37 static char *read_line(int fd);
45 event = read_line(fd);
47 /* if we're locked, don't process the event */
49 if (logevents && event != NULL) {
51 "lockfile present, not processing "
52 "event \"%s\"", event);
57 /* handle the event */
61 "procfs received event \"%s\"", event);
63 acpid_handle_event(event);
66 "procfs completed event \"%s\"", event);
68 } else if (errno == EPIPE) {
69 acpid_log(LOG_WARNING,
70 "events file connection closed");
74 if (++nerrs >= ACPID_MAX_ERRS) {
76 "too many errors reading "
77 "events file - aborting");
89 fd = open(eventfile, O_RDONLY);
91 if (errno == ENOENT) {
92 acpid_log(LOG_DEBUG, "Deprecated %s was not found. "
93 "Trying netlink and the input layer...", eventfile);
95 acpid_log(LOG_ERR, "can't open %s: %s (%d)", eventfile,
96 strerror(errno), errno);
102 /* Make sure scripts we exec() (in event.c) don't get our file
104 fcntl(fd, F_SETFD, FD_CLOEXEC);
106 acpid_log(LOG_DEBUG, "proc fs opened successfully");
108 /* add a connection to the list */
110 c.process = process_proc;
119 * This depends on fixes in linux ACPI after 2.4.8
125 static char buf[BUFLEN];
131 memset(buf+i, 0, BUFLEN-i);
133 /* only go to BUFLEN-1 so there will always be a 0 at the end */
134 while (i < BUFLEN-1) {
135 r = read(fd, buf+i, 1);
136 if (r < 0 && errno != EINTR) {
137 /* we should do something with the data */
138 acpid_log(LOG_ERR, "read(): %s",
142 /* signal this in an almost standard way */
146 /* scan for a newline */
147 if (buf[i] == '\n') {
163 /* This version leaks memory. The above version is simpler and leak-free. */
164 /* Downside is that the above version always uses 1k of RAM. */
166 * This depends on fixes in linux ACPI after 2.4.8
168 #define MAX_BUFLEN 1024
179 /* ??? This memory is leaked since it is never freed */
180 buf = realloc(buf, buflen);
182 acpid_log(LOG_ERR, "malloc(%d): %s",
183 buflen, strerror(errno));
186 memset(buf+i, 0, buflen-i);
189 r = read(fd, buf+i, 1);
190 if (r < 0 && errno != EINTR) {
191 /* we should do something with the data */
192 acpid_log(LOG_ERR, "read(): %s",
196 /* signal this in an almost standard way */
200 /* scan for a newline */
201 if (buf[i] == '\n') {
209 if (buflen >= MAX_BUFLEN) {