From 60b0fbf1b141566fd5f559c1d7db2317676a3be4 Mon Sep 17 00:00:00 2001 From: "Michael J. Pounders" Date: Sun, 30 Nov 2014 02:04:43 -0500 Subject: [PATCH] Last bit of code clean up, and bug fixes. Release? --- shm_utils/mon-umurmurd/mon-umurmurd.c | 33 ++++++++++----- src/main.c | 2 +- src/sharedmemory.c | 61 +++++++++++++++------------ src/sharedmemory.h | 2 +- src/sharedmemory_struct.h | 9 ++-- 5 files changed, 63 insertions(+), 44 deletions(-) diff --git a/shm_utils/mon-umurmurd/mon-umurmurd.c b/shm_utils/mon-umurmurd/mon-umurmurd.c index 18a8a97..931a34d 100644 --- a/shm_utils/mon-umurmurd/mon-umurmurd.c +++ b/shm_utils/mon-umurmurd/mon-umurmurd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include "../../src/sharedmemory.h" @@ -18,25 +19,35 @@ void run_shm(void); int main(int argc, char **argv) { - - - while ( (opt = getopt(argc, argv, "w")) != -1 ) + struct stat buf; + int bindport = 0; + + while ( (opt = getopt(argc, argv, "wb:")) != -1 ) { switch(opt) { case 'w': wait = 1; break; - default: - fprintf(stderr, "Usage: %s [-w]\n", argv[0]); + case 'b': + bindport = atoi(optarg); + break; + default: + fprintf(stderr, "Usage: %s [-w] [-b ]\n", argv[0]); fprintf(stderr, "\t-w - Wait for umurmurd to create shm area. useful if you need to start from init.d script\n" ); + fprintf(stderr, "\t-b - Change this to the port used when starting umurmurd. Defaults to \"/umurmurd:64738\" \n"); exit(EXIT_FAILURE); } } shmptr = NULL; - sprintf( shm_file_name, "/umurmurd:%i", 64738 ); + if( !bindport ) + { + bindport = 64738; + } + + sprintf( shm_file_name, "/umurmurd:%i", bindport ); if( wait ) shm_statem = WAIT_ATTACH_SHM; @@ -62,10 +73,10 @@ int main(int argc, char **argv) } shm_statem = MAT_SHM; break; - case MAT_SHM: - if( ( shmptr = mmap(0, 1, PROT_READ, MAP_SHARED, shm_fd, 0) ) == (void *) (-1) ) //MJP BUG? + case MAT_SHM: + fstat( shm_fd, &buf); + if( ( shmptr = mmap(0, buf.st_size, PROT_READ, MAP_SHARED, shm_fd, 0) ) == (void *) (-1) ) //MJP BUG? { - exit(EXIT_FAILURE); } printf( "umumurd PID: %u\n\r", shmptr->umurmurd_pid ); @@ -94,7 +105,7 @@ void run_shm(void) int cc; printf( "\033[2J\033[H" ); fflush(stdout); //clear screen VT100 - + printf( "%s Clients(CONECTED/MAX) %i/%i\n\n", shm_file_name, shmptr->clientcount, shmptr->server_max_clients ); for( cc = 0 ; cc < shmptr->server_max_clients ; cc++ ) { @@ -150,4 +161,4 @@ int cc; { exit(EXIT_FAILURE); //You dont have to exit you could just report the fact that the data is not valid } -} +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index ceebd02..89ca280 100644 --- a/src/main.c +++ b/src/main.c @@ -352,7 +352,7 @@ int main(int argc, char **argv) Ban_init(); #ifdef USE_SHAREDMEMORY_API - Sharedmemory_init(); + Sharedmemory_init( bindport, bindport6 ); #endif #ifdef POSIX_PRIORITY_SCHEDULING diff --git a/src/sharedmemory.c b/src/sharedmemory.c index 46f54f2..b4bfee5 100644 --- a/src/sharedmemory.c +++ b/src/sharedmemory.c @@ -1,40 +1,47 @@ #include "sharedmemory.h" #include "sharedmemory_global.h" -void Sharedmemory_init(void) +void Sharedmemory_init( int bindport, int bindport6 ) { + + int server_max_clients = getIntConf(MAX_CLIENTS); + int shmtotal_size = sizeof( shm_t ) + (sizeof( shmclient_t ) * server_max_clients); - int bindport = getIntConf(BINDPORT); //MJP BUG commandline option for address and port dont work this way going to have - int server_max_clients = getIntConf(MAX_CLIENTS); //to bring them across as prameters to Sharedmemory_init(void) - int shmptr_size = sizeof( shm_t ) + (sizeof( shmclient_t ) * server_max_clients); + if( !bindport ) + { + bindport = getIntConf(BINDPORT); + } - sprintf( shm_file_name, "umurmurd:%i", bindport ); + sprintf( shm_file_name, "/umurmurd:%i", bindport ); Log_info("SHM_API: shm_fd=\"%s\"", shm_file_name ); - shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0660 ); - if(shm_fd == -1) - { - Log_fatal( "SHM_API: Open failed:%s\n", strerror(errno)); - exit(1); - } + shm_fd = shm_open( shm_file_name, O_CREAT | O_RDWR, 0660 ); + if(shm_fd == -1) + { + Log_fatal( "SHM_API: Open failed:%s\n", strerror(errno)); + exit(1); + } - if( ftruncate( shm_fd, shmptr_size ) == -1 ) - { - Log_fatal( "SHM_API: ftruncate : %s\n", strerror(errno)); - exit(1); - } + if( ftruncate( shm_fd, shmtotal_size ) == -1 ) + { + Log_fatal( "SHM_API: ftruncate : %s\n", strerror(errno)); + exit(1); + } + + shmptr = mmap(0, shmtotal_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); + if (shmptr == (void *) -1) + { + Log_fatal( "SHM_API: mmap failed : %s\n", strerror(errno)); + exit(1); + } - shmptr = mmap(0, shmptr_size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); - if (shmptr == (void *) -1) - { - Log_fatal( "SHM_API: mmap failed : %s\n", strerror(errno)); - exit(1); - } + memset( shmptr, 0, shmtotal_size ); - memset( shmptr, 0, shmptr_size ); - shmptr->umurmurd_pid = getpid(); - shmptr->server_max_clients = server_max_clients; + shmptr->server_max_clients = server_max_clients; + shmptr->shmtotal_size = shmtotal_size; + shmptr->shmclient_size = sizeof( shmclient_t ) * shmptr->server_max_clients; + } void Sharedmemory_update(void) @@ -44,7 +51,7 @@ void Sharedmemory_update(void) unsigned int cc = 0; client_t *client_itr = NULL; - memset( &shmptr->client[0], 0, sizeof( shmclient_t ) * shmptr->server_max_clients ); + memset( &shmptr->client[0], 0, shmptr->shmclient_size ); shmptr->clientcount = Client_count(); if( shmptr->clientcount ) @@ -107,4 +114,4 @@ void Sharedmemory_deinit(void) close( shm_fd ); shm_unlink( shm_file_name ); shmptr->umurmurd_pid = 0; -} +} \ No newline at end of file diff --git a/src/sharedmemory.h b/src/sharedmemory.h index fd9c120..16573ea 100644 --- a/src/sharedmemory.h +++ b/src/sharedmemory.h @@ -14,7 +14,7 @@ #include "channel.h" #include "sharedmemory_struct.h" -void Sharedmemory_init(void); +void Sharedmemory_init( int bindport, int bindport6 ); void Sharedmemory_update(void); void Sharedmemory_alivetick(void); void Sharedmemory_deinit(void); diff --git a/src/sharedmemory_struct.h b/src/sharedmemory_struct.h index d1453fb..c1f4430 100644 --- a/src/sharedmemory_struct.h +++ b/src/sharedmemory_struct.h @@ -7,7 +7,7 @@ typedef struct char os[121], release[121], os_version[121]; int tcp_port, udp_port; bool_t bUDP, authenticated, deaf, mute, self_deaf, self_mute, recording, bOpus; - int availableBandwidth; + int availableBandwidth; uint32_t online_secs, idle_secs; bool_t isAdmin; bool_t isSuppressed; @@ -19,9 +19,10 @@ typedef struct typedef struct { + int shmtotal_size, shmclient_size; int clientcount, server_max_clients; unsigned int umurmurd_pid; - uint8_t alive; - shmclient_t client[]; - + uint8_t alive; + shmclient_t client[]; + }shm_t; -- 2.30.2