forked from FreeSpacenav/spnavcfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
back.c
139 lines (116 loc) · 2.73 KB
/
back.c
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
/*
spnavcfg - an interactive GUI configurator for the spacenavd daemon.
Copyright (C) 2007-2009 John Tsiombikas <[email protected]>
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 3 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include "cfgfile.h"
#include "cmd.h"
#define CFGFILE "/etc/spnavrc"
#define PIDFILE "/var/run/spnavd.pid"
int get_daemon_pid(void);
static int update_cfg(void);
static void sig(int s);
static struct cfg cfg;
static int dpid = -1;
int backend(int pfd)
{
signal(SIGTERM, sig);
for(;;) {
ssize_t res;
int cmd, tmp;
/* get command */
cmd = 0;
if(read(pfd, &cmd, 1) == -1 && errno != EINTR) {
perror("pipe read blew up in my face! wtf");
return -1;
}
switch(cmd) {
case CMD_PING:
tmp = (dpid = get_daemon_pid()) != -1;
write(pfd, &tmp, 1);
break;
case CMD_CFG:
{
char *buf = (char*)&cfg;
int sz = sizeof cfg;
while(sz && (res = read(pfd, buf, sz)) > 0) {
buf += res;
sz -= res;
}
update_cfg();
}
break;
case CMD_STARTX:
case CMD_STOPX:
if(dpid == -1) {
if((dpid = get_daemon_pid()) == -1) {
return -1;
}
}
if(kill(dpid, cmd == CMD_STARTX ? SIGUSR1 : SIGUSR2) == -1) {
if(errno != EPERM) {
dpid = -1;
kill(getppid(), SIGUSR2);
}
}
break;
default:
fprintf(stderr, "unknown CMD: %d\n", (int)cmd);
break;
}
}
return 0;
}
int get_daemon_pid(void)
{
FILE *fp;
char buf[64];
if(!(fp = fopen(PIDFILE, "r"))) {
fprintf(stderr, "no spacenav pid file, can't find daemon\n");
return -1;
}
fgets(buf, sizeof buf, fp);
fclose(fp);
if(!isdigit(buf[0])) {
fprintf(stderr, "fucked up pidfile, can't find daemon\n");
return -1;
}
return atoi(buf);
}
static int update_cfg(void)
{
if(write_cfg(CFGFILE, &cfg) == -1) {
fprintf(stderr, "failed to update config file\n");
return -1;
}
if(dpid == -1) {
if((dpid = get_daemon_pid()) == -1) {
return -1;
}
}
if(kill(dpid, SIGHUP) == -1 && errno != EPERM) {
dpid = -1; /* invalidate pid, will be searched again next time */
return -1;
}
return 0;
}
static void sig(int s)
{
_exit(0);
}