cleanup function and signal handling

This commit is contained in:
Igor Socec 2014-03-10 16:42:49 +01:00
parent 175075d2d5
commit 496198f488
4 changed files with 65 additions and 36 deletions

View file

@ -8,7 +8,7 @@ int client_close(struct client_t *client) {
close(client->socket);
}
client->socket = -1;
fprintf(stderr,"[%s]: socket closed for client %s\n", __func__, client->ip_string);
fprintf(stderr,"[%s] socket closed for client %s\n", __func__, client->ip_string);
return 0;
}

View file

@ -1,10 +1,18 @@
#include "moxerver.h"
#include <signal.h> /* handling quit signals */
#define NAME "moxerver"
#define SERVER_WAIT_TIMEOUT 5 /* seconds for select() timeout in server loop */
#define PORT_MIN 4001 /* minimum TCP port number */
#define PORT_MAX 4008 /* maximum TCP port number */
/* Global variables used throughout the application */
struct server_t server;
struct client_t client; //TODO working with only 1 client, this can be expanded into a list
struct tty_t tty_dev;
/* Prints help message. */
static void usage() {
//TODO maybe some styling should be done
@ -12,15 +20,29 @@ static void usage() {
fprintf(stderr, "- tcp_port range [%d .. %d]\n\n", PORT_MIN, PORT_MAX);
}
/* Performs cleanup and exit. */
void cleanup(int exit_code) {
fprintf(stderr, "[%s] cleanup and exit with %d\n", NAME, exit_code);
/* close server and client */
server_close(&server);
if (client.socket != -1) {
client_close(&client);
}
exit(exit_code);
}
/* Handles received quit signals, use it for all quit signals of interest. */
void quit_handler(int signum) {
/* perform cleanup and exit with 0 */
fprintf(stderr, "[%s] received signal %d\n", NAME, signum);
cleanup(0);
}
/* MoxaNix main program loop. */
int main(int argc, char *argv[]) {
int ret;
struct server_t server;
struct client_t client; //TODO working with only 1 client, this can be expanded into a list
struct tty_t tty_dev;
unsigned int tcp_port = -1;
char tty_path[DEV_PATH] = {'\0'};
@ -29,10 +51,15 @@ int main(int argc, char *argv[]) {
struct timeval tv;
/* catch and handle some quit signals, SIGKILL can't be caught */
signal(SIGTERM, quit_handler);
signal(SIGQUIT, quit_handler);
signal(SIGINT, quit_handler);
/* check argument count */
if (argc <= 1) {
usage();
return 0;
return -1;
}
/* grab arguments */
while ((ret = getopt(argc, argv, ":p:t:h")) != -1) {
@ -50,28 +77,29 @@ int main(int argc, char *argv[]) {
usage();
return 0;
default:
fprintf(stderr, "error parsing arguments\n");
fprintf(stderr, "[%s] error parsing arguments\n", NAME);
usage();
return 0;
return -1;
}
}
/* check arguments */
if (tcp_port < PORT_MIN || tcp_port > PORT_MAX) {
fprintf(stderr, "error: port number out of %d-%d range\n\n", PORT_MIN, PORT_MAX);
fprintf(stderr, "[%s] error: port number out of %d-%d range\n\n",
NAME, PORT_MIN, PORT_MAX);
usage();
return -1;
}
if (strlen(tty_path) == 0) {
fprintf(stderr, "error: tty path was not specified\n\n");
fprintf(stderr, "[%s] error: tty path was not specified\n\n", NAME);
usage();
return 0;
return -1;
}
/* introduction message */
fprintf(stderr, "=== MoxaNix ===\n");
fprintf(stderr, "[%s] === MoxaNix ===\n", NAME);
//TODO remove the following line after development phase
fprintf(stderr, "TCP port: %d, TTY device path: %s\n", tcp_port, tty_path);
fprintf(stderr, "[%s] TCP port: %d, TTY device path: %s\n", NAME, tcp_port, tty_path);
/* initialize */
server_setup(&server, tcp_port);
@ -97,20 +125,20 @@ int main(int argc, char *argv[]) {
/* wait with select() */
ret = select(fdmax+1, &read_fds, NULL, NULL, &tv);
if (ret == -1) {
//TODO do we really break and stop server when select returns an error?
//TODO do we really break here and stop server when select returns an error?
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
break;
}
if (ret > 0) {
/* check server status */
if (FD_ISSET(server.socket, &read_fds)) {
fprintf(stderr, "received client connection request\n");
fprintf(stderr, "[%s] received client connection request\n", NAME);
/* accept connection request if there is no client connected yet */
if (client.socket == -1) {
ret = server_accept(&server, &client);
if ( ret != 0) {
/* print error but continue waiting for connection request */
fprintf(stderr, "problem accepting client\n");
fprintf(stderr, "[%s] problem accepting client\n", NAME);
continue;
}
/* put client in "character" mode */
@ -127,7 +155,7 @@ int main(int argc, char *argv[]) {
ret = client_read(&client);
if ( ret < 0) {
/* print error but continue waiting for new data */
fprintf(stderr, "problem reading client\n");
fprintf(stderr, "[%s] problem reading client\n", NAME);
continue;
}
/* echo back to client */
@ -141,21 +169,22 @@ int main(int argc, char *argv[]) {
/* a disconnected client socket is ready for reading but read returns 0 */
if ( (client.socket != -1) && FD_ISSET(client.socket, &read_fds) ) {
if (client_read(&client) == 0) {
fprintf(stderr, "client %s disconnected\n", client.ip_string);
fprintf(stderr, "[%s] client %s disconnected\n", NAME, client.ip_string);
/* close client connection */
client_close(&client);
}
}
else {
fprintf(stderr, "server waiting\n");
fprintf(stderr, "[%s] server waiting\n", NAME);
}
}
} /* END while loop */
} /* END while() loop */
/* close server and client */
server_close(&server);
client_close(&client);
/* unexpected break from while() loop */
fprintf(stderr, "[%s] unexpected condition\n", NAME);
/* cleanup and exit with -1 */
cleanup(-1);
return 0;
return -1;
}

View file

@ -17,7 +17,7 @@ int server_setup(struct server_t *server, unsigned int port) {
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
return -errno;
}
fprintf(stderr,"[%s]: socket created\n", __func__);
fprintf(stderr,"[%s] socket created\n", __func__);
/* try to avoid "Address already in use" error */
opt = 1; /* true value for setsockopt option */
@ -37,7 +37,7 @@ int server_setup(struct server_t *server, unsigned int port) {
fprintf(stderr, "[%s:%d] error %d: %s\n", __func__, __LINE__, errno, strerror(errno));
return -errno;
}
fprintf(stderr,"[%s]: bind successful\n", __func__);
fprintf(stderr,"[%s] bind successful\n", __func__);
/* check port assignment */
namelen = sizeof(server->address);
@ -51,7 +51,7 @@ int server_setup(struct server_t *server, unsigned int port) {
}
/* save server port number */
server->port = port;
fprintf(stderr,"[%s]: assigned port %u\n", __func__, server->port); //ntohs(server->address.sin_port)
fprintf(stderr,"[%s] assigned port %u\n", __func__, server->port); //ntohs(server->address.sin_port)
/* listen for a client connection, allow 2 connections in queue */
if (listen(server->socket, 2) == -1) {
@ -59,7 +59,7 @@ int server_setup(struct server_t *server, unsigned int port) {
return -errno;
}
fprintf(stderr,"[%s]: server is up, listening for client connection\n", __func__);
fprintf(stderr,"[%s] server is up, listening for client connection\n", __func__);
return 0;
}
@ -69,7 +69,7 @@ int server_close(struct server_t *server) {
if (close(server->socket) == -1) {
close(server->socket);
}
fprintf(stderr,"[%s]: socket closed, server is down\n", __func__);
fprintf(stderr,"[%s] socket closed, server is down\n", __func__);
return 0;
}
@ -97,7 +97,7 @@ int server_accept(struct server_t *server, struct client_t *accepted_client) {
/* print client information */
//TODO also print timestamp
fprintf(stderr, "[%s]: accepted client %s on port %u\n", __func__,
fprintf(stderr, "[%s] accepted client %s on port %u\n", __func__,
accepted_client->ip_string, server->port);
return 0;
}
@ -113,11 +113,11 @@ int server_reject(struct server_t *server) {
namelen = sizeof(rclient.address);
rclient.socket = accept(server->socket, (struct sockaddr *) &rclient.address, (socklen_t *) &namelen);
/* send reject message */
sprintf(reject_msg, "[%s]: port %u is already being used\n", __func__, server->port);
sprintf(reject_msg, "[%s] port %u is already being used\n", __func__, server->port);
send(rclient.socket, reject_msg, strlen(reject_msg), 0);
/* close connection */
close(rclient.socket);
fprintf(stderr, "[%s]: rejected new client request, there is alredy a client connected\n", __func__);
fprintf(stderr, "[%s] rejected new client request, there is alredy a client connected\n", __func__);
return 0;
}

View file

@ -55,7 +55,7 @@ int telnet_send_command(struct client_t *client, const char* option, const char*
return -1;
}
fprintf(stderr, "[%s]: sent %s %s\n", __func__, option, command);
fprintf(stderr, "[%s] sent %s %s\n", __func__, option, command);
return 0;
}
@ -63,7 +63,7 @@ int telnet_send_command(struct client_t *client, const char* option, const char*
int telnet_handle_command(char *databuf, int datalen) {
if (databuf[0] == telnet_option_value("IAC")) {
fprintf(stderr, "[%s]: received %s %s\n", __func__,
fprintf(stderr, "[%s] received %s %s\n", __func__,
telnet_option_name(databuf[1]), telnet_option_name(databuf[2]));
}
@ -122,13 +122,13 @@ int telnet_handle_client_write(char *databuf, int *datalen) {
for (i = 0; i < *datalen; i++) {
/* pressed ENTER */
if (databuf[i] == 13) {
fprintf(stderr, "[%s]: handling ENTER\n", __func__);
fprintf(stderr, "[%s] handling ENTER\n", __func__);
newdata[newlen++] = '\r';
newdata[newlen++] = '\n';
}
/* pressed BACKSPACE */
if (databuf[i] == 127) {
fprintf(stderr, "[%s]: handling BACKSPACE\n", __func__);
fprintf(stderr, "[%s] handling BACKSPACE\n", __func__);
newdata[newlen++] = 8;
newdata[newlen++] = ' ';
newdata[newlen++] = 8;