|
Packit |
a94d48 |
/*
|
|
Packit |
a94d48 |
* inotify_handler.c - inotify Handler for New Devices
|
|
Packit |
a94d48 |
*
|
|
Packit |
a94d48 |
* Watches /dev/input for new input layer device files.
|
|
Packit |
a94d48 |
*
|
|
Packit |
a94d48 |
* Copyright (C) 2009, Ted Felix (www.tedfelix.com)
|
|
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 |
* (tabs at 4)
|
|
Packit |
a94d48 |
*/
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* system */
|
|
Packit |
a94d48 |
#include <sys/inotify.h>
|
|
Packit |
a94d48 |
#include <sys/select.h>
|
|
Packit |
a94d48 |
#include <sys/ioctl.h>
|
|
Packit |
a94d48 |
#include <unistd.h>
|
|
Packit |
a94d48 |
#include <stdio.h>
|
|
Packit |
a94d48 |
#include <errno.h>
|
|
Packit |
a94d48 |
#include <string.h>
|
|
Packit |
a94d48 |
#include <limits.h>
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* local */
|
|
Packit |
a94d48 |
#include "acpid.h"
|
|
Packit |
a94d48 |
#include "log.h"
|
|
Packit |
a94d48 |
#include "connection_list.h"
|
|
Packit |
a94d48 |
#include "input_layer.h"
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
#include "inotify_handler.h"
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/*-----------------------------------------------------------------*/
|
|
Packit |
a94d48 |
/* called when an inotify event is received */
|
|
Packit |
a94d48 |
static void process_inotify(int fd)
|
|
Packit |
a94d48 |
{
|
|
Packit |
a94d48 |
int bytes;
|
|
Packit |
a94d48 |
int processed_bytes = 0;
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
char eventbuf[sizeof(struct inotify_event) + NAME_MAX + 1];
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
bytes = read(fd, &eventbuf, sizeof(eventbuf));
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify read bytes: %d", bytes);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* eof is not expected */
|
|
Packit |
a94d48 |
if (bytes == 0) {
|
|
Packit |
a94d48 |
acpid_log(LOG_WARNING, "inotify fd eof encountered");
|
|
Packit |
a94d48 |
return;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
else if (bytes < 0) {
|
|
Packit |
a94d48 |
/* EINVAL means buffer wasn't big enough. See inotify(7). */
|
|
Packit |
a94d48 |
acpid_log(LOG_ERR, "inotify read error: %s (%d)",
|
|
Packit |
a94d48 |
strerror(errno), errno);
|
|
Packit |
a94d48 |
acpid_log(LOG_ERR, "disconnecting from inotify");
|
|
Packit |
a94d48 |
delete_connection(fd);
|
|
Packit |
a94d48 |
return;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
const int dnsize = NAME_MAX + 1;
|
|
Packit |
a94d48 |
char devname[dnsize];
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* while there are still messages in eventbuf */
|
|
Packit |
a94d48 |
while (processed_bytes < bytes) {
|
|
Packit |
a94d48 |
struct inotify_event* curevent = (struct inotify_event *)
|
|
Packit |
a94d48 |
&eventbuf[processed_bytes];
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify name len: %d", curevent->len);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* if a name is included */
|
|
Packit |
a94d48 |
if (curevent->len > 0) {
|
|
Packit |
a94d48 |
/* devname = ACPID_INPUTLAYERDIR + "/" + pevent -> name */
|
|
Packit |
a94d48 |
strcpy(devname, ACPID_INPUTLAYERDIR);
|
|
Packit |
a94d48 |
strcat(devname, "/");
|
|
Packit |
a94d48 |
strncat(devname, curevent->name, dnsize - strlen(devname) - 1);
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* if this is a create */
|
|
Packit |
a94d48 |
if (curevent->mask & IN_CREATE) {
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify about to open: %s", devname);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
open_inputfile(devname);
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* if this is a delete */
|
|
Packit |
a94d48 |
if (curevent->mask & IN_DELETE) {
|
|
Packit |
a94d48 |
/* struct connection *c; */
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify received a delete for: %s", devname);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
#if 0
|
|
Packit |
a94d48 |
/* Switching back to the original ENODEV detection scheme. See
|
|
Packit |
a94d48 |
process_input() in input_layer.c. */
|
|
Packit |
a94d48 |
/* keeping this for future reference */
|
|
Packit |
a94d48 |
/* search for the event file in the connection list */
|
|
Packit |
a94d48 |
/* ??? Or should we just have a delete_connection_name()? */
|
|
Packit |
a94d48 |
c = find_connection_name(devname);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* close that connection if found */
|
|
Packit |
a94d48 |
if (c)
|
|
Packit |
a94d48 |
delete_connection(c->fd);
|
|
Packit |
a94d48 |
#endif
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
processed_bytes += sizeof(struct inotify_event) + curevent->len;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/*-----------------------------------------------------------------*/
|
|
Packit |
a94d48 |
/* Set up an inotify watch on /dev/input. */
|
|
Packit |
a94d48 |
void open_inotify(void)
|
|
Packit |
a94d48 |
{
|
|
Packit |
a94d48 |
int fd = -1;
|
|
Packit |
a94d48 |
int wd = -1;
|
|
Packit |
a94d48 |
struct connection c;
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* set up inotify */
|
|
Packit |
a94d48 |
fd = inotify_init1(IN_CLOEXEC);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
if (fd < 0) {
|
|
Packit |
a94d48 |
acpid_log(LOG_ERR, "inotify_init() failed: %s (%d)",
|
|
Packit |
a94d48 |
strerror(errno), errno);
|
|
Packit |
a94d48 |
return;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify fd: %d", fd);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* watch for files being created or deleted in /dev/input */
|
|
Packit |
a94d48 |
wd = inotify_add_watch(fd, ACPID_INPUTLAYERDIR, IN_CREATE | IN_DELETE);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
if (wd < 0) {
|
|
Packit |
a94d48 |
if (errno == ENOENT) {
|
|
Packit |
a94d48 |
/* Common with headless devices. */
|
|
Packit |
a94d48 |
acpid_log(LOG_WARNING, "inotify_add_watch(): input layer not found");
|
|
Packit |
a94d48 |
} else {
|
|
Packit |
a94d48 |
acpid_log(LOG_ERR, "inotify_add_watch() failed: %s (%d)",
|
|
Packit |
a94d48 |
strerror(errno), errno);
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
close(fd);
|
|
Packit |
a94d48 |
return;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
acpid_log(LOG_DEBUG, "inotify wd: %d", wd);
|
|
Packit |
a94d48 |
|
|
Packit |
a94d48 |
/* add a connection to the list */
|
|
Packit |
a94d48 |
c.fd = fd;
|
|
Packit |
a94d48 |
c.process = process_inotify;
|
|
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 inotify");
|
|
Packit |
a94d48 |
return;
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
}
|
|
Packit |
a94d48 |
|