diff --git a/packages/c/cmake/atsdk.cmake b/packages/c/cmake/atsdk.cmake index f2ac6c7d0..35207f004 100644 --- a/packages/c/cmake/atsdk.cmake +++ b/packages/c/cmake/atsdk.cmake @@ -3,7 +3,7 @@ if(NOT atsdk_FOUND) FetchContent_Declare( atsdk GIT_REPOSITORY https://github.com/atsign-foundation/at_c.git - GIT_TAG 504c299c48efdf7c9e6ecf9f7969ae9bedf495c7 + GIT_TAG d8afd18535259b389344bc9d6392e213e7243650 ) FetchContent_MakeAvailable(atsdk) install(TARGETS atclient atchops atlogger) diff --git a/packages/c/sshnpd/include/sshnpd/background_jobs.h b/packages/c/sshnpd/include/sshnpd/background_jobs.h index 145eae934..8ba8353c7 100644 --- a/packages/c/sshnpd/include/sshnpd/background_jobs.h +++ b/packages/c/sshnpd/include/sshnpd/background_jobs.h @@ -21,6 +21,8 @@ struct refresh_device_entry_params { const char *payload; const char *username; volatile sig_atomic_t *should_run; + atclient_atkey *infokeys; + atclient_atkey *usernamekeys; }; /** diff --git a/packages/c/sshnpd/src/background_jobs.c b/packages/c/sshnpd/src/background_jobs.c index 4559a4b5d..a9893ae69 100644 --- a/packages/c/sshnpd/src/background_jobs.c +++ b/packages/c/sshnpd/src/background_jobs.c @@ -17,12 +17,9 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { struct refresh_device_entry_params *params = void_refresh_device_entry_params; - // Buffer for the atkeys - size_t num_managers = params->params->manager_list_len; - size_t num_username_keys = params->params->hide ? 0 : num_managers; - - atclient_atkey infokeys[num_managers]; - atclient_atkey usernamekeys[num_username_keys]; + const size_t num_managers = params->params->manager_list_len; + atclient_atkey *infokeys = params->infokeys; + atclient_atkey *usernamekeys = params->usernamekeys; // Buffer for the base portion of each atkey size_t infokey_base_len = strlen(params->params->device) + strlen(params->params->atsign) + @@ -55,7 +52,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { int index; for (index = 0; index < num_managers; index++) { // device_info - atclient_atkey_init(infokeys + index); size_t buffer_len = strlen(params->params->manager_list[index]) + infokey_base_len; char atkey_buffer[buffer_len]; // example: @client_atsign:device_info.device_name.sshnp@client_atsign @@ -65,7 +61,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { if (ret != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create device_info atkey for %s\n", params->params->manager_list[index]); - atclient_atkey_free(infokeys + index); break; } @@ -76,8 +71,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { atclient_atkey_metadata_set_ccd(metadata, true); atclient_atkey_metadata_set_ttl(metadata, (long)30 * 24 * 60 * 60 * 1000); // 30 days in ms - // username - atclient_atkey_init(usernamekeys + index); buffer_len = strlen(params->params->manager_list[index]) + usernamekey_base_len; // example: @client_atsign:device_info.device_name.sshnp@client_atsign snprintf(atkey_buffer, buffer_len, "%s%s", params->params->manager_list[index], username_key_base); @@ -85,8 +78,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { if (ret != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to create username atkey for %s\n", params->params->manager_list[index]); - atclient_atkey_free(infokeys + index); - atclient_atkey_free(usernamekeys + index); break; } @@ -100,8 +91,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { if (ret != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to delete username atkey for %s\n", params->params->manager_list[index]); - atclient_atkey_free(infokeys + index); - atclient_atkey_free(usernamekeys + index); break; } } else { @@ -109,18 +98,12 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { if (ret != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to put username atkey for %s\n", params->params->manager_list[index]); - atclient_atkey_free(infokeys + index); - atclient_atkey_free(usernamekeys + index); break; } } } if (ret != 0) { - for (int i = 0; i < index; i++) { - atclient_atkey_free(infokeys + i); - atclient_atkey_free(usernamekeys + i); - } *params->should_run = 0; } @@ -135,10 +118,6 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { pthread_exit(NULL); } - for (int i = 0; i < num_managers; i++) { - atclient_atkey_free(usernamekeys + i); - } - // Build each atkey int interval_seconds = 60 * 60; // once an hour int counter = 0; @@ -190,10 +169,5 @@ void *refresh_device_entry(void *void_refresh_device_entry_params) { sleep(1); } - // Clean up upon exit - for (int i = 0; i < num_managers; i++) { - atclient_atkey_free(infokeys + i); // automatically cleans up metadata as well - } - pthread_exit(NULL); } diff --git a/packages/c/sshnpd/src/main.c b/packages/c/sshnpd/src/main.c index 098d14a5b..db7525150 100644 --- a/packages/c/sshnpd/src/main.c +++ b/packages/c/sshnpd/src/main.c @@ -243,8 +243,27 @@ int main(int argc, char **argv) { // 9. Start the device refresh loop - if hide is off pthread_t refresh_tid; + atclient_atkey *infokeys = malloc(sizeof(atclient_atkey) * params.manager_list_len); + if (infokeys == NULL) { + atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for infokeys\n"); + exit_res = 1; + goto cancel_atclient; + } + + atclient_atkey *usernamekeys = malloc(sizeof(atclient_atkey) * params.manager_list_len); + if (usernamekeys == NULL) { + atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to allocate memory for usernamekeys\n"); + exit_res = 1; + goto clean_atkeys; + } + + for(int i = 0; i < params.manager_list_len; i++) { + atclient_atkey_init(infokeys + i); + atclient_atkey_init(usernamekeys + i); + } + struct refresh_device_entry_params refresh_params = {&worker, &atclient_lock, ¶ms, - ping_response, username, &should_run}; + ping_response, username, &should_run, infokeys, usernamekeys}; res = pthread_create(&refresh_tid, NULL, refresh_device_entry, (void *)&refresh_params); if (res != 0) { atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed to start refresh device entry thread\n"); @@ -308,6 +327,14 @@ int main(int argc, char **argv) { main_loop(); atlogger_log(LOGGER_TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Exited main loop\n"); + for(int i = 0; i < params.manager_list_len; i++) { + atclient_atkey_free(infokeys + i); + atclient_atkey_free(usernamekeys + i); + } + + free(infokeys); + free(usernamekeys); + close_authkeys: fclose(authkeys_file); free(authkeys_filename);