Browse Source

allowing for larger passdb (simple) database size

* previously the cardid, mag and rf hashes would be statically
  allocated, bursting the stack. Now allocates dynamically.
* Upped database size from ~131k to ~1M (uses ~18% mem on rpi)
* cosmetic code changes
clementinecomputing 6 years ago
parent
commit
3d4f4cb1be
4 changed files with 121 additions and 60 deletions
  1. 4 4
      busunit/common/common_config.h
  2. 59 21
      busunit/passdb/pass_communication.c
  3. 42 23
      busunit/passdb/passdb.c
  4. 16 12
      busunit/passdb/passdb.h

+ 4 - 4
busunit/common/common_config.h

@@ -398,14 +398,14 @@
 //This defines the expected size of the pass database and its RAM based index tables:
 //
 
-#define NUM_STORED_PASSES  (131072)  //A nice round number that multiplies by sizeof(rider_record) to a page boundary
-#define STORED_PASS_HASH  (131101)  //A prime number larger than NUM_STORED_PASSES
+//#define NUM_STORED_PASSES  (131072)  //A nice round number that multiplies by sizeof(rider_record) to a page boundary
+//#define STORED_PASS_HASH  (131101)  //A prime number larger than NUM_STORED_PASSES
 
 //#define NUM_STORED_PASSES  (262144)  //A nice round number that multiplies by sizeof(rider_record) to a page boundary
 //#define STORED_PASS_HASH  (262147)  //A prime number larger than NUM_STORED_PASSES
 
-//#define NUM_STORED_PASSES  (1048576)  //A nice round number that multiplies by sizeof(rider_record) to a page boundary
-//#define STORED_PASS_HASH  (1048583)  //A prime number larger than NUM_STORED_PASSES
+#define NUM_STORED_PASSES  (1048576)  //A nice round number that multiplies by sizeof(rider_record) to a page boundary
+#define STORED_PASS_HASH  (1048583)  //A prime number larger than NUM_STORED_PASSES
 
 //This defines the expected size of the billing cache:
 

+ 59 - 21
busunit/passdb/pass_communication.c

@@ -185,27 +185,32 @@ int connect_to_pass_server()
   return fd;
 }
 
-int do_database_flush(passdb_context *ctx)
-{
-        int retval;
-
-        //Detach from the current pass database
-        detach_from_passdb(ctx);
-        //Format a new database file...
-        format_new_passdb();
-
-        retval = attach_to_passdb(ctx);
-
-        if( DB_FAIL(retval) )  //If we fail to attach to our new database
-        {
-            fprintf(stderr, "Error (%d) attaching to pass database during flush attempt\n", retval);  //report failure
-            return retval;
-        }
-        else  //Otherwise, report success.
-        {
-            printf("Pass database flushed!\n");
-            return 0;
-        }
+int do_database_flush(passdb_context *ctx) {
+  int retval;
+
+  // Detach from the current pass database
+  //
+  detach_from_passdb(ctx);
+
+  // Format a new database file...
+  //
+  format_new_passdb();
+
+  retval = attach_to_passdb(ctx);
+
+  // If we fail to attach to our new database
+  //
+  if( DB_FAIL(retval) ) {
+    fprintf(stderr, "Error (%d) attaching to pass database during flush attempt\n", retval);  //report failure
+    return retval;
+  }
+
+  // Otherwise, report success.
+  //
+  else {
+    printf("Pass database flushed!\n");
+    return 0;
+  }
 }
 
 int send_query_message(int fd, passdb_context *ctx)
@@ -640,6 +645,8 @@ int do_zflush(passdb_context *ctx, void *data, int len)
 
 //    printf("do_zflush(%p, %p, %d)\n", ctx, data, len);
 
+    memset(&incoming_msg, 0, sizeof(struct message_record));
+
     retval = inflateInit(&foo);
 
     if(retval != Z_OK)
@@ -996,6 +1003,29 @@ void maintain_ipc_hub_connect(char *progname)
     }
 }
 
+int passdb_context_alloc(passdb_context *ctx) {
+  ctx->logical_card_id_hash = (rider_node **)malloc(sizeof(rider_node *)*STORED_PASS_HASH);
+  if (!(ctx->logical_card_id_hash)) { return -1; }
+
+  ctx->rider_mag_hash = (rider_node **)malloc(sizeof(rider_node *)*STORED_PASS_HASH);
+  if (!(ctx->rider_mag_hash)) { return -1; }
+
+  ctx->rider_rf_hash = (rider_node **)malloc(sizeof(rider_node *)*STORED_PASS_HASH);
+  if (!(ctx->rider_rf_hash)) { return -1; }
+
+  memset(ctx->logical_card_id_hash, 0, sizeof(rider_node **)*STORED_PASS_HASH);
+  memset(ctx->rider_mag_hash, 0, sizeof(rider_node **)*STORED_PASS_HASH);
+  memset(ctx->rider_rf_hash, 0, sizeof(rider_node **)*STORED_PASS_HASH);
+
+  return 0;
+}
+
+void passdb_context_dealloc(passdb_context *ctx) {
+  if (ctx->logical_card_id_hash)  { free(ctx->logical_card_id_hash); }
+  if (ctx->rider_mag_hash)        { free(ctx->rider_mag_hash); }
+  if (ctx->rider_rf_hash)         { free(ctx->rider_rf_hash); }
+}
+
 int main(int argc, char **argv)
 {
     struct pollfd fds[2];
@@ -1013,6 +1043,13 @@ int main(int argc, char **argv)
 
     passdb_context ctx = {0};
 
+    passdb_context_alloc(&ctx);
+
+    //------------------
+
+    memset(input_line, 0, sizeof(char)*LINE_BUFFER_SIZE);
+    memset(&incoming_msg, 0, sizeof(struct message_record));
+
     //------------------
 
     configure_signal_handlers(argv[0]);
@@ -1395,6 +1432,7 @@ int main(int argc, char **argv)
 
     printf("Detatching from Pass Database\n");
     detach_from_passdb(&ctx);
+    passdb_context_dealloc(&ctx);
 
     printf("Closing connections\n");
     if(server_fd >= 0)

+ 42 - 23
busunit/passdb/passdb.c

@@ -365,36 +365,55 @@ int format_new_passdb()
     return 0;
 }
 
-int detach_from_passdb(passdb_context *ctx)
-{
-    int i;
 
-    if(!ctx)
-      return FAIL_PARAM;
+int detach_from_passdb(passdb_context *ctx) {
+  int i;
 
-    free_rider_node_list(ctx->freelist);
-    free_rider_node_list(ctx->activelist);
+  if(!ctx) {
+    return FAIL_PARAM;
+  }
 
-    for(i=0; i < STORED_PASS_HASH; i++)
-    {
-        free_rider_node_list(ctx->logical_card_id_hash[i]);
-        free_rider_node_list(ctx->rider_mag_hash[i]);
-        free_rider_node_list(ctx->rider_rf_hash[i]);
-    }
+  free_rider_node_list(ctx->freelist);
+  free_rider_node_list(ctx->activelist);
 
-    if(ctx->riders != NULL)
-    {
-        munmap(ctx->riders, PASS_MAP_SIZE);
-    }
+  for(i=0; i < STORED_PASS_HASH; i++) {
+    free_rider_node_list(ctx->logical_card_id_hash[i]);
+    free_rider_node_list(ctx->rider_mag_hash[i]);
+    free_rider_node_list(ctx->rider_rf_hash[i]);
+  }
 
-    if(ctx->mmap_broken)
-    {
-        close(ctx->passes_fd);
-    }
+  if(ctx->riders != NULL) {
+    munmap(ctx->riders, PASS_MAP_SIZE);
+  }
 
-    memset(ctx, 0, sizeof(passdb_context));
+  if(ctx->mmap_broken) {
+    close(ctx->passes_fd);
+  }
 
-    return 0;
+  //memset(ctx, 0, sizeof(passdb_context));
+
+  // explicitely zero out fields
+  //
+  ctx->riders = NULL;
+  ctx->freelist = NULL;
+  ctx->activelist = NULL;
+  ctx->seq = 0;
+  ctx->mmap_broken = 0;
+  ctx->passes_fd = 0;
+
+  if (ctx->logical_card_id_hash) {
+    memset(ctx->logical_card_id_hash, 0, sizeof(rider_node *)*STORED_PASS_HASH);
+  }
+
+  if (ctx->rider_mag_hash) {
+    memset(ctx->rider_mag_hash, 0, sizeof(rider_node *)*STORED_PASS_HASH);
+  }
+
+  if (ctx->rider_rf_hash) {
+    memset(ctx->rider_rf_hash, 0, sizeof(rider_node *)*STORED_PASS_HASH);
+  }
+
+  return 0;
 }
 
 int attach_to_passdb(passdb_context *ctx)

+ 16 - 12
busunit/passdb/passdb.h

@@ -2,17 +2,17 @@
  * Copyright (c) 2019 Clementine Computing LLC.
  *
  * This file is part of PopuFare.
- * 
+ *
  * PopuFare is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- * 
+ *
  * PopuFare is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Affero General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Affero General Public License
  * along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
  *
@@ -35,12 +35,12 @@ typedef struct rider_record_struct
 {
     seq_t seq;				//sequence number
     logical_card_id_t  id;			//rider ID
-      
+
     char magstripe_value[CREDENTIAL_LEN];
     char rfid_value[CREDENTIAL_LEN];
-    
+
     char rule_name[RULENAME_LEN];
-    char rule_param[PARAM_LEN];    
+    char rule_param[PARAM_LEN];
 
 } rider_record;
 
@@ -55,14 +55,18 @@ typedef struct rider_node_struct
 typedef struct passdb_context_struct
 {
   rider_record *riders;
-  
+
   rider_node *freelist;
   rider_node *activelist;
-  
-  rider_node *logical_card_id_hash[STORED_PASS_HASH];
-  rider_node *rider_mag_hash[STORED_PASS_HASH];
-  rider_node *rider_rf_hash[STORED_PASS_HASH];
-  
+
+  //rider_node *logical_card_id_hash[STORED_PASS_HASH];
+  //rider_node *rider_mag_hash[STORED_PASS_HASH];
+  //rider_node *rider_rf_hash[STORED_PASS_HASH];
+
+  rider_node **logical_card_id_hash;
+  rider_node **rider_mag_hash;
+  rider_node **rider_rf_hash;
+
   seq_t seq;
 
   //THESE ARE USED IF MMAP IS DETERMINED TO BE BROKEN...