Blame SOURCES/autofs-5.0.7-use-numeric-protocol-ids-instead-of-protoent-structs.patch

304803
autofs-5.0.7 - use numeric protocol ids instead of protoent structs
304803
304803
From: Leonardo Chiquitto <leonardo.lists@gmail.com>
304803
304803
The function getprotobyname() is not reentrant, so we can't call
304803
it simultaneously from multiple threads. Instead of switching to
304803
the reentrant version which adds more complexity to the code,
304803
lets use numeric protocol IDs instead of protoent structures.
304803
---
304803
304803
 CHANGELOG            |    1 +
304803
 include/rpc_subs.h   |    4 +--
304803
 lib/rpc_subs.c       |   80 ++++++++++++++++++--------------------------------
304803
 modules/replicated.c |   42 +++++++++++---------------
304803
 4 files changed, 50 insertions(+), 77 deletions(-)
304803
304803
304803
diff --git a/CHANGELOG b/CHANGELOG
304803
index 4cf5621..ba1d65b 100644
304803
--- a/CHANGELOG
304803
+++ b/CHANGELOG
304803
@@ -23,6 +23,7 @@
304803
 - fix use get_proximity() without libtirpc.
304803
 - don't use dirent d_type to filter out files in scandir()
304803
 - don't schedule new alarms after readmap.
304803
+- use numeric protocol ids instead of protoent structs.
304803
 
304803
 25/07/2012 autofs-5.0.7
304803
 =======================
304803
diff --git a/include/rpc_subs.h b/include/rpc_subs.h
304803
index ca474d9..b6d59f9 100644
304803
--- a/include/rpc_subs.h
304803
+++ b/include/rpc_subs.h
304803
@@ -54,7 +54,7 @@ struct conn_info {
304803
 	unsigned short port;
304803
 	unsigned long program;
304803
 	unsigned long version;
304803
-	struct protoent *proto;
304803
+	int proto;
304803
 	unsigned int send_sz;
304803
 	unsigned int recv_sz;
304803
 	struct timeval timeout;
304803
@@ -66,7 +66,7 @@ int rpc_udp_getclient(struct conn_info *, unsigned int, unsigned int);
304803
 void rpc_destroy_udp_client(struct conn_info *);
304803
 int rpc_tcp_getclient(struct conn_info *, unsigned int, unsigned int);
304803
 void rpc_destroy_tcp_client(struct conn_info *);
304803
-int rpc_portmap_getclient(struct conn_info *, const char *, struct sockaddr *, size_t, const char *, unsigned int);
304803
+int rpc_portmap_getclient(struct conn_info *, const char *, struct sockaddr *, size_t, int, unsigned int);
304803
 int rpc_portmap_getport(struct conn_info *, struct pmap *, unsigned short *);
304803
 int rpc_ping_proto(struct conn_info *);
304803
 int rpc_ping(const char *, long, long, unsigned int);
304803
diff --git a/lib/rpc_subs.c b/lib/rpc_subs.c
304803
index d33a3c4..ad1d557 100644
304803
--- a/lib/rpc_subs.c
304803
+++ b/lib/rpc_subs.c
304803
@@ -170,7 +170,7 @@ static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, i
304803
 
304803
 	*client = NULL;
304803
 
304803
-	proto = info->proto->p_proto;
304803
+	proto = info->proto;
304803
 	if (proto == IPPROTO_UDP)
304803
 		type = SOCK_DGRAM;
304803
 	else
304803
@@ -201,7 +201,7 @@ static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, i
304803
 	in4_raddr = (struct sockaddr_in *) addr;
304803
 	in4_raddr->sin_port = htons(info->port);
304803
 
304803
-	switch (info->proto->p_proto) {
304803
+	switch (info->proto) {
304803
 	case IPPROTO_UDP:
304803
 		clnt = clntudp_bufcreate(in4_raddr,
304803
 					 info->program, info->version,
304803
@@ -241,7 +241,7 @@ static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, i
304803
 
304803
 	*client = NULL;
304803
 
304803
-	proto = info->proto->p_proto;
304803
+	proto = info->proto;
304803
 	if (proto == IPPROTO_UDP)
304803
 		type = SOCK_DGRAM;
304803
 	else
304803
@@ -292,11 +292,11 @@ static int rpc_do_create_client(struct sockaddr *addr, struct conn_info *info, i
304803
 	nb_addr.maxlen = nb_addr.len = slen;
304803
 	nb_addr.buf = addr;
304803
 
304803
-	if (info->proto->p_proto == IPPROTO_UDP)
304803
+	if (info->proto == IPPROTO_UDP)
304803
 		clnt = clnt_dg_create(*fd, &nb_addr,
304803
 				      info->program, info->version,
304803
 				      info->send_sz, info->recv_sz);
304803
-	else if (info->proto->p_proto == IPPROTO_TCP) {
304803
+	else if (info->proto == IPPROTO_TCP) {
304803
 		ret = connect_nb(*fd, addr, slen, &info->timeout);
304803
 		if (ret < 0)
304803
 			return ret;
304803
@@ -355,7 +355,7 @@ static int create_client(struct conn_info *info, CLIENT **client)
304803
 	memset(&hints, 0, sizeof(hints));
304803
 	hints.ai_flags = AI_ADDRCONFIG;
304803
 	hints.ai_family = AF_UNSPEC;
304803
-	if (info->proto->p_proto == IPPROTO_UDP)
304803
+	if (info->proto == IPPROTO_UDP)
304803
 		hints.ai_socktype = SOCK_DGRAM;
304803
 	else
304803
 		hints.ai_socktype = SOCK_STREAM;
304803
@@ -370,7 +370,7 @@ static int create_client(struct conn_info *info, CLIENT **client)
304803
 
304803
 	haddr = ai;
304803
 	while (haddr) {
304803
-		if (haddr->ai_protocol != info->proto->p_proto) {
304803
+		if (haddr->ai_protocol != info->proto) {
304803
 			haddr = haddr->ai_next;
304803
 			continue;
304803
 		}
304803
@@ -417,16 +417,11 @@ out_close:
304803
 int rpc_udp_getclient(struct conn_info *info,
304803
 		      unsigned int program, unsigned int version)
304803
 {
304803
-	struct protoent *pe_proto;
304803
 	CLIENT *client;
304803
 	int ret;
304803
 
304803
 	if (!info->client) {
304803
-		pe_proto = getprotobyname("udp");
304803
-		if (!pe_proto)
304803
-			return -ENOENT;
304803
-
304803
-		info->proto = pe_proto;
304803
+		info->proto = IPPROTO_UDP;
304803
 		info->timeout.tv_sec = RPC_TOUT_UDP;
304803
 		info->timeout.tv_usec = 0;
304803
 		info->send_sz = UDPMSGSIZE;
304803
@@ -458,16 +453,11 @@ void rpc_destroy_udp_client(struct conn_info *info)
304803
 int rpc_tcp_getclient(struct conn_info *info,
304803
 		      unsigned int program, unsigned int version)
304803
 {
304803
-	struct protoent *pe_proto;
304803
 	CLIENT *client;
304803
 	int ret;
304803
 
304803
 	if (!info->client) {
304803
-		pe_proto = getprotobyname("tcp");
304803
-		if (!pe_proto)
304803
-			return -ENOENT;
304803
-
304803
-		info->proto = pe_proto;
304803
+		info->proto = IPPROTO_TCP;
304803
 		info->timeout.tv_sec = RPC_TOUT_TCP;
304803
 		info->timeout.tv_usec = 0;
304803
 		info->send_sz = 0;
304803
@@ -513,23 +503,18 @@ void rpc_destroy_tcp_client(struct conn_info *info)
304803
 
304803
 int rpc_portmap_getclient(struct conn_info *info,
304803
 			  const char *host, struct sockaddr *addr, size_t addr_len,
304803
-			  const char *proto, unsigned int option)
304803
+			  int proto, unsigned int option)
304803
 {
304803
-	struct protoent *pe_proto;
304803
 	CLIENT *client;
304803
 	int ret;
304803
 
304803
-	pe_proto = getprotobyname(proto);
304803
-	if (!pe_proto)
304803
-		return -ENOENT;
304803
-
304803
 	info->host = host;
304803
 	info->addr = addr;
304803
 	info->addr_len = addr_len;
304803
 	info->program = PMAPPROG;
304803
 	info->port = PMAPPORT;
304803
 	info->version = PMAPVERS;
304803
-	info->proto = pe_proto;
304803
+	info->proto = proto;
304803
 	info->send_sz = RPCSMALLMSGSIZE;
304803
 	info->recv_sz = RPCSMALLMSGSIZE;
304803
 	info->timeout.tv_sec = PMAP_TOUT_UDP;
304803
@@ -537,7 +522,7 @@ int rpc_portmap_getclient(struct conn_info *info,
304803
 	info->close_option = option;
304803
 	info->client = NULL;
304803
 
304803
-	if (pe_proto->p_proto == IPPROTO_TCP)
304803
+	if (info->proto == IPPROTO_TCP)
304803
 		info->timeout.tv_sec = PMAP_TOUT_TCP;
304803
 
304803
 	ret = create_client(info, &client);
304803
@@ -555,7 +540,7 @@ int rpc_portmap_getport(struct conn_info *info,
304803
 	struct conn_info pmap_info;
304803
 	CLIENT *client;
304803
 	enum clnt_stat status;
304803
-	int proto = info->proto->p_proto;
304803
+	int proto = info->proto;
304803
 	int ret;
304803
 
304803
 	memset(&pmap_info, 0, sizeof(struct conn_info));
304803
@@ -633,13 +618,13 @@ int rpc_ping_proto(struct conn_info *info)
304803
 {
304803
 	CLIENT *client;
304803
 	enum clnt_stat status;
304803
-	int proto = info->proto->p_proto;
304803
+	int proto = info->proto;
304803
 	int ret;
304803
 
304803
 	if (info->client)
304803
 		client = info->client;
304803
 	else {
304803
-		if (info->proto->p_proto == IPPROTO_UDP) {
304803
+		if (info->proto == IPPROTO_UDP) {
304803
 			info->send_sz = UDPMSGSIZE;
304803
 			info->recv_sz = UDPMSGSIZE;
304803
 		}
304803
@@ -688,7 +673,7 @@ int rpc_ping_proto(struct conn_info *info)
304803
 
304803
 static unsigned int __rpc_ping(const char *host,
304803
 				unsigned long version,
304803
-				char *proto,
304803
+				int proto,
304803
 				long seconds, long micros,
304803
 				unsigned int option)
304803
 {
304803
@@ -696,6 +681,7 @@ static unsigned int __rpc_ping(const char *host,
304803
 	struct conn_info info;
304803
 	struct pmap parms;
304803
 
304803
+	info.proto = proto;
304803
 	info.host = host;
304803
 	info.addr = NULL;
304803
 	info.addr_len = 0;
304803
@@ -710,13 +696,9 @@ static unsigned int __rpc_ping(const char *host,
304803
 
304803
 	status = RPC_PING_FAIL;
304803
 
304803
-	info.proto = getprotobyname(proto);
304803
-	if (!info.proto)
304803
-		return status;
304803
-
304803
 	parms.pm_prog = NFS_PROGRAM;
304803
 	parms.pm_vers = version;
304803
-	parms.pm_prot = info.proto->p_proto;
304803
+	parms.pm_prot = info.proto;
304803
 	parms.pm_port = 0;
304803
 
304803
 	status = rpc_portmap_getport(&info, &parms, &info.port);
304803
@@ -734,19 +716,19 @@ int rpc_ping(const char *host, long seconds, long micros, unsigned int option)
304803
 	unsigned long vers2 = NFS2_VERSION;
304803
 	unsigned int status;
304803
 
304803
-	status = __rpc_ping(host, vers2, "udp", seconds, micros, option);
304803
+	status = __rpc_ping(host, vers2, IPPROTO_UDP, seconds, micros, option);
304803
 	if (status > 0)
304803
 		return RPC_PING_V2 | RPC_PING_UDP;
304803
 
304803
-	status = __rpc_ping(host, vers3, "udp", seconds, micros, option);
304803
+	status = __rpc_ping(host, vers3, IPPROTO_UDP, seconds, micros, option);
304803
 	if (status > 0)
304803
 		return RPC_PING_V3 | RPC_PING_UDP;
304803
 
304803
-	status = __rpc_ping(host, vers2, "tcp", seconds, micros, option);
304803
+	status = __rpc_ping(host, vers2, IPPROTO_TCP, seconds, micros, option);
304803
 	if (status > 0)
304803
 		return RPC_PING_V2 | RPC_PING_TCP;
304803
 
304803
-	status = __rpc_ping(host, vers3, "tcp", seconds, micros, option);
304803
+	status = __rpc_ping(host, vers3, IPPROTO_TCP, seconds, micros, option);
304803
 	if (status > 0)
304803
 		return RPC_PING_V3 | RPC_PING_TCP;
304803
 
304803
@@ -769,7 +751,7 @@ int rpc_time(const char *host,
304803
 	double taken;
304803
 	struct timeval start, end;
304803
 	struct timezone tz;
304803
-	char *proto = (ping_proto & RPC_PING_UDP) ? "udp" : "tcp";
304803
+	int proto = (ping_proto & RPC_PING_UDP) ? IPPROTO_UDP : IPPROTO_TCP;
304803
 	unsigned long vers = ping_vers;
304803
 
304803
 	gettimeofday(&start, &tz;;
304803
@@ -791,12 +773,12 @@ static int rpc_get_exports_proto(struct conn_info *info, exports *exp)
304803
 {
304803
 	CLIENT *client;
304803
 	enum clnt_stat status;
304803
-	int proto = info->proto->p_proto;
304803
+	int proto = info->proto;
304803
 	unsigned int option = info->close_option;
304803
 	int vers_entry;
304803
 	int ret;
304803
 
304803
-	if (info->proto->p_proto == IPPROTO_UDP) {
304803
+	if (info->proto == IPPROTO_UDP) {
304803
 		info->send_sz = UDPMSGSIZE;
304803
 		info->recv_sz = UDPMSGSIZE;
304803
 	}
304803
@@ -903,11 +885,9 @@ exports rpc_get_exports(const char *host, long seconds, long micros, unsigned in
304803
 	parms.pm_port = 0;
304803
 
304803
 	/* Try UDP first */
304803
-	info.proto = getprotobyname("udp");
304803
-	if (!info.proto)
304803
-		goto try_tcp;
304803
+	info.proto = IPPROTO_UDP;
304803
 
304803
-	parms.pm_prot = info.proto->p_proto;
304803
+	parms.pm_prot = info.proto;
304803
 
304803
 	status = rpc_portmap_getport(&info, &parms, &info.port);
304803
 	if (status < 0)
304803
@@ -920,11 +900,9 @@ exports rpc_get_exports(const char *host, long seconds, long micros, unsigned in
304803
 		return exportlist;
304803
 
304803
 try_tcp:
304803
-	info.proto = getprotobyname("tcp");
304803
-	if (!info.proto)
304803
-		return NULL;
304803
+	info.proto = IPPROTO_TCP;
304803
 
304803
-	parms.pm_prot = info.proto->p_proto;
304803
+	parms.pm_prot = info.proto;
304803
 
304803
 	status = rpc_portmap_getport(&info, &parms, &info.port);
304803
 	if (status < 0)
304803
diff --git a/modules/replicated.c b/modules/replicated.c
304803
index 6b96320..dbd5513 100644
304803
--- a/modules/replicated.c
304803
+++ b/modules/replicated.c
304803
@@ -419,7 +419,7 @@ void free_host_list(struct host **list)
304803
 
304803
 static unsigned int get_nfs_info(unsigned logopt, struct host *host,
304803
 			 struct conn_info *pm_info, struct conn_info *rpc_info,
304803
-			 const char *proto, unsigned int version, int port)
304803
+			 int proto, unsigned int version, int port)
304803
 {
304803
 	unsigned int random_selection = host->options & MOUNT_FLAG_RANDOM_SELECT;
304803
 	unsigned int use_weight_only = host->options & MOUNT_FLAG_USE_WEIGHT_ONLY;
304803
@@ -433,22 +433,18 @@ static unsigned int get_nfs_info(unsigned logopt, struct host *host,
304803
 	int status, count = 0;
304803
 
304803
 	if (host->addr)
304803
-		debug(logopt, "called with host %s(%s) proto %s version 0x%x",
304803
+		debug(logopt, "called with host %s(%s) proto %d version 0x%x",
304803
 		      host->name, get_addr_string(host->addr, buf, len),
304803
 		      proto, version);
304803
 	else
304803
 		debug(logopt,
304803
-		      "called for host %s proto %s version 0x%x",
304803
+		      "called for host %s proto %d version 0x%x",
304803
 		      host->name, proto, version);
304803
 
304803
-	rpc_info->proto = getprotobyname(proto);
304803
-	if (!rpc_info->proto)
304803
-		return 0;
304803
-
304803
+	rpc_info->proto = proto;
304803
 	memset(&parms, 0, sizeof(struct pmap));
304803
-
304803
 	parms.pm_prog = NFS_PROGRAM;
304803
-	parms.pm_prot = rpc_info->proto->p_proto;
304803
+	parms.pm_prot = proto;
304803
 
304803
 	if (!(version & NFS4_REQUESTED))
304803
 		goto v3_ver;
304803
@@ -479,7 +475,7 @@ static unsigned int get_nfs_info(unsigned logopt, struct host *host,
304803
 		}
304803
 	}
304803
 
304803
-	if (rpc_info->proto->p_proto == IPPROTO_UDP)
304803
+	if (rpc_info->proto == IPPROTO_UDP)
304803
 		status = rpc_udp_getclient(rpc_info, NFS_PROGRAM, NFS4_VERSION);
304803
 	else
304803
 		status = rpc_tcp_getclient(rpc_info, NFS_PROGRAM, NFS4_VERSION);
304803
@@ -540,7 +536,7 @@ v3_ver:
304803
 			goto v2_ver;
304803
 	}
304803
 
304803
-	if (rpc_info->proto->p_proto == IPPROTO_UDP)
304803
+	if (rpc_info->proto == IPPROTO_UDP)
304803
 		status = rpc_udp_getclient(rpc_info, NFS_PROGRAM, NFS3_VERSION);
304803
 	else
304803
 		status = rpc_tcp_getclient(rpc_info, NFS_PROGRAM, NFS3_VERSION);
304803
@@ -587,7 +583,7 @@ v2_ver:
304803
 			goto done_ver;
304803
 	}
304803
 
304803
-	if (rpc_info->proto->p_proto == IPPROTO_UDP)
304803
+	if (rpc_info->proto == IPPROTO_UDP)
304803
 		status = rpc_udp_getclient(rpc_info, NFS_PROGRAM, NFS2_VERSION);
304803
 	else
304803
 		status = rpc_tcp_getclient(rpc_info, NFS_PROGRAM, NFS2_VERSION);
304803
@@ -618,7 +614,7 @@ v2_ver:
304803
 	}
304803
 
304803
 done_ver:
304803
-	if (rpc_info->proto->p_proto == IPPROTO_UDP) {
304803
+	if (rpc_info->proto == IPPROTO_UDP) {
304803
 		rpc_destroy_udp_client(rpc_info);
304803
 		rpc_destroy_udp_client(pm_info);
304803
 	} else {
304803
@@ -675,7 +671,7 @@ static int get_vers_and_cost(unsigned logopt, struct host *host,
304803
 
304803
 	if (version & TCP_REQUESTED) {
304803
 		supported = get_nfs_info(logopt, host,
304803
-				   &pm_info, &rpc_info, "tcp", vers, port);
304803
+				   &pm_info, &rpc_info, IPPROTO_TCP, vers, port);
304803
 		if (IS_ERR(supported)) {
304803
 			if (ERR(supported) == EHOSTUNREACH ||
304803
 			    ERR(supported) == ETIMEDOUT)
304803
@@ -688,7 +684,7 @@ static int get_vers_and_cost(unsigned logopt, struct host *host,
304803
 
304803
 	if (version & UDP_REQUESTED) {
304803
 		supported = get_nfs_info(logopt, host,
304803
-				   &pm_info, &rpc_info, "udp", vers, port);
304803
+				   &pm_info, &rpc_info, IPPROTO_UDP, vers, port);
304803
 		if (IS_ERR(supported)) {
304803
 			if (!ret && ERR(supported) == ETIMEDOUT)
304803
 				return ret;
304803
@@ -709,7 +705,7 @@ static int get_supported_ver_and_cost(unsigned logopt, struct host *host,
304803
 	socklen_t len = INET6_ADDRSTRLEN;
304803
 	char buf[len + 1];
304803
 	struct conn_info pm_info, rpc_info;
304803
-	const char *proto;
304803
+	int proto;
304803
 	unsigned int vers;
304803
 	struct timeval start, end;
304803
 	struct timezone tz;
304803
@@ -748,10 +744,10 @@ static int get_supported_ver_and_cost(unsigned logopt, struct host *host,
304803
 	 *  So, we do the conversion here.
304803
 	 */
304803
 	if (version & UDP_SELECTED_MASK) {
304803
-		proto = "udp";
304803
+		proto = IPPROTO_UDP;
304803
 		version >>= 8;
304803
 	} else
304803
-		proto = "tcp";
304803
+		proto = IPPROTO_TCP;
304803
 
304803
 	switch (version) {
304803
 	case NFS2_SUPPORTED:
304803
@@ -768,9 +764,7 @@ static int get_supported_ver_and_cost(unsigned logopt, struct host *host,
304803
 		return 0;
304803
 	}
304803
 
304803
-	rpc_info.proto = getprotobyname(proto);
304803
-	if (!rpc_info.proto)
304803
-		return 0;
304803
+	rpc_info.proto = proto;
304803
 
304803
 	if (port > 0)
304803
 		rpc_info.port = port;
304803
@@ -786,14 +780,14 @@ static int get_supported_ver_and_cost(unsigned logopt, struct host *host,
304803
 
304803
 		memset(&parms, 0, sizeof(struct pmap));
304803
 		parms.pm_prog = NFS_PROGRAM;
304803
-		parms.pm_prot = rpc_info.proto->p_proto;
304803
+		parms.pm_prot = rpc_info.proto;
304803
 		parms.pm_vers = vers;
304803
 		ret = rpc_portmap_getport(&pm_info, &parms, &rpc_info.port);
304803
 		if (ret < 0)
304803
 			goto done;
304803
 	}
304803
 
304803
-	if (rpc_info.proto->p_proto == IPPROTO_UDP)
304803
+	if (rpc_info.proto == IPPROTO_UDP)
304803
 		status = rpc_udp_getclient(&rpc_info, NFS_PROGRAM, vers);
304803
 	else
304803
 		status = rpc_tcp_getclient(&rpc_info, NFS_PROGRAM, vers);
304803
@@ -815,7 +809,7 @@ static int get_supported_ver_and_cost(unsigned logopt, struct host *host,
304803
 		}
304803
 	}
304803
 done:
304803
-	if (rpc_info.proto->p_proto == IPPROTO_UDP) {
304803
+	if (rpc_info.proto == IPPROTO_UDP) {
304803
 		rpc_destroy_udp_client(&rpc_info);
304803
 		rpc_destroy_udp_client(&pm_info);
304803
 	} else {