From 00f3636582b9588f2f17d69f0c5e7a850d51235d Mon Sep 17 00:00:00 2001 From: 20kdc Date: Sat, 22 Apr 2023 20:14:02 +0100 Subject: [PATCH 1/3] Clean up checkpoint connection reader code so it doesn't need to be run --- src/server.cc | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/server.cc b/src/server.cc index 60a6fb63..a9649162 100644 --- a/src/server.cc +++ b/src/server.cc @@ -96,7 +96,13 @@ static std::stringstream shutdown_message; static bool shutdown_triggered = false; static bool in_emergency_mode = false; -static Var checkpointed_connections; +/* Linked list of checkpointed connections so they don't need to be read in. */ +typedef struct checkpointed_connection { + Objid who, listener; + checkpointed_connection *next; +} checkpointed_connection; + +static checkpointed_connection *checkpointed_connections = NULL; typedef enum { CHKPT_OFF, CHKPT_TIMER, CHKPT_SIGNAL, CHKPT_FUNC @@ -774,14 +780,13 @@ main_loop(void) free_var(pending_list); /* Second, notify DB of disconnections for all checkpointed connections */ - for (i = 1; i <= checkpointed_connections.v.list[0].v.num; i++) { - Var v; - - v = checkpointed_connections.v.list[i]; - call_notifier(v.v.list[1].v.obj, v.v.list[2].v.obj, + while (checkpointed_connections) { + checkpointed_connection * conn = checkpointed_connections; + checkpointed_connections = conn->next; + call_notifier(conn->who, conn->listener, "user_disconnected"); + myfree(conn, M_STRUCT); } - free_var(checkpointed_connections); /* Third, run #0:server_started() */ run_server_task(-1, Var::new_obj(SYSTEM_OBJECT), "server_started", new_list(0), "", nullptr); @@ -1779,11 +1784,17 @@ int read_active_connections(void) { int count, i, have_listeners = 0; + checkpointed_connection **conn_next = &checkpointed_connections; char c; + /* + * Notably, this code MAY run once. + * If it ran twice we'd need to clean up old checkpointed_connections. + * Running not-at-all is fine. + */ + i = dbio_scanf("%d active connections%c", &count, &c); if (i == EOF) { /* older database format */ - checkpointed_connections = new_list(0); return 1; } else if (i != 2) { errlog("READ_ACTIVE_CONNECTIONS: Bad active connections count.\n"); @@ -1798,8 +1809,8 @@ read_active_connections(void) errlog("READ_ACTIVE_CONNECTIONS: Bad EOL.\n"); return 0; } - checkpointed_connections = new_list(count); for (i = 1; i <= count; i++) { + checkpointed_connection *conn; Objid who, listener; Var v; @@ -1812,10 +1823,15 @@ read_active_connections(void) who = dbio_read_num(); listener = SYSTEM_OBJECT; } - checkpointed_connections.v.list[i] = v = new_list(2); - v.v.list[1].type = v.v.list[2].type = TYPE_OBJ; - v.v.list[1].v.obj = who; - v.v.list[2].v.obj = listener; + + /* extend list and advance conn_next */ + conn = (checkpointed_connection *)mymalloc(sizeof(checkpointed_connection), M_STRUCT); + *conn_next = conn; + conn_next = &conn->next; + + conn->who = who; + conn->listener = listener; + conn->next = NULL; } return 1; From db1321723d599a03f660a1eff1edd45024e63d6d Mon Sep 17 00:00:00 2001 From: 20kdc Date: Sat, 22 Apr 2023 20:29:24 +0100 Subject: [PATCH 2/3] Add ability to make a new DB by inputting _ as input DB name --- src/db_file.cc | 30 +++++++++++++++++++++++++++++- src/server.cc | 2 ++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/db_file.cc b/src/db_file.cc index bdc5320f..f07eb3e1 100644 --- a/src/db_file.cc +++ b/src/db_file.cc @@ -824,6 +824,22 @@ fmt_verb_name(void *data) return reset_stream(s); } +static int +new_db_file(void) +{ + waif_before_loading(); + dbio_input_version = current_db_version; + + /* initialize anything we need to */ + dbpriv_set_all_users(new_list(0)); + + /* see db_objects.c */ + dbpriv_after_load(); + waif_after_loading(); + + return 1; +} + static int read_db_file(void) { @@ -1216,7 +1232,10 @@ db_initialize(int *pargc, char ***pargv) *pargc -= 2; *pargv += 2; - if (!(f = fopen(input_db_name, "r"))) { + if (!strcmp(input_db_name, "_")) { + /* creating new DB, so leave input_db as NULL */ + f = NULL; + } else if (!(f = fopen(input_db_name, "r"))) { fprintf(stderr, "Cannot open input database file: %s\n", input_db_name); return 0; @@ -1230,6 +1249,15 @@ db_initialize(int *pargc, char ***pargv) int db_load(void) { + if (!input_db) { + oklog("DB_LOAD: Creating new DB...\n"); + if (!new_db_file()) { + errlog("DB_LOAD: Cannot create database!\n"); + return 0; + } + return 1; + } + dbpriv_set_dbio_input(input_db); str_intern_open(0); diff --git a/src/server.cc b/src/server.cc index a9649162..302357a0 100644 --- a/src/server.cc +++ b/src/server.cc @@ -1881,6 +1881,7 @@ print_usage() fprintf(stderr, " %-20s %s\n", "-e, --emergency", "emergency wizard mode"); fprintf(stderr, " %-20s %s\n", "-l, --log", "redirect standard output to log file"); fprintf(stderr, "\nDATABASE OPTIONS\n"); + fprintf(stderr, " %-20s %s\n", "_", "As a input-db-file, causes a new (completely blank) DB to be created."); fprintf(stderr, " %-20s %s\n", "-m, --clear-move", "clear the `last_move' builtin property on all objects"); fprintf(stderr, " %-20s %s\n", "-w, --waif-type", "convert waifs from the specified type (check with typeof(waif) in your old MOO)"); fprintf(stderr, " %-20s %s\n", "-f, --start-script", "file to load and pass to `#0:do_start_script()'"); @@ -1902,6 +1903,7 @@ print_usage() fprintf(stderr, "Examples:\n"); fprintf(stderr, "%s -c '$enable_debugging();' -f development.moo Minimal.db Minimal.db.new 7777\n", this_program); fprintf(stderr, "%s Minimal.db Minimal.db.new\n", this_program); + fprintf(stderr, "%s -e _ init.db\n", this_program); } int waif_conversion_type = _TYPE_WAIF; /* For shame. We can remove this someday. */ From a850129d4eefe624dce00953a54e13c5a8adfc1f Mon Sep 17 00:00:00 2001 From: 20kdc Date: Sat, 22 Apr 2023 20:32:32 +0100 Subject: [PATCH 3/3] Fix help printing twice, replace exit calls from main with return --- src/server.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/server.cc b/src/server.cc index 302357a0..95801ea7 100644 --- a/src/server.cc +++ b/src/server.cc @@ -1973,7 +1973,7 @@ main(int argc, char **argv) case 'v': /* --version; print version and exit */ { fprintf(stderr, "ToastStunt version %s\n", server_version); - exit(1); + return 1; } break; @@ -2016,7 +2016,7 @@ main(int argc, char **argv) { #ifndef OUTBOUND_NETWORK fprintf(stderr, "Outbound networking is disabled. The '--outbound' option is invalid.\n"); - exit(1); + return 1; #else cmdline_outbound = true; outbound_network_enabled = true; @@ -2058,7 +2058,7 @@ main(int argc, char **argv) { #ifndef USE_TLS fprintf(stderr, "TLS is disabled or not supported. The '--tls-port' option is invalid.\n"); - exit(1); + return 1; #else char *p = nullptr; initial_tls_ports.push_back(strtoul(optarg, &p, 10)); @@ -2070,7 +2070,7 @@ main(int argc, char **argv) { #ifndef USE_TLS fprintf(stderr, "TLS is disabled or not supported. The '--tls' option is invalid.\n"); - exit(1); + return 1; #else cmdline_cert = true; default_certificate_path = optarg; @@ -2082,7 +2082,7 @@ main(int argc, char **argv) { #ifndef USE_TLS fprintf(stderr, "TLS is disabled or not supported. The '--tls' option is invalid.\n"); - exit(1); + return 1; #else cmdline_key = true; default_key_path = optarg; @@ -2106,11 +2106,10 @@ main(int argc, char **argv) case 'h': /* --help; show usage instructions */ print_usage(); - break; default: // Should we print usage here? It's pretty spammy... - exit(1); + return 1; } } @@ -2124,7 +2123,7 @@ main(int argc, char **argv) set_log_file(f); else { perror("Error opening specified log file"); - exit(1); + return 1; } } else { set_log_file(stderr); @@ -2134,7 +2133,7 @@ main(int argc, char **argv) || !db_initialize(&argc, &argv) || !network_initialize(argc, argv, &desc)) { print_usage(); - exit(1); + return 1; } if (initial_ports.empty() @@ -2252,7 +2251,7 @@ main(int argc, char **argv) if (initial_listeners.size() < 1) { errlog("Can't create initial connection point!\n"); - exit(1); + return 1; } free_var(desc); // I doubt anybody will have enough listeners for this to be worthwhile, but why not. @@ -2264,7 +2263,7 @@ main(int argc, char **argv) #endif if (!db_load()) - exit(1); + return 1; free_reordered_rt_env_values(); @@ -2305,7 +2304,7 @@ main(int argc, char **argv) } if (listen_failures >= initial_listeners.size()) - exit(1); + return 1; initial_listeners.clear(); initial_listeners.shrink_to_fit();