blob: a958f2226ee34890e3fb04523d4ea01e57b87a86 [file] [log] [blame]
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001/*
2 * A Random IP reputation service acting as a Stream Processing Offload Agent
3 *
4 * This is a very simple service that implement a "random" ip reputation
5 * service. It will return random scores for all checked IP addresses. It only
6 * shows you how to implement a ip reputation service or such kind of services
7 * using the SPOE.
8 *
9 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +010010 * Copyright 2018 OZON / Thierry Fournier <thierry.fournier@ozon.io>
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 */
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +010018#include <limits.h>
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <stdbool.h>
23#include <unistd.h>
24#include <signal.h>
25#include <pthread.h>
26#include <sys/time.h>
27#include <sys/types.h>
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +010028#include <sys/wait.h>
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010029#include <sys/socket.h>
30#include <netinet/in.h>
31#include <netinet/tcp.h>
32#include <arpa/inet.h>
33
Thierry FOURNIER4aec0a42018-02-23 11:42:57 +010034#include "spoa.h"
35
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010036#define DEFAULT_PORT 12345
37#define NUM_WORKERS 5
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010038
39#define SLEN(str) (sizeof(str)-1)
40
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010041/* Frame Types sent by HAProxy and by agents */
42enum spoe_frame_type {
43 /* Frames sent by HAProxy */
44 SPOE_FRM_T_HAPROXY_HELLO = 1,
45 SPOE_FRM_T_HAPROXY_DISCON,
46 SPOE_FRM_T_HAPROXY_NOTIFY,
47
48 /* Frames sent by the agents */
49 SPOE_FRM_T_AGENT_HELLO = 101,
50 SPOE_FRM_T_AGENT_DISCON,
51 SPOE_FRM_T_AGENT_ACK
52};
53
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010054/* Errors triggerd by SPOE applet */
55enum spoe_frame_error {
56 SPOE_FRM_ERR_NONE = 0,
57 SPOE_FRM_ERR_IO,
58 SPOE_FRM_ERR_TOUT,
59 SPOE_FRM_ERR_TOO_BIG,
60 SPOE_FRM_ERR_INVALID,
61 SPOE_FRM_ERR_NO_VSN,
62 SPOE_FRM_ERR_NO_FRAME_SIZE,
63 SPOE_FRM_ERR_NO_CAP,
64 SPOE_FRM_ERR_BAD_VSN,
65 SPOE_FRM_ERR_BAD_FRAME_SIZE,
66 SPOE_FRM_ERR_UNKNOWN = 99,
67 SPOE_FRM_ERRS,
68};
69
70/* All supported SPOE actions */
71enum spoe_action_type {
72 SPOE_ACT_T_SET_VAR = 1,
73 SPOE_ACT_T_UNSET_VAR,
74 SPOE_ACT_TYPES,
75};
76
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +010077
78/* Masks to get data type or flags value */
79#define SPOE_DATA_T_MASK 0x0F
80#define SPOE_DATA_FL_MASK 0xF0
81
82/* Flags to set Boolean values */
83#define SPOE_DATA_FL_FALSE 0x00
84#define SPOE_DATA_FL_TRUE 0x10
85static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
86 [SPOE_FRM_ERR_NONE] = "normal",
87 [SPOE_FRM_ERR_IO] = "I/O error",
88 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
89 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
90 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
91 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
92 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
93 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
94 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
95 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
96 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
97};
98
Thierry FOURNIER880d7e12018-02-25 10:54:56 +010099bool debug = false;
100pthread_key_t worker_id;
Thierry FOURNIER64eaa332018-02-23 14:58:40 +0100101static struct ps *ps_list = NULL;
Thierry FOURNIER892f6642018-02-23 14:27:05 +0100102static struct ps_message *ps_messages = NULL;
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +0100103static int nfiles = 0;
104static char **files = NULL;
105
106static inline void add_file(const char *file)
107{
108 nfiles++;
109 files = realloc(files, sizeof(*files) * nfiles);
110 if (files == NULL) {
111 fprintf(stderr, "Out of memory error\n");
112 exit(EXIT_FAILURE);
113 }
114 files[nfiles - 1] = strdup(file);
115 if (files[nfiles - 1] == NULL) {
116 fprintf(stderr, "Out of memory error\n");
117 exit(EXIT_FAILURE);
118 }
119}
Thierry FOURNIER64eaa332018-02-23 14:58:40 +0100120
121void ps_register(struct ps *ps)
122{
123 ps->next = ps_list;
124 ps_list = ps;
125}
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100126
Thierry FOURNIER892f6642018-02-23 14:27:05 +0100127void ps_register_message(struct ps *ps, const char *name, void *ref)
128{
129 struct ps_message *msg;
130
131 /* Look for already registered name */
132 for (msg = ps_messages; msg; msg = msg->next) {
133 if (strcmp(name, msg->name) == 0) {
134 LOG("Message \"%s\" already registered\n", name);
135 exit(EXIT_FAILURE);
136 }
137 }
138
139 msg = calloc(1, sizeof(*msg));
140 if (msg == NULL) {
141 LOG("Out of memory error\n");
142 exit(EXIT_FAILURE);
143 }
144
145 msg->next = ps_messages;
146 ps_messages = msg;
147 msg->name = strdup(name);
148 if (msg->name == NULL) {
149 LOG("Out of memory error\n");
150 exit(EXIT_FAILURE);
151 }
152 msg->ref = ref;
153 msg->ps = ps;
154}
155
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100156static int
157do_read(int sock, void *buf, int read_len)
158{
159 fd_set readfds;
160 int n = 0, total = 0, bytesleft = read_len;
161
162 FD_ZERO(&readfds);
163 FD_SET(sock, &readfds);
164
165 while (total < read_len) {
166 if (select(FD_SETSIZE, &readfds, NULL, NULL, NULL) == -1)
167 return -1;
168 if (!FD_ISSET(sock, &readfds))
169 return -1;
170
171 n = read(sock, buf + total, bytesleft);
172 if (n <= 0)
173 break;
174
175 total += n;
176 bytesleft -= n;
177 }
178
179 return (n == -1) ? -1 : total;
180}
181
182static int
183do_write(int sock, void *buf, int write_len)
184{
185 fd_set writefds;
186 int n = 0, total = 0, bytesleft = write_len;
187
188 FD_ZERO(&writefds);
189 FD_SET(sock, &writefds);
190
191 while (total < write_len) {
192 if (select(FD_SETSIZE, NULL, &writefds, NULL, NULL) == -1)
193 return -1;
194 if (!FD_ISSET(sock, &writefds))
195 return -1;
196
197 n = write(sock, buf + total, bytesleft);
198 if (n <= 0)
199 break;
200
201 total += n;
202 bytesleft -= n;
203 }
204
205 return (n == -1) ? -1 : total;
206}
207
208/* Receive a frame sent by HAProxy. It returns -1 if an error occurred,
209 * otherwise the number of read bytes.*/
210static int
211read_frame(int sock, struct worker *w)
212{
213 uint32_t netint;
214 unsigned int framesz;
215
216 /* Read the frame size, on 4 bytes */
217 if (do_read(sock, &netint, sizeof(netint)) != 4) {
218 w->status_code = SPOE_FRM_ERR_IO;
219 return -1;
220 }
221
222 /* Check it against the max size */
223 framesz = ntohl(netint);
224 if (framesz > w->size) {
225 w->status_code = SPOE_FRM_ERR_TOO_BIG;
226 return -1;
227 }
228
229 /* Read the frame */
230 if (do_read(sock, w->buf, framesz) != framesz) {
231 w->status_code = SPOE_FRM_ERR_IO;
232 return -1;
233 }
234
235 w->len = framesz;
236 return framesz;
237}
238
239/* Send a frame to HAProxy. It returns -1 if an error occurred, otherwise the
240 * number of written bytes. */
241static int
242write_frame(int sock, struct worker *w)
243{
244 uint32_t netint;
245
246 /* Write the frame size, on 4 bytes */
247 netint = htonl(w->len);
248 if (do_write(sock, &netint, sizeof(netint)) != 4) {
249 w->status_code = SPOE_FRM_ERR_IO;
250 return -1;
251 }
252
253 /* Write the frame */
254 if (do_write(sock, w->buf, w->len) != w->len) {
255 w->status_code = SPOE_FRM_ERR_IO;
256 return -1;
257 }
258 return w->len;
259}
260
261/* Encode a variable-length integer. This function never fails and returns the
262 * number of written bytes. */
263static int
264encode_spoe_varint(uint64_t i, char *buf)
265{
266 int idx;
267
268 if (i < 240) {
269 buf[0] = (unsigned char)i;
270 return 1;
271 }
272
273 buf[0] = (unsigned char)i | 240;
274 i = (i - 240) >> 4;
275 for (idx = 1; i >= 128; ++idx) {
276 buf[idx] = (unsigned char)i | 128;
277 i = (i - 128) >> 7;
278 }
279 buf[idx++] = (unsigned char)i;
280 return idx;
281}
282
283/* Decode a varable-length integer. If the decoding fails, -1 is returned. This
284 * happens when the buffer's end in reached. On success, the number of read
285 * bytes is returned. */
286static int
287decode_spoe_varint(char *buf, char *end, uint64_t *i)
288{
289 unsigned char *msg = (unsigned char *)buf;
290 int idx = 0;
291
292 if (msg > (unsigned char *)end)
293 return -1;
294
295 if (msg[0] < 240) {
296 *i = msg[0];
297 return 1;
298 }
299 *i = msg[0];
300 do {
301 ++idx;
302 if (msg+idx > (unsigned char *)end)
303 return -1;
304 *i += (uint64_t)msg[idx] << (4 + 7 * (idx-1));
305 } while (msg[idx] >= 128);
306 return (idx + 1);
307}
308
309/* Encode a string. The string will be prefix by its length, encoded as a
310 * variable-length integer. This function never fails and returns the number of
311 * written bytes. */
312static int
313encode_spoe_string(const char *str, size_t len, char *dst)
314{
315 int idx = 0;
316
317 if (!len) {
318 dst[0] = 0;
319 return 1;
320 }
321
322 idx += encode_spoe_varint(len, dst);
323 memcpy(dst+idx, str, len);
324 return (idx + len);
325}
326
327/* Decode a string. Its length is decoded first as a variable-length integer. If
328 * it succeeds, and if the string length is valid, the begin of the string is
329 * saved in <*str>, its length is saved in <*len> and the total numbre of bytes
330 * read is returned. If an error occurred, -1 is returned and <*str> remains
331 * NULL. */
332static int
333decode_spoe_string(char *buf, char *end, char **str, uint64_t *len)
334{
335 int r, idx = 0;
336
337 *str = NULL;
338 *len = 0;
339
340 if ((r = decode_spoe_varint(buf, end, len)) == -1)
341 goto error;
342 idx += r;
343 if (buf + idx + *len > end)
344 goto error;
345
346 *str = buf+idx;
347 return (idx + *len);
348
349error:
350 return -1;
351}
352
353/* Skip a typed data. If an error occurred, -1 is returned, otherwise the number
354 * of bytes read is returned. A types data is composed of a type (1 byte) and
355 * corresponding data:
356 * - boolean: non additional data (0 bytes)
357 * - integers: a variable-length integer (see decode_spoe_varint)
358 * - ipv4: 4 bytes
359 * - ipv6: 16 bytes
360 * - binary and string: a buffer prefixed by its size, a variable-length
361 * integer (see decode_spoe_string) */
362static int
363skip_spoe_data(char *frame, char *end)
364{
365 uint64_t sz = 0;
366 int r, idx = 0;
367
368 if (frame > end)
369 return -1;
370
371 switch (frame[idx++] & SPOE_DATA_T_MASK) {
372 case SPOE_DATA_T_BOOL:
373 idx++;
374 break;
375 case SPOE_DATA_T_INT32:
376 case SPOE_DATA_T_INT64:
377 case SPOE_DATA_T_UINT32:
378 case SPOE_DATA_T_UINT64:
379 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
380 return -1;
381 idx += r;
382 break;
383 case SPOE_DATA_T_IPV4:
384 idx += 4;
385 break;
386 case SPOE_DATA_T_IPV6:
387 idx += 16;
388 break;
389 case SPOE_DATA_T_STR:
390 case SPOE_DATA_T_BIN:
391 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
392 return -1;
393 idx += r + sz;
394 break;
395 }
396
397 if (frame+idx > end)
398 return -1;
399 return idx;
400}
401
402/* Decode a typed data. If an error occurred, -1 is returned, otherwise the
403 * number of read bytes is returned. See skip_spoe_data for details. */
404static int
405decode_spoe_data(char *frame, char *end, struct spoe_data *data)
406{
407 uint64_t sz = 0;
408 int type, r, idx = 0;
409
410 if (frame > end)
411 return -1;
412
413 type = frame[idx++];
414 data->type = (type & SPOE_DATA_T_MASK);
415 switch (data->type) {
416 case SPOE_DATA_T_BOOL:
417 data->u.boolean = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
418 break;
419 case SPOE_DATA_T_INT32:
420 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
421 return -1;
422 data->u.sint32 = sz;
423 idx += r;
424 break;
425 case SPOE_DATA_T_INT64:
426 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
427 return -1;
428 data->u.uint32 = sz;
429 idx += r;
430 break;
431 case SPOE_DATA_T_UINT32:
432 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
433 return -1;
434 data->u.sint64 = sz;
435 idx += r;
436 break;
437 case SPOE_DATA_T_UINT64:
438 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
439 return -1;
440 data->u.uint64 = sz;
441 idx += r;
442 break;
443 case SPOE_DATA_T_IPV4:
444 if (frame+idx+4 > end)
445 return -1;
446 memcpy(&data->u.ipv4, frame+idx, 4);
447 idx += 4;
448 break;
449 case SPOE_DATA_T_IPV6:
450 if (frame+idx+16 > end)
451 return -1;
452 memcpy(&data->u.ipv6, frame+idx, 16);
453 idx += 16;
454 break;
455 case SPOE_DATA_T_STR:
456 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
457 return -1;
458 idx += r;
459 if (frame+idx+sz > end)
460 return -1;
461 data->u.buffer.str = frame+idx;
462 data->u.buffer.len = sz;
463 idx += sz;
464 break;
465 case SPOE_DATA_T_BIN:
466 if ((r = decode_spoe_varint(frame+idx, end, &sz)) == -1)
467 return -1;
468 idx += r;
469 if (frame+idx+sz > end)
470 return -1;
471 data->u.buffer.str = frame+idx;
472 data->u.buffer.len = sz;
473 idx += sz;
474 break;
475 default:
476 break;
477 }
478
479 if (frame+idx > end)
480 return -1;
481 return idx;
482}
483
484
485/* Check the protocol version. It returns -1 if an error occurred, the number of
486 * read bytes otherwise. */
487static int
488check_proto_version(struct worker *w, int idx)
489{
490 char *str;
491 uint64_t sz;
492
493 /* Get the list of all supported versions by HAProxy */
494 if ((w->buf[idx++] & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
495 w->status_code = SPOE_FRM_ERR_INVALID;
496 return -1;
497 }
498 idx += decode_spoe_string(w->buf+idx, w->buf+w->len, &str, &sz);
499 if (str == NULL) {
500 w->status_code = SPOE_FRM_ERR_INVALID;
501 return -1;
502 }
503
504 /* TODO: Find the right verion in supported ones */
505
506 return idx;
507}
508
509/* Check max frame size value. It returns -1 if an error occurred, the number of
510 * read bytes otherwise. */
511static int
512check_max_frame_size(struct worker *w, int idx)
513{
514 uint64_t sz;
515 int type, i;
516
517 /* Get the max-frame-size value of HAProxy */
518 type = w->buf[idx++];
519 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
520 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
521 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
522 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
523 w->status_code = SPOE_FRM_ERR_INVALID;
524 return -1;
525 }
526 if ((i = decode_spoe_varint(w->buf+idx, w->buf+w->len, &sz)) == -1) {
527 w->status_code = SPOE_FRM_ERR_INVALID;
528 return -1;
529 }
530 idx += i;
531
532 /* Keep the lower value */
533 if (sz < w->size)
534 w->size = sz;
535
536 return idx;
537}
538
539/* Check healthcheck value. It returns -1 if an error occurred, the number of
540 * read bytes otherwise. */
541static int
542check_healthcheck(struct worker *w, int idx)
543{
544 int type;
545
546 /* Get the "healthcheck" value of HAProxy */
547 type = w->buf[idx++];
548 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_BOOL) {
549 w->status_code = SPOE_FRM_ERR_INVALID;
550 return -1;
551 }
552 w->healthcheck = ((type & SPOE_DATA_FL_TRUE) == SPOE_DATA_FL_TRUE);
553 return idx;
554}
555
556
557/* Decode a HELLO frame received from HAProxy. It returns -1 if an error
558 * occurred, 0 if the frame must be skipped, otherwise the number of read
559 * bytes. */
560static int
561handle_hahello(struct worker *w)
562{
563 char *end = w->buf+w->len;
564 int i, idx = 0;
565
566 /* Check frame type */
567 if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_HELLO)
568 goto skip;
569
570 /* Skip flags */
571 idx += 4;
572
573 /* stream-id and frame-id must be cleared */
574 if (w->buf[idx] != 0 || w->buf[idx+1] != 0) {
575 w->status_code = SPOE_FRM_ERR_INVALID;
576 goto error;
577 }
578 idx += 2;
579
580 /* Loop on K/V items */
581 while (idx < w->len) {
582 char *str;
583 uint64_t sz;
584
585 /* Decode the item name */
586 idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
587 if (str == NULL) {
588 w->status_code = SPOE_FRM_ERR_INVALID;
589 goto error;
590 }
591
592 /* Check "supported-versions" K/V item */
593 if (!memcmp(str, "supported-versions", sz)) {
594 if ((i = check_proto_version(w, idx)) == -1)
595 goto error;
596 idx = i;
597 }
598 /* Check "max-frame-size" K/V item "*/
599 else if (!memcmp(str, "max-frame-size", sz)) {
600 if ((i = check_max_frame_size(w, idx)) == -1)
601 goto error;
602 idx = i;
603 }
604 /* Check "healthcheck" K/V item "*/
605 else if (!memcmp(str, "healthcheck", sz)) {
606 if ((i = check_healthcheck(w, idx)) == -1)
607 goto error;
608 idx = i;
609 }
610 /* Skip "capabilities" K/V item for now */
611 else {
612 /* Silently ignore unknown item */
613 if ((i = skip_spoe_data(w->buf+idx, end)) == -1) {
614 w->status_code = SPOE_FRM_ERR_INVALID;
615 goto error;
616 }
617 idx += i;
618 }
619 }
620
621 return idx;
622skip:
623 return 0;
624error:
625 return -1;
626}
627
628/* Decode a DISCONNECT frame received from HAProxy. It returns -1 if an error
629 * occurred, 0 if the frame must be skipped, otherwise the number of read
630 * bytes. */
631static int
632handle_hadiscon(struct worker *w)
633{
634 char *end = w->buf+w->len;
635 int i, idx = 0;
636
637 /* Check frame type */
638 if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_DISCON)
639 goto skip;
640
641 /* Skip flags */
642 idx += 4;
643
644 /* stream-id and frame-id must be cleared */
645 if (w->buf[idx] != 0 || w->buf[idx+1] != 0) {
646 w->status_code = SPOE_FRM_ERR_INVALID;
647 goto error;
648 }
649 idx += 2;
650
651 /* Loop on K/V items */
652 while (idx < w->len) {
653 char *str;
654 uint64_t sz;
655
656 /* Decode item key */
657 idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
658 if (str == NULL) {
659 w->status_code = SPOE_FRM_ERR_INVALID;
660 goto error;
661 }
662 /* Silently ignore unknown item */
663 if ((i = skip_spoe_data(w->buf+idx, end)) == -1) {
664 w->status_code = SPOE_FRM_ERR_INVALID;
665 goto error;
666 }
667 idx += i;
668 }
669
670 w->status_code = SPOE_FRM_ERR_NONE;
671 return idx;
672skip:
673 return 0;
674error:
675 return -1;
676}
677
Thierry FOURNIERfbd38242018-02-23 18:24:10 +0100678/* Encode a ACK frame to send it to HAProxy. It returns -1 if an error occurred,
679 * the number of written bytes otherwise. */
680static void prepare_agentack(struct worker *w)
681{
682 w->ack_len = 0;
683
684 /* Frame type */
685 w->ack[w->ack_len++] = SPOE_FRM_T_AGENT_ACK;
686
687 /* No flags for now */
688 memset(w->ack + w->ack_len, 0, 4); /* No flags */
689 w->ack_len += 4;
690
691 /* Set stream-id and frame-id for ACK frames */
692 w->ack_len += encode_spoe_varint(w->stream_id, w->ack + w->ack_len);
693 w->ack_len += encode_spoe_varint(w->frame_id, w->ack + w->ack_len);
694}
695
696static inline
697int set_var_name(struct worker *w, const char *name, int name_len, unsigned char scope)
698{
699 w->ack[w->ack_len++] = SPOE_ACT_T_SET_VAR; /* Action type */
700 w->ack[w->ack_len++] = 3; /* Number of args */
701 w->ack[w->ack_len++] = scope; /* Arg 1: the scope */
702 w->ack_len += encode_spoe_string(name, name_len, w->ack+w->ack_len); /* Arg 2: variable name */
703 return 1;
704}
705
706int set_var_null(struct worker *w,
707 const char *name, int name_len,
708 unsigned char scope)
709{
710 if (!set_var_name(w, name, name_len, scope))
711 return 0;
712 w->ack[w->ack_len++] = SPOE_DATA_T_NULL;
713 return 1;
714}
715
716int set_var_bool(struct worker *w,
717 const char *name, int name_len,
718 unsigned char scope, bool value)
719{
720 if (!set_var_name(w, name, name_len, scope))
721 return 0;
722 w->ack[w->ack_len++] = SPOE_DATA_T_BOOL | (!!value << 4);
723 return 1;
724}
725
726static inline
727int set_var_int(struct worker *w,
728 const char *name, int name_len,
729 unsigned char scope, int type, uint64_t value)
730{
731 if (!set_var_name(w, name, name_len, scope))
732 return 0;
733 w->ack[w->ack_len++] = SPOE_DATA_T_UINT32;
734 w->ack_len += encode_spoe_varint(value, w->ack+w->ack_len); /* Arg 3: variable value */
735 return 1;
736}
737
738int set_var_uint32(struct worker *w,
739 const char *name, int name_len,
740 unsigned char scope, uint32_t value)
741{
742 return set_var_int(w, name, name_len, scope, SPOE_DATA_T_UINT32, value);
743}
744
745int set_var_int32(struct worker *w,
746 const char *name, int name_len,
747 unsigned char scope, int32_t value)
748{
749 return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
750}
751
752int set_var_uint64(struct worker *w,
753 const char *name, int name_len,
754 unsigned char scope, uint64_t value)
755{
756 return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
757}
758
759int set_var_int64(struct worker *w,
760 const char *name, int name_len,
761 unsigned char scope, int64_t value)
762{
763 return set_var_int(w, name, name_len, scope, SPOE_DATA_T_INT32, value);
764}
765
766int set_var_ipv4(struct worker *w,
767 const char *name, int name_len,
768 unsigned char scope,
769 struct in_addr *ipv4)
770{
771 if (!set_var_name(w, name, name_len, scope))
772 return 0;
773 w->ack[w->ack_len++] = SPOE_DATA_T_IPV4;
774 memcpy(w->ack+w->ack_len, ipv4, 4);
775 w->ack_len += 4;
776 return 1;
777}
778
779int set_var_ipv6(struct worker *w,
780 const char *name, int name_len,
781 unsigned char scope,
782 struct in6_addr *ipv6)
783{
784 if (!set_var_name(w, name, name_len, scope))
785 return 0;
786 w->ack[w->ack_len++] = SPOE_DATA_T_IPV6;
787 memcpy(w->ack+w->ack_len, ipv6, 16);
788 w->ack_len += 16;
789 return 1;
790}
791
792static inline
793int set_var_buf(struct worker *w,
794 const char *name, int name_len,
795 unsigned char scope, int type,
796 const char *str, int str_len)
797{
798 if (!set_var_name(w, name, name_len, scope))
799 return 0;
800 w->ack[w->ack_len++] = type;
801 w->ack_len += encode_spoe_string(str, str_len, w->ack+w->ack_len);
802 return 1;
803}
804
805int set_var_string(struct worker *w,
806 const char *name, int name_len,
807 unsigned char scope,
808 const char *str, int strlen)
809{
810 return set_var_buf(w, name, name_len, scope, SPOE_DATA_T_STR, str, strlen);
811}
812
813int set_var_bin(struct worker *w,
814 const char *name, int name_len,
815 unsigned char scope,
816 const char *str, int strlen)
817{
818 return set_var_buf(w, name, name_len, scope, SPOE_DATA_T_BIN, str, strlen);
819}
820
821/* This function is a little bit ugly,
822 * TODO: improve the response without copying the bufer
823 */
824static int commit_agentack(struct worker *w)
825{
826 memcpy(w->buf, w->ack, w->ack_len);
827 w->len = w->ack_len;
828 return 1;
829}
830
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100831/* Decode a NOTIFY frame received from HAProxy. It returns -1 if an error
832 * occurred, 0 if the frame must be skipped, otherwise the number of read
833 * bytes. */
834static int
835handle_hanotify(struct worker *w)
836{
837 char *end = w->buf+w->len;
838 uint64_t stream_id, frame_id;
839 int nbargs, i, idx = 0;
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100840 int index;
841 struct spoe_kv args[256];
842 uint64_t length;
843 struct ps_message *msg;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100844
845 /* Check frame type */
846 if (w->buf[idx++] != SPOE_FRM_T_HAPROXY_NOTIFY)
847 goto skip;
848
849 /* Skip flags */
850 idx += 4;
851
852 /* Read the stream-id */
853 if ((i = decode_spoe_varint(w->buf+idx, end, &stream_id)) == -1) {
854 w->status_code = SPOE_FRM_ERR_INVALID;
855 goto error;
856 }
857 idx += i;
858
859 /* Read the frame-id */
860 if ((i = decode_spoe_varint(w->buf+idx, end, &frame_id)) == -1) {
861 w->status_code = SPOE_FRM_ERR_INVALID;
862 goto error;
863 }
864 idx += i;
865
866 w->stream_id = (unsigned int)stream_id;
867 w->frame_id = (unsigned int)frame_id;
868
869 DEBUG("Notify frame received: stream-id=%u - frame-id=%u",
870 w->stream_id, w->frame_id);
871
Thierry FOURNIERfbd38242018-02-23 18:24:10 +0100872 /* Prepara ack, if the processing fails tha ack will be cancelled */
873 prepare_agentack(w);
874
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100875 /* Loop on messages */
876 while (idx < w->len) {
877 char *str;
878 uint64_t sz;
879
880 /* Decode the message name */
881 idx += decode_spoe_string(w->buf+idx, end, &str, &sz);
882 if (str == NULL) {
883 w->status_code = SPOE_FRM_ERR_INVALID;
884 goto error;
885 }
886 DEBUG(" Message '%.*s' received", (int)sz, str);
887
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100888 /* Decode all SPOE data */
889 nbargs = (unsigned char)w->buf[idx++];
890 for (index = 0; index < nbargs; index++) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100891
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100892 /* Read the key name */
893 if ((i = decode_spoe_string(w->buf+idx, end,
894 &args[index].name.str,
895 &length)) == -1) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100896 w->status_code = SPOE_FRM_ERR_INVALID;
897 goto error;
898 }
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100899 if (length > INT_MAX) {
900 w->status_code = SPOE_FRM_ERR_TOO_BIG;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100901 goto error;
902 }
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100903 args[index].name.len = length;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100904 idx += i;
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100905
906 /* Read the value */
907 memset(&args[index].value, 0, sizeof(args[index].value));
908 if ((i = decode_spoe_data(w->buf+idx, end, &args[index].value)) == -1) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100909 w->status_code = SPOE_FRM_ERR_INVALID;
910 goto error;
911 }
912 idx += i;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100913 }
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100914
915 /* Lookup for existsing bindings. If no existing message
916 * where found, does nothing.
917 */
918 for (msg = ps_messages; msg; msg = msg->next)
919 if (sz == strlen(msg->name) && strncmp(str, msg->name, sz) == 0)
920 break;
921 if (msg == NULL || msg->ps->exec_message == NULL) {
922 DEBUG(" Message '%.*s' have no bindings registered", (int)sz, str);
923 continue;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100924 }
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +0100925
926 /* Process the message */
927 msg->ps->exec_message(w, msg->ref, nbargs, args);
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100928 }
929
930 return idx;
931skip:
932 return 0;
933error:
934 return -1;
935}
936
937/* Encode a HELLO frame to send it to HAProxy. It returns -1 if an error
938 * occurred, the number of written bytes otherwise. */
939static int
940prepare_agenthello(struct worker *w)
941{
942 int idx = 0;
943
944 /* Frame Type */
945 w->buf[idx++] = SPOE_FRM_T_AGENT_HELLO;
946
947 /* No flags for now */
948 memset(w->buf+idx, 0, 4); /* No flags */
949 idx += 4;
950
951 /* No stream-id and frame-id for HELLO frames */
952 w->buf[idx++] = 0;
953 w->buf[idx++] = 0;
954
955 /* "version" K/V item */
956 idx += encode_spoe_string("version", 7, w->buf+idx);
957 w->buf[idx++] = SPOE_DATA_T_STR;
958 idx += encode_spoe_string(SPOP_VERSION, SLEN(SPOP_VERSION), w->buf+idx);
959
960 /* "max-frame-size" K/V item */
961 idx += encode_spoe_string("max-frame-size", 14, w->buf+idx);
962 w->buf[idx++] = SPOE_DATA_T_UINT32;
963 idx += encode_spoe_varint(w->size, w->buf+idx);
964
965 /* "capabilities" K/V item */
966 idx += encode_spoe_string("capabilities", 12, w->buf+idx);
967 w->buf[idx++] = SPOE_DATA_T_STR;
968 idx += encode_spoe_string(SPOA_CAPABILITIES, SLEN(SPOA_CAPABILITIES), w->buf+idx);
969
970 w->len = idx;
971 return idx;
972}
973
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +0100974/* Encode a DISCONNECT frame to send it to HAProxy. It returns -1 if an error
975 * occurred, the number of written bytes otherwise. */
976static int
977prepare_agentdicon(struct worker *w)
978{
979 const char *reason;
980 int rlen, idx = 0;
981
982 if (w->status_code >= SPOE_FRM_ERRS)
983 w->status_code = SPOE_FRM_ERR_UNKNOWN;
984 reason = spoe_frm_err_reasons[w->status_code];
985 rlen = strlen(reason);
986
987 /* Frame type */
988 w->buf[idx++] = SPOE_FRM_T_AGENT_DISCON;
989
990 /* No flags for now */
991 memset(w->buf+idx, 0, 4);
992 idx += 4;
993
994 /* No stream-id and frame-id for DISCONNECT frames */
995 w->buf[idx++] = 0;
996 w->buf[idx++] = 0;
997
998 /* There are 2 mandatory items: "status-code" and "message" */
999
1000 /* "status-code" K/V item */
1001 idx += encode_spoe_string("status-code", 11, w->buf+idx);
1002 w->buf[idx++] = SPOE_DATA_T_UINT32;
1003 idx += encode_spoe_varint(w->status_code, w->buf+idx);
1004
1005 /* "message" K/V item */
1006 idx += encode_spoe_string("message", 7, w->buf+idx);
1007 w->buf[idx++] = SPOE_DATA_T_STR;
1008 idx += encode_spoe_string(reason, rlen, w->buf+idx);
1009
1010 w->len = idx;
1011 return idx;
1012}
1013
1014static int
1015hello_handshake(int sock, struct worker *w)
1016{
1017 if (read_frame(sock, w) < 0) {
1018 LOG("Failed to read Haproxy HELLO frame");
1019 goto error;
1020 }
1021 if (handle_hahello(w) < 0) {
1022 LOG("Failed to handle Haproxy HELLO frame");
1023 goto error;
1024 }
1025 if (prepare_agenthello(w) < 0) {
1026 LOG("Failed to prepare Agent HELLO frame");
1027 goto error;
1028 }
1029 if (write_frame(sock, w) < 0) {
1030 LOG("Failed to write Agent frame");
1031 goto error;
1032 }
1033 DEBUG("Hello handshake done: version=%s - max-frame-size=%u - healthcheck=%s",
1034 SPOP_VERSION, w->size, (w->healthcheck ? "true" : "false"));
1035 return 0;
1036error:
1037 return -1;
1038}
1039
1040static int
1041notify_ack_roundtip(int sock, struct worker *w)
1042{
1043 if (read_frame(sock, w) < 0) {
1044 LOG("Failed to read Haproxy NOTIFY frame");
1045 goto error_or_quit;
1046 }
1047 if (handle_hadiscon(w) != 0) {
1048 if (w->status_code != SPOE_FRM_ERR_NONE)
1049 LOG("Failed to handle Haproxy DISCONNECT frame");
1050 DEBUG("Disconnect frame received: reason=%s",
1051 spoe_frm_err_reasons[w->status_code]);
1052 goto error_or_quit;
1053 }
1054 if (handle_hanotify(w) < 0) {
1055 LOG("Failed to handle Haproxy NOTIFY frame");
1056 goto error_or_quit;
1057 }
Thierry FOURNIERfbd38242018-02-23 18:24:10 +01001058 if (commit_agentack(w) < 0) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001059 LOG("Failed to prepare Agent ACK frame");
1060 goto error_or_quit;
1061 }
1062 if (write_frame(sock, w) < 0) {
1063 LOG("Failed to write Agent ACK frame");
1064 goto error_or_quit;
1065 }
1066 DEBUG("Ack frame sent: stream-id=%u - frame-id=%u",
1067 w->stream_id, w->frame_id);
1068 return 0;
1069error_or_quit:
1070 return -1;
1071}
1072
1073static void *
Thierry FOURNIER5301ed12018-02-23 11:59:15 +01001074spoa_worker(void *data)
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001075{
1076 struct worker w;
1077 struct sockaddr_in client;
1078 int *info = (int *)data;
1079 int csock, lsock = info[0];
Thierry FOURNIER64eaa332018-02-23 14:58:40 +01001080 struct ps *ps;
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001081 int i;
1082 int len;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001083
1084 signal(SIGPIPE, SIG_IGN);
1085 pthread_setspecific(worker_id, &info[1]);
1086
Thierry FOURNIER64eaa332018-02-23 14:58:40 +01001087 /* Init registered processors */
1088 for (ps = ps_list; ps != NULL; ps = ps->next)
1089 ps->init_worker(&w);
1090
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001091 /* Load files */
1092 for (i = 0; i < nfiles; i++) {
1093 len = strlen(files[i]);
1094 for (ps = ps_list; ps != NULL; ps = ps->next)
1095 if (strcmp(files[i] + len - strlen(ps->ext), ps->ext) == 0)
1096 break;
1097 if (ps == NULL) {
1098 LOG("Can't load file \"%s\"\n", files[i]);
1099 goto out;
1100 }
1101 if (!ps->load_file(&w, files[i]))
1102 goto out;
1103 }
1104
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001105 while (1) {
1106 socklen_t sz = sizeof(client);
1107
1108 if ((csock = accept(lsock, (struct sockaddr *)&client, &sz)) < 0) {
1109 LOG("Failed to accept client connection: %m");
1110 goto out;
1111 }
1112 memset(&w, 0, sizeof(w));
1113 w.id = info[1];
1114 w.size = MAX_FRAME_SIZE;
1115
1116 DEBUG("New connection from HAProxy accepted");
1117
1118 if (hello_handshake(csock, &w) < 0)
1119 goto disconnect;
1120 if (w.healthcheck == true)
1121 goto close;
1122 while (1) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001123 if (notify_ack_roundtip(csock, &w) < 0)
1124 break;
1125 }
1126
1127 disconnect:
1128 if (w.status_code == SPOE_FRM_ERR_IO) {
1129 LOG("Close the client socket because of I/O errors");
1130 goto close;
1131 }
1132 if (prepare_agentdicon(&w) < 0) {
1133 LOG("Failed to prepare Agent DISCONNECT frame");
1134 goto close;
1135 }
1136 if (write_frame(csock, &w) < 0) {
1137 LOG("Failed to write Agent DISCONNECT frame");
1138 goto close;
1139 }
1140 DEBUG("Disconnect frame sent: reason=%s",
1141 spoe_frm_err_reasons[w.status_code]);
1142
1143 close:
1144 close(csock);
1145 }
1146
1147out:
1148 free(info);
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001149#if 0
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001150 pthread_exit(NULL);
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001151#endif
1152 return NULL;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001153}
1154
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001155int process_create(pid_t *pid, void *(*ps)(void *), void *data)
1156{
Thierry FOURNIER786e9e62018-02-23 19:11:47 +01001157 if (debug) {
1158 ps(data);
1159 exit(EXIT_SUCCESS);
1160 }
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001161 *pid = fork();
1162 if (*pid == -1)
1163 return -1;
1164 if (*pid > 0)
1165 return 0;
1166 ps(data);
1167 return 0;
1168}
1169
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001170static void
1171usage(char *prog)
1172{
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001173 fprintf(stderr, "Usage: %s [-h] [-d] [-p <port>] [-n <num-workers>] -f <file>\n", prog);
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001174 fprintf(stderr, " -h Print this message\n");
1175 fprintf(stderr, " -d Enable the debug mode\n");
1176 fprintf(stderr, " -p <port> Specify the port to listen on (default: 12345)\n");
1177 fprintf(stderr, " -n <num-workers> Specify the number of workers (default: 5)\n");
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001178 fprintf(stderr, " -f <file> Specify the file whoch contains the processing code.\n");
1179 fprintf(stderr, " This argument can specified more than once.\n");
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001180}
1181
1182int
1183main(int argc, char **argv)
1184{
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001185#if 0
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001186 pthread_t *ts = NULL;
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001187#endif
1188 pid_t *pids;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001189 struct sockaddr_in server;
1190 int i, sock, opt, nbworkers, port;
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001191 int status;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001192
1193 nbworkers = NUM_WORKERS;
1194 port = DEFAULT_PORT;
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001195 while ((opt = getopt(argc, argv, "hdn:p:f:")) != -1) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001196 switch (opt) {
1197 case 'h':
1198 usage(argv[0]);
1199 return EXIT_SUCCESS;
1200 case 'd':
1201 debug = true;
1202 break;
1203 case 'n':
1204 nbworkers = atoi(optarg);
1205 break;
1206 case 'p':
1207 port = atoi(optarg);
1208 break;
Thierry FOURNIER8b9a73b2018-02-23 15:12:55 +01001209 case 'f':
1210 add_file(optarg);
1211 break;
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001212 default:
1213 usage(argv[0]);
1214 return EXIT_FAILURE;
1215 }
1216 }
1217
1218 if (nbworkers <= 0) {
1219 fprintf(stderr, "%s: Invalid number of workers '%d'\n",
1220 argv[0], nbworkers);
1221 goto error;
1222 }
1223 if (port <= 0) {
1224 fprintf(stderr, "%s: Invalid port '%d'\n", argv[0], port);
1225 goto error;
1226 }
1227
1228 if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
1229 fprintf(stderr, "Failed creating socket: %m\n");
1230 goto error;
1231 }
1232
1233 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (int []){1}, sizeof(int));
1234 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (int []){1}, sizeof(int));
1235
1236 memset(&server, 0, sizeof(server));
1237 server.sin_family = AF_INET;
1238 server.sin_addr.s_addr = INADDR_ANY;
1239 server.sin_port = htons(port);
1240
1241 if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
1242 fprintf(stderr, "Failed to bind the socket: %m\n");
1243 goto error;
1244 }
1245
1246 if (listen(sock , 10) < 0) {
1247 fprintf(stderr, "Failed to listen on the socket: %m\n");
1248 goto error;
1249 }
1250 fprintf(stderr, "SPOA is listening on port %d\n", port);
1251
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001252 pthread_key_create(&worker_id, NULL);
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001253
1254 /* Initialise the server in thread mode. This code is commented
1255 * out and not deleted, because later I expect to work with
1256 * process ansd threads. This first version just support processes.
1257 */
1258#if 0
1259 ts = calloc(nbworkers, sizeof(*ts));
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001260 for (i = 0; i < nbworkers; i++) {
1261 int *info = calloc(2, sizeof(*info));
1262
1263 info[0] = sock;
1264 info[1] = i+1;
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001265
Thierry FOURNIER5301ed12018-02-23 11:59:15 +01001266 if (pthread_create(&ts[i], NULL, spoa_worker, info) < 0) {
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001267 fprintf(stderr, "Failed to create thread %d: %m\n", i+1);
1268 goto error;
1269 }
1270 fprintf(stderr, "SPOA worker %02d started\n", i+1);
1271 }
1272
1273 for (i = 0; i < nbworkers; i++) {
1274 pthread_join(ts[i], NULL);
1275 fprintf(stderr, "SPOA worker %02d stopped\n", i+1);
1276 }
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001277 free(ts);
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001278#endif
1279
1280 /* Start processes */
1281 pids = calloc(nbworkers, sizeof(*pids));
1282 if (!pids) {
1283 fprintf(stderr, "Out of memory error\n");
1284 goto error;
1285 }
1286 for (i = 0; i < nbworkers; i++) {
1287 int *info = calloc(2, sizeof(*info));
1288
1289 info[0] = sock;
1290 info[1] = i+1;
1291
1292 if (process_create(&pids[i], spoa_worker, info) == -1) {
1293 fprintf(stderr, "SPOA worker %02d started\n", i+1);
1294 goto error;
1295 }
1296 fprintf(stderr, "SPOA worker %02d started\n", i+1);
1297 }
1298 for (i = 0; i < nbworkers; i++) {
1299 waitpid(pids[0], &status, 0);
1300 fprintf(stderr, "SPOA worker %02d stopped\n", i+1);
1301 }
1302
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001303 close(sock);
Thierry FOURNIER7de6fc62018-02-23 13:50:26 +01001304 pthread_key_delete(worker_id);
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001305 return EXIT_SUCCESS;
Thierry FOURNIERa09df3f2018-02-23 14:42:46 +01001306
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001307error:
Thierry FOURNIERd8b5c772018-02-23 11:40:03 +01001308 return EXIT_FAILURE;
1309}