diff options
-rw-r--r-- | src/utils.c | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/utils.c b/src/utils.c index 8e7bed7..b93634c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -246,6 +246,8 @@ char *shell_path_expand(const char *path) return strdup(line); } +#define MAX_TRY 10 + char *device_lock(const char *path) { int tmp; @@ -253,6 +255,7 @@ char *device_lock(const char *path) int fd; char str[32]; char *lock; + unsigned char num_try=0; tmp = asprintf(&lock, "/var/lock/LCK..%s", name); if (tmp < 0) { @@ -260,12 +263,21 @@ char *device_lock(const char *path) return NULL; } - fd = open(lock, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); - if (fd < 0) { - log_error("failed to create %s: %m", lock); - free(lock); - 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()); |