From f4b78a288c9e5c2a7a05d71b9bbe9e5fc26f2a63 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Mon, 30 Mar 2020 11:45:18 +0300 Subject: MTX-3262 mpower lockfile rewrite for libmts-io compatibility --- src/atcmd.c | 12 ++++------ src/utils.c | 77 +++++++++++++++++++++++++++++++++---------------------------- src/utils.h | 8 ++++++- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/src/atcmd.c b/src/atcmd.c index 97fd34b..e85758d 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -1471,7 +1471,7 @@ static int sms_atcmd_init(int fd) return 0; } -static char *lock_path; +static struct Lock *lock_file; int sms_device_close(int fd) { @@ -1479,11 +1479,7 @@ int sms_device_close(int fd) ret = tty_close(fd); - if (lock_path) { - unlink(lock_path); - free(lock_path); - lock_path = NULL; - } + lock_file = device_unlock(lock_file); return ret; } @@ -1498,8 +1494,8 @@ int sms_device_open(void) return -1; } - lock_path = device_lock(Global.core.device); - if (!lock_path) { + lock_file = device_lock(Global.core.device); + if (!lock_file) { sms_device_close(fd); return -1; } diff --git a/src/utils.c b/src/utils.c index b93634c..7da0186 100644 --- a/src/utils.c +++ b/src/utils.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "utils.h" #include "log.h" @@ -248,44 +249,50 @@ char *shell_path_expand(const char *path) #define MAX_TRY 10 -char *device_lock(const char *path) +struct Lock *device_lock(const char *path) { - int tmp; - char *name = basename(path); - int fd; - char str[32]; - char *lock; - unsigned char num_try=0; - - tmp = asprintf(&lock, "/var/lock/LCK..%s", name); - if (tmp < 0) { - log_error("out of memory"); - return NULL; + struct Lock *result = (struct Lock*)calloc(1, sizeof(struct Lock)); + if (result == NULL) { + log_error("out of memory"); + return NULL; + } + + if (asprintf(&result->path, "/var/lock/LCK..%s", basename(path)) < 0) { + log_error("out of memory"); + return NULL; } - do{ - fd = open(lock, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); - if (fd > 0) { - break; - }else{ - log_error("%i number of trying to create %s: %m",num_try+1,lock); - }; - if(num_try++>=MAX_TRY){ - //something locks device too long - log_error("failed to create %s: %m", lock); - free(lock); - return NULL; - } - usleep(100000ul);//100ms - }while(1); - - snprintf(str, sizeof(str), "%10d\n", getpid()); - - full_write(fd, str, strlen(str)); - - close(fd); - - return lock; + result->fd = open(result->path, O_CREAT | O_RDWR, 0644); + if (result->fd >= 0) { + unsigned char num_try = 0; + while (1) { + int lockErr = flock(result->fd, LOCK_EX | LOCK_NB); + if (lockErr == 0) { + return result; + } else if (errno != EWOULDBLOCK) { + break; + } + if (num_try++>=MAX_TRY) { + //something locks device too long + break; + } + usleep(100000ul);//100ms + } + log_error("failed to create %s: %m", result->path); + device_unlock(result); + } + return NULL; +} + +struct Lock *device_unlock(struct Lock *lock) +{ + if (lock) { + close(lock->fd); + unlink(lock->path); + free(lock->path); + free(lock); + } + return NULL; } int indexOfChar(const char *array, int len, char character) diff --git a/src/utils.h b/src/utils.h index 04a71b6..eb9d5bc 100644 --- a/src/utils.h +++ b/src/utils.h @@ -30,12 +30,18 @@ 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); -char *device_lock(const char *path); +struct Lock *device_lock(const char *path); +struct Lock *device_unlock(struct Lock *lock); int indexOfChar(const char *array, int len, char character); -- cgit v1.2.3 From 7568024baa8e6a2488e307e38ade8e7b6a46720f Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Mon, 30 Mar 2020 14:38:28 +0300 Subject: MTX-3262 mpower lockfile: do not unlink lock file --- src/utils.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 7da0186..c48686b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -288,7 +288,6 @@ struct Lock *device_unlock(struct Lock *lock) { if (lock) { close(lock->fd); - unlink(lock->path); free(lock->path); free(lock); } -- cgit v1.2.3 From d48fa68d6207b25061d5276021b1dd25ce5da89d Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Mon, 30 Mar 2020 15:04:21 +0300 Subject: MTX-3262 mpower lockfile: code style --- src/atcmd.c | 2 +- src/utils.c | 58 +++++++++++++++++++++++++++++----------------------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/atcmd.c b/src/atcmd.c index e85758d..eab4210 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -1479,7 +1479,7 @@ int sms_device_close(int fd) ret = tty_close(fd); - lock_file = device_unlock(lock_file); + lock_file = device_unlock(lock_file); return ret; } diff --git a/src/utils.c b/src/utils.c index c48686b..e7d6b69 100644 --- a/src/utils.c +++ b/src/utils.c @@ -251,47 +251,47 @@ char *shell_path_expand(const char *path) 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; - } + struct Lock *result = (struct Lock*)calloc(1, sizeof(struct Lock)); + if (result == NULL) { + log_error("out of memory"); + return NULL; + } if (asprintf(&result->path, "/var/lock/LCK..%s", basename(path)) < 0) { - log_error("out of memory"); - return NULL; + log_error("out of memory"); + return NULL; } - result->fd = open(result->path, O_CREAT | O_RDWR, 0644); - if (result->fd >= 0) { - unsigned char num_try = 0; - while (1) { - int lockErr = flock(result->fd, LOCK_EX | LOCK_NB); - if (lockErr == 0) { - return result; - } else if (errno != EWOULDBLOCK) { - break; - } - if (num_try++>=MAX_TRY) { - //something locks device too long - break; - } - usleep(100000ul);//100ms - } - log_error("failed to create %s: %m", result->path); - device_unlock(result); - } + result->fd = open(result->path, O_CREAT | O_RDWR, 0644); + if (result->fd >= 0) { + unsigned char num_try = 0; + while (1) { + int lockErr = flock(result->fd, LOCK_EX | LOCK_NB); + if (lockErr == 0) { + return result; + } else if (errno != EWOULDBLOCK) { + break; + } + if (num_try++>=MAX_TRY) { + //something locks device too long + break; + } + usleep(100000ul);//100ms + } + log_error("failed to create %s: %m", result->path); + device_unlock(result); + } return NULL; } struct Lock *device_unlock(struct Lock *lock) { if (lock) { - close(lock->fd); + close(lock->fd); free(lock->path); - free(lock); + free(lock); } - return NULL; + return NULL; } int indexOfChar(const char *array, int len, char character) -- cgit v1.2.3