Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * vsock test utilities
4 *
5 * Copyright (C) 2017 Red Hat, Inc.
6 *
7 * Author: Stefan Hajnoczi <stefanha@redhat.com>
8 */
9
10#include <errno.h>
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15#include <signal.h>
16#include <unistd.h>
17#include <assert.h>
18#include <sys/epoll.h>
19#include <sys/mman.h>
20#include <linux/sockios.h>
21
22#include "timeout.h"
23#include "control.h"
24#include "util.h"
25
26/* Install signal handlers */
27void init_signals(void)
28{
29 struct sigaction act = {
30 .sa_handler = sigalrm,
31 };
32
33 sigaction(SIGALRM, &act, NULL);
34 signal(SIGPIPE, SIG_IGN);
35}
36
37static unsigned int parse_uint(const char *str, const char *err_str)
38{
39 char *endptr = NULL;
40 unsigned long n;
41
42 errno = 0;
43 n = strtoul(str, &endptr, 10);
44 if (errno || *endptr != '\0') {
45 fprintf(stderr, "malformed %s \"%s\"\n", err_str, str);
46 exit(EXIT_FAILURE);
47 }
48 return n;
49}
50
51/* Parse a CID in string representation */
52unsigned int parse_cid(const char *str)
53{
54 return parse_uint(str, "CID");
55}
56
57/* Parse a port in string representation */
58unsigned int parse_port(const char *str)
59{
60 return parse_uint(str, "port");
61}
62
63/* Wait for the remote to close the connection */
64void vsock_wait_remote_close(int fd)
65{
66 struct epoll_event ev;
67 int epollfd, nfds;
68
69 epollfd = epoll_create1(0);
70 if (epollfd == -1) {
71 perror("epoll_create1");
72 exit(EXIT_FAILURE);
73 }
74
75 ev.events = EPOLLRDHUP | EPOLLHUP;
76 ev.data.fd = fd;
77 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
78 perror("epoll_ctl");
79 exit(EXIT_FAILURE);
80 }
81
82 nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
83 if (nfds == -1) {
84 perror("epoll_wait");
85 exit(EXIT_FAILURE);
86 }
87
88 if (nfds == 0) {
89 fprintf(stderr, "epoll_wait timed out\n");
90 exit(EXIT_FAILURE);
91 }
92
93 assert(nfds == 1);
94 assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
95 assert(ev.data.fd == fd);
96
97 close(epollfd);
98}
99
100/* Wait until transport reports no data left to be sent.
101 * Return false if transport does not implement the unsent_bytes() callback.
102 */
103bool vsock_wait_sent(int fd)
104{
105 int ret, sock_bytes_unsent;
106
107 timeout_begin(TIMEOUT);
108 do {
109 ret = ioctl(fd, SIOCOUTQ, &sock_bytes_unsent);
110 if (ret < 0) {
111 if (errno == EOPNOTSUPP)
112 break;
113
114 perror("ioctl(SIOCOUTQ)");
115 exit(EXIT_FAILURE);
116 }
117 timeout_check("SIOCOUTQ");
118 } while (sock_bytes_unsent != 0);
119 timeout_end();
120
121 return !ret;
122}
123
124/* Create socket <type>, bind to <cid, port> and return the file descriptor. */
125int vsock_bind(unsigned int cid, unsigned int port, int type)
126{
127 struct sockaddr_vm sa = {
128 .svm_family = AF_VSOCK,
129 .svm_cid = cid,
130 .svm_port = port,
131 };
132 int fd;
133
134 fd = socket(AF_VSOCK, type, 0);
135 if (fd < 0) {
136 perror("socket");
137 exit(EXIT_FAILURE);
138 }
139
140 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
141 perror("bind");
142 exit(EXIT_FAILURE);
143 }
144
145 return fd;
146}
147
148int vsock_connect_fd(int fd, unsigned int cid, unsigned int port)
149{
150 struct sockaddr_vm sa = {
151 .svm_family = AF_VSOCK,
152 .svm_cid = cid,
153 .svm_port = port,
154 };
155 int ret;
156
157 timeout_begin(TIMEOUT);
158 do {
159 ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
160 timeout_check("connect");
161 } while (ret < 0 && errno == EINTR);
162 timeout_end();
163
164 return ret;
165}
166
167/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
168int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
169{
170 int client_fd;
171
172 client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
173
174 if (vsock_connect_fd(client_fd, cid, port)) {
175 perror("connect");
176 exit(EXIT_FAILURE);
177 }
178
179 return client_fd;
180}
181
182/* Connect to <cid, port> and return the file descriptor. */
183int vsock_connect(unsigned int cid, unsigned int port, int type)
184{
185 int fd;
186
187 control_expectln("LISTENING");
188
189 fd = socket(AF_VSOCK, type, 0);
190 if (fd < 0) {
191 perror("socket");
192 exit(EXIT_FAILURE);
193 }
194
195 if (vsock_connect_fd(fd, cid, port)) {
196 int old_errno = errno;
197
198 close(fd);
199 fd = -1;
200 errno = old_errno;
201 }
202
203 return fd;
204}
205
206int vsock_stream_connect(unsigned int cid, unsigned int port)
207{
208 return vsock_connect(cid, port, SOCK_STREAM);
209}
210
211int vsock_seqpacket_connect(unsigned int cid, unsigned int port)
212{
213 return vsock_connect(cid, port, SOCK_SEQPACKET);
214}
215
216/* Listen on <cid, port> and return the file descriptor. */
217static int vsock_listen(unsigned int cid, unsigned int port, int type)
218{
219 int fd;
220
221 fd = vsock_bind(cid, port, type);
222
223 if (listen(fd, 1) < 0) {
224 perror("listen");
225 exit(EXIT_FAILURE);
226 }
227
228 return fd;
229}
230
231/* Listen on <cid, port> and return the first incoming connection. The remote
232 * address is stored to clientaddrp. clientaddrp may be NULL.
233 */
234int vsock_accept(unsigned int cid, unsigned int port,
235 struct sockaddr_vm *clientaddrp, int type)
236{
237 union {
238 struct sockaddr sa;
239 struct sockaddr_vm svm;
240 } clientaddr;
241 socklen_t clientaddr_len = sizeof(clientaddr.svm);
242 int fd, client_fd, old_errno;
243
244 fd = vsock_listen(cid, port, type);
245
246 control_writeln("LISTENING");
247
248 timeout_begin(TIMEOUT);
249 do {
250 client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
251 timeout_check("accept");
252 } while (client_fd < 0 && errno == EINTR);
253 timeout_end();
254
255 old_errno = errno;
256 close(fd);
257 errno = old_errno;
258
259 if (client_fd < 0)
260 return client_fd;
261
262 if (clientaddr_len != sizeof(clientaddr.svm)) {
263 fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
264 (size_t)clientaddr_len);
265 exit(EXIT_FAILURE);
266 }
267 if (clientaddr.sa.sa_family != AF_VSOCK) {
268 fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
269 clientaddr.sa.sa_family);
270 exit(EXIT_FAILURE);
271 }
272
273 if (clientaddrp)
274 *clientaddrp = clientaddr.svm;
275 return client_fd;
276}
277
278int vsock_stream_accept(unsigned int cid, unsigned int port,
279 struct sockaddr_vm *clientaddrp)
280{
281 return vsock_accept(cid, port, clientaddrp, SOCK_STREAM);
282}
283
284int vsock_stream_listen(unsigned int cid, unsigned int port)
285{
286 return vsock_listen(cid, port, SOCK_STREAM);
287}
288
289int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
290 struct sockaddr_vm *clientaddrp)
291{
292 return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
293}
294
295/* Transmit bytes from a buffer and check the return value.
296 *
297 * expected_ret:
298 * <0 Negative errno (for testing errors)
299 * 0 End-of-file
300 * >0 Success (bytes successfully written)
301 */
302void send_buf(int fd, const void *buf, size_t len, int flags,
303 ssize_t expected_ret)
304{
305 ssize_t nwritten = 0;
306 ssize_t ret;
307
308 timeout_begin(TIMEOUT);
309 do {
310 ret = send(fd, buf + nwritten, len - nwritten, flags);
311 timeout_check("send");
312
313 if (ret == 0 || (ret < 0 && errno != EINTR))
314 break;
315
316 nwritten += ret;
317 } while (nwritten < len);
318 timeout_end();
319
320 if (expected_ret < 0) {
321 if (ret != -1) {
322 fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
323 ret, expected_ret);
324 exit(EXIT_FAILURE);
325 }
326 if (errno != -expected_ret) {
327 perror("send");
328 exit(EXIT_FAILURE);
329 }
330 return;
331 }
332
333 if (ret < 0) {
334 perror("send");
335 exit(EXIT_FAILURE);
336 }
337
338 if (nwritten != expected_ret) {
339 if (ret == 0)
340 fprintf(stderr, "unexpected EOF while sending bytes\n");
341
342 fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
343 nwritten, expected_ret);
344 exit(EXIT_FAILURE);
345 }
346}
347
348/* Receive bytes in a buffer and check the return value.
349 *
350 * expected_ret:
351 * <0 Negative errno (for testing errors)
352 * 0 End-of-file
353 * >0 Success (bytes successfully read)
354 */
355void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
356{
357 ssize_t nread = 0;
358 ssize_t ret;
359
360 timeout_begin(TIMEOUT);
361 do {
362 ret = recv(fd, buf + nread, len - nread, flags);
363 timeout_check("recv");
364
365 if (ret == 0 || (ret < 0 && errno != EINTR))
366 break;
367
368 nread += ret;
369 } while (nread < len);
370 timeout_end();
371
372 if (expected_ret < 0) {
373 if (ret != -1) {
374 fprintf(stderr, "bogus recv(2) return value %zd (expected %zd)\n",
375 ret, expected_ret);
376 exit(EXIT_FAILURE);
377 }
378 if (errno != -expected_ret) {
379 perror("recv");
380 exit(EXIT_FAILURE);
381 }
382 return;
383 }
384
385 if (ret < 0) {
386 perror("recv");
387 exit(EXIT_FAILURE);
388 }
389
390 if (nread != expected_ret) {
391 if (ret == 0)
392 fprintf(stderr, "unexpected EOF while receiving bytes\n");
393
394 fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n",
395 nread, expected_ret);
396 exit(EXIT_FAILURE);
397 }
398}
399
400/* Transmit one byte and check the return value.
401 *
402 * expected_ret:
403 * <0 Negative errno (for testing errors)
404 * 0 End-of-file
405 * 1 Success
406 */
407void send_byte(int fd, int expected_ret, int flags)
408{
409 static const uint8_t byte = 'A';
410
411 send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
412}
413
414/* Receive one byte and check the return value.
415 *
416 * expected_ret:
417 * <0 Negative errno (for testing errors)
418 * 0 End-of-file
419 * 1 Success
420 */
421void recv_byte(int fd, int expected_ret, int flags)
422{
423 uint8_t byte;
424
425 recv_buf(fd, &byte, sizeof(byte), flags, expected_ret);
426
427 if (byte != 'A') {
428 fprintf(stderr, "unexpected byte read 0x%02x\n", byte);
429 exit(EXIT_FAILURE);
430 }
431}
432
433/* Run test cases. The program terminates if a failure occurs. */
434void run_tests(const struct test_case *test_cases,
435 const struct test_opts *opts)
436{
437 int i;
438
439 for (i = 0; test_cases[i].name; i++) {
440 void (*run)(const struct test_opts *opts);
441 char *line;
442
443 printf("%d - %s...", i, test_cases[i].name);
444 fflush(stdout);
445
446 /* Full barrier before executing the next test. This
447 * ensures that client and server are executing the
448 * same test case. In particular, it means whoever is
449 * faster will not see the peer still executing the
450 * last test. This is important because port numbers
451 * can be used by multiple test cases.
452 */
453 if (test_cases[i].skip)
454 control_writeln("SKIP");
455 else
456 control_writeln("NEXT");
457
458 line = control_readln();
459 if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
460
461 printf("skipped\n");
462
463 free(line);
464 continue;
465 }
466
467 control_cmpln(line, "NEXT", true);
468 free(line);
469
470 if (opts->mode == TEST_MODE_CLIENT)
471 run = test_cases[i].run_client;
472 else
473 run = test_cases[i].run_server;
474
475 if (run)
476 run(opts);
477
478 printf("ok\n");
479 }
480}
481
482void list_tests(const struct test_case *test_cases)
483{
484 int i;
485
486 printf("ID\tTest name\n");
487
488 for (i = 0; test_cases[i].name; i++)
489 printf("%d\t%s\n", i, test_cases[i].name);
490
491 exit(EXIT_FAILURE);
492}
493
494static unsigned long parse_test_id(const char *test_id_str, size_t test_cases_len)
495{
496 unsigned long test_id;
497 char *endptr = NULL;
498
499 errno = 0;
500 test_id = strtoul(test_id_str, &endptr, 10);
501 if (errno || *endptr != '\0') {
502 fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
503 exit(EXIT_FAILURE);
504 }
505
506 if (test_id >= test_cases_len) {
507 fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
508 test_id, test_cases_len - 1);
509 exit(EXIT_FAILURE);
510 }
511
512 return test_id;
513}
514
515void skip_test(struct test_case *test_cases, size_t test_cases_len,
516 const char *test_id_str)
517{
518 unsigned long test_id = parse_test_id(test_id_str, test_cases_len);
519 test_cases[test_id].skip = true;
520}
521
522void pick_test(struct test_case *test_cases, size_t test_cases_len,
523 const char *test_id_str)
524{
525 static bool skip_all = true;
526 unsigned long test_id;
527
528 if (skip_all) {
529 unsigned long i;
530
531 for (i = 0; i < test_cases_len; ++i)
532 test_cases[i].skip = true;
533
534 skip_all = false;
535 }
536
537 test_id = parse_test_id(test_id_str, test_cases_len);
538 test_cases[test_id].skip = false;
539}
540
541unsigned long hash_djb2(const void *data, size_t len)
542{
543 unsigned long hash = 5381;
544 int i = 0;
545
546 while (i < len) {
547 hash = ((hash << 5) + hash) + ((unsigned char *)data)[i];
548 i++;
549 }
550
551 return hash;
552}
553
554size_t iovec_bytes(const struct iovec *iov, size_t iovnum)
555{
556 size_t bytes;
557 int i;
558
559 for (bytes = 0, i = 0; i < iovnum; i++)
560 bytes += iov[i].iov_len;
561
562 return bytes;
563}
564
565unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum)
566{
567 unsigned long hash;
568 size_t iov_bytes;
569 size_t offs;
570 void *tmp;
571 int i;
572
573 iov_bytes = iovec_bytes(iov, iovnum);
574
575 tmp = malloc(iov_bytes);
576 if (!tmp) {
577 perror("malloc");
578 exit(EXIT_FAILURE);
579 }
580
581 for (offs = 0, i = 0; i < iovnum; i++) {
582 memcpy(tmp + offs, iov[i].iov_base, iov[i].iov_len);
583 offs += iov[i].iov_len;
584 }
585
586 hash = hash_djb2(tmp, iov_bytes);
587 free(tmp);
588
589 return hash;
590}
591
592/* Allocates and returns new 'struct iovec *' according pattern
593 * in the 'test_iovec'. For each element in the 'test_iovec' it
594 * allocates new element in the resulting 'iovec'. 'iov_len'
595 * of the new element is copied from 'test_iovec'. 'iov_base' is
596 * allocated depending on the 'iov_base' of 'test_iovec':
597 *
598 * 'iov_base' == NULL -> valid buf: mmap('iov_len').
599 *
600 * 'iov_base' == MAP_FAILED -> invalid buf:
601 * mmap('iov_len'), then munmap('iov_len').
602 * 'iov_base' still contains result of
603 * mmap().
604 *
605 * 'iov_base' == number -> unaligned valid buf:
606 * mmap('iov_len') + number.
607 *
608 * 'iovnum' is number of elements in 'test_iovec'.
609 *
610 * Returns new 'iovec' or calls 'exit()' on error.
611 */
612struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum)
613{
614 struct iovec *iovec;
615 int i;
616
617 iovec = malloc(sizeof(*iovec) * iovnum);
618 if (!iovec) {
619 perror("malloc");
620 exit(EXIT_FAILURE);
621 }
622
623 for (i = 0; i < iovnum; i++) {
624 iovec[i].iov_len = test_iovec[i].iov_len;
625
626 iovec[i].iov_base = mmap(NULL, iovec[i].iov_len,
627 PROT_READ | PROT_WRITE,
628 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE,
629 -1, 0);
630 if (iovec[i].iov_base == MAP_FAILED) {
631 perror("mmap");
632 exit(EXIT_FAILURE);
633 }
634
635 if (test_iovec[i].iov_base != MAP_FAILED)
636 iovec[i].iov_base += (uintptr_t)test_iovec[i].iov_base;
637 }
638
639 /* Unmap "invalid" elements. */
640 for (i = 0; i < iovnum; i++) {
641 if (test_iovec[i].iov_base == MAP_FAILED) {
642 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
643 perror("munmap");
644 exit(EXIT_FAILURE);
645 }
646 }
647 }
648
649 for (i = 0; i < iovnum; i++) {
650 int j;
651
652 if (test_iovec[i].iov_base == MAP_FAILED)
653 continue;
654
655 for (j = 0; j < iovec[i].iov_len; j++)
656 ((uint8_t *)iovec[i].iov_base)[j] = rand() & 0xff;
657 }
658
659 return iovec;
660}
661
662/* Frees 'iovec *', previously allocated by 'alloc_test_iovec()'.
663 * On error calls 'exit()'.
664 */
665void free_test_iovec(const struct iovec *test_iovec,
666 struct iovec *iovec, int iovnum)
667{
668 int i;
669
670 for (i = 0; i < iovnum; i++) {
671 if (test_iovec[i].iov_base != MAP_FAILED) {
672 if (test_iovec[i].iov_base)
673 iovec[i].iov_base -= (uintptr_t)test_iovec[i].iov_base;
674
675 if (munmap(iovec[i].iov_base, iovec[i].iov_len)) {
676 perror("munmap");
677 exit(EXIT_FAILURE);
678 }
679 }
680 }
681
682 free(iovec);
683}
684
685/* Set "unsigned long long" socket option and check that it's indeed set */
686void setsockopt_ull_check(int fd, int level, int optname,
687 unsigned long long val, char const *errmsg)
688{
689 unsigned long long chkval;
690 socklen_t chklen;
691 int err;
692
693 err = setsockopt(fd, level, optname, &val, sizeof(val));
694 if (err) {
695 fprintf(stderr, "setsockopt err: %s (%d)\n",
696 strerror(errno), errno);
697 goto fail;
698 }
699
700 chkval = ~val; /* just make storage != val */
701 chklen = sizeof(chkval);
702
703 err = getsockopt(fd, level, optname, &chkval, &chklen);
704 if (err) {
705 fprintf(stderr, "getsockopt err: %s (%d)\n",
706 strerror(errno), errno);
707 goto fail;
708 }
709
710 if (chklen != sizeof(chkval)) {
711 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
712 chklen);
713 goto fail;
714 }
715
716 if (chkval != val) {
717 fprintf(stderr, "value mismatch: set %llu got %llu\n", val,
718 chkval);
719 goto fail;
720 }
721 return;
722fail:
723 fprintf(stderr, "%s val %llu\n", errmsg, val);
724 exit(EXIT_FAILURE);
725;
726}
727
728/* Set "int" socket option and check that it's indeed set */
729void setsockopt_int_check(int fd, int level, int optname, int val,
730 char const *errmsg)
731{
732 int chkval;
733 socklen_t chklen;
734 int err;
735
736 err = setsockopt(fd, level, optname, &val, sizeof(val));
737 if (err) {
738 fprintf(stderr, "setsockopt err: %s (%d)\n",
739 strerror(errno), errno);
740 goto fail;
741 }
742
743 chkval = ~val; /* just make storage != val */
744 chklen = sizeof(chkval);
745
746 err = getsockopt(fd, level, optname, &chkval, &chklen);
747 if (err) {
748 fprintf(stderr, "getsockopt err: %s (%d)\n",
749 strerror(errno), errno);
750 goto fail;
751 }
752
753 if (chklen != sizeof(chkval)) {
754 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
755 chklen);
756 goto fail;
757 }
758
759 if (chkval != val) {
760 fprintf(stderr, "value mismatch: set %d got %d\n", val, chkval);
761 goto fail;
762 }
763 return;
764fail:
765 fprintf(stderr, "%s val %d\n", errmsg, val);
766 exit(EXIT_FAILURE);
767}
768
769static void mem_invert(unsigned char *mem, size_t size)
770{
771 size_t i;
772
773 for (i = 0; i < size; i++)
774 mem[i] = ~mem[i];
775}
776
777/* Set "timeval" socket option and check that it's indeed set */
778void setsockopt_timeval_check(int fd, int level, int optname,
779 struct timeval val, char const *errmsg)
780{
781 struct timeval chkval;
782 socklen_t chklen;
783 int err;
784
785 err = setsockopt(fd, level, optname, &val, sizeof(val));
786 if (err) {
787 fprintf(stderr, "setsockopt err: %s (%d)\n",
788 strerror(errno), errno);
789 goto fail;
790 }
791
792 /* just make storage != val */
793 chkval = val;
794 mem_invert((unsigned char *)&chkval, sizeof(chkval));
795 chklen = sizeof(chkval);
796
797 err = getsockopt(fd, level, optname, &chkval, &chklen);
798 if (err) {
799 fprintf(stderr, "getsockopt err: %s (%d)\n",
800 strerror(errno), errno);
801 goto fail;
802 }
803
804 if (chklen != sizeof(chkval)) {
805 fprintf(stderr, "size mismatch: set %zu got %d\n", sizeof(val),
806 chklen);
807 goto fail;
808 }
809
810 if (memcmp(&chkval, &val, sizeof(val)) != 0) {
811 fprintf(stderr, "value mismatch: set %ld:%ld got %ld:%ld\n",
812 val.tv_sec, val.tv_usec, chkval.tv_sec, chkval.tv_usec);
813 goto fail;
814 }
815 return;
816fail:
817 fprintf(stderr, "%s val %ld:%ld\n", errmsg, val.tv_sec, val.tv_usec);
818 exit(EXIT_FAILURE);
819}
820
821void enable_so_zerocopy_check(int fd)
822{
823 setsockopt_int_check(fd, SOL_SOCKET, SO_ZEROCOPY, 1,
824 "setsockopt SO_ZEROCOPY");
825}
826
827void enable_so_linger(int fd, int timeout)
828{
829 struct linger optval = {
830 .l_onoff = 1,
831 .l_linger = timeout
832 };
833
834 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &optval, sizeof(optval))) {
835 perror("setsockopt(SO_LINGER)");
836 exit(EXIT_FAILURE);
837 }
838}