From 3775f2cc016eefc615c1adc4a22b16ea7b010a91 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Tue, 14 Apr 2020 19:16:13 +0300 Subject: MTX-3262 mpower: fix memory leak --- src/utils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.c b/src/utils.c index e7d6b69..deb36f8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -254,12 +254,12 @@ struct Lock *device_lock(const char *path) struct Lock *result = (struct Lock*)calloc(1, sizeof(struct Lock)); if (result == NULL) { log_error("out of memory"); - return NULL; + return device_unlock(result); } if (asprintf(&result->path, "/var/lock/LCK..%s", basename(path)) < 0) { log_error("out of memory"); - return NULL; + return device_unlock(result); } result->fd = open(result->path, O_CREAT | O_RDWR, 0644); @@ -279,9 +279,8 @@ struct Lock *device_lock(const char *path) usleep(100000ul);//100ms } log_error("failed to create %s: %m", result->path); - device_unlock(result); } - return NULL; + return device_unlock(result); } struct Lock *device_unlock(struct Lock *lock) @@ -289,6 +288,7 @@ struct Lock *device_unlock(struct Lock *lock) if (lock) { close(lock->fd); free(lock->path); + lock->path = NULL; free(lock); } return NULL; -- cgit v1.2.3 From af7b696d3b516aa68995503f642495f3a7082e6d Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Thu, 16 Apr 2020 22:48:28 +0300 Subject: MTX-3262 mpower: fix resources leak --- src/utils.c | 41 ++++++++++++++++------------------------- src/utils.h | 9 ++------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/utils.c b/src/utils.c index deb36f8..a223462 100644 --- a/src/utils.c +++ b/src/utils.c @@ -249,49 +249,40 @@ char *shell_path_expand(const char *path) #define MAX_TRY 10 -struct Lock *device_lock(const char *path) +int device_lock(const char *path) { - struct Lock *result = (struct Lock*)calloc(1, sizeof(struct Lock)); - if (result == NULL) { + char *lock_path; + if (asprintf(&lock_path, "/var/lock/LCK..%s", basename(path)) < 0) { log_error("out of memory"); - return device_unlock(result); - } - - if (asprintf(&result->path, "/var/lock/LCK..%s", basename(path)) < 0) { - log_error("out of memory"); - return device_unlock(result); + return -1; } - result->fd = open(result->path, O_CREAT | O_RDWR, 0644); - if (result->fd >= 0) { + int lock_fd = open(lock_path, O_CREAT | O_RDWR, 0644); + if (lock_fd >= 0) { unsigned char num_try = 0; while (1) { - int lockErr = flock(result->fd, LOCK_EX | LOCK_NB); + int lockErr = flock(lock_fd, LOCK_EX | LOCK_NB); if (lockErr == 0) { - return result; + free(lock_path); + return lock_fd; } else if (errno != EWOULDBLOCK) { break; } if (num_try++>=MAX_TRY) { - //something locks device too long - break; + break; //something locks device too long } usleep(100000ul);//100ms } - log_error("failed to create %s: %m", result->path); + log_error("failed to create %s: %m", lock_path); } - return device_unlock(result); + free(lock_path); + return device_unlock(lock_fd); } -struct Lock *device_unlock(struct Lock *lock) +int device_unlock(int lock) { - if (lock) { - close(lock->fd); - free(lock->path); - lock->path = NULL; - free(lock); - } - return NULL; + close(lock); + return -1; } int indexOfChar(const char *array, int len, char character) diff --git a/src/utils.h b/src/utils.h index eb9d5bc..62d76f1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -30,18 +30,13 @@ enum { USER_YESNO_YES = 1, }; -struct Lock { - char *path; - int fd; -}; - int user_yesno(int def, const char *fmt, ...); int systemf(const char *fmt, ...); FILE *popenf(const char *mode, const char *fmt, ...); char *shell_path_expand(const char *path); -struct Lock *device_lock(const char *path); -struct Lock *device_unlock(struct Lock *lock); +int device_lock(const char *path); +int device_unlock(int lock); int indexOfChar(const char *array, int len, char character); -- cgit v1.2.3 From 637610f24cb0f05ebe0edc7ab0c0be37ede2aee7 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Fri, 17 Apr 2020 09:57:13 +0300 Subject: MTX-3262 mpower: fix lock return value --- src/atcmd.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/atcmd.c b/src/atcmd.c index eab4210..1aa415c 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -1471,7 +1471,7 @@ static int sms_atcmd_init(int fd) return 0; } -static struct Lock *lock_file; +static int lock_file; int sms_device_close(int fd) { @@ -1495,7 +1495,7 @@ int sms_device_open(void) } lock_file = device_lock(Global.core.device); - if (!lock_file) { + if (lock_file < 0) { sms_device_close(fd); return -1; } -- cgit v1.2.3 From fc75f3820e0769d86fcb5ed48ad719eb7a969f63 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Fri, 17 Apr 2020 10:47:52 +0300 Subject: MTX-3262 mpower: restored log output --- src/utils.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils.c b/src/utils.c index a223462..8facbdf 100644 --- a/src/utils.c +++ b/src/utils.c @@ -268,6 +268,7 @@ int device_lock(const char *path) } else if (errno != EWOULDBLOCK) { break; } + log_error("%i number of trying to create %s: %m", num_try+1, lock_path); if (num_try++>=MAX_TRY) { break; //something locks device too long } -- cgit v1.2.3 From ee1965a9d8dc206a3b712071058fe0f7f5aee759 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Fri, 17 Apr 2020 11:29:28 +0300 Subject: MTX-3262 mpower: fix log output --- src/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 8facbdf..138c87d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -274,8 +274,8 @@ int device_lock(const char *path) } usleep(100000ul);//100ms } - log_error("failed to create %s: %m", lock_path); } + log_error("failed to make lock %s: %m", lock_path); free(lock_path); return device_unlock(lock_fd); } -- cgit v1.2.3