Linux 6.12 compat: META
[zfs.git] / lib / libshare / nfs.c
blob77ed14f322edbaa613d59c10557bdf2f3a109b6d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/file.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <libshare.h>
31 #include <unistd.h>
32 #include <libzutil.h>
33 #include "nfs.h"
37 * nfs_exports_[lock|unlock] are used to guard against conconcurrent
38 * updates to the exports file. Each protocol is responsible for
39 * providing the necessary locking to ensure consistency.
41 static int
42 nfs_exports_lock(const char *name, int *nfs_lock_fd)
44 int err;
46 *nfs_lock_fd = open(name, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
47 if (*nfs_lock_fd == -1) {
48 err = errno;
49 fprintf(stderr, "failed to lock %s: %s\n", name,
50 zfs_strerror(err));
51 return (err);
54 while ((err = flock(*nfs_lock_fd, LOCK_EX)) != 0 && errno == EINTR)
56 if (err != 0) {
57 err = errno;
58 fprintf(stderr, "failed to lock %s: %s\n", name,
59 zfs_strerror(err));
60 (void) close(*nfs_lock_fd);
61 *nfs_lock_fd = -1;
62 return (err);
65 return (0);
68 static void
69 nfs_exports_unlock(const char *name, int *nfs_lock_fd)
71 verify(*nfs_lock_fd > 0);
73 if (flock(*nfs_lock_fd, LOCK_UN) != 0)
74 fprintf(stderr, "failed to unlock %s: %s\n",
75 name, zfs_strerror(errno));
77 (void) close(*nfs_lock_fd);
78 *nfs_lock_fd = -1;
81 struct tmpfile {
83 * This only needs to be as wide as ZFS_EXPORTS_FILE and mktemp suffix,
84 * 64 is more than enough.
86 char name[64];
87 FILE *fp;
90 static boolean_t
91 nfs_init_tmpfile(const char *prefix, const char *mdir, struct tmpfile *tmpf)
93 if (mdir != NULL &&
94 mkdir(mdir, 0755) < 0 &&
95 errno != EEXIST) {
96 fprintf(stderr, "failed to create %s: %s\n",
97 // cppcheck-suppress uninitvar
98 mdir, zfs_strerror(errno));
99 return (B_FALSE);
102 strlcpy(tmpf->name, prefix, sizeof (tmpf->name));
103 strlcat(tmpf->name, ".XXXXXXXX", sizeof (tmpf->name));
105 int fd = mkostemp(tmpf->name, O_CLOEXEC);
106 if (fd == -1) {
107 fprintf(stderr, "Unable to create temporary file: %s",
108 zfs_strerror(errno));
109 return (B_FALSE);
112 tmpf->fp = fdopen(fd, "w+");
113 if (tmpf->fp == NULL) {
114 fprintf(stderr, "Unable to reopen temporary file: %s",
115 zfs_strerror(errno));
116 close(fd);
117 return (B_FALSE);
120 return (B_TRUE);
123 static void
124 nfs_abort_tmpfile(struct tmpfile *tmpf)
126 unlink(tmpf->name);
127 fclose(tmpf->fp);
130 static int
131 nfs_fini_tmpfile(const char *exports, struct tmpfile *tmpf)
133 if (fflush(tmpf->fp) != 0) {
134 fprintf(stderr, "Failed to write to temporary file: %s\n",
135 zfs_strerror(errno));
136 nfs_abort_tmpfile(tmpf);
137 return (SA_SYSTEM_ERR);
140 if (rename(tmpf->name, exports) == -1) {
141 fprintf(stderr, "Unable to rename %s -> %s: %s\n",
142 tmpf->name, exports, zfs_strerror(errno));
143 nfs_abort_tmpfile(tmpf);
144 return (SA_SYSTEM_ERR);
147 (void) fchmod(fileno(tmpf->fp), 0644);
148 fclose(tmpf->fp);
149 return (SA_OK);
153 nfs_escape_mountpoint(const char *mp, char **out, boolean_t *need_free)
155 if (strpbrk(mp, "\t\n\v\f\r \\") == NULL) {
156 *out = (char *)mp;
157 *need_free = B_FALSE;
158 return (SA_OK);
159 } else {
160 size_t len = strlen(mp);
161 *out = malloc(len * 4 + 1);
162 if (!*out)
163 return (SA_NO_MEMORY);
164 *need_free = B_TRUE;
166 char *oc = *out;
167 for (const char *c = mp; c < mp + len; ++c)
168 if (memchr("\t\n\v\f\r \\", *c,
169 strlen("\t\n\v\f\r \\"))) {
170 sprintf(oc, "\\%03hho", *c);
171 oc += 4;
172 } else
173 *oc++ = *c;
174 *oc = '\0';
177 return (SA_OK);
180 static int
181 nfs_process_exports(const char *exports, const char *mountpoint,
182 boolean_t (*cbk)(void *userdata, char *line, boolean_t found_mountpoint),
183 void *userdata)
185 int error = SA_OK;
186 boolean_t cont = B_TRUE;
188 FILE *oldfp = fopen(exports, "re");
189 if (oldfp != NULL) {
190 boolean_t need_mp_free;
191 char *mp;
192 if ((error = nfs_escape_mountpoint(mountpoint,
193 &mp, &need_mp_free)) != SA_OK) {
194 (void) fclose(oldfp);
195 return (error);
198 char *buf = NULL, *sep;
199 size_t buflen = 0, mplen = strlen(mp);
201 while (cont && getline(&buf, &buflen, oldfp) != -1) {
202 if (buf[0] == '\n' || buf[0] == '#')
203 continue;
205 cont = cbk(userdata, buf,
206 (sep = strpbrk(buf, "\t \n")) != NULL &&
207 sep - buf == mplen &&
208 strncmp(buf, mp, mplen) == 0);
210 free(buf);
211 if (need_mp_free)
212 free(mp);
214 if (ferror(oldfp) != 0)
215 error = ferror(oldfp);
217 if (fclose(oldfp) != 0) {
218 fprintf(stderr, "Unable to close file %s: %s\n",
219 exports, zfs_strerror(errno));
220 error = error != SA_OK ? error : SA_SYSTEM_ERR;
224 return (error);
227 static boolean_t
228 nfs_copy_entries_cb(void *userdata, char *line, boolean_t found_mountpoint)
230 FILE *newfp = userdata;
231 if (!found_mountpoint)
232 fputs(line, newfp);
233 return (B_TRUE);
237 * Copy all entries from the exports file (if it exists) to newfp,
238 * omitting any entries for the specified mountpoint.
240 static int
241 nfs_copy_entries(FILE *newfp, const char *exports, const char *mountpoint)
243 fputs(FILE_HEADER, newfp);
245 int error = nfs_process_exports(
246 exports, mountpoint, nfs_copy_entries_cb, newfp);
248 if (error == SA_OK && ferror(newfp) != 0)
249 error = ferror(newfp);
251 return (error);
255 nfs_toggle_share(const char *lockfile, const char *exports,
256 const char *expdir, sa_share_impl_t impl_share,
257 int(*cbk)(sa_share_impl_t impl_share, FILE *tmpfile))
259 int error, nfs_lock_fd = -1;
260 struct tmpfile tmpf;
262 if (!nfs_init_tmpfile(exports, expdir, &tmpf))
263 return (SA_SYSTEM_ERR);
265 error = nfs_exports_lock(lockfile, &nfs_lock_fd);
266 if (error != 0) {
267 nfs_abort_tmpfile(&tmpf);
268 return (error);
271 error = nfs_copy_entries(tmpf.fp, exports, impl_share->sa_mountpoint);
272 if (error != SA_OK)
273 goto fullerr;
275 error = cbk(impl_share, tmpf.fp);
276 if (error != SA_OK)
277 goto fullerr;
279 error = nfs_fini_tmpfile(exports, &tmpf);
280 nfs_exports_unlock(lockfile, &nfs_lock_fd);
281 return (error);
283 fullerr:
284 nfs_abort_tmpfile(&tmpf);
285 nfs_exports_unlock(lockfile, &nfs_lock_fd);
286 return (error);
289 void
290 nfs_reset_shares(const char *lockfile, const char *exports)
292 int nfs_lock_fd = -1;
294 if (nfs_exports_lock(lockfile, &nfs_lock_fd) == 0) {
295 (void) ! truncate(exports, 0);
296 nfs_exports_unlock(lockfile, &nfs_lock_fd);
300 static boolean_t
301 nfs_is_shared_cb(void *userdata, char *line, boolean_t found_mountpoint)
303 (void) line;
305 boolean_t *found = userdata;
306 *found = found_mountpoint;
307 return (!found_mountpoint);
310 boolean_t
311 nfs_is_shared_impl(const char *exports, sa_share_impl_t impl_share)
313 boolean_t found = B_FALSE;
314 nfs_process_exports(exports, impl_share->sa_mountpoint,
315 nfs_is_shared_cb, &found);
316 return (found);