From 72d619666d013b24fa6aa017a1cef949ab1e5b4f Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 14:54:49 +0800 Subject: [PATCH 1/6] * MDF [platform.h] add missing nng_timestamp declareration Signed-off-by: jaylin --- include/nng/supplemental/util/platform.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/nng/supplemental/util/platform.h b/include/nng/supplemental/util/platform.h index 330824c99..c20ceee9e 100644 --- a/include/nng/supplemental/util/platform.h +++ b/include/nng/supplemental/util/platform.h @@ -29,6 +29,8 @@ extern "C" { #endif +// Return unix timestamp (milliseconds) . +NNG_DECL nng_time nng_timestamp(void); // Get current process Id. NNG_DECL int nng_getpid(); From 7276fbe59fc52f4e5f21f472aebfd536348cdb37 Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 14:55:33 +0800 Subject: [PATCH 2/6] * NEW [nng] add nng_access func Signed-off-by: jaylin --- include/nng/nng.h | 4 ++++ src/nng.c | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/nng/nng.h b/include/nng/nng.h index 2730c39ec..4a6d8190f 100644 --- a/include/nng/nng.h +++ b/include/nng/nng.h @@ -1344,6 +1344,10 @@ NNG_DECL bool nng_lmq_empty(nng_lmq *); typedef struct conn_param conn_param; typedef struct pub_packet_struct pub_packet_struct; typedef struct pipe_db nano_pipe_db; + +NNG_DECL int nng_access(const char* name, int flag); + +// NANOMQ MQTT API ends // UDP operations. These are provided for convenience, // and should be considered somewhat experimental. diff --git a/src/nng.c b/src/nng.c index ceb233ada..0029ec284 100644 --- a/src/nng.c +++ b/src/nng.c @@ -2478,7 +2478,7 @@ conn_param_set_username(conn_param *cparam, const char *username) void conn_param_set_password(conn_param *cparam, const char *password) { - cparam->password.body = nng_strdup(password); + cparam->password.body = (uint8_t *)nng_strdup(password); cparam->password.len = strlen(password) + 1; } @@ -2555,6 +2555,16 @@ nng_file_is_dir(const char *path) return nni_file_is_dir(path); } +int nng_access(const char* name, int flag) +{ +#ifdef NNG_PLATFORM_WINDOWS + return _access(name, flag); +#else + return access(name, flag); +#endif +} +// Ends of NANOMQ API + void nng_init_set_parameter(nng_init_parameter p, uint64_t value) { From 34393804b00b5a35413d39790e32c3bdb061a2ab Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 14:55:47 +0800 Subject: [PATCH 3/6] * MDF [log] check disk authority each time of writting Signed-off-by: jaylin --- src/supplemental/nanolib/log.c | 46 ++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/supplemental/nanolib/log.c b/src/supplemental/nanolib/log.c index ba2222678..1f3202145 100644 --- a/src/supplemental/nanolib/log.c +++ b/src/supplemental/nanolib/log.c @@ -98,6 +98,18 @@ file_callback(log_event *ev) #else pid_t pid = syscall(__NR_gettid); #endif +#ifndef NANO_PLATFORM_WINDOWS + if (nng_access(ev->config->dir, W_OK) < 0) { + fprintf(stderr, "open path %s failed! close file!\n", + ev->config->dir); + if (ev->config->fp != NULL) + fclose(ev->config->fp); + ev->config->fp = NULL; + return; + } +#endif + if (ev->config->fp == NULL) + ev->config->fp = fopen(ev->config->abs_path, "a"); FILE *fp = ev->config->fp; buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &ev->time)] = '\0'; fprintf(fp, "%s [%i] %-5s %s:%d: ", buf, pid, @@ -300,8 +312,7 @@ log_clear_callback() static void file_rotation(FILE *fp, conf_log *config) { - // Note : do not call log_xxx() in this function, it will cause dead - // lock + // Note : do not call log_xxx() in this function, it will cause dead lock size_t sz = 0; int rv; if ((rv = nni_plat_file_size(config->abs_path, &sz)) != 0) { @@ -309,10 +320,23 @@ file_rotation(FILE *fp, conf_log *config) config->abs_path, nng_strerror(rv)); if (!nni_plat_file_exists(config->abs_path)) { // file missing, recreate one - fclose(fp); - if (nng_file_put(config->abs_path, "\n", 1) != 0) - fprintf(stderr, "create file %s failed: %s\n", + if (fp) + fclose(fp); +#ifndef NANO_PLATFORM_WINDOWS + if (nng_access(config->dir, W_OK) < 0) { + fprintf(stderr, "open path %s failed\n", + config->dir); + config->fp = NULL; + return; + } +#endif + if (nng_file_put(config->abs_path, "\n", 1) != 0) { + fprintf(stderr, "write to file %s failed: %s\n", config->abs_path, nng_strerror(rv)); + config->fp = NULL; + return; + } + config->fp = fopen(config->abs_path, "a"); fp = config->fp; } @@ -342,13 +366,21 @@ file_rotation(FILE *fp, conf_log *config) log_name, log_name_len, "%s.%lu", config->file, index); char *backup_log_path = nano_concat_path(config->dir, log_name); - fclose(fp); + if (fp) + fclose(fp); fp = NULL; remove(backup_log_path); rename(config->abs_path, backup_log_path); nni_free(log_name, log_name_len); nni_strfree(backup_log_path); - +#ifndef NANO_PLATFORM_WINDOWS + if (nng_access(config->dir, W_OK) < 0) { + fprintf(stderr, "open path %s failed\n", + config->dir); + config->fp = NULL; + return; + } +#endif fp = fopen(config->abs_path, "a"); config->fp = fp; char num[20] = { 0 }; From 3e56f56cb78efb47bfdd47b7b0a631c90b1b8fee Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 15:29:46 +0800 Subject: [PATCH 4/6] * MDF [platform] make access platform compatible Signed-off-by: jaylin --- src/core/platform.h | 2 ++ src/nng.c | 6 +----- src/platform/posix/posix_file.c | 4 ++++ src/platform/windows/win_file.c | 4 ++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/platform.h b/src/core/platform.h index 4225d612b..57dc94748 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -582,6 +582,8 @@ extern int nni_plat_getpid(void); // nni_plat_file_size get file size. extern int nni_plat_file_size(const char *, size_t *); +// nni_access check accessibility of path +extern int nni_plat_access(const char* name, int flag); // // Actual platforms we support. This is included up front so that we can // get the specific types that are supplied by the platform. diff --git a/src/nng.c b/src/nng.c index 0029ec284..43c7a469d 100644 --- a/src/nng.c +++ b/src/nng.c @@ -2557,11 +2557,7 @@ nng_file_is_dir(const char *path) int nng_access(const char* name, int flag) { -#ifdef NNG_PLATFORM_WINDOWS - return _access(name, flag); -#else - return access(name, flag); -#endif + return nni_plat_access(name, flag); } // Ends of NANOMQ API diff --git a/src/platform/posix/posix_file.c b/src/platform/posix/posix_file.c index 29b495997..95f77781c 100644 --- a/src/platform/posix/posix_file.c +++ b/src/platform/posix/posix_file.c @@ -356,4 +356,8 @@ nni_plat_file_size(const char *path, size_t *size) return (0); } +int nni_plat_access(const char* name, int flag) +{ + return access(name, flag); +} #endif // NNG_PLATFORM_POSIX diff --git a/src/platform/windows/win_file.c b/src/platform/windows/win_file.c index 98f64bfbf..f3b6eb02a 100644 --- a/src/platform/windows/win_file.c +++ b/src/platform/windows/win_file.c @@ -388,4 +388,8 @@ nni_plat_file_size(const char *path, size_t *size) return (rv); } +int nni_plat_access(const char* name, int flag) +{ + return _access(name, flag); +} #endif // NNG_PLATFORM_WINDOWS From f5ada64038fca0fb74c7583f702106f166dc7689 Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 15:41:21 +0800 Subject: [PATCH 5/6] * MDF [log] use correct MACRO NNG_PLATFORM_WINDOWS Signed-off-by: jaylin --- src/supplemental/mqtt/mqtt_public.c | 6 ++++-- src/supplemental/nanolib/log.c | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/supplemental/mqtt/mqtt_public.c b/src/supplemental/mqtt/mqtt_public.c index 326d3e2f2..99519429f 100644 --- a/src/supplemental/mqtt/mqtt_public.c +++ b/src/supplemental/mqtt/mqtt_public.c @@ -725,7 +725,8 @@ mqtt_property_free(property *prop) void mqtt_property_foreach(property *prop, void (*cb)(property *)) { - return property_foreach(prop, cb); + property_foreach(prop, cb); + return; } int @@ -809,7 +810,8 @@ mqtt_property_get_value(property *prop, uint8_t prop_id) void mqtt_property_append(property *prop_list, property *last) { - return property_append(prop_list, last); + property_append(prop_list, last); + return; } diff --git a/src/supplemental/nanolib/log.c b/src/supplemental/nanolib/log.c index 1f3202145..8abc855ff 100644 --- a/src/supplemental/nanolib/log.c +++ b/src/supplemental/nanolib/log.c @@ -98,7 +98,7 @@ file_callback(log_event *ev) #else pid_t pid = syscall(__NR_gettid); #endif -#ifndef NANO_PLATFORM_WINDOWS +#ifndef NNG_PLATFORM_WINDOWS if (nng_access(ev->config->dir, W_OK) < 0) { fprintf(stderr, "open path %s failed! close file!\n", ev->config->dir); @@ -322,7 +322,7 @@ file_rotation(FILE *fp, conf_log *config) // file missing, recreate one if (fp) fclose(fp); -#ifndef NANO_PLATFORM_WINDOWS +#ifndef NNG_PLATFORM_WINDOWS if (nng_access(config->dir, W_OK) < 0) { fprintf(stderr, "open path %s failed\n", config->dir); @@ -373,7 +373,7 @@ file_rotation(FILE *fp, conf_log *config) rename(config->abs_path, backup_log_path); nni_free(log_name, log_name_len); nni_strfree(backup_log_path); -#ifndef NANO_PLATFORM_WINDOWS +#ifndef NNG_PLATFORM_WINDOWS if (nng_access(config->dir, W_OK) < 0) { fprintf(stderr, "open path %s failed\n", config->dir); From 0201ff374330779d5df725a6779e031c69ce775a Mon Sep 17 00:00:00 2001 From: jaylin Date: Tue, 13 Aug 2024 16:13:15 +0800 Subject: [PATCH 6/6] * MDF [mqtt_publich] avoid TSAN warning by finish App AIO sync. Signed-off-by: jaylin --- src/supplemental/mqtt/mqtt_msg.c | 2 +- src/supplemental/mqtt/mqtt_public.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/supplemental/mqtt/mqtt_msg.c b/src/supplemental/mqtt/mqtt_msg.c index a1dccb435..f630f3d95 100644 --- a/src/supplemental/mqtt/mqtt_msg.c +++ b/src/supplemental/mqtt/mqtt_msg.c @@ -1024,7 +1024,7 @@ mqtt_close_unack_aio_cb(void *key, void *val) nni_aio * aio = val; if (aio) { - nni_aio_finish_error(aio, NNG_ECLOSED); + nni_aio_finish_sync(aio, NNG_ECLOSED, 0); nni_msg_free(nni_aio_get_msg(aio)); nni_aio_set_msg(aio, NULL); nni_aio_set_prov_data(aio, NULL); diff --git a/src/supplemental/mqtt/mqtt_public.c b/src/supplemental/mqtt/mqtt_public.c index 99519429f..4655bb32f 100644 --- a/src/supplemental/mqtt/mqtt_public.c +++ b/src/supplemental/mqtt/mqtt_public.c @@ -841,11 +841,14 @@ nng_mqtt_client_send_cb(void* arg) { nng_mqtt_client *client = (nng_mqtt_client *) arg; nng_aio * aio = client->send_aio; - nng_msg * msg = nng_aio_get_msg(aio); nng_msg * tmsg = NULL; nni_lmq * lmq = (nni_lmq *)client->msgq; + // in case of data conention while fini pipes + if (nng_aio_result(aio) == NNG_ECLOSED) + return; + nng_msg * msg = nng_aio_get_msg(aio); if (msg == NULL || nng_aio_result(aio) != 0) { client->cb(client, NULL, client->obj); return;