-
Notifications
You must be signed in to change notification settings - Fork 49
/
socket.c
608 lines (500 loc) · 11.8 KB
/
socket.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
* General functions for AF_LOCAL sockets
*
* Copyright (C) 2010-2012 Olaf Kirch <okir@suse.de>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <sys/un.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <wicked/netinfo.h>
#include <wicked/logging.h>
#include <wicked/xml.h>
#include <wicked/socket.h>
#include "netinfo_priv.h"
#include "socket_priv.h"
#include "appconfig.h"
#define NI_SOCKET_ARRAY_CHUNK 16
static void __ni_socket_close(ni_socket_t *);
static void __ni_socket_accept(ni_socket_t *);
static void __ni_default_error_handler(ni_socket_t *);
static void __ni_default_hangup_handler(ni_socket_t *);
static ni_socket_array_t __ni_sockets;
/*
* Install a socket so we check it for incoming data.
*/
ni_bool_t
ni_socket_activate(ni_socket_t *sock)
{
return ni_socket_array_activate(&__ni_sockets, sock);
}
static inline void
__ni_socket_deactivate(ni_socket_t **slot)
{
ni_socket_t *sock = *slot;
*slot = NULL;
sock->active = NULL;
ni_socket_release(sock);
}
ni_bool_t
ni_socket_deactivate(ni_socket_t *sock)
{
if (!sock || !sock->active)
return FALSE;
return ni_socket_array_deactivate(sock->active, sock);
}
void
ni_socket_deactivate_all(void)
{
ni_socket_array_destroy(&__ni_sockets);
}
ni_socket_t *
ni_socket_hold(ni_socket_t *sock)
{
ni_assert(sock);
ni_assert(sock->refcount);
sock->refcount++;
return sock;
}
void
ni_socket_release(ni_socket_t *sock)
{
ni_assert(sock);
ni_assert(sock->refcount);
sock->refcount--;
if (sock->refcount == 0) {
__ni_socket_close(sock);
ni_assert(!sock->active);
if (sock->release_user_data)
sock->release_user_data(sock->user_data);
free(sock);
}
}
void
__ni_default_error_handler(ni_socket_t *sock)
{
/* Deactivate socket */
ni_warn("POLLERR on socket - deactivating. Note: this is not the right approach, fix it");
sock->error = 1;
}
void
__ni_default_hangup_handler(ni_socket_t *sock)
{
}
/*
* Wait for incoming data on any of the sockets.
*/
int
ni_socket_array_wait(ni_socket_array_t *array, long timeout)
{
struct pollfd pfd[array->count];
struct timeval now, expires;
unsigned int i, socket_count;
/* First step - cleanup empty socket slots from the array. */
ni_socket_array_cleanup(array);
/* Second step - build pollfd array and get timeouts */
timerclear(&expires);
socket_count = 0;
for (i = 0; i < array->count; ++i) {
ni_socket_t *sock = array->data[i];
struct timeval socket_expires;
if (sock->active != array)
continue;
timerclear(&socket_expires);
if (sock->get_timeout && sock->get_timeout(sock, &socket_expires) == 0) {
if (!timerisset(&expires) || timercmp(&socket_expires, &expires, <))
expires = socket_expires;
}
pfd[socket_count].fd = sock->__fd;
pfd[socket_count].events = sock->poll_flags;
socket_count++;
}
gettimeofday(&now, NULL);
if (timerisset(&expires)) {
struct timeval delta;
long delta_ms;
if (timercmp(&expires, &now, <)) {
timeout = 0;
} else {
timersub(&expires, &now, &delta);
delta_ms = 1000 * delta.tv_sec + delta.tv_usec / 1000;
if (timeout < 0 || delta_ms < timeout)
timeout = delta_ms;
}
}
if (socket_count == 0 && timeout < 0) {
ni_debug_socket("no sockets left to watch");
return 1;
}
if (poll(pfd, socket_count, timeout) < 0) {
if (errno == EINTR)
return 0;
ni_error("poll returns error: %m");
return -1;
}
for (i = 0; i < socket_count; ++i) {
ni_socket_t *sock = array->data[i];
if (!sock || sock->active != array)
continue;
if (pfd[i].fd != sock->__fd)
continue;
ni_socket_hold(sock);
if (pfd[i].revents & POLLERR) {
/* Deactivate socket */
__ni_socket_deactivate(&array->data[i]);
sock->handle_error(sock);
goto done_with_this_socket;
}
if (pfd[i].revents & POLLIN) {
if (sock->receive == NULL) {
ni_error("socket %d has no receive callback", sock->__fd);
__ni_socket_deactivate(&array->data[i]);
} else {
sock->receive(sock);
}
if (sock->__fd < 0)
goto done_with_this_socket;
}
if (pfd[i].revents & POLLHUP) {
if (sock->handle_hangup)
sock->handle_hangup(sock);
if (sock->__fd < 0)
goto done_with_this_socket;
} else
if (pfd[i].revents & POLLOUT) {
if (sock->transmit == NULL) {
ni_error("socket %d has no transmit callback", sock->__fd);
__ni_socket_deactivate(&array->data[i]);
} else {
sock->transmit(sock);
}
}
done_with_this_socket:
ni_socket_release(sock);
}
gettimeofday(&now, NULL);
for (i = 0; i < array->count && i < socket_count; ++i) {
ni_socket_t *sock = array->data[i];
if (!sock || sock->active != array)
continue;
if (sock->check_timeout)
sock->check_timeout(sock, &now);
}
/* Finally cleanup deactivated/released sockets */
ni_socket_array_cleanup(array);
return 0;
}
int
ni_socket_wait(long timeout)
{
return ni_socket_array_wait(&__ni_sockets, timeout);
}
/*
* Wrap a file descriptor in a ni_socket object
*/
static ni_socket_t *
__ni_socket_wrap(int fd, int sotype)
{
ni_socket_t *socket;
socket = xcalloc(1, sizeof(*socket));
socket->refcount = 1;
socket->__fd = fd;
socket->handle_error = __ni_default_error_handler;
socket->handle_hangup = __ni_default_hangup_handler;
return socket;
}
ni_socket_t *
ni_socket_wrap(int fd, int sotype)
{
if (sotype < 0) {
socklen_t len = sizeof(sotype);
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &sotype, &len) < 0) {
ni_error("%s: cannot determine socket type", __FUNCTION__);
return NULL;
}
}
return __ni_socket_wrap(fd, sotype);
}
/*
* Close socket
*/
static void
__ni_socket_close(ni_socket_t *sock)
{
if (sock->close) {
sock->close(sock);
} else if (sock->__fd >= 0) {
close(sock->__fd);
}
sock->__fd = -1;
ni_buffer_destroy(&sock->wbuf);
ni_buffer_destroy(&sock->rbuf);
if (sock->active)
ni_socket_deactivate(sock);
}
void
ni_socket_close(ni_socket_t *sock)
{
__ni_socket_close(sock);
ni_socket_release(sock);
}
/*
* Create a listener socket
*/
ni_socket_t *
ni_local_socket_listen(const char *path, unsigned int permissions)
{
ni_socket_t *sock;
int fd, bound = 0;
permissions &= 0777;
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
ni_error("cannot open AF_LOCAL socket: %m");
return NULL;
}
if (path) {
struct sockaddr_un sun;
unsigned int len = strlen(path);
if (len + 1 > sizeof(sun.sun_path)) {
ni_error("can't set AF_LOCAL address: path too long!");
return NULL;
}
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_LOCAL;
strcpy(sun.sun_path, path);
unlink(path);
if (bind(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
ni_error("bind(%s) failed: %m", path);
goto failed;
}
bound = 1;
if (chmod(path, permissions) < 0) {
ni_error("chmod(%s, 0%3o) failed: %m", path, permissions);
goto failed;
}
}
if (listen(fd, 128) < 0) {
ni_error("cannot listen on local socket: %m");
goto failed;
}
sock = __ni_socket_wrap(fd, SOCK_STREAM);
sock->receive = __ni_socket_accept;
ni_socket_activate(sock);
return sock;
failed:
if (bound && path)
unlink(path);
close(fd);
return NULL;
}
ni_socket_t *
ni_local_socket_connect(const char *path)
{
int fd;
if (!path) {
ni_error("cannot connect to server - no server socket path specified");
return NULL;
}
fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if (fd < 0) {
ni_error("cannot open AF_LOCAL socket: %m");
return NULL;
}
{
struct sockaddr_un sun;
unsigned int len = strlen(path);
if (len + 1 > sizeof(sun.sun_path)) {
ni_error("can't set AF_LOCAL address: path too long!");
goto failed;
}
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_LOCAL;
strcpy(sun.sun_path, path);
if (connect(fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
ni_error("connect(%s) failed: %m", path);
goto failed;
}
}
return ni_socket_wrap(fd, SOCK_STREAM);
failed:
close(fd);
return NULL;
}
void
__ni_socket_accept(ni_socket_t *master)
{
ni_socket_t *sock;
struct ucred cred;
socklen_t clen;
int fd;
fd = accept(master->__fd, NULL, NULL);
if (fd < 0)
return;
clen = sizeof(cred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &clen) < 0) {
ni_error("failed to get client credentials: %m");
close(fd);
return;
}
sock = __ni_socket_wrap(fd, SOCK_STREAM);
if (master->accept == NULL || master->accept(sock, cred.uid, cred.gid) >= 0) {
ni_buffer_init_dynamic(&sock->rbuf, ni_global.config->recv_max);
ni_socket_array_activate(master->active, sock);
}
ni_socket_release(sock);
}
/*
* Create a local socket pair
*/
int
ni_local_socket_pair(ni_socket_t **p1, ni_socket_t **p2)
{
int fd[2];
if (socketpair(AF_LOCAL, SOCK_DGRAM, 0, fd) < 0) {
ni_error("unable to create AF_LOCAL socketpair: %m");
return -1;
}
*p1 = ni_socket_wrap(fd[0], SOCK_DGRAM);
*p2 = ni_socket_wrap(fd[1], SOCK_DGRAM);
return 0;
}
/*
* Socket array manipulation functions
*/
void
ni_socket_array_init(ni_socket_array_t *array)
{
memset(array, 0, sizeof(*array));
}
void
ni_socket_array_destroy(ni_socket_array_t *array)
{
ni_socket_t *sock;
if (array) {
while (array->count--) {
sock = array->data[array->count];
array->data[array->count] = NULL;
if (sock) {
if (sock->active == array)
sock->active = NULL;
ni_socket_release(sock);
}
}
free(array->data);
memset(array, 0, sizeof(*array));
}
}
void
ni_socket_array_cleanup(ni_socket_array_t *array)
{
unsigned int i, j;
for (i = j = 0; i < array->count; ++i) {
if (array->data[i])
array->data[j++] = array->data[i];
}
array->count = j;
}
static inline void
__ni_socket_array_realloc(ni_socket_array_t *array, unsigned int newsize)
{
ni_socket_t **newdata;
unsigned int i;
newsize = (newsize + NI_SOCKET_ARRAY_CHUNK);
newdata = xrealloc(array->data, newsize * sizeof(ni_socket_t *));
array->data = newdata;
for (i = array->count; i < newsize; ++i)
array->data[i] = NULL;
}
ni_bool_t
ni_socket_array_append(ni_socket_array_t *array, ni_socket_t *sock)
{
if (array && sock) {
if (ni_socket_array_find(array, sock) != -1U)
return TRUE;
if ((array->count % NI_SOCKET_ARRAY_CHUNK) == 0)
__ni_socket_array_realloc(array, array->count);
array->data[array->count++] = sock;
return TRUE;
}
return FALSE;
}
ni_socket_t *
ni_socket_array_remove_at(ni_socket_array_t *array, unsigned int index)
{
ni_socket_t *sock;
if (!array || index >= array->count)
return NULL;
sock = array->data[index];
memmove(&array->data[index], &array->data[index + 1],
(array->count - index) * sizeof(ni_socket_t *));
array->count--;
array->data[array->count] = NULL;
if (sock && sock->active == array)
sock->active = NULL;
return sock;
}
ni_socket_t *
ni_socket_array_remove(ni_socket_array_t *array, ni_socket_t *sock)
{
unsigned int i;
if (array && sock) {
for (i = 0; i < array->count; ++i) {
if (sock != array->data[i])
continue;
return ni_socket_array_remove_at(array, i);
}
}
return NULL;
}
unsigned int
ni_socket_array_find(ni_socket_array_t *array, ni_socket_t *sock)
{
unsigned int i;
if (array && sock) {
for (i = 0; i < array->count; ++i) {
if (sock == array->data[i])
return i;
}
}
return -1U;
}
ni_bool_t
ni_socket_array_activate(ni_socket_array_t *array, ni_socket_t *sock)
{
if (!array || !sock)
return FALSE;
if (sock->active)
return sock->active == array;
if (!ni_socket_array_append(&__ni_sockets, sock))
return FALSE;
ni_socket_hold(sock);
sock->active = array;
sock->poll_flags = POLLIN;
return TRUE;
}
ni_bool_t
ni_socket_array_deactivate(ni_socket_array_t *array, ni_socket_t *sock)
{
unsigned int i;
if (!array || !sock || !sock->active || sock->active != array)
return FALSE;
for (i = 0; i < array->count; ++i) {
if (sock == array->data[i]) {
ni_socket_array_remove_at(array, i);
ni_socket_release(sock);
return TRUE;
}
}
return FALSE;
}