-
Notifications
You must be signed in to change notification settings - Fork 24
/
rgpiod.c
303 lines (237 loc) · 7.55 KB
/
rgpiod.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netdb.h>
#include "lgpio.h"
#include "rgpiod.h"
#include "lgDbg.h"
/* globals */
int gPermits = 0;
int gNumSockNetAddr = 0;
uint32_t gSockNetAddr[MAX_CONNECT_ADDRESSES];
int gFdSock = -1;
/* locals */
static int CfgIfFlags = LG_DEFAULT_IF_FLAGS;
static int CfgSocketPort = LG_DEFAULT_SOCKET_PORT;
static pthread_t pthSocket;
/* prototypes */
void *pthSocketThread(void *x);
/* ----------------------------------------------------------------------- */
static int xOpenSocket(void)
{
int i;
int opt=1;
struct sockaddr_in server;
struct sockaddr_in6 server6;
char *portStr;
int port;
pthread_attr_t pthAttr;
LG_DBG(LG_DEBUG_STARTUP, "");
gFdSock = -1;
if (pthread_attr_init(&pthAttr))
PARAM_ERROR(LG_INIT_FAILED, "pthread_attr_init failed (%m)");
if (pthread_attr_setstacksize(&pthAttr, STACK_SIZE))
PARAM_ERROR(LG_INIT_FAILED, "pthread_attr_setstacksize failed (%m)");
portStr = getenv(LG_ENVPORT);
if (portStr) port = atoi(portStr); else port = CfgSocketPort;
// Accept connections on IPv6, unless we have an IPv4-only whitelist
if (!gNumSockNetAddr)
{
gFdSock = socket(AF_INET6, SOCK_STREAM , 0);
if (gFdSock != -1)
{
bzero((char *)&server6, sizeof(server6));
server6.sin6_family = AF_INET6;
if (CfgIfFlags & LG_LOCALHOST_SOCK_IF)
{
server6.sin6_addr = in6addr_loopback;
}
else
{
server6.sin6_addr = in6addr_any;
}
server6.sin6_port = htons(port);
setsockopt(gFdSock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(gFdSock,(struct sockaddr *)&server6, sizeof(server6)) < 0)
PARAM_ERROR(LG_INIT_FAILED, "bind to port %d failed (%m)", port);
}
}
if (gNumSockNetAddr || gFdSock == -1)
{
gFdSock = socket(AF_INET , SOCK_STREAM , 0);
if (gFdSock == -1)
PARAM_ERROR(LG_INIT_FAILED, "socket failed (%m)");
else
{
setsockopt(gFdSock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
}
server.sin_family = AF_INET;
if (CfgIfFlags & LG_LOCALHOST_SOCK_IF)
{
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
else
{
server.sin_addr.s_addr = htonl(INADDR_ANY);
}
server.sin_port = htons(port);
if (bind(gFdSock,(struct sockaddr *)&server , sizeof(server)) < 0)
PARAM_ERROR(LG_INIT_FAILED, "bind to port %d failed (%m)", port);
}
if (pthread_create(&pthSocket, &pthAttr, pthSocketThread, &i))
PARAM_ERROR(LG_INIT_FAILED, "pthread_create socket failed (%m)");
return LG_OKAY;
}
static void xFatal(char *fmt, ...)
{
char buf[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
fflush(stderr);
exit(EXIT_FAILURE);
}
static void xUsage()
{
fprintf(stderr, "\n" \
"Usage: rgpiod [OPTION] ...\n" \
" -c dir, set config dir (default launch dir)\n" \
" -l, localhost socket only (default local+remote)\n" \
" -n IP addr, allow address, name or dotted (default allow all)\n" \
" -p value, socket port (1024-32000, default 8889)\n" \
" -v, display rgpiod version and exit\n" \
" -w dir, set working directory (default launch directory)\n" \
" -x, enable access control (default off)\n" \
"EXAMPLE\n" \
"rgpiod -p 9000 &\n" \
" Start with socket port 9000\n" \
"\n");
}
static uint64_t xGetNum(char *str, int *err)
{
uint64_t val;
char *endptr;
*err = 0;
val = strtoll(str, &endptr, 0);
if (*endptr) {*err = 1; val = -1;}
return val;
}
static uint32_t xCheckAddr(char *addrStr)
{
int err;
struct addrinfo hints, *res;
struct sockaddr_in *sin;
const char *portStr;
uint32_t addr;
portStr = getenv(LG_ENVPORT);
if (!portStr) portStr = LG_DEFAULT_SOCKET_PORT_STR;
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
err = getaddrinfo(addrStr, portStr, &hints, &res);
if (err) return 0;
sin = (struct sockaddr_in *)res->ai_addr;
addr = sin->sin_addr.s_addr;
freeaddrinfo(res);
return addr;
}
static void xInitOpts(int argc, char *argv[])
{
int opt, err, i;
uint32_t addr;
while ((opt = getopt(argc, argv, "c:ln:p:vw:x")) != -1)
{
switch (opt)
{
case 'c': /* configuration directory */
lguSetConfigDir(optarg);
break;
case 'l':
CfgIfFlags |= LG_LOCALHOST_SOCK_IF;
break;
case 'n':
addr = xCheckAddr(optarg);
if (addr && (gNumSockNetAddr<MAX_CONNECT_ADDRESSES))
gSockNetAddr[gNumSockNetAddr++] = addr;
else xFatal("invalid -n option (%s)", optarg);
break;
case 'p':
i = xGetNum(optarg, &err);
if ((i >= LG_MIN_SOCKET_PORT) && (i <= LG_MAX_SOCKET_PORT))
CfgSocketPort = i;
else xFatal("invalid -p option (%d)", i);
break;
case 'v':
printf("rgpiod_%d.%d.%d.%d\n",
(RGPIOD_VERSION>>24)&0xff, (RGPIOD_VERSION>>16)&0xff,
(RGPIOD_VERSION>>8)&0xff, RGPIOD_VERSION&0xff);
exit(EXIT_SUCCESS);
break;
case 'w': /* working directory */
lguSetWorkDir(optarg);
break;
case 'x': /* enable user permissions checks */
gPermits = 1;
break;
default: /* '?' */
xUsage();
exit(EXIT_FAILURE);
}
}
}
int main(int argc, char **argv)
{
int flags;
/* check command line parameters */
xInitOpts(argc, argv);
/* initialise */
if (xOpenSocket() >= 0)
{
/* set stderr non-blocking */
flags = fcntl(fileno(stderr), F_GETFL, 0);
fcntl(fileno(stderr), F_SETFL, flags | O_NONBLOCK);
/* sleep forever */
while (1)
{
sleep(1);
fflush(stderr);
}
}
return -1;
}