Remove signal handler for SIGINT, print a \r at the end of cleanup, normalize error output

This commit is contained in:
Franck STAUFFER 2020-09-08 23:02:09 +02:00
parent 98e7632d8c
commit 851e2797db
Signed by: franck.stauffer
GPG Key ID: AAF5A94045CEC261
2 changed files with 13 additions and 15 deletions

View File

@ -26,7 +26,7 @@ if it failed to setup terminal
if it failed to create epoll instance if it failed to create epoll instance
.TP .TP
5 5
if it failed to set signal handler (for SIGINT or SIGTERM) if it failed to set signal handler for SIGTERM
.TP .TP
6 6
if it failed to add a file descriptor to the epoll instance (for stdin or the socket) if it failed to add a file descriptor to the epoll instance (for stdin or the socket)
@ -39,6 +39,9 @@ if it failed to receive data
.TP .TP
9 9
if it failed to send data if it failed to send data
.TP
10
if unattended file descriptor is raised while polling
.SH AUTHOR .SH AUTHOR
Franck STAUFFER Franck STAUFFER
.I <franck.stauffer@monaco.mc> .I <franck.stauffer@monaco.mc>

View File

@ -107,7 +107,7 @@ net_init(const char* host, const char* port)
void void
sighandler(int sig) sighandler(int sig)
{ {
fprintf(stderr, "Received %s", (sig == SIGTERM) ? "SIGTERM" : "SIGINT"); fputs("\n\rINFO: Received SIGTERM\n", stderr);
exit(sig); exit(sig);
} }
@ -147,18 +147,13 @@ main(int argc, char** argv)
epoll_fd = epoll_create1(0); epoll_fd = epoll_create1(0);
if (epoll_fd == -1) { if (epoll_fd == -1) {
perror("epoll_create1"); perror("ERROR: epoll_create1");
return 4; return 4;
} }
atexit(cleanup); atexit(cleanup);
if (signal(SIGINT, sighandler) == SIG_ERR) {
perror("signal");
return 5;
}
if (signal(SIGTERM, sighandler) == SIG_ERR) { if (signal(SIGTERM, sighandler) == SIG_ERR) {
perror("signal"); perror("ERROR: signal");
return 5; return 5;
} }
@ -168,14 +163,14 @@ main(int argc, char** argv)
ev[0].events = EPOLLIN; ev[0].events = EPOLLIN;
ev[0].data.fd = 0; ev[0].data.fd = 0;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &ev[0])) { if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, 0, &ev[0])) {
perror("epoll_ctl"); perror("ERROR: epoll_ctl");
return 6; return 6;
} }
ev[1].events = EPOLLIN; ev[1].events = EPOLLIN;
ev[1].data.fd = sock; ev[1].data.fd = sock;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev[1])) { if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev[1])) {
perror("epoll_ctl"); perror("ERROR: epoll_ctl");
return 6; return 6;
} }
@ -185,7 +180,7 @@ main(int argc, char** argv)
struct epoll_event events[2]; struct epoll_event events[2];
int_fast8_t count = epoll_wait(epoll_fd, events, 2, -1); int_fast8_t count = epoll_wait(epoll_fd, events, 2, -1);
if (count == -1) { if (count == -1) {
perror("epoll_wait"); perror("ERROR: epoll_wait");
return 7; return 7;
} }
@ -195,7 +190,7 @@ main(int argc, char** argv)
memset(buf, 0, 3); memset(buf, 0, 3);
int_fast8_t ret = recv(sock, buf, 1, 0); int_fast8_t ret = recv(sock, buf, 1, 0);
if (ret == -1) { if (ret == -1) {
perror("recv"); perror("ERROR: recv");
return 8; return 8;
} }
@ -205,7 +200,7 @@ main(int argc, char** argv)
if (buf[0] == IAC) { if (buf[0] == IAC) {
ret = recv(sock, buf + 1, 2, 0); ret = recv(sock, buf + 1, 2, 0);
if (ret < 0) { if (ret < 0) {
perror("recv"); perror("ERROR: recv");
return 8; return 8;
} }
@ -225,7 +220,7 @@ main(int argc, char** argv)
} }
if (send(sock, &chr, 1, 0) != 1) { if (send(sock, &chr, 1, 0) != 1) {
perror("send"); perror("ERROR: send");
return 9; return 9;
} }