From 41abe2c0e76ede64a043f710f217caaca16763b6 Mon Sep 17 00:00:00 2001 From: Jeff Hatch Date: Thu, 23 Jun 2016 12:31:51 -0500 Subject: Add retry code for getting the lock on the modem tty, tries every 100ms for one second before giving up. --- src/utils.c | 24 ++++++++++++++++++------ 1 file 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()); -- cgit v1.2.3