1 /* nbdkit
2  * Copyright (C) 2017-2020 Red Hat Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
18  *
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.
31  */
32 
33 /* Test socket activation.
34  *
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.
39  *
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.
43  */
44 
45 #include <config.h>
46 
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>
60 
61 #include "byte-swapping.h"
62 #include "nbd-protocol.h"
63 
64 #ifndef SOCK_CLOEXEC
65 /* For this file, we don't care if fds are marked cloexec; leaking is okay.  */
66 #define SOCK_CLOEXEC 0
67 #endif
68 
69 #define FIRST_SOCKET_ACTIVATION_FD 3
70 
71 #define NBDKIT_START_TIMEOUT 30 /* seconds */
72 
73 /* Declare program_name. */
74 #if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME == 1
75 #include <errno.h>
76 #define program_name program_invocation_short_name
77 #else
78 #define program_name "nbdkit"
79 #endif
80 
81 static char tmpdir[] =   "/tmp/nbdkitXXXXXX";
82 static char sockpath[] = "/tmp/nbdkitXXXXXX/sock";
83 static char pidpath[] =  "/tmp/nbdkitXXXXXX/pid";
84 
85 static pid_t pid = 0;
86 
87 static void
cleanup(void)88 cleanup (void)
89 {
90   if (pid > 0)
91     kill (pid, SIGTERM);
92 
93   unlink (pidpath);
94   unlink (sockpath);
95   rmdir (tmpdir);
96 }
97 
98 int
main(int argc,char * argv[])99 main (int argc, char *argv[])
100 {
101   int sock;
102   struct sockaddr_un addr;
103   char pid_str[16];
104   size_t i, len;
105   uint64_t magic;
106 
107   if (mkdtemp (tmpdir) == NULL) {
108     perror ("mkdtemp");
109     exit (EXIT_FAILURE);
110   }
111   len = strlen (tmpdir);
112   memcpy (sockpath, tmpdir, len);
113   memcpy (pidpath, tmpdir, len);
114 
115   atexit (cleanup);
116 
117   /* Open the listening socket which will be passed into nbdkit. */
118   sock = socket (AF_UNIX, SOCK_STREAM /* NB do not use SOCK_CLOEXEC */, 0);
119   if (sock == -1) {
120     perror ("socket");
121     exit (EXIT_FAILURE);
122   }
123 
124   addr.sun_family = AF_UNIX;
125   len = strlen (sockpath);
126   memcpy (addr.sun_path, sockpath, len+1 /* trailing \0 */);
127 
128   if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
129     perror (sockpath);
130     exit (EXIT_FAILURE);
131   }
132 
133   if (listen (sock, 1) == -1) {
134     perror ("listen");
135     exit (EXIT_FAILURE);
136   }
137 
138   if (sock != FIRST_SOCKET_ACTIVATION_FD) {
139     if (dup2 (sock, FIRST_SOCKET_ACTIVATION_FD) == -1) {
140       perror ("dup2");
141       exit (EXIT_FAILURE);
142     }
143     close (sock);
144   }
145 
146   /* Run nbdkit. */
147   pid = fork ();
148   if (pid == -1) {
149     perror ("fork");
150     exit (EXIT_FAILURE);
151   }
152   if (pid == 0) {
153     /* Run nbdkit in the child. */
154     setenv ("LISTEN_FDS", "1", 1);
155     snprintf (pid_str, sizeof pid_str, "%d", (int) getpid ());
156     setenv ("LISTEN_PID", pid_str, 1);
157 
158     execlp ("nbdkit",
159             "nbdkit",
160             "-P", pidpath,
161             "-o",
162             "-v",
163             "example1", NULL);
164     perror ("exec: nbdkit");
165     _exit (EXIT_FAILURE);
166   }
167 
168   /* We don't need the listening socket now. */
169   close (sock);
170 
171   /* Wait for the pidfile to turn up, which indicates that nbdkit has
172    * started up successfully and is ready to serve requests.  However
173    * if 'pid' exits in this time it indicates a failure to start up.
174    * Also there is a timeout in case nbdkit hangs.
175    */
176   for (i = 0; i < NBDKIT_START_TIMEOUT; ++i) {
177     if (waitpid (pid, NULL, WNOHANG) == pid)
178       goto early_exit;
179 
180     if (kill (pid, 0) == -1) {
181       if (errno == ESRCH) {
182       early_exit:
183         fprintf (stderr,
184                  "%s FAILED: nbdkit exited before starting to serve files\n",
185                  program_name);
186         pid = 0;
187         exit (EXIT_FAILURE);
188       }
189       perror ("kill");
190     }
191 
192     if (access (pidpath, F_OK) == 0)
193       break;
194 
195     sleep (1);
196   }
197 
198   /* Now nbdkit is supposed to be listening on the Unix domain socket
199    * (which it got via the listening socket that we passed down to it,
200    * not from the path), so we should be able to connect to the Unix
201    * domain socket by its path and receive an NBD magic string.
202    */
203   sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
204   if (sock == -1) {
205     perror ("socket");
206     exit (EXIT_FAILURE);
207   }
208 
209   /* Reuse addr which was set up above. */
210   if (connect (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
211     perror (sockpath);
212     exit (EXIT_FAILURE);
213   }
214 
215   if (read (sock, &magic, sizeof magic) != sizeof magic) {
216     perror ("read");
217     exit (EXIT_FAILURE);
218   }
219 
220   if (be64toh (magic) != NBD_MAGIC) {
221     fprintf (stderr, "%s FAILED: did not read magic string from server\n",
222              program_name);
223     exit (EXIT_FAILURE);
224   }
225 
226   close (sock);
227 
228   /* Test succeeded. */
229   exit (EXIT_SUCCESS);
230 }
231