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