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]
23 #include <sys/types.h>
36 * nfs_exports_[lock|unlock] are used to guard against conconcurrent
37 * updates to the exports file. Each protocol is responsible for
38 * providing the necessary locking to ensure consistency.
41 nfs_exports_lock(const char *name
, int *nfs_lock_fd
)
45 *nfs_lock_fd
= open(name
, O_RDWR
| O_CREAT
| O_CLOEXEC
, 0600);
46 if (*nfs_lock_fd
== -1) {
48 fprintf(stderr
, "failed to lock %s: %s\n", name
, strerror(err
));
52 while ((err
= flock(*nfs_lock_fd
, LOCK_EX
)) != 0 && errno
== EINTR
)
56 fprintf(stderr
, "failed to lock %s: %s\n", name
, strerror(err
));
57 (void) close(*nfs_lock_fd
);
66 nfs_exports_unlock(const char *name
, int *nfs_lock_fd
)
68 verify(*nfs_lock_fd
> 0);
70 if (flock(*nfs_lock_fd
, LOCK_UN
) != 0)
71 fprintf(stderr
, "failed to unlock %s: %s\n",
72 name
, strerror(errno
));
74 (void) close(*nfs_lock_fd
);
80 * This only needs to be as wide as ZFS_EXPORTS_FILE and mktemp suffix,
81 * 64 is more than enough.
88 nfs_init_tmpfile(const char *prefix
, const char *mdir
, struct tmpfile
*tmpf
)
91 mkdir(mdir
, 0755) < 0 &&
93 fprintf(stderr
, "failed to create %s: %s\n",
94 // cppcheck-suppress uninitvar
95 mdir
, strerror(errno
));
99 strlcpy(tmpf
->name
, prefix
, sizeof (tmpf
->name
));
100 strlcat(tmpf
->name
, ".XXXXXXXX", sizeof (tmpf
->name
));
102 int fd
= mkostemp(tmpf
->name
, O_CLOEXEC
);
104 fprintf(stderr
, "Unable to create temporary file: %s",
109 tmpf
->fp
= fdopen(fd
, "w+");
110 if (tmpf
->fp
== NULL
) {
111 fprintf(stderr
, "Unable to reopen temporary file: %s",
121 nfs_abort_tmpfile(struct tmpfile
*tmpf
)
128 nfs_fini_tmpfile(const char *exports
, struct tmpfile
*tmpf
)
130 if (fflush(tmpf
->fp
) != 0) {
131 fprintf(stderr
, "Failed to write to temporary file: %s\n",
133 nfs_abort_tmpfile(tmpf
);
134 return (SA_SYSTEM_ERR
);
137 if (rename(tmpf
->name
, exports
) == -1) {
138 fprintf(stderr
, "Unable to rename %s -> %s: %s\n",
139 tmpf
->name
, exports
, strerror(errno
));
140 nfs_abort_tmpfile(tmpf
);
141 return (SA_SYSTEM_ERR
);
144 (void) fchmod(fileno(tmpf
->fp
), 0644);
150 nfs_escape_mountpoint(const char *mp
, char **out
, boolean_t
*need_free
)
152 if (strpbrk(mp
, "\t\n\v\f\r \\") == NULL
) {
154 *need_free
= B_FALSE
;
157 size_t len
= strlen(mp
);
158 *out
= malloc(len
* 4 + 1);
160 return (SA_NO_MEMORY
);
164 for (const char *c
= mp
; c
< mp
+ len
; ++c
)
165 if (memchr("\t\n\v\f\r \\", *c
,
166 strlen("\t\n\v\f\r \\"))) {
167 sprintf(oc
, "\\%03hho", *c
);
178 nfs_process_exports(const char *exports
, const char *mountpoint
,
179 boolean_t (*cbk
)(void *userdata
, char *line
, boolean_t found_mountpoint
),
183 boolean_t cont
= B_TRUE
;
185 FILE *oldfp
= fopen(exports
, "re");
187 boolean_t need_mp_free
;
189 if ((error
= nfs_escape_mountpoint(mountpoint
,
190 &mp
, &need_mp_free
)) != SA_OK
) {
191 (void) fclose(oldfp
);
195 char *buf
= NULL
, *sep
;
196 size_t buflen
= 0, mplen
= strlen(mp
);
198 while (cont
&& getline(&buf
, &buflen
, oldfp
) != -1) {
199 if (buf
[0] == '\n' || buf
[0] == '#')
202 cont
= cbk(userdata
, buf
,
203 (sep
= strpbrk(buf
, "\t \n")) != NULL
&&
204 sep
- buf
== mplen
&&
205 strncmp(buf
, mp
, mplen
) == 0);
211 if (ferror(oldfp
) != 0)
212 error
= ferror(oldfp
);
214 if (fclose(oldfp
) != 0) {
215 fprintf(stderr
, "Unable to close file %s: %s\n",
216 exports
, strerror(errno
));
217 error
= error
!= SA_OK
? error
: SA_SYSTEM_ERR
;
225 nfs_copy_entries_cb(void *userdata
, char *line
, boolean_t found_mountpoint
)
227 FILE *newfp
= userdata
;
228 if (!found_mountpoint
)
234 * Copy all entries from the exports file (if it exists) to newfp,
235 * omitting any entries for the specified mountpoint.
238 nfs_copy_entries(FILE *newfp
, const char *exports
, const char *mountpoint
)
240 fputs(FILE_HEADER
, newfp
);
242 int error
= nfs_process_exports(
243 exports
, mountpoint
, nfs_copy_entries_cb
, newfp
);
245 if (error
== SA_OK
&& ferror(newfp
) != 0)
246 error
= ferror(newfp
);
252 nfs_toggle_share(const char *lockfile
, const char *exports
,
253 const char *expdir
, sa_share_impl_t impl_share
,
254 int(*cbk
)(sa_share_impl_t impl_share
, FILE *tmpfile
))
256 int error
, nfs_lock_fd
= -1;
259 if (!nfs_init_tmpfile(exports
, expdir
, &tmpf
))
260 return (SA_SYSTEM_ERR
);
262 error
= nfs_exports_lock(lockfile
, &nfs_lock_fd
);
264 nfs_abort_tmpfile(&tmpf
);
268 error
= nfs_copy_entries(tmpf
.fp
, exports
, impl_share
->sa_mountpoint
);
272 error
= cbk(impl_share
, tmpf
.fp
);
276 error
= nfs_fini_tmpfile(exports
, &tmpf
);
277 nfs_exports_unlock(lockfile
, &nfs_lock_fd
);
281 nfs_abort_tmpfile(&tmpf
);
282 nfs_exports_unlock(lockfile
, &nfs_lock_fd
);
287 nfs_reset_shares(const char *lockfile
, const char *exports
)
289 int nfs_lock_fd
= -1;
291 if (nfs_exports_lock(lockfile
, &nfs_lock_fd
) == 0) {
292 (void) ! truncate(exports
, 0);
293 nfs_exports_unlock(lockfile
, &nfs_lock_fd
);
298 nfs_is_shared_cb(void *userdata
, char *line
, boolean_t found_mountpoint
)
302 boolean_t
*found
= userdata
;
303 *found
= found_mountpoint
;
304 return (!found_mountpoint
);
308 nfs_is_shared_impl(const char *exports
, sa_share_impl_t impl_share
)
310 boolean_t found
= B_FALSE
;
311 nfs_process_exports(exports
, impl_share
->sa_mountpoint
,
312 nfs_is_shared_cb
, &found
);