Skip to content

Commit

Permalink
added: global structure to hold openned thins.
Browse files Browse the repository at this point in the history
this structure holds openned databases, pointers to linked lists, openned socket file descriptors and many more. This structure is usefull while unexpected exits.
  • Loading branch information
rishithaminol committed Mar 14, 2018
1 parent da73b33 commit 508c716
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmarketcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pthread_mutex_t shift_column_locker = PTHREAD_MUTEX_INITIALIZER;
#define LOCK_SHIFT_COLUMN_LOCKER pthread_mutex_lock(&shift_column_locker)
#define UNLOCK_SHIFT_COLUMN_LOCKER pthread_mutex_unlock(&shift_column_locker)

static size_t number_of_coins = 0;
struct global_data_handle global_data_handle;

/* @brief coin_history column map */
char *col_names[] = {
Expand Down Expand Up @@ -102,6 +102,7 @@ int main(int argc, char *argv[])

pthread_mutex_init(&sql_db_access, NULL);
pthread_mutex_init(&shift_column_locker, NULL);
global_data_handle.httpd_sockfd = 0;

prog_name = *argv;

Expand Down
5 changes: 5 additions & 0 deletions cmarketcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

#include <pthread.h>

struct global_data_handle {
int httpd_sockfd;
};

extern char *prog_name;
extern pthread_mutex_t shift_column_locker;
extern struct global_data_handle global_data_handle;

#endif
19 changes: 9 additions & 10 deletions httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ int write_to_client(int sockfd)
/* @brief needs openned database */
int __cb_main_thread(sqlite3 *db)
{
int httpd_sockfd;
int httpd_port;
int sockfd;

Expand All @@ -401,8 +400,8 @@ int __cb_main_thread(sqlite3 *db)
httpd_port = 1040;

/* init server socket (ipv4, tcp) */
httpd_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (httpd_sockfd < 0) {
global_data_handle.httpd_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (global_data_handle.httpd_sockfd < 0) {
CM_ERROR("httpd socket failed to create\n");
exit(1);
}
Expand All @@ -412,24 +411,24 @@ int __cb_main_thread(sqlite3 *db)
httpd_sockaddr.sin_port = htons(httpd_port);
httpd_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(httpd_sockfd, (struct sockaddr *)&httpd_sockaddr,
if (bind(global_data_handle.httpd_sockfd, (struct sockaddr *)&httpd_sockaddr,
sizeof(httpd_sockaddr)) == -1) {
close(httpd_sockfd);
close(global_data_handle.httpd_sockfd);
CM_ERROR("bind failed\n");
exit(1);
}

if (listen(httpd_sockfd, CLIENT_MAX) == -1) {
close(httpd_sockfd);
if (listen(global_data_handle.httpd_sockfd, CLIENT_MAX) == -1) {
close(global_data_handle.httpd_sockfd);
CM_ERROR("listen failed\n");
exit(1);
}

/* at this stage 'httpd_sockfd', 'httpd_sockaddr', 'httpd_port', 'clilen'
/* at this stage 'global_data_handle.httpd_sockfd', 'httpd_sockaddr', 'httpd_port', 'clilen'
* settled up */
while (1) {
struct __cb_args *cb_arg = NULL;
sockfd = accept(httpd_sockfd, (struct sockaddr *)&client_sockaddr, &clilen);
sockfd = accept(global_data_handle.httpd_sockfd, (struct sockaddr *)&client_sockaddr, &clilen);
if (sockfd == -1) {
CM_ERROR("Connection accept error\n");
continue;
Expand All @@ -450,7 +449,7 @@ int __cb_main_thread(sqlite3 *db)
}
}

close(httpd_sockfd);
close(global_data_handle.httpd_sockfd);

//pthread_exit(NULL);
return 0;
Expand Down
2 changes: 2 additions & 0 deletions signal_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unistd.h> // alarm()

#include "signal_handler.h"
#include "cmarketcap.h"

void handle_signal(int signal)
{
Expand All @@ -20,6 +21,7 @@ void handle_signal(int signal)
break;
case SIGINT:
printf("Caught SIGINT, exiting now\n");
close(global_data_handle.httpd_sockfd);
exit(0);
default:
fprintf(stderr, "Caught wrong signal: %d\n", signal);
Expand Down

0 comments on commit 508c716

Please sign in to comment.