2 * Copyright (C) 2013 Red Hat Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/types.h>
48 static char tmpdir
[] = "/tmp/nbdkitXXXXXX";
49 static char sockpath
[] = "/tmp/nbdkitXXXXXX/sock";
50 static char unixsockpath
[] = "unix:/tmp/nbdkitXXXXXX/sock";
51 static char pidpath
[] = "/tmp/nbdkitXXXXXX/pid";
54 const char *server
[2] = { unixsockpath
, NULL
};
68 test_start_nbdkit (const char *plugin
, ...)
72 if (mkdtemp (tmpdir
) == NULL
) {
76 len
= strlen (tmpdir
);
77 memcpy (sockpath
, tmpdir
, len
);
78 memcpy (unixsockpath
+5, tmpdir
, len
);
79 memcpy (pidpath
, tmpdir
, len
);
82 if (pid
== 0) { /* Child (nbdkit). */
84 const int MAX_ARGS
= 64;
85 const char *argv
[MAX_ARGS
+1];
97 va_start (args
, plugin
);
98 while ((p
= va_arg (args
, const char *)) != NULL
) {
107 execvp ("../src/nbdkit", (char **) argv
);
108 perror ("exec: nbdkit");
109 _exit (EXIT_FAILURE
);
112 /* Ensure nbdkit is killed and temporary files are deleted when the
113 * main program exits.
117 /* Wait for the pidfile to turn up, which indicates that nbdkit has
118 * started up successfully and is ready to serve requests. However
119 * if 'pid' exits in this time it indicates a failure to start up.
120 * Also there is a timeout in case nbdkit hangs.
122 for (i
= 0; i
< NBDKIT_START_TIMEOUT
; ++i
) {
123 if (waitpid (pid
, NULL
, WNOHANG
) == pid
)
126 if (kill (pid
, 0) == -1) {
127 if (errno
== ESRCH
) {
130 "%s FAILED: nbdkit exited before starting to serve files\n",
138 if (access (pidpath
, F_OK
) == 0)