Blame SOURCES/nfs-utils-1.3.0-mountd-filedes.patch

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