summaryrefslogtreecommitdiff
path: root/libc/inet/rpc/rexec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc/inet/rpc/rexec.c')
-rw-r--r--libc/inet/rpc/rexec.c95
1 files changed, 38 insertions, 57 deletions
diff --git a/libc/inet/rpc/rexec.c b/libc/inet/rpc/rexec.c
index 2eb567ed2..e17be50b2 100644
--- a/libc/inet/rpc/rexec.c
+++ b/libc/inet/rpc/rexec.c
@@ -27,8 +27,6 @@
* SUCH DAMAGE.
*/
-#define __FORCE_GLIBC
-#include <features.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -41,35 +39,14 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-
-/* Experimentally off - libc_hidden_proto(memset) */
-/* Experimentally off - libc_hidden_proto(strlen) */
-/* Experimentally off - libc_hidden_proto(strncpy) */
-libc_hidden_proto(read)
-libc_hidden_proto(write)
-libc_hidden_proto(close)
-libc_hidden_proto(socket)
-libc_hidden_proto(perror)
-libc_hidden_proto(sprintf)
-libc_hidden_proto(snprintf)
-libc_hidden_proto(getsockname)
-libc_hidden_proto(getnameinfo)
-libc_hidden_proto(getaddrinfo)
-libc_hidden_proto(freeaddrinfo)
-libc_hidden_proto(sleep)
-libc_hidden_proto(atoi)
-libc_hidden_proto(connect)
-libc_hidden_proto(accept)
-libc_hidden_proto(listen)
-libc_hidden_proto(ruserpass)
+#include <malloc.h>
#define SA_LEN(_x) __libc_sa_len((_x)->sa_family)
-extern int __libc_sa_len (sa_family_t __af) __THROW attribute_hidden;
+extern int __libc_sa_len(sa_family_t __af) __THROW attribute_hidden;
-int rexecoptions;
-char ahostbuf[NI_MAXHOST] attribute_hidden;
+/* int rexecoptions; - google does not know it */
+static char *ahostbuf = NULL;
-libc_hidden_proto(rexec_af)
int
rexec_af(char **ahost, int rport, const char *name, const char *pass, const char *cmd, int *fd2p, sa_family_t af)
{
@@ -83,27 +60,33 @@ rexec_af(char **ahost, int rport, const char *name, const char *pass, const char
int gai;
char servbuff[NI_MAXSERV];
- snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
- servbuff[sizeof(servbuff) - 1] = '\0';
+ if (sizeof(servbuff) < sizeof(int)*3 + 2) {
+ snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
+ servbuff[sizeof(servbuff) - 1] = '\0';
+ } else {
+ sprintf(servbuff, "%d", ntohs(rport));
+ }
memset(&hints, '\0', sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
- if (gai){
+ if (gai) {
/* XXX: set errno? */
return -1;
}
- if (res0->ai_canonname){
- strncpy(ahostbuf, res0->ai_canonname, sizeof(ahostbuf));
- ahostbuf[sizeof(ahostbuf)-1] = '\0';
+ if (res0->ai_canonname) {
+ if (!ahostbuf)
+ ahostbuf = __uc_malloc(NI_MAXHOST);
+ strncpy(ahostbuf, res0->ai_canonname, NI_MAXHOST);
+ ahostbuf[NI_MAXHOST-1] = '\0';
*ahost = ahostbuf;
}
- else{
+ else {
*ahost = NULL;
- __set_errno (ENOENT);
+ __set_errno(ENOENT);
return -1;
}
ruserpass(res0->ai_canonname, &name, &pass);
@@ -111,7 +94,7 @@ retry:
s = socket(res0->ai_family, res0->ai_socktype, 0);
if (s < 0) {
perror("rexec: socket");
- return (-1);
+ return -1;
}
if (connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
if (errno == ECONNREFUSED && timo <= 16) {
@@ -121,7 +104,7 @@ retry:
goto retry;
}
perror(res0->ai_canonname);
- return (-1);
+ return -1;
}
if (fd2p == 0) {
(void) write(s, "", 1);
@@ -134,10 +117,10 @@ retry:
s2 = socket(res0->ai_family, res0->ai_socktype, 0);
if (s2 < 0) {
(void) close(s);
- return (-1);
+ return -1;
}
listen(s2, 1);
- sa2len = sizeof (sa2);
+ sa2len = sizeof(sa2);
if (getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
perror("getsockname");
(void) close(s2);
@@ -154,15 +137,16 @@ retry:
port = atoi(servbuff);
(void) sprintf(num, "%u", port);
(void) write(s, num, strlen(num)+1);
- { socklen_t len = sizeof (from);
- s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from,
- &len));
- close(s2);
- if (s3 < 0) {
- perror("accept");
- port = 0;
- goto bad;
- }
+ {
+ socklen_t len = sizeof(from);
+ s3 = TEMP_FAILURE_RETRY(accept(s2,
+ (struct sockaddr *)&from, &len));
+ close(s2);
+ if (s3 < 0) {
+ perror("accept");
+ port = 0;
+ goto bad;
+ }
}
*fd2p = s3;
}
@@ -174,9 +158,9 @@ retry:
/* We don't need the memory allocated for the name and the password
in ruserpass anymore. */
if (name != orig_name)
- free ((char *) name);
+ free((char *) name);
if (pass != orig_pass)
- free ((char *) pass);
+ free((char *) pass);
if (read(s, &c, 1) != 1) {
perror(*ahost);
@@ -191,22 +175,19 @@ retry:
goto bad;
}
freeaddrinfo(res0);
- return (s);
+ return s;
bad:
if (port)
(void) close(*fd2p);
(void) close(s);
freeaddrinfo(res0);
- return (-1);
+ return -1;
}
libc_hidden_def(rexec_af)
int
-rexec(ahost, rport, name, pass, cmd, fd2p)
- char **ahost;
- int rport;
- const char *name, *pass, *cmd;
- int *fd2p;
+rexec(char **ahost, int rport, const char *name, const char *pass,
+ const char *cmd, int *fd2p)
{
return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
}