summaryrefslogtreecommitdiffstats
path: root/backend/x11lock.c
blob: ba4eb35a7268bda991d1a3d5a7dd472bfdda9398 (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
/*
 *   Copyright (C) 2010 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 "x11lock.h"
#include "log.h"
#include "util.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>


#define X11LOCK_HELPER		"pwrtray-xlock"
#define X11LOCK_HELPER_PATH	stringify(PREFIX) "/bin/" X11LOCK_HELPER


int block_x11_input(struct x11lock *xl)
{
	pid_t pid;

#ifndef FEATURE_XLOCK
# warning "X11 input blocking feature disabled"
	logdebug("X11 input blocking feature disabled\n");
	return 0;
#endif

	if (!xl)
		return 0;
	if (xl->helper_pid)
		return 0;

	pid = subprocess_exec(X11LOCK_HELPER_PATH);
	if (pid < 0) {
		logerr("block_x11_input: Failed to execute '%s'.\n",
		       X11LOCK_HELPER_PATH);
		return -ENOENT;
	}

	xl->helper_pid = pid;
	logdebug("Forked X11 input blocker helper %s (pid=%d)\n",
		 X11LOCK_HELPER_PATH, (int)pid);

	return 0;
}

void unblock_x11_input(struct x11lock *xl)
{
	int err;
	pid_t pid;

#ifndef FEATURE_XLOCK
	return;
#endif

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

	block_signals();

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

	unblock_signals();
}

void x11lock_sigchld(struct x11lock *xl, int wait)
{
	int status;
	pid_t pid, res;

#ifndef FEATURE_XLOCK
	return;
#endif

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

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

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