c51ace18ea078c223aa597afb4637b9fbd1663a1
[nfs-utils.git] / support / nfs / nfssvc.c
1 /*
2  * support/nfs/nfssvc.c
3  *
4  * Run an NFS daemon.
5  *
6  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <syslog.h>
17
18 #include "nfslib.h"
19
20 static void
21 nfssvc_versbits(unsigned int ctlbits)
22 {
23         int fd, n, off;
24         char buf[BUFSIZ], *ptr;
25
26         ptr = buf;
27         off = 0;
28         fd = open("/proc/fs/nfsd/versions", O_WRONLY);
29         if (fd < 0)
30                 return;
31
32         for (n = NFSD_MINVERS; n <= NFSD_MAXVERS; n++) {
33                 if (NFSCTL_VERISSET(ctlbits, n))
34                     off += snprintf(ptr+off, BUFSIZ - off, "+%d ", n);
35                 else
36                     off += snprintf(ptr+off, BUFSIZ - off, "-%d ", n);
37         }
38         snprintf(ptr+off, BUFSIZ - off, "\n");
39         if (write(fd, buf, strlen(buf)) != strlen(buf)) {
40                 syslog(LOG_ERR, "nfssvc: Setting version failed: errno %d (%s)", 
41                         errno, strerror(errno));
42         }
43         close(fd);
44
45         return;
46 }
47 int
48 nfssvc(int port, int nrservs, unsigned int versbits)
49 {
50         struct nfsctl_arg       arg;
51         int fd;
52
53         nfssvc_versbits(versbits);
54
55         fd = open("/proc/fs/nfsd/threads", O_WRONLY);
56         if (fd < 0)
57                 fd = open("/proc/fs/nfs/threads", O_WRONLY);
58         if (fd >= 0) {
59                 /* 2.5+ kernel with nfsd filesystem mounted.
60                  * Just write the number in.
61                  * Cannot handle port number yet, but does anyone care?
62                  */
63                 char buf[20];
64                 int n;
65                 snprintf(buf, 20,"%d\n", nrservs);
66                 n = write(fd, buf, strlen(buf));
67                 close(fd);
68                 if (n != strlen(buf))
69                         return -1;
70                 else
71                         return 0;
72         }
73
74         arg.ca_version = NFSCTL_VERSION;
75         arg.ca_svc.svc_nthreads = nrservs;
76         arg.ca_svc.svc_port = port;
77         return nfsctl(NFSCTL_SVC, &arg, NULL);
78 }