summaryrefslogtreecommitdiffstats
path: root/backend/xevrep.c
blob: e8de4314ab7e155e519255d357676890773467db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 *   Copyright (C) 2011 Michael Buesch <m@bues.ch>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 */

#include "xevrep.h"
#include "log.h"
#include "util.h"
#include "conf.h"
#include "main.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/resource.h>


#define XEVREP_HELPER		"pwrtray-xevrep"
#define XEVREP_HELPER_PATH	stringify(PREFIX) "/bin/" XEVREP_HELPER


int xevrep_enable(struct xevrep *xr)
{
	pid_t pid;
	int err, grace_period, nice_level;
	char command[128];

#ifndef FEATURE_XEVREP
# warning "X11 input event reporting feature disabled"
	logdebug("X11 input event reporting feature disabled");
	return 0;
#endif

	if (!xr)
		return 0;
	if (xr->helper_pid)
		return 0;
	grace_period = config_get_int(backend.config,
				      "XEVREP", "grace_period",
				      1000);
	snprintf(command, sizeof(command), "%s %d %d %d",
		 XEVREP_HELPER_PATH,
		 getpid(), SIGUSR1, grace_period);
	pid = subprocess_exec(command);
	if (pid < 0) {
		logerr("xevrep_enable: Failed to execute '%s'.\n",
		       XEVREP_HELPER_PATH);
		return -ENOENT;
	}

	xr->helper_pid = pid;
	logdebug("Forked X11 input event helper %s (pid=%d)\n",
		 XEVREP_HELPER_PATH, (int)pid);

	nice_level = config_get_int(backend.config,
				    "XEVREP", "nice", 5);
	nice_level = clamp(nice_level, -20, 19);
	err = setpriority(PRIO_PROCESS, pid, nice_level);
	if (err) {
		logerr("xevrep_enable: Failed to set %s niceness to %d: %s\n",
		       XEVREP_HELPER, nice_level, strerror(errno));
	}

	return 0;
}

void xevrep_disable(struct xevrep *xr)
{
	int err;
	pid_t pid;

#ifndef FEATURE_XEVREP
	return;
#endif

	if (!xr)
		return;
	pid = xr->helper_pid;
	if (!pid)
		return;

	block_signals();

	xr->killed = 1;
	err = kill(pid, SIGTERM);
	if (err) {
		logerr("xevrep_disable: Failed to kill helper process PID %d\n",
		       (int)pid);
	}

	unblock_signals();
}

void xevrep_sigchld(struct xevrep *xr, int wait)
{
	int status;
	pid_t pid, res;

#ifndef FEATURE_XEVREP
	return;
#endif

	if (!xr)
		return;
	pid = xr->helper_pid;
	if (!pid)
		return;

	res = waitpid(pid, &status, wait ? 0 : WNOHANG);
	if (res < 0) {
		if (errno == ECHILD)
			return;
		logerr("xevrep_sigchld: waitpid failed for PID %d: %s\n",
		       (int)pid, strerror(errno));
		return;
	}
	if (!xr->killed) {
		logerr("xevrep_sigchld: X11 input event helper was "
		       "murdered by a stranger.\n");
	}
	xr->helper_pid = 0;
	xr->killed = 0;

	if (status) {
		logerr("xevrep_sigchld: The helper process "
		       "returned an error code: %d\n", status);
	} else {
		logdebug("X11 input event helper (pid=%d) terminated\n",
			 (int)pid);
	}
}
bues.ch cgit interface