]>
git.saurik.com Git - apple/network_cmds.git/blob - nfsiod.tproj/nfsiod.c
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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,
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1989, 1993
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
30 * Rick Macklem at The University of Guelph.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
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.
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
62 #include <sys/param.h>
63 #include <sys/ioctl.h>
64 #include <sys/syslog.h>
65 #include <sys/ucred.h>
68 #include <sys/mount.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
90 int *thread_status
= NULL
;
92 pthread_mutex_t mutex
;
94 void nonfs
__P((int));
95 void usage
__P((void));
96 void *nfsiod_thread
__P((void *));
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.
109 int i
, rv
, threadcnt
;
111 #define MAXNFSIODCNT 32
112 #define DEFNFSIODCNT 1
113 num_servers
= DEFNFSIODCNT
;
114 while ((ch
= getopt(argc
, argv
, "n:")) != EOF
)
117 num_servers
= atoi(optarg
);
118 if (num_servers
< 1 || num_servers
> MAXNFSIODCNT
) {
119 warnx("nfsiod count %d; reset to %d",
120 num_servers
, DEFNFSIODCNT
);
121 num_servers
= DEFNFSIODCNT
;
133 * Backward compatibility, trailing number is the count of daemons.
138 num_servers
= atoi(argv
[0]);
139 if (num_servers
< 1 || num_servers
> MAXNFSIODCNT
) {
140 warnx("nfsiod count %d; reset to %d",
141 num_servers
, DEFNFSIODCNT
);
142 num_servers
= DEFNFSIODCNT
;
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
);
151 errc(1, rv
, "condition variable init failed");
152 rv
= pthread_mutex_init(&mutex
, NULL
);
154 errc(1, rv
, "mutex init failed");
158 (void)signal(SIGHUP
, SIG_IGN
);
159 (void)signal(SIGINT
, SIG_IGN
);
160 (void)signal(SIGQUIT
, SIG_IGN
);
161 (void)signal(SIGSYS
, nonfs
);
164 openlog("nfsiod:", LOG_PID
, LOG_DAEMON
);
167 for (i
=0; i
< num_servers
; i
++) {
169 thread_status
[i
] = 1;
170 rv
= pthread_create(&thd
, NULL
, nfsiod_thread
, (void*)i
);
172 syslog(LOG_ERR
, "thread_create: %s", strerror(rv
));
173 thread_status
[i
] = 0;
178 /* if no threads started exit */
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
);
185 /* wait for threads to complete */
186 rv
= pthread_mutex_lock(&mutex
);
188 errc(1, rv
, "mutex lock failed");
189 while (threadcnt
> 0) {
190 rv
= pthread_cond_wait(&cond
, &mutex
);
192 errc(1, rv
, "nfsiod: cond wait failed");
193 for (i
=0; i
< num_servers
; i
++) {
194 if (!thread_status
[i
])
196 if (thread_status
[i
] == 1)
199 thread_status
[i
] = 0;
200 syslog(LOG_ERR
, "lost nfsiod thread %d - "
201 "%d of %d threads remain",
202 i
, threadcnt
, num_servers
);
204 rv
= pthread_mutex_lock(&mutex
);
206 errc(1, rv
, "mutex lock failed");
213 nfsiod_thread(void *arg
)
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
);
222 thread_status
[thread
] = 0;
223 pthread_cond_signal(&cond
);
231 syslog(LOG_ERR
, "missing system call: NFS not available.");
237 (void)fprintf(stderr
, "usage: nfsiod [-n num_servers]\n");