nserver

nihilistic stats server
Log | Files | Refs | README | LICENSE

commit 8a279d818020306f0d6e055971b3c763527091ea
parent 881cee2229dc166af63d4f464799f4e73309d6e4
Author: rsiddharth <s@ricketyspace.net>
Date:   Sat, 18 Apr 2020 22:06:02 -0400

bin/statserve.c -> bin/nserver.c

Diffstat:
.gitignore | 2+-
README | 2+-
bin/nserver.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
bin/statserve.c | 54------------------------------------------------------
4 files changed, 56 insertions(+), 56 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -5,5 +5,5 @@ build/ TAGS nserver.db -bin/statserve +bin/nserver tests/*_tests diff --git a/README b/README @@ -38,7 +38,7 @@ clean up things. To start this server, do: ``` -./bin/statserve +./bin/nserver ``` It'll be listening on port 7899. diff --git a/bin/nserver.c b/bin/nserver.c @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: BSD-3-Clause */ +/* + * Copyright © 2020 rsiddharth <s@ricketyspace.net> + */ + +#include <nserve.h> +#include <nsocket.h> + +#define BACKLOG 10 + +int main(void) +{ + int rc = 0; + int sockfd_s = 0, sockfd_c = 0; + struct sockaddr sockaddr_c; + socklen_t sockaddr_c_len; + pid_t pidc; + + sockfd_s = get_socket(); + check(sockfd_s > 0, "nserver: unable to get socket"); + + rc = listen(sockfd_s, BACKLOG); + check(rc == 0, "nserver: listen failed"); + + do { + printf("Waiting for connection...\n"); + sockfd_c = accept(sockfd_s, &sockaddr_c, &sockaddr_c_len); + + pidc = fork(); + check(pidc != -1, "nserver: fork failed"); + + if (pidc == 0) { + nserve(sockfd_c); + } + + rc = close(sockfd_c); + check(rc == 0, "nserver: close failed"); + } while(1); + + rc = close(sockfd_s); + check(rc == 0, "nserver: close failed"); + + return 0; + + error: + if (sockfd_s) { + rc = close(sockfd_s); + } + if (sockfd_c) { + rc = close(sockfd_c); + } + + return -1; +} diff --git a/bin/statserve.c b/bin/statserve.c @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: BSD-3-Clause */ -/* - * Copyright © 2020 rsiddharth <s@ricketyspace.net> - */ - -#include <nserve.h> -#include <nsocket.h> - -#define BACKLOG 10 - -int main(void) -{ - int rc = 0; - int sockfd_s = 0, sockfd_c = 0; - struct sockaddr sockaddr_c; - socklen_t sockaddr_c_len; - pid_t pidc; - - sockfd_s = get_socket(); - check(sockfd_s > 0, "stateserv: unable to get socket"); - - rc = listen(sockfd_s, BACKLOG); - check(rc == 0, "statserve: listen failed"); - - do { - printf("Waiting for connection...\n"); - sockfd_c = accept(sockfd_s, &sockaddr_c, &sockaddr_c_len); - - pidc = fork(); - check(pidc != -1, "statserve: fork failed"); - - if (pidc == 0) { - nserve(sockfd_c); - } - - rc = close(sockfd_c); - check(rc == 0, "statserv: close failed"); - } while(1); - - rc = close(sockfd_s); - check(rc == 0, "statserv: close failed"); - - return 0; - - error: - if (sockfd_s) { - rc = close(sockfd_s); - } - if (sockfd_c) { - rc = close(sockfd_c); - } - - return -1; -}