commit 451a9fae7303cc2457d025d51c6c153413020211
parent a4c4ddaceafa6d22ee03502b6b4d764c51ba8d49
Author: Christoph Lohmann <20h@r-36.net>
Date: Sun, 6 Mar 2011 10:48:45 +0100
Fixing various major bugs.
Diffstat:
handlr.c | | | 14 | -------------- |
ind.c | | | 2 | +- |
main.c | | | 45 | ++++++++++++++++++++++++++++----------------- |
rc.d/NetBSD.rc.d | | | 110 | ++++++++++++++++++++++++++++++++++++++++---------------------------------------- |
4 files changed, 84 insertions(+), 87 deletions(-)
diff --git a/handlr.c b/handlr.c
@@ -40,7 +40,6 @@ handledir(int sock, char *path, char *port, char *base, char *args,
addr = gmallocz(512, 2);
if(gethostname(addr, 512) == -1) {
perror("gethostname");
- close(sock);
free(addr);
free(pa);
return;
@@ -60,7 +59,6 @@ handledir(int sock, char *path, char *port, char *base, char *args,
ndir = scandir(pa, &dirent, 0, alphasort);
if(ndir < 0) {
perror("scandir");
- close(sock);
free(addr);
free(pa);
return;
@@ -90,8 +88,6 @@ handledir(int sock, char *path, char *port, char *base, char *args,
if(addr != nil)
free(addr);
free(pa);
- close(sock);
- return;
}
void
@@ -111,7 +107,6 @@ handlegph(int sock, char *file, char *port, char *base, char *args,
if(args == nil) {
if(gethostname(addr, sizeof(addr)) == -1) {
perror("gethostname");
- close(sock);
return;
}
} else
@@ -139,9 +134,6 @@ handlegph(int sock, char *file, char *port, char *base, char *args,
freeindex(act);
}
-
- close(sock);
- return;
}
void
@@ -163,9 +155,6 @@ handlebin(int sock, char *file, char *port, char *base, char *args,
send(sock, sendb, len, 0);
close(fd);
}
-
- close(sock);
- return;
}
void
@@ -189,8 +178,5 @@ handlecgi(int sock, char *file, char *port, char *base, char *args,
sear = "";
execl(file, p, sear, args, (char *)nil);
-
- close(sock);
- return;
}
diff --git a/ind.c b/ind.c
@@ -148,7 +148,7 @@ addelem(Elems *e, char *s)
e->num++;
e->e = realloc(e->e, sizeof(char *) * e->num);
- e->e[e->num - 1] = gmallocz(slen, 0);
+ e->e[e->num - 1] = gmallocz(slen, 2);
strncpy(e->e[e->num - 1], s, slen - 1);
return;
diff --git a/main.c b/main.c
@@ -38,6 +38,7 @@ enum {
int glfd = -1;
int loglvl = 15;
int running = 1;
+int listfd = -1;
char *logfile = nil;
char *argv0;
@@ -129,7 +130,7 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth,
char *clientp)
{
struct stat dir;
- char recvc[1024], recvb[1024], path[1024], *args, *sear, *c;
+ char recvc[1025], recvb[1025], path[1025], *args, *sear, *c;
int len, fd;
filetype *type;
@@ -137,14 +138,14 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth,
bzero(recvb, sizeof(recvb));
bzero(recvc, sizeof(recvc));
- len = recv(sock, recvb, sizeof(recvb), 0);
- if(len > 1) {
+ len = recv(sock, recvb, sizeof(recvb)-1, 0);
+ if(len > 0) {
if(recvb[len - 2] == '\r')
recvb[len - 2] = '\0';
if(recvb[len - 1] == '\n')
recvb[len - 1] = '\0';
}
- strncpy(recvc, recvb, sizeof(recvc) - 1);
+ memmove(recvc, recvb, len+1);
if(!strncmp(recvb, "URL:", 4)) {
len = snprintf(path, sizeof(path), htredir,
@@ -194,7 +195,6 @@ handlerequest(int sock, char *base, char *ohost, char *port, char *clienth,
send(sock, err, strlen(err), 0);
if(loglvl & ERRORS)
logentry(clienth, clientp, recvc, "not found");
- close(sock);
}
return;
@@ -214,6 +214,8 @@ sighandler(int sig)
case SIGTERM:
if(logfile != nil)
stoplogging(glfd);
+ if(listfd >= 0)
+ close(listfd);
exit(EXIT_SUCCESS);
break;
default:
@@ -250,7 +252,7 @@ main(int argc, char *argv[])
struct addrinfo hints, *ai, *rp;
struct sockaddr_storage clt;
socklen_t cltlen;
- int sock, list, opt, dofork;
+ int sock, opt, dofork;
char *port, *base, clienth[NI_MAXHOST], clientp[NI_MAXSERV];
char *user, *group, *bindip, *ohost, *sport;
struct passwd *us;
@@ -342,33 +344,36 @@ main(int argc, char *argv[])
}
for(rp = ai; rp != nil; rp = rp->ai_next) {
- list = socket(rp->ai_family, rp->ai_socktype,
+ listfd = socket(rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
- if(list < 0)
+ if(listfd < 0)
continue;
- if(bind(list, rp->ai_addr, rp->ai_addrlen) == 0)
+ if(bind(listfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
- close(list);
+ close(listfd);
}
if(rp == nil) {
- perror("Could not find any suitable bindable address.");
+ perror("getaddrinfo");
return 1;
}
freeaddrinfo(ai);
opt = 1;
- if(setsockopt(list, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
+ if(setsockopt(listfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
+ close(listfd);
return 1;
}
- if(listen(list, 255)) {
+ if(listen(listfd, 255)) {
perror("listen");
+ close(listfd);
return 1;
}
if(dropprivileges(gr, us) < 0) {
- perror("cannot drop privileges");
+ perror("dropprivileges");
+ close(listfd);
return 1;
}
@@ -376,15 +381,19 @@ main(int argc, char *argv[])
cltlen = sizeof(clt);
while(running) {
- sock = accept(list, (struct sockaddr *)&clt, &cltlen);
+ sock = accept(listfd, (struct sockaddr *)&clt, &cltlen);
if(sock < 0) {
switch(errno) {
case ECONNABORTED:
case EINTR:
+ if (!running) {
+ close(listfd);
+ return 0;
+ }
continue;
default:
perror("accept");
- close(list);
+ close(listfd);
return 1;
}
}
@@ -401,13 +410,15 @@ main(int argc, char *argv[])
case 0:
handlerequest(sock, base, ohost, sport, clienth,
clientp);
+ shutdown(sock, SHUT_RDWR);
+ close(sock);
return 0;
default:
break;
}
}
- close(list);
+ close(listfd);
if(logfile != nil)
stoplogging(glfd);
return 0;
diff --git a/rc.d/NetBSD.rc.d b/rc.d/NetBSD.rc.d
@@ -1,55 +1,55 @@
-#!/bin/sh
-#
-
-# REQUIRE: local
-# PROVIDE: geomyidae
-
-$_rc_subr_loaded . /etc/rc.subr
-
-name="geomyidae"
-rcvar=$name
-command="/usr/pkg/sbin/${name}"
-
-#####################################################
-# Geomyidae Options Section - "?" => geomyidae(8) #
-# Uncomment & define options (defaults are shown) #
-#####################################################
-#
-#LOGFILE="-l /var/log/gopherd.log"
-#LOGLEVEL="-v 15"
-#HTDOCS="-b /var/gopher"
-#PORT="-p 70"
-#SPORT="-o 70"
-#USR="-u $USER"
-#GRP="-g $GROUP"
-#HOST="-h localhost"
-#IP="-i 127.0.0.1"
-
-######################################################
-# Now remove any UNDEFINED options from line below: #
-######################################################
-#
-command_args="$LOGFILE $LOGLEVEL $HTDOCS $PORT $SPORT $USR $GRP $HOST $IP"
-
-
-######################################################
-# Uncomment this section if a PID file is desired #
-######################################################
-
-#pidfile="/var/run/${name}.pid"
-#start_cmd="geomyidae_start"
-#
-#geomyidae_start()
-#{
-# echo "Starting $name"
-# $command $command_args
-# pgrep -x $name > $pidfile
-#}
-
-######################################################
-# Lastly, add the following to /etc/rc.conf: #
-# "geomyidae=YES" (without the quotes) #
-######################################################
-
-load_rc_config $name
-run_rc_command "$1"
+#!/bin/sh
+#
+
+# REQUIRE: local
+# PROVIDE: geomyidae
+
+$_rc_subr_loaded . /etc/rc.subr
+
+name="geomyidae"
+rcvar=$name
+command="/usr/pkg/sbin/${name}"
+
+#####################################################
+# Geomyidae Options Section - "?" => geomyidae(8) #
+# Uncomment & define options (defaults are shown) #
+#####################################################
+#
+#LOGFILE="-l /var/log/gopherd.log"
+#LOGLEVEL="-v 15"
+#HTDOCS="-b /var/gopher"
+#PORT="-p 70"
+#SPORT="-o 70"
+#USR="-u $USER"
+#GRP="-g $GROUP"
+#HOST="-h localhost"
+#IP="-i 127.0.0.1"
+
+######################################################
+# Now remove any UNDEFINED options from line below: #
+######################################################
+#
+command_args="$LOGFILE $LOGLEVEL $HTDOCS $PORT $SPORT $USR $GRP $HOST $IP"
+
+
+######################################################
+# Uncomment this section if a PID file is desired #
+######################################################
+
+#pidfile="/var/run/${name}.pid"
+#start_cmd="geomyidae_start"
+#
+#geomyidae_start()
+#{
+# echo "Starting $name"
+# $command $command_args
+# pgrep -x $name > $pidfile
+#}
+
+######################################################
+# Lastly, add the following to /etc/rc.conf: #
+# "geomyidae=YES" (without the quotes) #
+######################################################
+
+load_rc_config $name
+run_rc_command "$1"