tests: Increase timeouts on test-shutdown.sh
[nbdkit/ericb.git] / tests / test-socket-activation.c
bloba7f9fa140f4193b8bac0313c594aad13fb30a1b5
1 /* nbdkit
2 * Copyright (C) 2017-2019 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 /* Test socket activation.
35 * We cannot use the test framework for this since the framework
36 * always uses the -U flag which is incompatible with socket
37 * activation. Unfortunately this does mean we duplicate some code
38 * from the test framework.
40 * It's *almost* possible to test this from a shell script
41 * (cf. test-ip.sh) but as far as I can tell setting LISTEN_PID
42 * correctly is impossible from shell.
45 #include <config.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdint.h>
50 #include <inttypes.h>
51 #include <string.h>
52 #include <fcntl.h>
53 #include <unistd.h>
54 #include <signal.h>
55 #include <errno.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59 #include <sys/wait.h>
61 #ifndef SOCK_CLOEXEC
62 /* For this file, we don't care if fds are marked cloexec; leaking is okay. */
63 #define SOCK_CLOEXEC 0
64 #endif
66 #define FIRST_SOCKET_ACTIVATION_FD 3
68 #define NBDKIT_START_TIMEOUT 30 /* seconds */
70 /* Declare program_name. */
71 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
72 #include <errno.h>
73 #define program_name program_invocation_short_name
74 #else
75 #define program_name "nbdkit"
76 #endif
78 static char tmpdir[] = "/tmp/nbdkitXXXXXX";
79 static char sockpath[] = "/tmp/nbdkitXXXXXX/sock";
80 static char pidpath[] = "/tmp/nbdkitXXXXXX/pid";
82 static pid_t pid = 0;
84 static void
85 cleanup (void)
87 int status;
89 if (pid > 0) {
90 kill (pid, SIGTERM);
92 /* Check the status of nbdkit is normal on exit. */
93 if (waitpid (pid, &status, 0) == -1) {
94 perror ("waitpid");
95 _exit (EXIT_FAILURE);
97 if (WIFEXITED (status) && WEXITSTATUS (status) != 0) {
98 _exit (WEXITSTATUS (status));
100 if (WIFSIGNALED (status)) {
101 /* Note that nbdkit is supposed to catch the signal we send and
102 * exit cleanly, so the following shouldn't happen.
104 fprintf (stderr, "nbdkit terminated by signal %d\n", WTERMSIG (status));
105 _exit (EXIT_FAILURE);
107 if (WIFSTOPPED (status)) {
108 fprintf (stderr, "nbdkit stopped by signal %d\n", WSTOPSIG (status));
109 _exit (EXIT_FAILURE);
113 unlink (pidpath);
114 unlink (sockpath);
115 rmdir (tmpdir);
119 main (int argc, char *argv[])
121 int sock;
122 struct sockaddr_un addr;
123 char pid_str[16];
124 size_t i, len;
125 char magic[8];
127 if (mkdtemp (tmpdir) == NULL) {
128 perror ("mkdtemp");
129 exit (EXIT_FAILURE);
131 len = strlen (tmpdir);
132 memcpy (sockpath, tmpdir, len);
133 memcpy (pidpath, tmpdir, len);
135 atexit (cleanup);
137 /* Open the listening socket which will be passed into nbdkit. */
138 sock = socket (AF_UNIX, SOCK_STREAM /* NB do not use SOCK_CLOEXEC */, 0);
139 if (sock == -1) {
140 perror ("socket");
141 exit (EXIT_FAILURE);
144 addr.sun_family = AF_UNIX;
145 len = strlen (sockpath);
146 memcpy (addr.sun_path, sockpath, len+1 /* trailing \0 */);
148 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
149 perror (sockpath);
150 exit (EXIT_FAILURE);
153 if (listen (sock, 1) == -1) {
154 perror ("listen");
155 exit (EXIT_FAILURE);
158 if (sock != FIRST_SOCKET_ACTIVATION_FD) {
159 if (dup2 (sock, FIRST_SOCKET_ACTIVATION_FD) == -1) {
160 perror ("dup2");
161 exit (EXIT_FAILURE);
163 close (sock);
166 /* Run nbdkit. */
167 pid = fork ();
168 if (pid == -1) {
169 perror ("fork");
170 exit (EXIT_FAILURE);
172 if (pid == 0) {
173 /* Run nbdkit in the child. */
174 setenv ("LISTEN_FDS", "1", 1);
175 snprintf (pid_str, sizeof pid_str, "%d", (int) getpid ());
176 setenv ("LISTEN_PID", pid_str, 1);
178 execlp ("nbdkit",
179 "nbdkit",
180 "-P", pidpath,
181 "-o",
182 "-v",
183 "example1", NULL);
184 perror ("exec: nbdkit");
185 _exit (EXIT_FAILURE);
188 /* We don't need the listening socket now. */
189 close (sock);
191 /* Wait for the pidfile to turn up, which indicates that nbdkit has
192 * started up successfully and is ready to serve requests. However
193 * if 'pid' exits in this time it indicates a failure to start up.
194 * Also there is a timeout in case nbdkit hangs.
196 for (i = 0; i < NBDKIT_START_TIMEOUT; ++i) {
197 if (waitpid (pid, NULL, WNOHANG) == pid)
198 goto early_exit;
200 if (kill (pid, 0) == -1) {
201 if (errno == ESRCH) {
202 early_exit:
203 fprintf (stderr,
204 "%s FAILED: nbdkit exited before starting to serve files\n",
205 program_name);
206 pid = 0;
207 exit (EXIT_FAILURE);
209 perror ("kill");
212 if (access (pidpath, F_OK) == 0)
213 break;
215 sleep (1);
218 /* Now nbdkit is supposed to be listening on the Unix domain socket
219 * (which it got via the listening socket that we passed down to it,
220 * not from the path), so we should be able to connect to the Unix
221 * domain socket by its path and receive an NBD magic string.
223 sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
224 if (sock == -1) {
225 perror ("socket");
226 exit (EXIT_FAILURE);
229 /* Reuse addr which was set up above. */
230 if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
231 perror (sockpath);
232 exit (EXIT_FAILURE);
235 if (read (sock, magic, 8) != 8) {
236 perror ("read");
237 exit (EXIT_FAILURE);
240 if (memcmp (magic, "NBDMAGIC", 8) != 0) {
241 fprintf (stderr, "%s FAILED: did not read magic string from server\n",
242 program_name);
243 exit (EXIT_FAILURE);
246 close (sock);
248 /* Test succeeded. */
249 exit (EXIT_SUCCESS);