cow, cache: Better mkostemp fallback
[nbdkit/ericb.git] / common / utils / utils.c
blob7b63b4d4bbd391923ff327d2704a7899f3fdd388
1 /* nbdkit
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
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 #include <config.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <unistd.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
48 * license for nbdkit.
50 void
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.-_=,:/";
58 size_t i, len;
60 /* If the string consists only of safe characters, output it as-is. */
61 len = strlen (str);
62 if (len > 0 && len == strspn (str, safe_chars)) {
63 fputs (str, fp);
64 return;
67 /* Double-quote the string. */
68 fputc ('"', fp);
69 for (i = 0; i < len; ++i) {
70 switch (str[i]) {
71 case '$': case '`': case '\\': case '"':
72 fputc ('\\', fp);
73 /*FALLTHROUGH*/
74 default:
75 fputc (str[i], fp);
78 fputc ('"', fp);
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.
85 void
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.-_~/";
91 size_t i, len;
93 /* If the string consists only of safe characters, output it as-is. */
94 len = strlen (str);
95 if (len == strspn (str, safe_chars)) {
96 fputs (str, fp);
97 return;
100 for (i = 0; i < len; ++i) {
101 if (strchr (safe_chars, str[i]))
102 fputc (str[i], fp);
103 else
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));
117 return -1;
119 else if (WIFSIGNALED (status)) {
120 nbdkit_error ("%s: command was killed by signal %d",
121 cmd, WTERMSIG (status));
122 return -1;
124 else if (WIFSTOPPED (status)) {
125 nbdkit_error ("%s: command was stopped by signal %d",
126 cmd, WSTOPSIG (status));
127 return -1;
130 return 0;
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");
145 close (fd);
146 errno = EBADF;
147 return -1;
148 #else
149 # if defined SOCK_CLOEXEC || defined HAVE_MKOSTEMP
150 # error "Unexpected: your system has incomplete atomic CLOEXEC support"
151 # endif
152 int f;
153 int err;
155 if (fd == -1)
156 return -1;
158 f = fcntl (fd, F_GETFD);
159 if (f == -1 || fcntl (fd, F_SETFD, f | FD_CLOEXEC) == -1) {
160 err = errno;
161 nbdkit_error ("fcntl: %m");
162 close (fd);
163 errno = err;
164 return -1;
166 return fd;
167 #endif
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) {
175 int f;
176 int err;
178 if (fd == -1)
179 return -1;
181 f = fcntl (fd, F_GETFL);
182 if (f == -1 || fcntl (fd, F_SETFL, f | O_NONBLOCK) == -1) {
183 err = errno;
184 nbdkit_error ("fcntl: %m");
185 close (fd);
186 errno = err;
187 return -1;
189 return fd;