-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multi-tun support for windows #1020
base: main
Are you sure you want to change the base?
Changes from 2 commits
dd2d7d7
4ea6f2b
f2d5361
7d01843
f9d83c8
d253fea
befd2b0
aca03db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,21 +14,39 @@ | |
limitations under the License. | ||
*/ | ||
|
||
#include "instance-config.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <config-utils.h> | ||
#include <string.h> | ||
#include "identity-utils.h" | ||
#include "instance.h" | ||
#include <ziti/ziti_log.h> | ||
|
||
#if _WIN32 | ||
#define realpath(rel, abs) _fullpath(abs, rel, MAX_PATH) | ||
#endif | ||
|
||
// to store the whole tunnel status data | ||
#define MIN_BUFFER_LEN 512 | ||
|
||
static uv_sem_t sem; | ||
static unsigned int sem_value = 1; | ||
static int sem_initialized = -1; | ||
|
||
void initialize_instance_config() { | ||
static char* config_dir = NULL; | ||
static bool config_saving_enabled = true; | ||
|
||
/// | ||
/// \param path represents the location of the configuration to be used | ||
void initialize_instance_config(const char* path) { | ||
if (!path) { | ||
//indicates this was started with -i (not -I). Operations should not do anything in this case | ||
return; | ||
} | ||
|
||
char actual_config_dir[FILENAME_MAX]; | ||
realpath(path, actual_config_dir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we check the return value of |
||
|
||
config_dir = strdup(actual_config_dir); //save the location of the config dir | ||
sem_initialized = uv_sem_init(&sem, sem_value); | ||
if (sem_initialized < 0) { | ||
ZITI_LOG(WARN, "Could not initialize lock for the config, config file may not be updated"); | ||
|
@@ -66,74 +84,62 @@ bool load_config_from_file(char* config_file_name) { | |
} | ||
|
||
bool load_tunnel_status_from_file(uv_loop_t* ziti_loop) { | ||
char* config_path = get_system_config_path(); | ||
|
||
uv_fs_t fs; | ||
int check = uv_fs_mkdir(ziti_loop, &fs, config_path, 0755, NULL); | ||
int check = uv_fs_mkdir(ziti_loop, &fs, config_dir, 0755, NULL); | ||
if (check == 0) { | ||
ZITI_LOG(TRACE, "config path is created at %s", config_path); | ||
ZITI_LOG(TRACE, "config path is created at %s", config_dir); | ||
} else if (check == UV_EEXIST) { | ||
ZITI_LOG(TRACE, "config path exists at %s", config_path); | ||
ZITI_LOG(TRACE, "config path exists at %s", config_dir); | ||
} else { | ||
ZITI_LOG(ERROR, "error creating %s: %s", config_path, uv_strerror(check)); | ||
ZITI_LOG(ERROR, "error creating %s: %s", config_dir, uv_strerror(check)); | ||
return false; | ||
} | ||
bool loaded = false; | ||
|
||
char* config_file_name = get_config_file_name(config_path); | ||
char* bkp_config_file_name = get_backup_config_file_name(config_path); | ||
char* config_file_name = get_config_file_name(); | ||
ZITI_LOG(INFO,"Loading config file from %s", config_file_name); | ||
|
||
// try to load tunnel status from config file | ||
loaded = load_config_from_file(config_file_name); | ||
|
||
// try to load tunnel status from backup config file | ||
// not able to load the tunnel status | ||
if (!loaded) { | ||
loaded = load_config_from_file(bkp_config_file_name); | ||
ZITI_LOG(WARN, "Config file %s cannot be read, does not exist, or is invalid. a new config file will be written if possible.", config_file_name); | ||
} | ||
|
||
// not able to load the tunnel status from both the config and backup files | ||
if (!loaded) { | ||
ZITI_LOG(WARN, "Config files %s and the backup file cannot be read or they do not exist, will create a new config file or the old one will be overwritten", config_file_name); | ||
} | ||
set_config_dir(config_dir); | ||
|
||
free(config_file_name); | ||
free(bkp_config_file_name); | ||
free(config_path); | ||
return loaded; | ||
} | ||
|
||
bool save_tunnel_status_to_file() { | ||
if(!config_saving_enabled) { | ||
ZITI_LOG(DEBUG, "config_saving_enabled is false. not saving config"); | ||
return true; | ||
} | ||
if(!config_dir) { | ||
// no base_dir indicates using -i not -I | ||
ZITI_LOG(DEBUG, "skipping save_tunnel_status_to_file. config_dir is not set"); | ||
return true; | ||
} | ||
size_t json_len; | ||
char* tunnel_status = get_tunnel_config(&json_len); | ||
bool saved = false; | ||
|
||
if (json_len > 0) { | ||
char* config_path = get_system_config_path(); | ||
|
||
char* config_file_name = get_config_file_name(config_path); | ||
char* bkp_config_file_name = get_backup_config_file_name(config_path); | ||
char* config_path = get_system_config_path(config_dir); | ||
char* config_file_name = get_config_file_name(); | ||
|
||
if (sem_initialized == 0) { | ||
uv_sem_wait(&sem); | ||
} else { | ||
ZITI_LOG(ZITI_WTF, "Could not save the config file [%s] due to semaphore lock not initialized error.", config_file_name); | ||
free(config_file_name); | ||
free(bkp_config_file_name); | ||
free(config_path); | ||
free(tunnel_status); | ||
return saved; | ||
} | ||
//copy config to backup file | ||
int rem = remove(bkp_config_file_name); | ||
if (rem == 0) { | ||
ZITI_LOG(DEBUG, "Deleted backup config file %s", bkp_config_file_name); | ||
} | ||
if (rename(config_file_name, bkp_config_file_name) == 0) { | ||
ZITI_LOG(DEBUG, "Copied config file to backup config file %s", bkp_config_file_name); | ||
} else { | ||
ZITI_LOG(ERROR, "Could not copy config file [%s] to backup config file, the config might not exists at the moment", config_file_name); | ||
} | ||
|
||
// write tunnel status to the config file | ||
FILE* config = fopen(config_file_name, "w"); | ||
|
@@ -155,11 +161,8 @@ bool save_tunnel_status_to_file() { | |
ZITI_LOG(DEBUG, "Saved current tunnel status into Config file %s", config_file_name); | ||
} | ||
uv_sem_post(&sem); | ||
|
||
ZITI_LOG(TRACE, "Cleaning up resources used for the backup of tunnel config file %s", config_file_name); | ||
|
||
free(config_file_name); | ||
free(bkp_config_file_name); | ||
free(config_path); | ||
} | ||
free(tunnel_status); | ||
|
@@ -176,4 +179,15 @@ void cleanup_instance_config() { | |
} else { | ||
ZITI_LOG(ZITI_WTF, "Could not clean instance config. The semaphore is not initialized."); | ||
} | ||
} | ||
|
||
char* get_config_file_name() { | ||
if (config_dir != NULL) { | ||
char* config_file_name = calloc(FILENAME_MAX, sizeof(char)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leak There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bad practice -- since it's sometimes allocated that caller has no idea if it can be freed |
||
snprintf(config_file_name, FILENAME_MAX, "%s%c%s", config_dir, PATH_SEP, DEFAULT_STATE_FILE_NAME); | ||
normalize_identifier(config_file_name); | ||
return config_file_name; | ||
} else { | ||
return DEFAULT_STATE_FILE_NAME; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong place -- should not be in the library code