e19a30
diff -up nfs-utils-1.3.0/utils/mountd/cache.c.orig nfs-utils-1.3.0/utils/mountd/cache.c
e19a30
--- nfs-utils-1.3.0/utils/mountd/cache.c.orig	2017-01-06 09:31:53.155412013 -0500
e19a30
+++ nfs-utils-1.3.0/utils/mountd/cache.c	2017-01-06 09:50:52.438190388 -0500
e19a30
@@ -61,15 +61,13 @@ enum nfsd_fsid {
e19a30
  * Record is terminated with newline.
e19a30
  *
e19a30
  */
e19a30
-static int cache_export_ent(char *domain, struct exportent *exp, char *p);
e19a30
+static int cache_export_ent(char *buf, int buflen, char *domain, struct exportent *exp, char *path);
e19a30
 
e19a30
 #define INITIAL_MANAGED_GROUPS 100
e19a30
 
e19a30
-char *lbuf  = NULL;
e19a30
-int lbuflen = 0;
e19a30
 extern int use_ipaddr;
e19a30
 
e19a30
-static void auth_unix_ip(FILE *f)
e19a30
+static void auth_unix_ip(int f)
e19a30
 {
e19a30
 	/* requests are
e19a30
 	 *  class IP-ADDR
e19a30
@@ -78,23 +76,26 @@ static void auth_unix_ip(FILE *f)
e19a30
 	 *
e19a30
 	 *  "nfsd" IP-ADDR expiry domainname
e19a30
 	 */
e19a30
-	char *cp;
e19a30
 	char class[20];
e19a30
 	char ipaddr[INET6_ADDRSTRLEN + 1];
e19a30
 	char *client = NULL;
e19a30
 	struct addrinfo *tmp = NULL;
e19a30
-	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
e19a30
-		return;
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen;
e19a30
+
e19a30
+	blen = read(f, buf, sizeof(buf));
e19a30
+	if (blen <= 0 || buf[blen-1] != '\n') return;
e19a30
+	buf[blen-1] = 0;
e19a30
 
e19a30
-	xlog(D_CALL, "auth_unix_ip: inbuf '%s'", lbuf);
e19a30
+	xlog(D_CALL, "auth_unix_ip: inbuf '%s'", buf);
e19a30
 
e19a30
-	cp = lbuf;
e19a30
+	bp = buf;
e19a30
 
e19a30
-	if (qword_get(&cp, class, 20) <= 0 ||
e19a30
+	if (qword_get(&bp, class, 20) <= 0 ||
e19a30
 	    strcmp(class, "nfsd") != 0)
e19a30
 		return;
e19a30
 
e19a30
-	if (qword_get(&cp, ipaddr, sizeof(ipaddr) - 1) <= 0)
e19a30
+	if (qword_get(&bp, ipaddr, sizeof(ipaddr) - 1) <= 0)
e19a30
 		return;
e19a30
 
e19a30
 	tmp = host_pton(ipaddr);
e19a30
@@ -113,16 +114,20 @@ static void auth_unix_ip(FILE *f)
e19a30
 			freeaddrinfo(ai);
e19a30
 		}
e19a30
 	}
e19a30
-	qword_print(f, "nfsd");
e19a30
-	qword_print(f, ipaddr);
e19a30
-	qword_printtimefrom(f, DEFAULT_TTL);
e19a30
+	bp = buf; blen = sizeof(buf);
e19a30
+	qword_add(&bp, &blen, "nfsd");
e19a30
+	qword_add(&bp, &blen, ipaddr);
e19a30
+	qword_adduint(&bp, &blen, time(0) + DEFAULT_TTL);
e19a30
 	if (use_ipaddr) {
e19a30
 		memmove(ipaddr + 1, ipaddr, strlen(ipaddr) + 1);
e19a30
 		ipaddr[0] = '$';
e19a30
-		qword_print(f, ipaddr);
e19a30
+		qword_add(&bp, &blen, ipaddr);
e19a30
 	} else if (client)
e19a30
-		qword_print(f, *client?client:"DEFAULT");
e19a30
-	qword_eol(f);
e19a30
+		qword_add(&bp, &blen, *client?client:"DEFAULT");
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0 || write(f, buf, bp - buf) != bp - buf)
e19a30
+		xlog(L_ERROR, "auth_unix_ip: error writing reply");
e19a30
+
e19a30
 	xlog(D_CALL, "auth_unix_ip: client %p '%s'", client, client?client: "DEFAULT");
e19a30
 
e19a30
 	free(client);
e19a30
@@ -130,7 +135,7 @@ static void auth_unix_ip(FILE *f)
e19a30
 
e19a30
 }
e19a30
 
e19a30
-static void auth_unix_gid(FILE *f)
e19a30
+static void auth_unix_gid(int f)
e19a30
 {
e19a30
 	/* Request are
e19a30
 	 *  uid
e19a30
@@ -144,7 +149,8 @@ static void auth_unix_gid(FILE *f)
e19a30
 	gid_t *more_groups;
e19a30
 	int ngroups;
e19a30
 	int rv, i;
e19a30
-	char *cp;
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen;
e19a30
 
e19a30
 	if (groups_len == 0) {
e19a30
 		groups = malloc(sizeof(gid_t) * INITIAL_MANAGED_GROUPS);
e19a30
@@ -156,11 +162,12 @@ static void auth_unix_gid(FILE *f)
e19a30
 
e19a30
 	ngroups = groups_len;
e19a30
 
e19a30
-	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
e19a30
-		return;
e19a30
+	blen = read(f, buf, sizeof(buf));
e19a30
+	if (blen <= 0 || buf[blen-1] != '\n') return;
e19a30
+	buf[blen-1] = 0;
e19a30
 
e19a30
-	cp = lbuf;
e19a30
-	if (qword_get_uint(&cp, &uid) != 0)
e19a30
+	bp = buf;
e19a30
+	if (qword_get_uint(&bp, &uid) != 0)
e19a30
 		return;
e19a30
 
e19a30
 	pw = getpwuid(uid);
e19a30
@@ -180,15 +187,19 @@ static void auth_unix_gid(FILE *f)
e19a30
 			}
e19a30
 		}
e19a30
 	}
e19a30
-	qword_printuint(f, uid);
e19a30
-	qword_printtimefrom(f, DEFAULT_TTL);
e19a30
+
e19a30
+	bp = buf; blen = sizeof(buf);
e19a30
+	qword_adduint(&bp, &blen, uid);
e19a30
+	qword_adduint(&bp, &blen, time(0) + DEFAULT_TTL);
e19a30
 	if (rv >= 0) {
e19a30
-		qword_printuint(f, ngroups);
e19a30
+		qword_adduint(&bp, &blen, ngroups);
e19a30
 		for (i=0; i
e19a30
-			qword_printuint(f, groups[i]);
e19a30
+			qword_adduint(&bp, &blen, groups[i]);
e19a30
 	} else
e19a30
-		qword_printuint(f, 0);
e19a30
-	qword_eol(f);
e19a30
+		qword_adduint(&bp, &blen, 0);
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0 || write(f, buf, bp - buf) != bp - buf)
e19a30
+		xlog(L_ERROR, "auth_unix_gid: error writing reply");
e19a30
 }
e19a30
 
e19a30
 #if USE_BLKID
e19a30
@@ -578,14 +589,13 @@ static struct addrinfo *lookup_client_ad
e19a30
 	return ret;
e19a30
 }
e19a30
 
e19a30
-static void nfsd_fh(FILE *f)
e19a30
+static void nfsd_fh(int f)
e19a30
 {
e19a30
 	/* request are:
e19a30
 	 *  domain fsidtype fsid
e19a30
 	 * interpret fsid, find export point and options, and write:
e19a30
 	 *  domain fsidtype fsid expiry path
e19a30
 	 */
e19a30
-	char *cp;
e19a30
 	char *dom;
e19a30
 	int fsidtype;
e19a30
 	int fsidlen;
e19a30
@@ -597,24 +607,27 @@ static void nfsd_fh(FILE *f)
e19a30
 	nfs_export *exp;
e19a30
 	int i;
e19a30
 	int dev_missing = 0;
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen;
e19a30
 
e19a30
-	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
e19a30
-		return;
e19a30
+	blen = read(f, buf, sizeof(buf));
e19a30
+	if (blen <= 0 || buf[blen-1] != '\n') return;
e19a30
+	buf[blen-1] = 0;
e19a30
 
e19a30
-	xlog(D_CALL, "nfsd_fh: inbuf '%s'", lbuf);
e19a30
+	xlog(D_CALL, "nfsd_fh: inbuf '%s'", buf);
e19a30
 
e19a30
-	cp = lbuf;
e19a30
+	bp = buf;
e19a30
 
e19a30
-	dom = malloc(strlen(cp));
e19a30
+	dom = malloc(blen);
e19a30
 	if (dom == NULL)
e19a30
 		return;
e19a30
-	if (qword_get(&cp, dom, strlen(cp)) <= 0)
e19a30
+	if (qword_get(&bp, dom, blen) <= 0)
e19a30
 		goto out;
e19a30
-	if (qword_get_int(&cp, &fsidtype) != 0)
e19a30
+	if (qword_get_int(&bp, &fsidtype) != 0)
e19a30
 		goto out;
e19a30
 	if (fsidtype < 0 || fsidtype > 7)
e19a30
 		goto out; /* unknown type */
e19a30
-	if ((fsidlen = qword_get(&cp, fsid, 32)) <= 0)
e19a30
+	if ((fsidlen = qword_get(&bp, fsid, 32)) <= 0)
e19a30
 		goto out;
e19a30
 	if (parse_fsid(fsidtype, fsidlen, fsid, &parsed))
e19a30
 		goto out;
e19a30
@@ -715,12 +728,13 @@ static void nfsd_fh(FILE *f)
e19a30
 	}
e19a30
 
e19a30
 	if (found)
e19a30
-		if (cache_export_ent(dom, found, found_path) < 0)
e19a30
+		if (cache_export_ent(buf, sizeof(buf), dom, found, found_path) < 0)
e19a30
 			found = 0;
e19a30
 
e19a30
-	qword_print(f, dom);
e19a30
-	qword_printint(f, fsidtype);
e19a30
-	qword_printhex(f, fsid, fsidlen);
e19a30
+	bp = buf; blen = sizeof(buf);
e19a30
+	qword_add(&bp, &blen, dom);
e19a30
+	qword_addint(&bp, &blen, fsidtype);
e19a30
+	qword_addhex(&bp, &blen, fsid, fsidlen);
e19a30
 	/* The fsid -> path lookup can be quite expensive as it
e19a30
 	 * potentially stats and reads lots of devices, and some of those
e19a30
 	 * might have spun-down.  The Answer is not likely to
e19a30
@@ -729,20 +743,21 @@ static void nfsd_fh(FILE *f)
e19a30
 	 * timeout.  Maybe this should be configurable on the command
e19a30
 	 * line.
e19a30
 	 */
e19a30
-	qword_printint(f, 0x7fffffff);
e19a30
+	qword_addint(&bp, &blen, 0x7fffffff);
e19a30
 	if (found)
e19a30
-		qword_print(f, found_path);
e19a30
-	qword_eol(f);
e19a30
- out:
e19a30
+		qword_add(&bp, &blen, found_path);
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0 || write(f, buf, bp - buf) != bp - buf)
e19a30
+		xlog(L_ERROR, "nfsd_fh: error writing reply");
e19a30
+out:
e19a30
 	if (found_path)
e19a30
 		free(found_path);
e19a30
 	freeaddrinfo(ai);
e19a30
 	free(dom);
e19a30
 	xlog(D_CALL, "nfsd_fh: found %p path %s", found, found ? found->e_path : NULL);
e19a30
-	return;		
e19a30
 }
e19a30
 
e19a30
-static void write_fsloc(FILE *f, struct exportent *ep)
e19a30
+static void write_fsloc(char **bp, int *blen, struct exportent *ep)
e19a30
 {
e19a30
 	struct servers *servers;
e19a30
 
e19a30
@@ -752,20 +767,20 @@ static void write_fsloc(FILE *f, struct
e19a30
 	servers = replicas_lookup(ep->e_fslocmethod, ep->e_fslocdata);
e19a30
 	if (!servers)
e19a30
 		return;
e19a30
-	qword_print(f, "fsloc");
e19a30
-	qword_printint(f, servers->h_num);
e19a30
+	qword_add(bp, blen, "fsloc");
e19a30
+	qword_addint(bp, blen, servers->h_num);
e19a30
 	if (servers->h_num >= 0) {
e19a30
 		int i;
e19a30
 		for (i=0; i<servers->h_num; i++) {
e19a30
-			qword_print(f, servers->h_mp[i]->h_host);
e19a30
-			qword_print(f, servers->h_mp[i]->h_path);
e19a30
+			qword_add(bp, blen, servers->h_mp[i]->h_host);
e19a30
+			qword_add(bp, blen, servers->h_mp[i]->h_path);
e19a30
 		}
e19a30
 	}
e19a30
-	qword_printint(f, servers->h_referral);
e19a30
+	qword_addint(bp, blen, servers->h_referral);
e19a30
 	release_replicas(servers);
e19a30
 }
e19a30
 
e19a30
-static void write_secinfo(FILE *f, struct exportent *ep, int flag_mask)
e19a30
+static void write_secinfo(char **bp, int *blen, struct exportent *ep, int flag_mask)
e19a30
 {
e19a30
 	struct sec_entry *p;
e19a30
 
e19a30
@@ -776,45 +791,52 @@ static void write_secinfo(FILE *f, struc
e19a30
 		return;
e19a30
 	}
e19a30
 	fix_pseudoflavor_flags(ep);
e19a30
-	qword_print(f, "secinfo");
e19a30
-	qword_printint(f, p - ep->e_secinfo);
e19a30
+	qword_add(bp, blen, "secinfo");
e19a30
+	qword_addint(bp, blen, p - ep->e_secinfo);
e19a30
 	for (p = ep->e_secinfo; p->flav; p++) {
e19a30
-		qword_printint(f, p->flav->fnum);
e19a30
-		qword_printint(f, p->flags & flag_mask);
e19a30
+		qword_addint(bp, blen, p->flav->fnum);
e19a30
+		qword_addint(bp, blen, p->flags & flag_mask);
e19a30
 	}
e19a30
 
e19a30
 }
e19a30
 
e19a30
-static int dump_to_cache(FILE *f, char *domain, char *path, struct exportent *exp)
e19a30
+static int dump_to_cache(int f, char *buf, int buflen, char *domain, char *path, struct exportent *exp)
e19a30
 {
e19a30
-	qword_print(f, domain);
e19a30
-	qword_print(f, path);
e19a30
+	char *bp = buf;
e19a30
+	int blen = buflen;
e19a30
+	time_t now = time(0);
e19a30
+
e19a30
+	qword_add(&bp, &blen, domain);
e19a30
+	qword_add(&bp, &blen, path);
e19a30
 	if (exp) {
e19a30
 		int different_fs = strcmp(path, exp->e_path) != 0;
e19a30
 		int flag_mask = different_fs ? ~NFSEXP_FSID : ~0;
e19a30
 
e19a30
-		qword_printtimefrom(f, exp->e_ttl);
e19a30
-		qword_printint(f, exp->e_flags & flag_mask);
e19a30
-		qword_printint(f, exp->e_anonuid);
e19a30
-		qword_printint(f, exp->e_anongid);
e19a30
-		qword_printint(f, exp->e_fsid);
e19a30
-		write_fsloc(f, exp);
e19a30
-		write_secinfo(f, exp, flag_mask);
e19a30
- 		if (exp->e_uuid == NULL || different_fs) {
e19a30
- 			char u[16];
e19a30
- 			if (uuid_by_path(path, 0, 16, u)) {
e19a30
- 				qword_print(f, "uuid");
e19a30
- 				qword_printhex(f, u, 16);
e19a30
- 			}
e19a30
- 		} else {
e19a30
- 			char u[16];
e19a30
- 			get_uuid(exp->e_uuid, 16, u);
e19a30
- 			qword_print(f, "uuid");
e19a30
- 			qword_printhex(f, u, 16);
e19a30
- 		}
e19a30
+		qword_adduint(&bp, &blen, now + exp->e_ttl);
e19a30
+		qword_addint(&bp, &blen, exp->e_flags & flag_mask);
e19a30
+		qword_addint(&bp, &blen, exp->e_anonuid);
e19a30
+		qword_addint(&bp, &blen, exp->e_anongid);
e19a30
+		qword_addint(&bp, &blen, exp->e_fsid);
e19a30
+		write_fsloc(&bp, &blen, exp);
e19a30
+		write_secinfo(&bp, &blen, exp, flag_mask);
e19a30
+		if (exp->e_uuid == NULL || different_fs) {
e19a30
+			char u[16];
e19a30
+			if (uuid_by_path(path, 0, 16, u)) {
e19a30
+				qword_add(&bp, &blen, "uuid");
e19a30
+				qword_addhex(&bp, &blen, u, 16);
e19a30
+			}
e19a30
+		} else {
e19a30
+			char u[16];
e19a30
+			get_uuid(exp->e_uuid, 16, u);
e19a30
+			qword_add(&bp, &blen, "uuid");
e19a30
+			qword_addhex(&bp, &blen, u, 16);
e19a30
+		}
e19a30
 	} else
e19a30
-		qword_printtimefrom(f, DEFAULT_TTL);
e19a30
-	return qword_eol(f);
e19a30
+		qword_adduint(&bp, &blen, now + DEFAULT_TTL);
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0) return -1;
e19a30
+	if (write(f, buf, bp - buf) != bp - buf) return -1;
e19a30
+	return 0;
e19a30
 }
e19a30
 
e19a30
 static nfs_export *
e19a30
@@ -1164,27 +1186,27 @@ static struct exportent *lookup_junction
e19a30
 	return exp;
e19a30
 }
e19a30
 
e19a30
-static void lookup_nonexport(FILE *f, char *dom, char *path,
e19a30
+static void lookup_nonexport(int f, char *buf, int buflen, char *dom, char *path,
e19a30
 		struct addrinfo *ai)
e19a30
 {
e19a30
 	struct exportent *eep;
e19a30
 
e19a30
 	eep = lookup_junction(dom, path, ai);
e19a30
-	dump_to_cache(f, dom, path, eep);
e19a30
+	dump_to_cache(f, buf, buflen, dom, path, eep);
e19a30
 	if (eep == NULL)
e19a30
 		return;
e19a30
 	exportent_release(eep);
e19a30
 	free(eep);
e19a30
 }
e19a30
 #else	/* !HAVE_NFS_PLUGIN_H */
e19a30
-static void lookup_nonexport(FILE *f, char *dom, char *path,
e19a30
+static void lookup_nonexport(int f, char *buf, int buflen, char *dom, char *path,
e19a30
 		struct addrinfo *UNUSED(ai))
e19a30
 {
e19a30
-	dump_to_cache(f, dom, path, NULL);
e19a30
+	dump_to_cache(f, buf, buflen, dom, path, NULL);
e19a30
 }
e19a30
 #endif	/* !HAVE_NFS_PLUGIN_H */
e19a30
 
e19a30
-static void nfsd_export(FILE *f)
e19a30
+static void nfsd_export(int f)
e19a30
 {
e19a30
 	/* requests are:
e19a30
 	 *  domain path
e19a30
@@ -1192,26 +1214,28 @@ static void nfsd_export(FILE *f)
e19a30
 	 *  domain path expiry flags anonuid anongid fsid
e19a30
 	 */
e19a30
 
e19a30
-	char *cp;
e19a30
 	char *dom, *path;
e19a30
 	nfs_export *found = NULL;
e19a30
 	struct addrinfo *ai = NULL;
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen;
e19a30
 
e19a30
-	if (readline(fileno(f), &lbuf, &lbuflen) != 1)
e19a30
-		return;
e19a30
-
e19a30
-	xlog(D_CALL, "nfsd_export: inbuf '%s'", lbuf);
e19a30
-
e19a30
-	cp = lbuf;
e19a30
-	dom = malloc(strlen(cp));
e19a30
-	path = malloc(strlen(cp));
e19a30
+	blen = read(f, buf, sizeof(buf));
e19a30
+	if (blen <= 0 || buf[blen-1] != '\n') return;
e19a30
+	buf[blen-1] = 0;
e19a30
+
e19a30
+	xlog(D_CALL, "nfsd_export: inbuf '%s'", buf);
e19a30
+
e19a30
+	bp = buf;
e19a30
+	dom = malloc(blen);
e19a30
+	path = malloc(blen);
e19a30
 
e19a30
 	if (!dom || !path)
e19a30
 		goto out;
e19a30
 
e19a30
-	if (qword_get(&cp, dom, strlen(lbuf)) <= 0)
e19a30
+	if (qword_get(&bp, dom, blen) <= 0)
e19a30
 		goto out;
e19a30
-	if (qword_get(&cp, path, strlen(lbuf)) <= 0)
e19a30
+	if (qword_get(&bp, path, blen) <= 0)
e19a30
 		goto out;
e19a30
 
e19a30
 	auth_reload();
e19a30
@@ -1225,14 +1249,14 @@ static void nfsd_export(FILE *f)
e19a30
 	found = lookup_export(dom, path, ai);
e19a30
 
e19a30
 	if (found) {
e19a30
-		if (dump_to_cache(f, dom, path, &found->m_export) < 0) {
e19a30
+		if (dump_to_cache(f, buf, sizeof(buf), dom, path, &found->m_export) < 0) {
e19a30
 			xlog(L_WARNING,
e19a30
 			     "Cannot export %s, possibly unsupported filesystem"
e19a30
 			     " or fsid= required", path);
e19a30
-			dump_to_cache(f, dom, path, NULL);
e19a30
+			dump_to_cache(f, buf, sizeof(buf), dom, path, NULL);
e19a30
 		}
e19a30
 	} else
e19a30
-		lookup_nonexport(f, dom, path, ai);
e19a30
+		lookup_nonexport(f, buf, sizeof(buf), dom, path, ai);
e19a30
 
e19a30
  out:
e19a30
 	xlog(D_CALL, "nfsd_export: found %p path %s", found, path ? path : NULL);
e19a30
@@ -1244,15 +1268,14 @@ static void nfsd_export(FILE *f)
e19a30
 
e19a30
 struct {
e19a30
 	char *cache_name;
e19a30
-	void (*cache_handle)(FILE *f);
e19a30
-	FILE *f;
e19a30
-	char vbuf[RPC_CHAN_BUF_SIZE];
e19a30
+	void (*cache_handle)(int f);
e19a30
+	int f;
e19a30
 } cachelist[] = {
e19a30
-	{ "auth.unix.ip", auth_unix_ip, NULL, ""},
e19a30
-	{ "auth.unix.gid", auth_unix_gid, NULL, ""},
e19a30
-	{ "nfsd.export", nfsd_export, NULL, ""},
e19a30
-	{ "nfsd.fh", nfsd_fh, NULL, ""},
e19a30
-	{ NULL, NULL, NULL, ""}
e19a30
+	{ "auth.unix.ip", auth_unix_ip, -1 },
e19a30
+	{ "auth.unix.gid", auth_unix_gid, -1 },
e19a30
+	{ "nfsd.export", nfsd_export, -1 },
e19a30
+	{ "nfsd.fh", nfsd_fh, -1 },
e19a30
+	{ NULL, NULL, -1 }
e19a30
 };
e19a30
 
e19a30
 extern int manage_gids;
e19a30
@@ -1269,11 +1292,7 @@ void cache_open(void)
e19a30
 		if (!manage_gids && cachelist[i].cache_handle == auth_unix_gid)
e19a30
 			continue;
e19a30
 		sprintf(path, "/proc/net/rpc/%s/channel", cachelist[i].cache_name);
e19a30
-		cachelist[i].f = fopen(path, "r+");
e19a30
-		if (cachelist[i].f != NULL) {
e19a30
-			setvbuf(cachelist[i].f, cachelist[i].vbuf, _IOLBF, 
e19a30
-				RPC_CHAN_BUF_SIZE);
e19a30
-		}
e19a30
+		cachelist[i].f = open(path, O_RDWR);
e19a30
 	}
e19a30
 }
e19a30
 
e19a30
@@ -1285,8 +1304,8 @@ void cache_set_fds(fd_set *fdset)
e19a30
 {
e19a30
 	int i;
e19a30
 	for (i=0; cachelist[i].cache_name; i++) {
e19a30
-		if (cachelist[i].f)
e19a30
-			FD_SET(fileno(cachelist[i].f), fdset);
e19a30
+		if (cachelist[i].f >= 0)
e19a30
+			FD_SET(cachelist[i].f, fdset);
e19a30
 	}
e19a30
 }
e19a30
 
e19a30
@@ -1299,11 +1318,11 @@ int cache_process_req(fd_set *readfds)
e19a30
 	int i;
e19a30
 	int cnt = 0;
e19a30
 	for (i=0; cachelist[i].cache_name; i++) {
e19a30
-		if (cachelist[i].f != NULL &&
e19a30
-		    FD_ISSET(fileno(cachelist[i].f), readfds)) {
e19a30
+		if (cachelist[i].f >= 0 &&
e19a30
+		    FD_ISSET(cachelist[i].f, readfds)) {
e19a30
 			cnt++;
e19a30
 			cachelist[i].cache_handle(cachelist[i].f);
e19a30
-			FD_CLR(fileno(cachelist[i].f), readfds);
e19a30
+			FD_CLR(cachelist[i].f, readfds);
e19a30
 		}
e19a30
 	}
e19a30
 	return cnt;
e19a30
@@ -1316,14 +1335,14 @@ int cache_process_req(fd_set *readfds)
e19a30
  * % echo $domain $path $[now+DEFAULT_TTL] $options $anonuid $anongid $fsid > /proc/net/rpc/nfsd.export/channel
e19a30
  */
e19a30
 
e19a30
-static int cache_export_ent(char *domain, struct exportent *exp, char *path)
e19a30
+static int cache_export_ent(char *buf, int buflen, char *domain, struct exportent *exp, char *path)
e19a30
 {
e19a30
-	int err;
e19a30
-	FILE *f = fopen("/proc/net/rpc/nfsd.export/channel", "w");
e19a30
-	if (!f)
e19a30
-		return -1;
e19a30
+	int f, err;
e19a30
+
e19a30
+	f = open("/proc/net/rpc/nfsd.export/channel", O_WRONLY);
e19a30
+	if (f < 0) return -1;
e19a30
 
e19a30
-	err = dump_to_cache(f, domain, exp->e_path, exp);
e19a30
+	err = dump_to_cache(f, buf, buflen, domain, exp->e_path, exp);
e19a30
 	if (err) {
e19a30
 		xlog(L_WARNING,
e19a30
 		     "Cannot export %s, possibly unsupported filesystem or"
e19a30
@@ -1364,13 +1383,13 @@ static int cache_export_ent(char *domain
e19a30
 				continue;
e19a30
 			dev = stb.st_dev;
e19a30
 			path[l] = 0;
e19a30
-			dump_to_cache(f, domain, path, exp);
e19a30
+			dump_to_cache(f, buf, buflen, domain, path, exp);
e19a30
 			path[l] = c;
e19a30
 		}
e19a30
 		break;
e19a30
 	}
e19a30
 
e19a30
-	fclose(f);
e19a30
+	close(f);
e19a30
 	return err;
e19a30
 }
e19a30
 
e19a30
@@ -1381,27 +1400,25 @@ static int cache_export_ent(char *domain
e19a30
  */
e19a30
 int cache_export(nfs_export *exp, char *path)
e19a30
 {
e19a30
-	char buf[INET6_ADDRSTRLEN];
e19a30
-	int err;
e19a30
-	FILE *f;
e19a30
+	char ip[INET6_ADDRSTRLEN];
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen, f;
e19a30
 
e19a30
-	f = fopen("/proc/net/rpc/auth.unix.ip/channel", "w");
e19a30
-	if (!f)
e19a30
+	f = open("/proc/net/rpc/auth.unix.ip/channel", O_WRONLY);
e19a30
+	if (f < 0)
e19a30
 		return -1;
e19a30
 
e19a30
+	bp = buf, blen = sizeof(buf);
e19a30
+	qword_add(&bp, &blen, "nfsd");
e19a30
+	qword_add(&bp, &blen, host_ntop(get_addrlist(exp->m_client, 0), ip, sizeof(ip)));
e19a30
+	qword_adduint(&bp, &blen, time(0) + exp->m_export.e_ttl);
e19a30
+	qword_add(&bp, &blen, exp->m_client->m_hostname);
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0 || write(f, buf, bp - buf) != bp - buf) blen = -1;
e19a30
+	close(f);
e19a30
+	if (blen < 0) return -1;
e19a30
 
e19a30
-	qword_print(f, "nfsd");
e19a30
-	qword_print(f,
e19a30
-		host_ntop(get_addrlist(exp->m_client, 0), buf, sizeof(buf)));
e19a30
-	qword_printtimefrom(f, exp->m_export.e_ttl);
e19a30
-	qword_print(f, exp->m_client->m_hostname);
e19a30
-	err = qword_eol(f);
e19a30
-	
e19a30
-	fclose(f);
e19a30
-
e19a30
-	err = cache_export_ent(exp->m_client->m_hostname, &exp->m_export, path)
e19a30
-		|| err;
e19a30
-	return err;
e19a30
+	return cache_export_ent(buf, sizeof(buf), exp->m_client->m_hostname, &exp->m_export, path);
e19a30
 }
e19a30
 
e19a30
 /**
e19a30
@@ -1420,27 +1437,33 @@ int cache_export(nfs_export *exp, char *
e19a30
 struct nfs_fh_len *
e19a30
 cache_get_filehandle(nfs_export *exp, int len, char *p)
e19a30
 {
e19a30
-	FILE *f = fopen("/proc/fs/nfsd/filehandle", "r+");
e19a30
-	char buf[200];
e19a30
-	char *bp = buf;
e19a30
-	int failed;
e19a30
 	static struct nfs_fh_len fh;
e19a30
+	char buf[RPC_CHAN_BUF_SIZE], *bp;
e19a30
+	int blen, f;
e19a30
 
e19a30
-	if (!f)
e19a30
-		f = fopen("/proc/fs/nfs/filehandle", "r+");
e19a30
-	if (!f)
e19a30
+	f = open("/proc/fs/nfsd/filehandle", O_RDWR);
e19a30
+	if (f < 0) {
e19a30
+		f = open("/proc/fs/nfs/filehandle", O_RDWR);
e19a30
+		if (f < 0) return NULL;
e19a30
+	}
e19a30
+
e19a30
+	bp = buf, blen = sizeof(buf);
e19a30
+	qword_add(&bp, &blen, exp->m_client->m_hostname);
e19a30
+	qword_add(&bp, &blen, p);
e19a30
+	qword_addint(&bp, &blen, len);
e19a30
+	qword_addeol(&bp, &blen);
e19a30
+	if (blen <= 0 || write(f, buf, bp - buf) != bp - buf) {
e19a30
+		close(f);
e19a30
 		return NULL;
e19a30
+	}
e19a30
+	bp = buf;
e19a30
+	blen = read(f, buf, sizeof(buf));
e19a30
+	close(f);
e19a30
 
e19a30
-	qword_print(f, exp->m_client->m_hostname);
e19a30
-	qword_print(f, p);
e19a30
-	qword_printint(f, len);	
e19a30
-	failed = qword_eol(f);
e19a30
-	
e19a30
-	if (!failed)
e19a30
-		failed = (fgets(buf, sizeof(buf), f) == NULL);
e19a30
-	fclose(f);
e19a30
-	if (failed)
e19a30
+	if (blen <= 0 || buf[blen-1] != '\n')
e19a30
 		return NULL;
e19a30
+	buf[blen-1] = 0;
e19a30
+
e19a30
 	memset(fh.fh_handle, 0, sizeof(fh.fh_handle));
e19a30
 	fh.fh_size = qword_get(&bp, (char *)fh.fh_handle, NFS3_FHSIZE);
e19a30
 	return &fh;