@cryptotaxi247 / netdata-1 / commits / fa8c70489

Spawn server improvements 5 (#18131)

spawn server feedback is always sent from the spawn server itself

Costa Tsaousis committed Jul 12, 2024 at 21:23 UTC fa8c70489f5616caa9f52cc97488d4717f7eba2e
3 files changed +235 -199
src/libnetdata/os/close_range.c
+17 -6
@@ -67,20 +67,31 @@ static int compare_ints(const void *a, const void *b) {
67 return (int_a > int_b) - (int_a < int_b);
68 }
69
70 -void os_close_all_non_std_open_fds_except(int fds[], size_t fds_num) {
70 +void os_close_all_non_std_open_fds_except(const int fds[], size_t fds_num) {
71 if (fds_num == 0 || fds == NULL) {
72 os_close_range(STDERR_FILENO + 1, CLOSE_RANGE_FD_MAX);
73 return;
74 }
75
76 - qsort(fds, fds_num, sizeof(int), compare_ints);
76 + // copy the fds array to ensure we will not alter them
77 + int fds_copy[fds_num];
78 + memcpy(fds_copy, fds, sizeof(fds_copy));
79 +
80 + qsort(fds_copy, fds_num, sizeof(int), compare_ints);
81
82 int start = STDERR_FILENO + 1;
79 - for (size_t i = 0; i < fds_num; i++) {
80 - if (fds[i] > start)
81 - os_close_range(start, fds[i] - 1);
83 + size_t i = 0;
84 +
85 + // filter out all fds with a number smaller than our start
86 + for (; i < fds_num; i++)
87 + if(fds_copy[i] >= start) break;
88 +
89 + // call os_close_range() as many times as needed
90 + for (; i < fds_num; i++) {
91 + if (fds_copy[i] > start)
92 + os_close_range(start, fds_copy[i] - 1);
93
83 - start = fds[i] + 1;
94 + start = fds_copy[i] + 1;
95 }
96
97 os_close_range(start, CLOSE_RANGE_FD_MAX);
src/libnetdata/os/close_range.h
+1 -1
@@ -7,6 +7,6 @@
7
8 int os_get_fd_open_max(void);
9 void os_close_range(int first, int last);
10 -void os_close_all_non_std_open_fds_except(int fds[], size_t fds_num);
10 +void os_close_all_non_std_open_fds_except(const int fds[], size_t fds_num);
11
12 #endif //CLOSE_RANGE_H
src/libnetdata/spawn_server/spawn_server.c
+217 -192
@@ -23,7 +23,7 @@ struct spawn_server {
23 // it is ignored for PING requests
24
25 int pipe[2];
26 - int server_sock;
26 + int sock; // the listening socket of the server
27 pid_t server_pid;
28 char *path;
29 spawn_request_callback_t cb;
@@ -35,7 +35,7 @@ struct spawn_server {
35
36 struct spawm_instance {
37 size_t request_id;
38 - int client_sock;
38 + int sock;
39 int write_fd;
40 int read_fd;
41 pid_t child_pid;
@@ -322,95 +322,9 @@ static int connect_to_spawn_server(const char *path, bool log) {
322 // --------------------------------------------------------------------------------------------------------------------
323 // the child created by the spawn server
324
325 -typedef enum __attribute__((packed)) {
326 - STATUS_REPORT_NONE = 0,
327 - STATUS_REPORT_STARTED,
328 - STATUS_REPORT_FAILED,
329 - STATUS_REPORT_EXITED,
330 - STATUS_REPORT_PING,
331 -} STATUS_REPORT;
332 -
333 -#define STATUS_REPORT_MAGIC 0xBADA55EE
334 -
335 -struct status_report {
336 - uint32_t magic;
337 - STATUS_REPORT status;
338 - union {
339 - struct {
340 - pid_t pid;
341 - } started;
342 -
343 - struct {
344 - int err_no;
345 - } failed;
346 -
347 - struct {
348 - int waitpid_status;
349 - } exited;
350 - };
351 -};
352 -
353 -static void spawn_server_send_status_ping(int sock) {
354 - struct status_report sr = {
355 - .magic = STATUS_REPORT_MAGIC,
356 - .status = STATUS_REPORT_PING,
357 - };
358 -
359 - if(write(sock, &sr, sizeof(sr)) != sizeof(sr))
360 - nd_log(NDLS_COLLECTORS, NDLP_ERR,
361 - "SPAWN SERVER: Cannot send ping reply.");
362 -}
363 -
364 -static void spawn_server_send_status_success(SPAWN_REQUEST *rq) {
365 - const struct status_report sr = {
366 - .magic = STATUS_REPORT_MAGIC,
367 - .status = STATUS_REPORT_STARTED,
368 - .started = {
369 - .pid = getpid(),
370 - },
371 - };
372 -
373 - if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
374 - nd_log(NDLS_COLLECTORS, NDLP_ERR,
375 - "SPAWN SERVER: Cannot send success status report for request %zu: %s",
376 - rq->request_id, rq->cmdline);
377 -}
378 -
379 -static void spawn_server_send_status_failure(SPAWN_REQUEST *rq) {
380 - struct status_report sr = {
381 - .magic = STATUS_REPORT_MAGIC,
382 - .status = STATUS_REPORT_FAILED,
383 - .failed = {
384 - .err_no = errno,
385 - },
386 - };
387 -
388 - if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
389 - nd_log(NDLS_COLLECTORS, NDLP_ERR,
390 - "SPAWN SERVER: Cannot send failure status report for request %zu: %s",
391 - rq->request_id, rq->cmdline);
392 -}
393 -
394 -static void spawn_server_send_status_exit(SPAWN_REQUEST *rq, int waitpid_status) {
395 - struct status_report sr = {
396 - .magic = STATUS_REPORT_MAGIC,
397 - .status = STATUS_REPORT_EXITED,
398 - .exited = {
399 - .waitpid_status = waitpid_status,
400 - },
401 - };
402 -
403 - if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
404 - nd_log(NDLS_COLLECTORS, NDLP_ERR,
405 - "SPAWN SERVER: Cannot send exit status (%d) report for request %zu: %s",
406 - waitpid_status, rq->request_id, rq->cmdline);
407 -}
408 -
325 static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
410 - // fprintf(stderr, "CHILD: running request %zu on pid %d\n", request->request_id, getpid());
411 -
326 // close the server sockets;
413 - close(server->server_sock); server->server_sock = -1;
327 + close(server->sock); server->sock = -1;
328 if(server->pipe[0] != -1) { close(server->pipe[0]); server->pipe[0] = -1; }
329 if(server->pipe[1] != -1) { close(server->pipe[1]); server->pipe[1] = -1; }
330
@@ -421,6 +335,9 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
335 os_setproctitle(buf, server->argc, server->argv);
336 }
337
338 + // just a precausion in case we have any left-over fds
339 + os_close_all_non_std_open_fds_except(rq->fds, SPAWN_SERVER_TRANSFER_FDS);
340 +
341 // get the fds from the request
342 int stdin_fd = rq->fds[0];
343 int stdout_fd = rq->fds[1];
@@ -429,15 +346,21 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
346
347 // change stdio fds to the ones in the request
348 if (dup2(stdin_fd, STDIN_FILENO) == -1) {
432 - spawn_server_send_status_failure(rq);
349 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
350 + "SPAWN SERVER: cannot dup2(%d) stdin of request No %zu: %s",
351 + stdin_fd, rq->request_id, rq->cmdline);
352 exit(1);
353 }
354 if (dup2(stdout_fd, STDOUT_FILENO) == -1) {
436 - spawn_server_send_status_failure(rq);
355 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
356 + "SPAWN SERVER: cannot dup2(%d) stdin of request No %zu: %s",
357 + stdout_fd, rq->request_id, rq->cmdline);
358 exit(1);
359 }
360 if (dup2(stderr_fd, STDERR_FILENO) == -1) {
440 - spawn_server_send_status_failure(rq);
361 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
362 + "SPAWN SERVER: cannot dup2(%d) stderr of request No %zu: %s",
363 + stderr_fd, rq->request_id, rq->cmdline);
364 exit(1);
365 }
366
@@ -453,25 +376,15 @@ static void spawn_server_run_child(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
376 switch (rq->type) {
377
378 case SPAWN_INSTANCE_TYPE_EXEC:
456 - spawn_server_send_status_success(rq);
457 - close(rq->sock); rq->sock = -1;
458 - close(custom_fd); custom_fd = -1;
379 + if(custom_fd != -1) { close(custom_fd); custom_fd = -1; }
380 execvp(rq->argv[0], (char **)rq->argv);
381 nd_log(NDLS_COLLECTORS, NDLP_ERR,
461 - "SPAWN SERVER: Failed to execute command of request No %zu (argv[0] = '%s')",
462 - rq->request_id, rq->argv[0]);
382 + "SPAWN SERVER: Failed to execute command of request No %zu: %s",
383 + rq->request_id, rq->cmdline);
384 exit(1);
385 break;
386
387 case SPAWN_INSTANCE_TYPE_CALLBACK:
467 - if(server->cb == NULL) {
468 - errno = ENOENT;
469 - spawn_server_send_status_failure(rq);
470 - close(rq->sock); rq->sock = -1;
471 - exit(1);
472 - }
473 - spawn_server_send_status_success(rq);
474 - close(rq->sock); rq->sock = -1;
388 server->cb(rq);
389 exit(0);
390 break;
@@ -592,6 +505,177 @@ static BUFFER *argv_to_cmdline_buffer(const char **argv) {
505 return wb;
506 }
507
508 +// --------------------------------------------------------------------------------------------------------------------
509 +// status reports
510 +
511 +typedef enum __attribute__((packed)) {
512 + STATUS_REPORT_NONE = 0,
513 + STATUS_REPORT_STARTED,
514 + STATUS_REPORT_FAILED,
515 + STATUS_REPORT_EXITED,
516 + STATUS_REPORT_PING,
517 +} STATUS_REPORT;
518 +
519 +#define STATUS_REPORT_MAGIC 0xBADA55EE
520 +
521 +struct status_report {
522 + uint32_t magic;
523 + STATUS_REPORT status;
524 + union {
525 + struct {
526 + pid_t pid;
527 + } started;
528 +
529 + struct {
530 + int err_no;
531 + } failed;
532 +
533 + struct {
534 + int waitpid_status;
535 + } exited;
536 + };
537 +};
538 +
539 +static void spawn_server_send_status_ping(int sock) {
540 + struct status_report sr = {
541 + .magic = STATUS_REPORT_MAGIC,
542 + .status = STATUS_REPORT_PING,
543 + };
544 +
545 + if(write(sock, &sr, sizeof(sr)) != sizeof(sr))
546 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
547 + "SPAWN SERVER: Cannot send ping reply.");
548 +}
549 +
550 +static void spawn_server_send_status_success(SPAWN_REQUEST *rq) {
551 + const struct status_report sr = {
552 + .magic = STATUS_REPORT_MAGIC,
553 + .status = STATUS_REPORT_STARTED,
554 + .started = {
555 + .pid = rq->pid,
556 + },
557 + };
558 +
559 + if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
560 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
561 + "SPAWN SERVER: Cannot send success status report for pid %d, request %zu: %s",
562 + rq->pid, rq->request_id, rq->cmdline);
563 +}
564 +
565 +static void spawn_server_send_status_failure(SPAWN_REQUEST *rq) {
566 + struct status_report sr = {
567 + .magic = STATUS_REPORT_MAGIC,
568 + .status = STATUS_REPORT_FAILED,
569 + .failed = {
570 + .err_no = errno,
571 + },
572 + };
573 +
574 + if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
575 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
576 + "SPAWN SERVER: Cannot send failure status report for request %zu: %s",
577 + rq->request_id, rq->cmdline);
578 +}
579 +
580 +static void spawn_server_send_status_exit(SPAWN_REQUEST *rq, int waitpid_status) {
581 + struct status_report sr = {
582 + .magic = STATUS_REPORT_MAGIC,
583 + .status = STATUS_REPORT_EXITED,
584 + .exited = {
585 + .waitpid_status = waitpid_status,
586 + },
587 + };
588 +
589 + if(write(rq->sock, &sr, sizeof(sr)) != sizeof(sr))
590 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
591 + "SPAWN SERVER: Cannot send exit status (%d) report for pid %d, request %zu: %s",
592 + waitpid_status, rq->pid, rq->request_id, rq->cmdline);
593 +}
594 +
595 +// --------------------------------------------------------------------------------------------------------------------
596 +// execute a received request
597 +
598 +static void request_free(SPAWN_REQUEST *rq) {
599 + if(rq->fds[0] != -1) close(rq->fds[0]);
600 + if(rq->fds[1] != -1) close(rq->fds[1]);
601 + if(rq->fds[2] != -1) close(rq->fds[2]);
602 + if(rq->fds[3] != -1) close(rq->fds[3]);
603 + if(rq->sock != -1) close(rq->sock);
604 + freez((void *)rq->argv);
605 + freez((void *)rq->environment);
606 + freez((void *)rq->data);
607 + freez((void *)rq->cmdline);
608 + freez((void *)rq);
609 +}
610 +
611 +static void spawn_server_execute_request(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
612 + switch(rq->type) {
613 + case SPAWN_INSTANCE_TYPE_EXEC:
614 + // close custom_fd - it is not needed for exec mode
615 + if(rq->fds[3] != -1) { close(rq->fds[3]); rq->fds[3] = -1; }
616 +
617 + // create the cmdline for logs
618 + if(rq->argv) {
619 + CLEAN_BUFFER *wb = argv_to_cmdline_buffer(rq->argv);
620 + rq->cmdline = strdupz(buffer_tostring(wb));
621 + }
622 + break;
623 +
624 + case SPAWN_INSTANCE_TYPE_CALLBACK:
625 + if(server->cb == NULL) {
626 + errno = ENOSYS;
627 + spawn_server_send_status_failure(rq);
628 + request_free(rq);
629 + return;
630 + }
631 + rq->cmdline = strdupz("callback() function");
632 + break;
633 +
634 + default:
635 + errno = EINVAL;
636 + spawn_server_send_status_failure(rq);
637 + request_free(rq);
638 + return;
639 + }
640 +
641 + pid_t pid = fork();
642 + if (pid < 0) {
643 + // fork failed
644 +
645 + nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: Failed to fork() child.");
646 + spawn_server_send_status_failure(rq);
647 + request_free(rq);
648 + return;
649 + }
650 + else if (pid == 0) {
651 + // the child
652 +
653 + spawn_server_run_child(server, rq);
654 + exit(63);
655 + }
656 +
657 + // the parent
658 + rq->pid = pid;
659 +
660 + // let the parent know
661 + spawn_server_send_status_success(rq);
662 +
663 + // do not keep data we don't need at the parent
664 + freez((void *)rq->environment); rq->environment = NULL;
665 + freez((void *)rq->argv); rq->argv = NULL;
666 + freez((void *)rq->data); rq->data = NULL;
667 + rq->data_size = 0;
668 +
669 + // do not keep fds we don't need at the parent
670 + if(rq->fds[0] != -1) { close(rq->fds[0]); rq->fds[0] = -1; }
671 + if(rq->fds[1] != -1) { close(rq->fds[1]); rq->fds[1] = -1; }
672 + if(rq->fds[2] != -1) { close(rq->fds[2]); rq->fds[2] = -1; }
673 + if(rq->fds[3] != -1) { close(rq->fds[3]); rq->fds[3] = -1; }
674 +
675 + // keep it in the list
676 + DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(spawn_server_requests, rq, prev, next);
677 +}
678 +
679 // --------------------------------------------------------------------------------------------------------------------
680 // Sending and receiving requests
681
@@ -747,75 +831,6 @@ cleanup:
831 return ret;
832 }
833
750 -static void request_free(SPAWN_REQUEST *rq) {
751 - if(rq->fds[0] != -1) close(rq->fds[0]);
752 - if(rq->fds[1] != -1) close(rq->fds[1]);
753 - if(rq->fds[2] != -1) close(rq->fds[2]);
754 - if(rq->fds[3] != -1) close(rq->fds[3]);
755 - if(rq->sock != -1) close(rq->sock);
756 - freez((void *)rq->argv);
757 - freez((void *)rq->environment);
758 - freez((void *)rq->data);
759 - freez((void *)rq->cmdline);
760 - freez((void *)rq);
761 -}
762 -
763 -static void spawn_server_execute_request(SPAWN_SERVER *server, SPAWN_REQUEST *rq) {
764 - switch(rq->type) {
765 - case SPAWN_INSTANCE_TYPE_EXEC:
766 - if(rq->argv) {
767 - CLEAN_BUFFER *wb = argv_to_cmdline_buffer(rq->argv);
768 - rq->cmdline = strdupz(buffer_tostring(wb));
769 - }
770 - break;
771 -
772 - case SPAWN_INSTANCE_TYPE_CALLBACK:
773 - rq->cmdline = strdupz("callback() function");
774 - break;
775 -
776 - default:
777 - rq->cmdline = strdupz("[unknown request type]");
778 - break;
779 - }
780 -
781 - pid_t pid = fork();
782 - if (pid < 0) {
783 - // fork failed
784 -
785 - nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: Failed to fork() child.");
786 - spawn_server_send_status_failure(rq);
787 - request_free(rq);
788 - return;
789 - }
790 - else if (pid == 0) {
791 - // the child
792 -
793 - spawn_server_run_child(server, rq);
794 - exit(63);
795 - }
796 -
797 - // the parent
798 - rq->pid = pid;
799 -
800 - // do not keep data we don't need at the parent
801 - freez((void *)rq->environment); rq->environment = NULL;
802 - freez((void *)rq->argv); rq->argv = NULL;
803 - freez((void *)rq->data); rq->data = NULL;
804 - rq->data_size = 0;
805 -
806 - // do not keep fds we don't need at the parent
807 - if(rq->fds[0] != -1) { close(rq->fds[0]); rq->fds[0] = -1; }
808 - if(rq->fds[1] != -1) { close(rq->fds[1]); rq->fds[1] = -1; }
809 - if(rq->fds[2] != -1) { close(rq->fds[2]); rq->fds[2] = -1; }
810 - if(rq->fds[3] != -1) { close(rq->fds[3]); rq->fds[3] = -1; }
811 -
812 - // keep it in the list
813 - DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(spawn_server_requests, rq, prev, next);
814 -
815 - // do not fork this socket on other children
816 - sock_setcloexec(rq->sock);
817 -}
818 -
834 static void spawn_server_receive_request(int sock, SPAWN_SERVER *server) {
835 struct msghdr msg = {0};
836 struct iovec iov[7];
@@ -1110,7 +1125,7 @@ static void spawn_server_event_loop(SPAWN_SERVER *server) {
1125 }
1126
1127 struct pollfd fds[2];
1113 - fds[0].fd = server->server_sock;
1128 + fds[0].fd = server->sock;
1129 fds[0].events = POLLIN;
1130 fds[1].fd = pipe_fd;
1131 fds[1].events = POLLHUP | POLLERR;
@@ -1137,13 +1152,17 @@ static void spawn_server_event_loop(SPAWN_SERVER *server) {
1152 }
1153
1154 if (fds[0].revents & POLLIN) {
1140 - int client_sock = accept(server->server_sock, NULL, NULL);
1141 - if (client_sock == -1) {
1155 + int sock = accept(server->sock, NULL, NULL);
1156 + if (sock == -1) {
1157 nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: accept() failed");
1158 continue;
1159 }
1160
1146 - spawn_server_receive_request(client_sock, server);
1161 + // do not fork this socket
1162 + sock_setcloexec(sock);
1163 +
1164 + // receive the request and process it
1165 + spawn_server_receive_request(sock, server);
1166 }
1167 }
1168
@@ -1174,7 +1193,7 @@ static void spawn_server_event_loop(SPAWN_SERVER *server) {
1193 void spawn_server_destroy(SPAWN_SERVER *server) {
1194 if(server->pipe[0] != -1) close(server->pipe[0]);
1195 if(server->pipe[1] != -1) close(server->pipe[1]);
1177 - if(server->server_sock != -1) close(server->server_sock);
1196 + if(server->sock != -1) close(server->sock);
1197
1198 if(server->server_pid) {
1199 kill(server->server_pid, SIGTERM);
@@ -1196,7 +1215,7 @@ static bool spawn_server_create_listening_socket(SPAWN_SERVER *server) {
1215 return false;
1216 }
1217
1199 - if ((server->server_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
1218 + if ((server->sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
1219 nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: Failed to create socket()");
1220 return false;
1221 }
@@ -1208,12 +1227,12 @@ static bool spawn_server_create_listening_socket(SPAWN_SERVER *server) {
1227 unlink(server->path);
1228 errno = 0;
1229
1211 - if (bind(server->server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
1230 + if (bind(server->sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
1231 nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: Failed to bind()");
1232 return false;
1233 }
1234
1216 - if (listen(server->server_sock, 5) == -1) {
1235 + if (listen(server->sock, 5) == -1) {
1236 nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN SERVER: Failed to listen()");
1237 return false;
1238 }
@@ -1250,7 +1269,7 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1269 SPAWN_SERVER *server = callocz(1, sizeof(SPAWN_SERVER));
1270 server->pipe[0] = -1;
1271 server->pipe[1] = -1;
1253 - server->server_sock = -1;
1272 + server->sock = -1;
1273 server->cb = child_callback;
1274 server->argc = argc;
1275 server->argv = argv;
@@ -1309,6 +1328,7 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1328 pid_t pid = fork();
1329 if (pid == 0) {
1330 // the child - the spawn server
1331 +
1332 {
1333 char buf[15];
1334 snprintfz(buf, sizeof(buf), "spawn-%s", server->name);
@@ -1316,13 +1336,13 @@ SPAWN_SERVER* spawn_server_create(SPAWN_SERVER_OPTIONS options, const char *name
1336 }
1337
1338 replace_stdio_with_dev_null();
1319 - os_close_all_non_std_open_fds_except((int[]){ server->server_sock, server->pipe[1] }, 2);
1339 + os_close_all_non_std_open_fds_except((int[]){ server->sock, server->pipe[1] }, 2);
1340 spawn_server_event_loop(server);
1341 }
1342 else if (pid > 0) {
1343 // the parent
1344 server->server_pid = pid;
1325 - close(server->server_sock); server->server_sock = -1;
1345 + close(server->sock); server->sock = -1;
1346 close(server->pipe[1]); server->pipe[1] = -1;
1347
1348 struct status_report sr = { 0 };
@@ -1358,7 +1378,7 @@ void spawn_server_exec_destroy(SPAWN_INSTANCE *instance) {
1378 if(instance->child_pid) kill(instance->child_pid, SIGTERM);
1379 if(instance->write_fd != -1) close(instance->write_fd);
1380 if(instance->read_fd != -1) close(instance->read_fd);
1361 - if(instance->client_sock != -1) close(instance->client_sock);
1381 + if(instance->sock != -1) close(instance->sock);
1382 freez(instance);
1383 }
1384
@@ -1371,7 +1391,7 @@ int spawn_server_exec_wait(SPAWN_SERVER *server __maybe_unused, SPAWN_INSTANCE *
1391
1392 // get the result
1393 struct status_report sr = { 0 };
1374 - if(read(instance->client_sock, &sr, sizeof(sr)) != sizeof(sr))
1394 + if(read(instance->sock, &sr, sizeof(sr)) != sizeof(sr))
1395 nd_log(NDLS_COLLECTORS, NDLP_ERR,
1396 "SPAWN PARENT: failed to read final status report for child %d, request %zu",
1397 instance->child_pid, instance->request_id);
@@ -1414,8 +1434,8 @@ SPAWN_INSTANCE* spawn_server_exec(SPAWN_SERVER *server, int stderr_fd, int custo
1434 instance->read_fd = -1;
1435 instance->write_fd = -1;
1436
1417 - instance->client_sock = connect_to_spawn_server(server->path, true);
1418 - if(instance->client_sock == -1)
1437 + instance->sock = connect_to_spawn_server(server->path, true);
1438 + if(instance->sock == -1)
1439 goto cleanup;
1440
1441 if (pipe(pipe_stdin) == -1) {
@@ -1430,7 +1450,7 @@ SPAWN_INSTANCE* spawn_server_exec(SPAWN_SERVER *server, int stderr_fd, int custo
1450
1451 SPAWN_REQUEST request = {
1452 .request_id = __atomic_add_fetch(&server->request_id, 1, __ATOMIC_RELAXED),
1433 - .sock = instance->client_sock,
1453 + .sock = instance->sock,
1454 .fds = {
1455 [0] = pipe_stdin[0],
1456 [1] = pipe_stdout[1],
@@ -1453,8 +1473,11 @@ SPAWN_INSTANCE* spawn_server_exec(SPAWN_SERVER *server, int stderr_fd, int custo
1473 close(pipe_stdout[1]); pipe_stdout[1] = -1;
1474 instance->read_fd = pipe_stdout[0]; pipe_stdout[0] = -1;
1475
1476 + // copy the request id to the instance
1477 + instance->request_id = request.request_id;
1478 +
1479 struct status_report sr = { 0 };
1457 - if(read(instance->client_sock, &sr, sizeof(sr)) != sizeof(sr)) {
1480 + if(read(instance->sock, &sr, sizeof(sr)) != sizeof(sr)) {
1481 nd_log(NDLS_COLLECTORS, NDLP_ERR,
1482 "SPAWN PARENT: Failed to exec spawn request %zu (cannot get initial status report)",
1483 request.request_id);
@@ -1491,7 +1514,9 @@ SPAWN_INSTANCE* spawn_server_exec(SPAWN_SERVER *server, int stderr_fd, int custo
1514
1515 default:
1516 errno = 0;
1494 - nd_log(NDLS_COLLECTORS, NDLP_ERR, "SPAWN PARENT: Invalid status report to exec spawn request %zu (received invalid data)", request.request_id);
1517 + nd_log(NDLS_COLLECTORS, NDLP_ERR,
1518 + "SPAWN PARENT: Invalid status report to exec spawn request %zu (received invalid data)",
1519 + request.request_id);
1520 break;
1521 }
1522