1 /* Test for nonblocking read and write on sockets.
3 Copyright (C) 2011-2025 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
25 #include <sys/socket.h>
27 #if defined _WIN32 && ! defined __CYGWIN__
33 #include "nonblocking.h"
34 #include "wait-process.h"
37 #include "socket-server.h"
38 #include "test-nonblocking-socket.h"
39 #define PROG_ROLE "main"
40 #include "test-nonblocking-writer.h"
43 main (int argc
, char *argv
[])
45 const char *child_path
;
54 test
= atoi (argv
[2]);
56 /* Create a server socket. */
57 server
= create_server (0, 1, &port
);
59 /* Spawn the child process. */
62 const char *child_argv
[4];
64 sprintf (port_arg
, "%u", port
);
65 child_argv
[0] = child_path
;
66 child_argv
[1] = argv
[2];
67 child_argv
[2] = port_arg
;
70 #if defined _WIN32 && ! defined __CYGWIN__
71 child
= _spawnvpe (P_NOWAIT
, child_path
, child_argv
,
72 (const char **) environ
);
78 posix_spawnp (&child_pid
, child_path
, NULL
, NULL
, (char **) child_argv
,
86 /* Accept a connection from the child process. */
87 server_socket
= create_server_socket (server
);
89 /* Prepare the file descriptor. */
91 ASSERT (set_nonblocking_flag (server_socket
, true) >= 0);
97 socklen_t value_len
= sizeof (value
);
98 if (getsockopt (server_socket
, SOL_SOCKET
, SO_SNDBUF
, &value
, &value_len
) >= 0)
99 fprintf (stderr
, "SO_SNDBUF = %d\n", value
);
105 socklen_t value_len
= sizeof (value
);
106 if (getsockopt (server_socket
, SOL_SOCKET
, SO_RCVBUF
, &value
, &value_len
) >= 0)
107 fprintf (stderr
, "SO_RCVBUF = %d\n", value
);
113 main_writer_loop (test
, SOCKET_DATA_BLOCK_SIZE
, server_socket
,
114 SOCKET_HAS_LARGE_BUFFER
);
118 wait_subprocess (child
, child_path
, false, false, false, false, NULL
);
122 return (exitcode
? exitcode
: test_exit_status
);