summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Hatch <jhatch@multitech.com>2020-06-10 08:14:39 -0500
committerJeff Hatch <jhatch@multitech.com>2020-06-10 08:14:39 -0500
commit10ccba808dab5a17b06456793e83d53501e39dc8 (patch)
treed238a79ee805657b7c00271d639b7cc867ff863e
parent203bf8164ae731682b6a524f8c961b6e2de96504 (diff)
parent84811012a52cc276e59038d5c85bf5d66425286a (diff)
downloadsms-utils-10ccba808dab5a17b06456793e83d53501e39dc8.tar.gz
sms-utils-10ccba808dab5a17b06456793e83d53501e39dc8.tar.bz2
sms-utils-10ccba808dab5a17b06456793e83d53501e39dc8.zip
Merge branch 'MTX-3262-single-instance-guard' into 'master'
Mtx 3262 single instance guard See merge request !8
-rw-r--r--src/atcmd.c8
-rw-r--r--src/utils.c66
-rw-r--r--src/utils.h5
3 files changed, 38 insertions, 41 deletions
diff --git a/src/atcmd.c b/src/atcmd.c
index 97fd34b..76ee98c 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 int lock_file;
int sms_device_close(int fd)
{
@@ -1498,8 +1498,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 < 0) {
sms_device_close(fd);
return -1;
}
@@ -1570,4 +1570,4 @@ int is_quectel_model()
}
return 0;
-}
+} \ No newline at end of file
diff --git a/src/utils.c b/src/utils.c
index b93634c..5d46a36 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -248,44 +248,41 @@ char *shell_path_expand(const char *path)
#define MAX_TRY 10
-char *device_lock(const char *path)
+int 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) {
+ char *lock_path;
+ if (asprintf(&lock_path, "/var/lock/LCK..%s", basename(path)) < 0) {
log_error("out of memory");
- return NULL;
+ return -1;
}
- 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;
+ int lock_fd = open(lock_path, O_CREAT | O_RDWR, 0644);
+ if (lock_fd >= 0) {
+ unsigned char num_try = 0;
+ while (1) {
+ int lockErr = flock(lock_fd, LOCK_EX | LOCK_NB);
+ if (lockErr == 0) {
+ free(lock_path);
+ return lock_fd;
+ } else if (errno != EWOULDBLOCK) {
+ break;
+ }
+ log_error("%i number of trying to create %s: %m", num_try+1, lock_path);
+ if (num_try++>=MAX_TRY) {
+ break; //something locks device too long
+ }
+ usleep(100000ul);//100ms
+ }
+ }
+ log_error("failed to make lock %s: %m", lock_path);
+ free(lock_path);
+ return device_unlock(lock_fd);
+}
+
+int device_unlock(int lock)
+{
+ close(lock);
+ return -1;
}
int indexOfChar(const char *array, int len, char character)
@@ -299,4 +296,3 @@ int indexOfChar(const char *array, int len, char character)
}
return -1;
}
-
diff --git a/src/utils.h b/src/utils.h
index 04a71b6..32b4c89 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -35,7 +35,8 @@ 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);
+int device_lock(const char *path);
+int device_unlock(int lock);
int indexOfChar(const char *array, int len, char character);
@@ -44,4 +45,4 @@ int indexOfChar(const char *array, int len, char character);
#define indentf(n, format, args...) \
printf("%*s" format , n, "" , ## args)
-#endif /* ~__UTILS_H */
+#endif /* ~__UTILS_H */ \ No newline at end of file