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/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(); 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 ceb233ada..43c7a469d 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,12 @@ nng_file_is_dir(const char *path) return nni_file_is_dir(path); } +int nng_access(const char* name, int flag) +{ + return nni_plat_access(name, flag); +} +// Ends of NANOMQ API + void nng_init_set_parameter(nng_init_parameter p, uint64_t value) { 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 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 326d3e2f2..4655bb32f 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; } @@ -839,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; diff --git a/src/supplemental/nanolib/log.c b/src/supplemental/nanolib/log.c index ba2222678..8abc855ff 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 NNG_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 NNG_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 NNG_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 };