aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-18 22:48:50 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-18 22:48:50 -0400
commit258aa6cc7e127afe8fea09114791c5dfdd23850f (patch)
tree76d218ec24fe9faade7b695e99ad71e922498cb1
parent05ce03786035fd7655a2bfb53b70f7ac5643c275 (diff)
downloadnserver-258aa6cc7e127afe8fea09114791c5dfdd23850f.tar.gz
nserver-258aa6cc7e127afe8fea09114791c5dfdd23850f.tar.bz2
nserver-258aa6cc7e127afe8fea09114791c5dfdd23850f.tar.xz
nserver-258aa6cc7e127afe8fea09114791c5dfdd23850f.zip
nserve.c: Update nserve.
* src/nserve.c (nserve): Refactor how it ends itself.
-rw-r--r--src/nserve.c29
1 files changed, 14 insertions, 15 deletions
diff --git a/src/nserve.c b/src/nserve.c
index 75457af..51dd2de 100644
--- a/src/nserve.c
+++ b/src/nserve.c
@@ -32,13 +32,15 @@ int barfsock(char *buf, size_t buf_sz, int sock)
void nserve(int sock)
{
- char *out = (char *) calloc(RSP_SIZE + 1, sizeof(char));
+ char *out = NULL, *cmd = NULL;
+
+ out = (char *) calloc(RSP_SIZE + 1, sizeof(char));
check_mem(out);
- char *cmd = (char *) calloc(CMD_MAX_SIZE + 1, sizeof(char));
+ cmd = (char *) calloc(CMD_MAX_SIZE + 1, sizeof(char));
check_mem(cmd);
- int rc = 0, done = 0;
+ int rc = 0;
do {
// clear out, cmd.
memset(out, '\0', RSP_SIZE + 1);
@@ -50,29 +52,26 @@ void nserve(int sock)
rc = process(cmd, out);
if (rc < 0) {
- done = 1;
+ break;
}
// Write response to socket.
rc = barfsock(out, strlen(out), sock);
check(rc != -1, "nserve: echo failed");
- } while(done != 1);
-
- // Close socket.
- rc = close(sock);
- check(rc == 0, "nserve: close failed");
+ } while(1);
- // Cleanup.
- free(cmd);
-
- exit(0);
+ // fallthrough.
error:
- rc = close(sock);
- check(rc == 0, "nserve: close failed");
+ strncpy(out, "closing connection\n", RSP_SIZE);
+ barfsock(out, strlen(out), sock);
+
+ close(sock);
// Cleanup if needed.
if (cmd)
free(cmd);
+ if (out)
+ free(out);
exit(1);
}