Blame proc.c

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