Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / interop / socket-activation.c
blobe3a511ce62c48ff738333c54ecda162068805355
1 /* NBD client library in userspace
2 * Copyright Red Hat
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /* Test interop with nbdkit or qemu-nbd, and systemd socket activation. */
21 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27 #include <unistd.h>
29 #include <libnbd.h>
31 #define SIZE 1048576
33 int
34 main (int argc, char *argv[])
36 struct nbd_handle *nbd;
37 char tmpfile[] = "/tmp/nbdXXXXXX";
38 int fd;
39 int64_t actual_size;
40 char buf[512];
41 int r = -1;
43 /* Create a large sparse temporary file. */
44 fd = mkstemp (tmpfile);
45 if (fd == -1 ||
46 ftruncate (fd, SIZE) == -1 ||
47 close (fd) == -1) {
48 perror (tmpfile);
49 goto out;
52 nbd = nbd_create ();
53 if (nbd == NULL) {
54 fprintf (stderr, "%s\n", nbd_get_error ());
55 goto out;
58 char *args[] = { SERVER, SERVER_PARAMS, NULL };
59 if (nbd_connect_systemd_socket_activation (nbd, args) == -1) {
60 fprintf (stderr, "%s\n", nbd_get_error ());
61 goto out;
64 actual_size = nbd_get_size (nbd);
65 if (actual_size == -1) {
66 fprintf (stderr, "%s\n", nbd_get_error ());
67 goto out;
69 if (actual_size != SIZE) {
70 fprintf (stderr, "%s: actual size %" PRIi64 " <> expected size %d",
71 argv[0], actual_size, SIZE);
72 goto out;
75 if (nbd_pread (nbd, buf, sizeof buf, 0, 0) == -1) {
76 fprintf (stderr, "%s\n", nbd_get_error ());
77 goto out;
80 if (nbd_shutdown (nbd, 0) == -1) {
81 fprintf (stderr, "%s\n", nbd_get_error ());
82 goto out;
85 nbd_close (nbd);
87 r = 0;
88 out:
89 unlink (tmpfile);
91 exit (r == 0 ? EXIT_SUCCESS : EXIT_FAILURE);