diff options
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 58 |
1 files changed, 29 insertions, 29 deletions
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) |