]>
Commit | Line | Data |
---|---|---|
b7080c8e | 1 | /* |
ac2f15b3 | 2 | * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved. |
b7080c8e A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
07f47057 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
b7080c8e A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
07f47057 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
b7080c8e A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1989, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * This code is derived from software contributed to Berkeley by | |
30 | * Rick Macklem at The University of Guelph. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | ||
62 | #include <sys/param.h> | |
63 | #include <sys/ioctl.h> | |
64 | #include <sys/syslog.h> | |
65 | #include <sys/ucred.h> | |
66 | #include <sys/wait.h> | |
67 | ||
68 | #include <sys/mount.h> | |
69 | #include <sys/time.h> | |
70 | #include <nfs/rpcv2.h> | |
71 | #include <nfs/nfsproto.h> | |
72 | #include <nfs/nfs.h> | |
73 | ||
74 | #include <err.h> | |
75 | #include <errno.h> | |
76 | #include <fcntl.h> | |
77 | #include <signal.h> | |
78 | #include <stdio.h> | |
79 | #include <stdlib.h> | |
80 | #include <unistd.h> | |
ac2f15b3 A |
81 | #include <string.h> |
82 | #include <pthread.h> | |
b7080c8e A |
83 | |
84 | /* Global defs */ | |
85 | #ifdef DEBUG | |
86 | int debug = 1; | |
87 | #else | |
88 | int debug = 0; | |
89 | #endif | |
ac2f15b3 A |
90 | int *thread_status = NULL; |
91 | pthread_cond_t cond; | |
92 | pthread_mutex_t mutex; | |
b7080c8e A |
93 | |
94 | void nonfs __P((int)); | |
b7080c8e | 95 | void usage __P((void)); |
ac2f15b3 | 96 | void *nfsiod_thread __P((void *)); |
b7080c8e A |
97 | |
98 | /* | |
99 | * Nfsiod does asynchronous buffered I/O on behalf of the NFS client. | |
100 | * It does not have to be running for correct operation, but will | |
101 | * improve throughput. | |
102 | */ | |
103 | int | |
104 | main(argc, argv) | |
105 | int argc; | |
106 | char *argv[]; | |
107 | { | |
108 | int ch, num_servers; | |
ac2f15b3 | 109 | int i, rv, threadcnt; |
b7080c8e | 110 | |
ac2f15b3 A |
111 | #define MAXNFSIODCNT 32 |
112 | #define DEFNFSIODCNT 1 | |
113 | num_servers = DEFNFSIODCNT; | |
b7080c8e A |
114 | while ((ch = getopt(argc, argv, "n:")) != EOF) |
115 | switch (ch) { | |
116 | case 'n': | |
117 | num_servers = atoi(optarg); | |
ac2f15b3 | 118 | if (num_servers < 1 || num_servers > MAXNFSIODCNT) { |
b7080c8e | 119 | warnx("nfsiod count %d; reset to %d", |
ac2f15b3 A |
120 | num_servers, DEFNFSIODCNT); |
121 | num_servers = DEFNFSIODCNT; | |
b7080c8e A |
122 | } |
123 | break; | |
124 | case '?': | |
125 | default: | |
126 | usage(); | |
127 | } | |
128 | argc -= optind; | |
129 | argv += optind; | |
130 | ||
131 | /* | |
132 | * XXX | |
133 | * Backward compatibility, trailing number is the count of daemons. | |
134 | */ | |
135 | if (argc > 1) | |
136 | usage(); | |
137 | if (argc == 1) { | |
138 | num_servers = atoi(argv[0]); | |
ac2f15b3 A |
139 | if (num_servers < 1 || num_servers > MAXNFSIODCNT) { |
140 | warnx("nfsiod count %d; reset to %d", | |
141 | num_servers, DEFNFSIODCNT); | |
142 | num_servers = DEFNFSIODCNT; | |
b7080c8e A |
143 | } |
144 | } | |
145 | ||
ac2f15b3 A |
146 | thread_status = malloc(sizeof(int) * num_servers); |
147 | if (thread_status == NULL) | |
148 | errx(1, "unable to allocate memory"); | |
149 | rv = pthread_cond_init(&cond, NULL); | |
150 | if (rv) | |
151 | errc(1, rv, "condition variable init failed"); | |
152 | rv = pthread_mutex_init(&mutex, NULL); | |
153 | if (rv) | |
154 | errc(1, rv, "mutex init failed"); | |
155 | ||
b7080c8e A |
156 | if (debug == 0) { |
157 | daemon(0, 0); | |
158 | (void)signal(SIGHUP, SIG_IGN); | |
159 | (void)signal(SIGINT, SIG_IGN); | |
160 | (void)signal(SIGQUIT, SIG_IGN); | |
161 | (void)signal(SIGSYS, nonfs); | |
162 | } | |
b7080c8e A |
163 | |
164 | openlog("nfsiod:", LOG_PID, LOG_DAEMON); | |
165 | ||
ac2f15b3 A |
166 | threadcnt = 0; |
167 | for (i=0; i < num_servers; i++) { | |
168 | pthread_t thd; | |
169 | thread_status[i] = 1; | |
170 | rv = pthread_create(&thd, NULL, nfsiod_thread, (void*)i); | |
171 | if (rv) { | |
172 | syslog(LOG_ERR, "thread_create: %s", strerror(rv)); | |
173 | thread_status[i] = 0; | |
174 | continue; | |
175 | } | |
176 | threadcnt++; | |
177 | } | |
178 | /* if no threads started exit */ | |
179 | if (!threadcnt) | |
180 | errx(1, "unable to start any threads"); | |
181 | if (threadcnt != num_servers) | |
182 | syslog(LOG_ERR, "only able to create %d of %d threads", | |
183 | threadcnt, num_servers); | |
184 | ||
185 | /* wait for threads to complete */ | |
186 | rv = pthread_mutex_lock(&mutex); | |
187 | if (rv) | |
188 | errc(1, rv, "mutex lock failed"); | |
189 | while (threadcnt > 0) { | |
190 | rv = pthread_cond_wait(&cond, &mutex); | |
191 | if (rv) | |
192 | errc(1, rv, "nfsiod: cond wait failed"); | |
193 | for (i=0; i < num_servers; i++) { | |
194 | if (!thread_status[i]) | |
195 | continue; | |
196 | if (thread_status[i] == 1) | |
197 | continue; | |
198 | threadcnt--; | |
199 | thread_status[i] = 0; | |
200 | syslog(LOG_ERR, "lost nfsiod thread %d - " | |
201 | "%d of %d threads remain", | |
202 | i, threadcnt, num_servers); | |
b7080c8e | 203 | } |
ac2f15b3 A |
204 | rv = pthread_mutex_lock(&mutex); |
205 | if (rv) | |
206 | errc(1, rv, "mutex lock failed"); | |
207 | } | |
208 | ||
b7080c8e A |
209 | exit (0); |
210 | } | |
211 | ||
ac2f15b3 A |
212 | void * |
213 | nfsiod_thread(void *arg) | |
b7080c8e | 214 | { |
ac2f15b3 A |
215 | int rv, thread = (int)arg; |
216 | if ((rv = nfssvc(NFSSVC_BIOD, NULL)) < 0) { | |
217 | thread_status[thread] = rv; | |
218 | syslog(LOG_ERR, "nfssvc: %s", strerror(rv)); | |
219 | pthread_cond_signal(&cond); | |
220 | return NULL; | |
221 | } | |
222 | thread_status[thread] = 0; | |
223 | pthread_cond_signal(&cond); | |
224 | return NULL; | |
b7080c8e A |
225 | } |
226 | ||
227 | void | |
ac2f15b3 | 228 | nonfs(signo) |
b7080c8e A |
229 | int signo; |
230 | { | |
ac2f15b3 | 231 | syslog(LOG_ERR, "missing system call: NFS not available."); |
b7080c8e A |
232 | } |
233 | ||
234 | void | |
235 | usage() | |
236 | { | |
237 | (void)fprintf(stderr, "usage: nfsiod [-n num_servers]\n"); | |
238 | exit(1); | |
239 | } |