|
Packit Service |
26469c |
/*
|
|
Packit Service |
26469c |
* inotify_handler.c - inotify Handler for New Devices
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* Watches /dev/input for new input layer device files.
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* Copyright (C) 2009, Ted Felix (www.tedfelix.com)
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* This program is free software; you can redistribute it and/or modify
|
|
Packit Service |
26469c |
* it under the terms of the GNU General Public License as published by
|
|
Packit Service |
26469c |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit Service |
26469c |
* (at your option) any later version.
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
26469c |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
26469c |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
26469c |
* GNU General Public License for more details.
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* You should have received a copy of the GNU General Public License
|
|
Packit Service |
26469c |
* along with this program; if not, write to the Free Software
|
|
Packit Service |
26469c |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
Packit Service |
26469c |
*
|
|
Packit Service |
26469c |
* (tabs at 4)
|
|
Packit Service |
26469c |
*/
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* system */
|
|
Packit Service |
26469c |
#include <sys/inotify.h>
|
|
Packit Service |
26469c |
#include <sys/select.h>
|
|
Packit Service |
26469c |
#include <sys/ioctl.h>
|
|
Packit Service |
26469c |
#include <unistd.h>
|
|
Packit Service |
26469c |
#include <stdio.h>
|
|
Packit Service |
26469c |
#include <errno.h>
|
|
Packit Service |
26469c |
#include <string.h>
|
|
Packit Service |
26469c |
#include <limits.h>
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* local */
|
|
Packit Service |
26469c |
#include "acpid.h"
|
|
Packit Service |
26469c |
#include "log.h"
|
|
Packit Service |
26469c |
#include "connection_list.h"
|
|
Packit Service |
26469c |
#include "input_layer.h"
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
#include "inotify_handler.h"
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/*-----------------------------------------------------------------*/
|
|
Packit Service |
26469c |
/* called when an inotify event is received */
|
|
Packit Service |
26469c |
static void process_inotify(int fd)
|
|
Packit Service |
26469c |
{
|
|
Packit Service |
26469c |
int bytes;
|
|
Packit Service |
26469c |
int processed_bytes = 0;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
char eventbuf[sizeof(struct inotify_event) + NAME_MAX + 1];
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
bytes = read(fd, &eventbuf, sizeof(eventbuf));
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify read bytes: %d", bytes);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* eof is not expected */
|
|
Packit Service |
26469c |
if (bytes == 0) {
|
|
Packit Service |
26469c |
acpid_log(LOG_WARNING, "inotify fd eof encountered");
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
else if (bytes < 0) {
|
|
Packit Service |
26469c |
/* EINVAL means buffer wasn't big enough. See inotify(7). */
|
|
Packit Service |
26469c |
acpid_log(LOG_ERR, "inotify read error: %s (%d)",
|
|
Packit Service |
26469c |
strerror(errno), errno);
|
|
Packit Service |
26469c |
acpid_log(LOG_ERR, "disconnecting from inotify");
|
|
Packit Service |
26469c |
delete_connection(fd);
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
const int dnsize = NAME_MAX + 1;
|
|
Packit Service |
26469c |
char devname[dnsize];
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* while there are still messages in eventbuf */
|
|
Packit Service |
26469c |
while (processed_bytes < bytes) {
|
|
Packit Service |
26469c |
struct inotify_event* curevent = (struct inotify_event *)
|
|
Packit Service |
26469c |
&eventbuf[processed_bytes];
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify name len: %d", curevent->len);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* if a name is included */
|
|
Packit Service |
26469c |
if (curevent->len > 0) {
|
|
Packit Service |
26469c |
/* devname = ACPID_INPUTLAYERDIR + "/" + pevent -> name */
|
|
Packit Service |
26469c |
strcpy(devname, ACPID_INPUTLAYERDIR);
|
|
Packit Service |
26469c |
strcat(devname, "/");
|
|
Packit Service |
26469c |
strncat(devname, curevent->name, dnsize - strlen(devname) - 1);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* if this is a create */
|
|
Packit Service |
26469c |
if (curevent->mask & IN_CREATE) {
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify about to open: %s", devname);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
open_inputfile(devname);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* if this is a delete */
|
|
Packit Service |
26469c |
if (curevent->mask & IN_DELETE) {
|
|
Packit Service |
26469c |
/* struct connection *c; */
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify received a delete for: %s", devname);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
#if 0
|
|
Packit Service |
26469c |
/* Switching back to the original ENODEV detection scheme. See
|
|
Packit Service |
26469c |
process_input() in input_layer.c. */
|
|
Packit Service |
26469c |
/* keeping this for future reference */
|
|
Packit Service |
26469c |
/* search for the event file in the connection list */
|
|
Packit Service |
26469c |
/* ??? Or should we just have a delete_connection_name()? */
|
|
Packit Service |
26469c |
c = find_connection_name(devname);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* close that connection if found */
|
|
Packit Service |
26469c |
if (c)
|
|
Packit Service |
26469c |
delete_connection(c->fd);
|
|
Packit Service |
26469c |
#endif
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
processed_bytes += sizeof(struct inotify_event) + curevent->len;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/*-----------------------------------------------------------------*/
|
|
Packit Service |
26469c |
/* Set up an inotify watch on /dev/input. */
|
|
Packit Service |
26469c |
void open_inotify(void)
|
|
Packit Service |
26469c |
{
|
|
Packit Service |
26469c |
int fd = -1;
|
|
Packit Service |
26469c |
int wd = -1;
|
|
Packit Service |
26469c |
struct connection c;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* set up inotify */
|
|
Packit Service |
26469c |
fd = inotify_init1(IN_CLOEXEC);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if (fd < 0) {
|
|
Packit Service |
26469c |
acpid_log(LOG_ERR, "inotify_init() failed: %s (%d)",
|
|
Packit Service |
26469c |
strerror(errno), errno);
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify fd: %d", fd);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* watch for files being created or deleted in /dev/input */
|
|
Packit Service |
26469c |
wd = inotify_add_watch(fd, ACPID_INPUTLAYERDIR, IN_CREATE | IN_DELETE);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if (wd < 0) {
|
|
Packit Service |
26469c |
if (errno == ENOENT) {
|
|
Packit Service |
26469c |
/* Common with headless devices. */
|
|
Packit Service |
26469c |
acpid_log(LOG_WARNING, "inotify_add_watch(): input layer not found");
|
|
Packit Service |
26469c |
} else {
|
|
Packit Service |
26469c |
acpid_log(LOG_ERR, "inotify_add_watch() failed: %s (%d)",
|
|
Packit Service |
26469c |
strerror(errno), errno);
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
close(fd);
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
acpid_log(LOG_DEBUG, "inotify wd: %d", wd);
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
/* add a connection to the list */
|
|
Packit Service |
26469c |
c.fd = fd;
|
|
Packit Service |
26469c |
c.process = process_inotify;
|
|
Packit Service |
26469c |
c.pathname = NULL;
|
|
Packit Service |
26469c |
c.kybd = 0;
|
|
Packit Service |
26469c |
|
|
Packit Service |
26469c |
if (add_connection(&c) < 0) {
|
|
Packit Service |
26469c |
close(fd);
|
|
Packit Service |
26469c |
acpid_log(LOG_ERR, "can't add connection for inotify");
|
|
Packit Service |
26469c |
return;
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
}
|
|
Packit Service |
26469c |
|