little bits to make things better. mostly style. things of note are
checking return value of chdir and changing a loop bound to be more clear.
unfortunately, rpc xdr code still uses char * as a generic pointer type,
and I'm not sure if this code really wants long instead of int, or why.
Index: portmap.c
===================================================================
RCS file: /home/tedu/cvs/src/usr.sbin/portmap/portmap.c,v
retrieving revision 1.39
diff -u -r1.39 portmap.c
--- portmap.c 12 Nov 2009 05:42:09 -0000 1.39
+++ portmap.c 13 Aug 2010 00:04:16 -0000
@@ -92,14 +92,15 @@
struct pmaplist *find_service(u_long, u_long, u_long);
struct pmaplist *pmaplist;
-int debugging = 0;
+int debugging;
SVCXPRT *ludpxprt, *ltcpxprt;
int
main(int argc, char *argv[])
{
- int sock, lsock, c, on = 1, len = sizeof(struct sockaddr_in);
+ int sock, lsock, c, on = 1;
+ socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in addr, laddr;
struct pmaplist *pml;
struct passwd *pw;
@@ -107,32 +108,30 @@
while ((c = getopt(argc, argv, "d")) != -1) {
switch (c) {
-
case 'd':
debugging = 1;
break;
-
default:
- (void) fprintf(stderr, "usage: %s [-d]\n", argv[0]);
+ (void)fprintf(stderr, "usage: %s [-d]\n", argv[0]);
exit(1);
}
}
if (!debugging && daemon(0, 0)) {
- (void) fprintf(stderr, "portmap: fork: %s", strerror(errno));
+ (void)fprintf(stderr, "portmap: fork: %s", strerror(errno));
exit(1);
}
- openlog("portmap", LOG_NDELAY | (debugging ? LOG_PID | LOG_PERROR :
LOG_PID),
- LOG_DAEMON);
+ openlog("portmap", LOG_NDELAY | (debugging ? LOG_PID | LOG_PERROR :
+ LOG_PID), LOG_DAEMON);
- bzero((char *)&addr, sizeof addr);
+ bzero(&addr, sizeof addr);
addr.sin_addr.s_addr = 0;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(PMAPPORT);
- bzero((char *)&laddr, sizeof laddr);
+ bzero(&laddr, sizeof laddr);
laddr.sin_addr.s_addr = 0;
laddr.sin_family = AF_INET;
laddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -148,7 +147,7 @@
exit(1);
}
- if ((xprt = svcudp_create(sock)) == (SVCXPRT *)NULL) {
+ if ((xprt = svcudp_create(sock)) == NULL) {
syslog(LOG_ERR, "couldn't do udp_create");
exit(1);
}
@@ -163,13 +162,13 @@
exit(1);
}
- if ((ludpxprt = svcudp_create(lsock)) == (SVCXPRT *)NULL) {
+ if ((ludpxprt = svcudp_create(lsock)) == NULL) {
syslog(LOG_ERR, "couldn't do udp_create");
exit(1);
}
/* make an entry for ourself */
- pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
+ pml = malloc(sizeof(struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "out of memory");
exit(1);
@@ -190,8 +189,8 @@
syslog(LOG_ERR, "cannot bind tcp: %m");
exit(1);
}
- if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE))
- == (SVCXPRT *)NULL) {
+ if ((xprt = svctcp_create(sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE)) ==
+ NULL) {
syslog(LOG_ERR, "couldn't do tcp_create");
exit(1);
}
@@ -206,13 +205,13 @@
exit(1);
}
if ((ltcpxprt = svctcp_create(lsock, RPCSMALLMSGSIZE,
- RPCSMALLMSGSIZE)) == (SVCXPRT *)NULL) {
+ RPCSMALLMSGSIZE)) == NULL) {
syslog(LOG_ERR, "couldn't do tcp_create");
exit(1);
}
/* make an entry for ourself */
- pml = (struct pmaplist *)malloc((u_int)sizeof(struct pmaplist));
+ pml = malloc(sizeof(struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "out of memory");
exit(1);
@@ -224,17 +223,19 @@
pml->pml_next = pmaplist;
pmaplist = pml;
-
if ((pw = getpwnam("_portmap")) == NULL) {
syslog(LOG_ERR, "no such user _portmap");
exit(1);
}
if (chroot("/var/empty") == -1) {
- syslog(LOG_ERR, "cannot chdir to /var/empty.");
+ syslog(LOG_ERR, "cannot chroot to /var/empty.");
+ exit(1);
+ }
+ if (chdir("/") == -1) {
+ syslog(LOG_ERR, "cannot chdir to new /.");
exit(1);
}
- chdir("/");
if (pw) {
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
@@ -293,23 +294,21 @@
struct pmaplist *pml, *prevpml, *fnd;
struct sockaddr_in *fromsin;
long ans = 0, port;
- caddr_t t;
+ void *t;
fromsin = svc_getcaller(xprt);
if (debugging)
- (void) fprintf(stderr, "server: about to do a switch\n");
+ (void)fprintf(stderr, "server: about to do a switch\n");
switch (rqstp->rq_proc) {
-
case PMAPPROC_NULL:
/*
* Null proc call
*/
- if (!svc_sendreply(xprt, xdr_void, (caddr_t)0) && debugging) {
+ if (!svc_sendreply(xprt, xdr_void, NULL) && debugging) {
abort();
}
break;
-
case PMAPPROC_SET:
/*
* Set a program,version to port mapping
@@ -360,7 +359,7 @@
/*
* add to END of list
*/
- pml = (struct pmaplist *)malloc(sizeof(struct pmaplist));
+ pml = malloc(sizeof(struct pmaplist));
if (pml == NULL) {
syslog(LOG_ERR, "out of memory");
svcerr_systemerr(xprt);
@@ -378,14 +377,13 @@
fnd->pml_next = pml;
}
ans = 1;
- done:
+done:
if ((!svc_sendreply(xprt, xdr_long, (caddr_t)&ans)) &&
debugging) {
- (void) fprintf(stderr, "svc_sendreply\n");
+ (void)fprintf(stderr, "svc_sendreply\n");
abort();
}
break;
-
case PMAPPROC_UNSET:
/*
* Remove a program,version to port mapping.
@@ -419,7 +417,7 @@
/* found it; pml moves forward, prevpml stays */
ans = 1;
- t = (caddr_t)pml;
+ t = pml;
pml = pml->pml_next;
if (prevpml == NULL)
pmaplist = pml;
@@ -433,7 +431,6 @@
abort();
}
break;
-
case PMAPPROC_GETPORT:
/*
* Lookup the mapping for a program,version and return its port
@@ -453,7 +450,6 @@
abort();
}
break;
-
case PMAPPROC_DUMP:
/*
* Return the current set of mapped program,version
@@ -468,7 +464,6 @@
abort();
}
break;
-
case PMAPPROC_CALLIT:
/*
* Calls a procedure on the local machine. If the requested
@@ -479,7 +474,6 @@
*/
callit(rqstp, xprt);
break;
-
default:
svcerr_noproc(xprt);
break;
@@ -555,7 +549,7 @@
beginpos = lowpos = pos = xdr_getpos(xdrs);
highpos = lowpos + ARGSIZE;
- while ((int)(highpos - lowpos) >= 0) {
+ while (highpos >= lowpos) {
currpos = (lowpos + highpos) / 2;
if (xdr_setpos(xdrs, currpos)) {
pos = currpos;
@@ -626,7 +620,7 @@
exit(1);
client = clntudp_create(&me, a.rmt_prog, a.rmt_vers, timeout, &so);
- if (client != (CLIENT *)NULL) {
+ if (client != NULL) {
if (rqstp->rq_cred.oa_flavor == AUTH_UNIX)
client->cl_auth = authunix_create(au->aup_machname,
au->aup_uid, au->aup_gid, au->aup_len,
au->aup_gids);
@@ -669,8 +663,8 @@
(prog == MOUNTPROG && aproc == MOUNTPROC_MNT) ||
(prog == YPPROG && aproc != YPPROC_DOMAIN_NONACK)) {
syslog(LOG_WARNING,
- "callit prog %d aproc %d (might be from %s)",
- (int)prog, (int)aproc, inet_ntoa(addr->sin_addr));
+ "callit prog %ld aproc %ld (might be from %s)",
+ prog, aproc, inet_ntoa(addr->sin_addr));
return (FALSE);
}
return (TRUE);