Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / cddl / osnet / lib / libzfs / fsshare.c
blob84476976fc7207dce3764f04cb973b8e66ac523a
1 /* $NetBSD: fsshare.c,v 1.1 2009/08/07 20:57:56 haad Exp $ */
3 /*-
4 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/compat/opensolaris/misc/fsshare.c,v 1.2 2007/04/21 13:17:23 pjd Exp $"); */
31 __RCSID("$NetBSD: fsshare.c,v 1.1 2009/08/07 20:57:56 haad Exp $");
33 #include <sys/param.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <signal.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <assert.h>
42 #include <pathnames.h> /* _PATH_MOUNTDPID */
43 #include <fsshare.h>
45 #define FILE_HEADER "# !!! DO NOT EDIT THIS FILE MANUALLY !!!\n\n"
46 #define OPTSSIZE 1024
47 #define MAXLINESIZE (PATH_MAX + OPTSSIZE)
49 static void
50 restart_mountd(void)
52 int pid;
53 FILE *pfh;
55 pfh = fopen(_PATH_MOUNTDPID, "r");
56 if (pfh == NULL)
57 return;
58 fscanf(pfh, "%d", &pid);
59 fclose(pfh);
60 kill((pid_t)pid, SIGHUP);
64 * Read one line from a file. Skip comments, empty lines and a line with a
65 * mountpoint specified in the 'skip' argument.
67 static char *
68 zgetline(FILE *fd, const char *skip)
70 static char line[MAXLINESIZE];
71 size_t len, skiplen;
72 char *s, last;
74 if (skip != NULL)
75 skiplen = strlen(skip);
76 for (;;) {
77 s = fgets(line, sizeof(line), fd);
78 if (s == NULL)
79 return (NULL);
80 /* Skip empty lines and comments. */
81 if (line[0] == '\n' || line[0] == '#')
82 continue;
83 len = strlen(line);
84 if (line[len - 1] == '\n')
85 line[len - 1] = '\0';
86 last = line[skiplen];
87 /* Skip the given mountpoint. */
88 if (skip != NULL && strncmp(skip, line, skiplen) == 0 &&
89 (last == '\t' || last == ' ' || last == '\0')) {
90 continue;
92 break;
94 return (line);
98 * Function translate options to a format acceptable by exports(5), eg.
100 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org 69.147.83.54
102 * Accepted input formats:
104 * ro,network=192.168.0.0,mask=255.255.255.0,maproot=0,freefall.freebsd.org
105 * ro network=192.168.0.0 mask=255.255.255.0 maproot=0 freefall.freebsd.org
106 * -ro,-network=192.168.0.0,-mask=255.255.255.0,-maproot=0,freefall.freebsd.org
107 * -ro -network=192.168.0.0 -mask=255.255.255.0 -maproot=0 freefall.freebsd.org
109 * Recognized keywords:
111 * ro, maproot, mapall, mask, network, alldirs, public, webnfs, index, quiet
114 static const char *known_opts[] = { "ro", "maproot", "mapall", "mask",
115 "network", "alldirs", "public", "webnfs", "index", "quiet", NULL };
116 static char *
117 translate_opts(const char *shareopts)
119 static char newopts[OPTSSIZE];
120 char oldopts[OPTSSIZE];
121 char *o, *s = NULL;
122 unsigned int i;
123 size_t len;
125 strlcpy(oldopts, shareopts, sizeof(oldopts));
126 newopts[0] = '\0';
127 s = oldopts;
128 while ((o = strsep(&s, "-, ")) != NULL) {
129 if (o[0] == '\0')
130 continue;
131 for (i = 0; known_opts[i] != NULL; i++) {
132 len = strlen(known_opts[i]);
133 if (strncmp(known_opts[i], o, len) == 0 &&
134 (o[len] == '\0' || o[len] == '=')) {
135 strlcat(newopts, "-", sizeof(newopts));
136 break;
139 strlcat(newopts, o, sizeof(newopts));
140 strlcat(newopts, " ", sizeof(newopts));
142 return (newopts);
145 static int
146 fsshare_main(const char *file, const char *mountpoint, const char *shareopts,
147 int share)
149 char tmpfile[PATH_MAX];
150 char *line;
151 FILE *newfd, *oldfd;
152 int fd, error;
154 newfd = oldfd = NULL;
155 error = 0;
158 * Create temporary file in the same directory, so we can atomically
159 * rename it.
161 if (strlcpy(tmpfile, file, sizeof(tmpfile)) >= sizeof(tmpfile))
162 return (ENAMETOOLONG);
163 if (strlcat(tmpfile, ".XXXXXXXX", sizeof(tmpfile)) >= sizeof(tmpfile))
164 return (ENAMETOOLONG);
165 fd = mkstemp(tmpfile);
166 if (fd == -1)
167 return (errno);
169 * File name is random, so we don't really need file lock now, but it
170 * will be needed after rename(2).
172 error = flock(fd, LOCK_EX);
173 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
174 newfd = fdopen(fd, "r+");
175 assert(newfd != NULL);
176 /* Open old exports file. */
177 oldfd = fopen(file, "r");
178 if (oldfd == NULL) {
179 if (share) {
180 if (errno != ENOENT) {
181 error = errno;
182 goto out;
184 } else {
185 /* If there is no exports file, ignore the error. */
186 if (errno == ENOENT)
187 errno = 0;
188 error = errno;
189 goto out;
191 } else {
192 error = flock(fileno(oldfd), LOCK_EX);
193 assert(error == 0 || (error == -1 && errno == EOPNOTSUPP));
194 error = 0;
197 /* Place big, fat warning at the begining of the file. */
198 fprintf(newfd, "%s", FILE_HEADER);
199 while (oldfd != NULL && (line = zgetline(oldfd, mountpoint)) != NULL)
200 fprintf(newfd, "%s\n", line);
201 if (oldfd != NULL && ferror(oldfd) != 0) {
202 error = ferror(oldfd);
203 goto out;
205 if (ferror(newfd) != 0) {
206 error = ferror(newfd);
207 goto out;
209 if (share) {
210 fprintf(newfd, "%s\t%s\n", mountpoint,
211 translate_opts(shareopts));
214 out:
215 if (error != 0)
216 unlink(tmpfile);
217 else {
218 if (rename(tmpfile, file) == -1) {
219 error = errno;
220 unlink(tmpfile);
221 } else {
223 * Send SIGHUP to mountd, but unlock exports file later.
225 restart_mountd();
228 if (oldfd != NULL) {
229 flock(fileno(oldfd), LOCK_UN);
230 fclose(oldfd);
232 if (newfd != NULL) {
233 flock(fileno(newfd), LOCK_UN);
234 fclose(newfd);
236 return (error);
240 * Add the given mountpoint to the given exports file.
243 fsshare(const char *file, const char *mountpoint, const char *shareopts)
246 return (fsshare_main(file, mountpoint, shareopts, 1));
250 * Remove the given mountpoint from the given exports file.
253 fsunshare(const char *file, const char *mountpoint)
256 return (fsshare_main(file, mountpoint, NULL, 0));