238 lines
5.3 KiB
C
238 lines
5.3 KiB
C
/* Shared helpers for the compiled Bananarchy idle daemons. */
|
|||
|
|
#ifndef BANANARCHY_DAEMONS_COMMON_H
|
||
|
|
#define BANANARCHY_DAEMONS_COMMON_H
|
||
|
|
|
||
|
|
#ifndef _GNU_SOURCE
|
||
|
|
#define _GNU_SOURCE
|
||
|
|
#endif
|
||
|
|
#include <errno.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <glob.h>
|
||
|
|
#include <signal.h>
|
||
|
|
#include <sys/time.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
#include <sys/file.h>
|
||
|
|
#include <sys/ioctl.h>
|
||
|
|
#include <sys/stat.h>
|
||
|
|
#include <sys/types.h>
|
||
|
|
#include <sys/wait.h>
|
||
|
|
#include <time.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
|
||
|
|
#define WATCH_MAX 32
|
||
|
|
#define SESSION_UID 10000
|
||
|
|
#define SESSION_GID 10000
|
||
|
|
#define BA_UNUSED __attribute__((unused))
|
||
|
|
|
||
|
|
#define EV_KEY 0x01
|
||
|
|
#define EV_SW 0x05
|
||
|
|
#define KEY_MAX 0x2ff
|
||
|
|
#define SW_LID 0x00
|
||
|
|
#define SW_KEYPAD_SLIDE 0x0a
|
||
|
|
#define KEY_VOLUMEDOWN 114
|
||
|
|
#define KEY_VOLUMEUP 115
|
||
|
|
#define BA_IOC(dir, type, nr, size) \
|
||
|
|
(((unsigned int)(dir) << 30) | ((unsigned int)(type) << 8) | \
|
||
|
|
((unsigned int)(nr) << 0) | ((unsigned int)(size) << 16))
|
||
|
|
#define BA_EVIOCGBIT(ev, len) \
|
||
|
|
BA_IOC(2U, 'E', 0x20U + (unsigned int)(ev), (unsigned int)(len))
|
||
|
|
|
||
|
|
struct input_event {
|
||
|
|
struct timeval time;
|
||
|
|
uint16_t type;
|
||
|
|
uint16_t code;
|
||
|
|
int32_t value;
|
||
|
|
};
|
||
|
|
|
||
|
|
enum watch_filter {
|
||
|
|
WATCH_ALL = 0,
|
||
|
|
WATCH_SWITCHES,
|
||
|
|
WATCH_VOLUME_KEYS,
|
||
|
|
};
|
||
|
|
|
||
|
|
struct watch_dev {
|
||
|
|
int fd;
|
||
|
|
ino_t ino;
|
||
|
|
char path[64];
|
||
|
|
};
|
||
|
|
|
||
|
|
struct watch_list {
|
||
|
|
struct watch_dev devs[WATCH_MAX];
|
||
|
|
int count;
|
||
|
|
};
|
||
|
|
|
||
|
|
static BA_UNUSED void safe_copy(char *dst, size_t len, const char *src)
|
||
|
|
{
|
||
|
|
size_t n;
|
||
|
|
if(len == 0)
|
||
|
|
return;
|
||
|
|
n = strlen(src);
|
||
|
|
if(n >= len)
|
||
|
|
n = len - 1;
|
||
|
|
memcpy(dst, src, n);
|
||
|
|
dst[n] = '\0';
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED int write_text_file(const char *path, const char *value)
|
||
|
|
{
|
||
|
|
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
||
|
|
if(fd < 0)
|
||
|
|
return -1;
|
||
|
|
if(write(fd, value, strlen(value)) < 0) {
|
||
|
|
close(fd);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
close(fd);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED int lock_singleton(const char *path)
|
||
|
|
{
|
||
|
|
int fd = open(path, O_CREAT | O_RDWR | O_CLOEXEC, 0644);
|
||
|
|
if(fd < 0)
|
||
|
|
return -1;
|
||
|
|
if(flock(fd, LOCK_EX | LOCK_NB) < 0) {
|
||
|
|
close(fd);
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
return fd;
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void reap_children(int sig)
|
||
|
|
{
|
||
|
|
int saved = errno;
|
||
|
|
(void)sig;
|
||
|
|
while(waitpid(-1, NULL, WNOHANG) > 0) {
|
||
|
|
}
|
||
|
|
errno = saved;
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void install_reaper(void)
|
||
|
|
{
|
||
|
|
struct sigaction sa;
|
||
|
|
memset(&sa, 0, sizeof(sa));
|
||
|
|
sa.sa_handler = reap_children;
|
||
|
|
sigemptyset(&sa.sa_mask);
|
||
|
|
sa.sa_flags = SA_NOCLDSTOP | SA_RESTART;
|
||
|
|
sigaction(SIGCHLD, &sa, NULL);
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void daemon_spawn_env(char *const argv[], char *const envp[])
|
||
|
|
{
|
||
|
|
pid_t pid = fork();
|
||
|
|
if(pid < 0)
|
||
|
|
return;
|
||
|
|
if(pid > 0)
|
||
|
|
return;
|
||
|
|
int nullfd = open("/dev/null", O_RDWR);
|
||
|
|
if(nullfd >= 0) {
|
||
|
|
dup2(nullfd, STDIN_FILENO);
|
||
|
|
dup2(nullfd, STDOUT_FILENO);
|
||
|
|
dup2(nullfd, STDERR_FILENO);
|
||
|
|
if(nullfd > STDERR_FILENO)
|
||
|
|
close(nullfd);
|
||
|
|
}
|
||
|
|
if(setgid(SESSION_GID) == 0 && setuid(SESSION_UID) == 0)
|
||
|
|
execve(argv[0], argv, envp);
|
||
|
|
_exit(127);
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void drop_device(struct watch_list *list, int fd)
|
||
|
|
{
|
||
|
|
int i;
|
||
|
|
for(i = 0; i < list->count; i++) {
|
||
|
|
if(list->devs[i].fd != fd)
|
||
|
|
continue;
|
||
|
|
close(list->devs[i].fd);
|
||
|
|
memmove(&list->devs[i], &list->devs[i + 1],
|
||
|
|
sizeof(list->devs[0]) * (size_t)(list->count - i - 1));
|
||
|
|
list->count--;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED int device_has_code(int fd, unsigned int ev, unsigned int code)
|
||
|
|
{
|
||
|
|
unsigned char bits[(KEY_MAX + 7) / 8 + 1];
|
||
|
|
memset(bits, 0, sizeof(bits));
|
||
|
|
if(ioctl(fd, BA_EVIOCGBIT(ev, sizeof(bits)), bits) < 0)
|
||
|
|
return 0;
|
||
|
|
if(code >= sizeof(bits) * 8)
|
||
|
|
return 0;
|
||
|
|
return (bits[code / 8] & (1u << (code % 8))) != 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED int wanted_device(int fd, enum watch_filter filter)
|
||
|
|
{
|
||
|
|
if(filter == WATCH_ALL)
|
||
|
|
return 1;
|
||
|
|
if(filter == WATCH_SWITCHES) {
|
||
|
|
return device_has_code(fd, EV_SW, SW_LID) ||
|
||
|
|
device_has_code(fd, EV_SW, SW_KEYPAD_SLIDE);
|
||
|
|
}
|
||
|
|
return device_has_code(fd, EV_KEY, KEY_VOLUMEUP) ||
|
||
|
|
device_has_code(fd, EV_KEY, KEY_VOLUMEDOWN);
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void prune_devices(struct watch_list *list)
|
||
|
|
{
|
||
|
|
int i;
|
||
|
|
for(i = list->count - 1; i >= 0; i--) {
|
||
|
|
struct stat st;
|
||
|
|
if(stat(list->devs[i].path, &st) == 0 && st.st_ino == list->devs[i].ino)
|
||
|
|
continue;
|
||
|
|
drop_device(list, list->devs[i].fd);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED void scan_devices(struct watch_list *list, enum watch_filter filter)
|
||
|
|
{
|
||
|
|
glob_t gl;
|
||
|
|
size_t i;
|
||
|
|
prune_devices(list);
|
||
|
|
if(glob("/dev/input/event*", 0, NULL, &gl) != 0)
|
||
|
|
return;
|
||
|
|
for(i = 0; i < gl.gl_pathc && list->count < WATCH_MAX; i++) {
|
||
|
|
const char *path = gl.gl_pathv[i];
|
||
|
|
struct stat st;
|
||
|
|
int j;
|
||
|
|
int fd;
|
||
|
|
int seen = 0;
|
||
|
|
if(stat(path, &st) < 0)
|
||
|
|
continue;
|
||
|
|
for(j = 0; j < list->count; j++) {
|
||
|
|
if(list->devs[j].ino == st.st_ino) {
|
||
|
|
seen = 1;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if(seen)
|
||
|
|
continue;
|
||
|
|
fd = open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
|
||
|
|
if(fd < 0)
|
||
|
|
continue;
|
||
|
|
if(!wanted_device(fd, filter)) {
|
||
|
|
close(fd);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
list->devs[list->count].fd = fd;
|
||
|
|
list->devs[list->count].ino = st.st_ino;
|
||
|
|
strncpy(list->devs[list->count].path, path,
|
||
|
|
sizeof(list->devs[list->count].path) - 1);
|
||
|
|
list->devs[list->count].path[sizeof(list->devs[0].path) - 1] = '\0';
|
||
|
|
list->count++;
|
||
|
|
}
|
||
|
|
globfree(&gl);
|
||
|
|
}
|
||
|
|
|
||
|
|
static BA_UNUSED long long now_ms(void)
|
||
|
|
{
|
||
|
|
struct timespec ts;
|
||
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||
|
|
return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000LL;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif
|