6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
24 #define NFSD_PORTS_FILE "/proc/fs/nfsd/portlist"
25 #define NFSD_VERS_FILE "/proc/fs/nfsd/versions"
26 #define NFSD_THREAD_FILE "/proc/fs/nfsd/threads"
29 nfssvc_setfds(int port, unsigned int ctlbits, char *haddr)
33 int udpfd = -1, tcpfd = -1;
34 struct sockaddr_in sin;
36 fd = open(NFSD_PORTS_FILE, O_WRONLY);
39 sin.sin_family = AF_INET;
40 sin.sin_port = htons(port);
41 sin.sin_addr.s_addr = inet_addr(haddr);
43 if (NFSCTL_UDPISSET(ctlbits)) {
44 udpfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
46 syslog(LOG_ERR, "nfssvc: unable to create UPD socket: "
47 "errno %d (%s)\n", errno, strerror(errno));
50 if (bind(udpfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
51 syslog(LOG_ERR, "nfssvc: unable to bind UPD socket: "
52 "errno %d (%s)\n", errno, strerror(errno));
57 if (NFSCTL_TCPISSET(ctlbits)) {
58 tcpfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
60 syslog(LOG_ERR, "nfssvc: unable to createt tcp socket: "
61 "errno %d (%s)\n", errno, strerror(errno));
64 if (setsockopt(tcpfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
65 syslog(LOG_ERR, "nfssvc: unable to set SO_REUSEADDR: "
66 "errno %d (%s)\n", errno, strerror(errno));
69 if (bind(tcpfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
70 syslog(LOG_ERR, "nfssvc: unable to bind TCP socket: "
71 "errno %d (%s)\n", errno, strerror(errno));
74 if (listen(tcpfd, 64) < 0){
75 syslog(LOG_ERR, "nfssvc: unable to create listening socket: "
76 "errno %d (%s)\n", errno, strerror(errno));
81 snprintf(buf, BUFSIZ,"%d\n", udpfd);
82 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
84 "nfssvc: writting fds to kernel failed: errno %d (%s)",
85 errno, strerror(errno));
92 fd = open(NFSD_PORTS_FILE, O_WRONLY);
93 snprintf(buf, BUFSIZ,"%d\n", tcpfd);
94 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
96 "nfssvc: writting fds to kernel failed: errno %d (%s)",
97 errno, strerror(errno));
105 nfssvc_versbits(unsigned int ctlbits)
108 char buf[BUFSIZ], *ptr;
112 fd = open(NFSD_VERS_FILE, O_WRONLY);
116 for (n = NFSD_MINVERS; n <= NFSD_MAXVERS; n++) {
117 if (NFSCTL_VERISSET(ctlbits, n))
118 off += snprintf(ptr+off, BUFSIZ - off, "+%d ", n);
120 off += snprintf(ptr+off, BUFSIZ - off, "-%d ", n);
122 snprintf(ptr+off, BUFSIZ - off, "\n");
123 if (write(fd, buf, strlen(buf)) != strlen(buf)) {
124 syslog(LOG_ERR, "nfssvc: Setting version failed: errno %d (%s)",
125 errno, strerror(errno));
132 nfssvc(int port, int nrservs, unsigned int versbits, unsigned protobits,
135 struct nfsctl_arg arg;
138 /* Note: must set versions before fds so that
139 * the ports get registered with portmap against correct
142 nfssvc_versbits(versbits);
143 nfssvc_setfds(port, protobits, haddr);
145 fd = open(NFSD_THREAD_FILE, O_WRONLY);
147 fd = open("/proc/fs/nfs/threads", O_WRONLY);
149 /* 2.5+ kernel with nfsd filesystem mounted.
150 * Just write the number in.
151 * Cannot handle port number yet, but does anyone care?
155 snprintf(buf, 20,"%d\n", nrservs);
156 n = write(fd, buf, strlen(buf));
158 if (n != strlen(buf))
164 arg.ca_version = NFSCTL_VERSION;
165 arg.ca_svc.svc_nthreads = nrservs;
166 arg.ca_svc.svc_port = port;
167 return nfsctl(NFSCTL_SVC, &arg, NULL);