2 * Copyright (C) 2018-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
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
39 #include <sys/socket.h>
40 #include <sys/types.h>
44 #include <nbdkit-plugin.h>
46 /* Print str to fp, shell quoting if necessary. This comes from
47 * libguestfs, but was written by me so I'm relicensing it to a BSD
51 shell_quote (const char *str
, FILE *fp
)
53 /* Note possible bug in this list (XXX):
54 * https://www.redhat.com/archives/libguestfs/2019-February/msg00036.html
56 const char *safe_chars
=
57 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_=,:/";
60 /* If the string consists only of safe characters, output it as-is. */
62 if (len
> 0 && len
== strspn (str
, safe_chars
)) {
67 /* Double-quote the string. */
69 for (i
= 0; i
< len
; ++i
) {
71 case '$': case '`': case '\\': case '"':
81 /* Print str to fp, URI quoting if necessary.
82 * The resulting string is safe for use in a URI path or query component,
83 * and can be passed through the shell without further quoting.
86 uri_quote (const char *str
, FILE *fp
)
88 /* safe_chars contains the RFC 3986 unreserved characters plus '/'. */
89 const char *safe_chars
=
90 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_~/";
93 /* If the string consists only of safe characters, output it as-is. */
95 if (len
== strspn (str
, safe_chars
)) {
100 for (i
= 0; i
< len
; ++i
) {
101 if (strchr (safe_chars
, str
[i
]))
104 fprintf (fp
, "%%%02X", str
[i
] & 0xff);
108 /* Convert exit status to nbd_error. If the exit status was nonzero
109 * or another failure then -1 is returned.
112 exit_status_to_nbd_error (int status
, const char *cmd
)
114 if (WIFEXITED (status
) && WEXITSTATUS (status
) != 0) {
115 nbdkit_error ("%s: command failed with exit code %d",
116 cmd
, WEXITSTATUS (status
));
119 else if (WIFSIGNALED (status
)) {
120 nbdkit_error ("%s: command was killed by signal %d",
121 cmd
, WTERMSIG (status
));
124 else if (WIFSTOPPED (status
)) {
125 nbdkit_error ("%s: command was stopped by signal %d",
126 cmd
, WSTOPSIG (status
));
133 /* Set the FD_CLOEXEC flag on the given fd, if it is non-negative.
134 * On failure, close fd and return -1; on success, return fd.
136 * Note that this function should ONLY be used on platforms that lack
137 * atomic CLOEXEC support during fd creation (such as Haiku in 2019);
138 * when using it as a fallback path, you must also consider how to
139 * prevent fd leaks to plugins that want to fork().
142 set_cloexec (int fd
) {
143 #if defined SOCK_CLOEXEC && defined HAVE_MKOSTEMP
144 nbdkit_error ("prefer creating fds with CLOEXEC atomically set");
149 # if defined SOCK_CLOEXEC || defined HAVE_MKOSTEMP
150 # error "Unexpected: your system has incomplete atomic CLOEXEC support"
158 f
= fcntl (fd
, F_GETFD
);
159 if (f
== -1 || fcntl (fd
, F_SETFD
, f
| FD_CLOEXEC
) == -1) {
161 nbdkit_error ("fcntl: %m");
170 /* Set the O_NONBLOCK flag on the given fd, if it is non-negative.
171 * On failure, close fd and return -1; on success, return fd.
174 set_nonblock (int fd
) {
181 f
= fcntl (fd
, F_GETFL
);
182 if (f
== -1 || fcntl (fd
, F_SETFL
, f
| O_NONBLOCK
) == -1) {
184 nbdkit_error ("fcntl: %m");