summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/atcmd.c27
-rw-r--r--src/atcmd.h1
-rw-r--r--src/sms_delete.c2
-rw-r--r--src/sms_list.c2
-rw-r--r--src/sms_main.c4
-rw-r--r--src/sms_send.c2
-rw-r--r--src/utils.c41
-rw-r--r--src/utils.h3
8 files changed, 77 insertions, 5 deletions
diff --git a/src/atcmd.c b/src/atcmd.c
index 55d2ebc..6dee9ed 100644
--- a/src/atcmd.c
+++ b/src/atcmd.c
@@ -267,6 +267,10 @@ int tty_set_read_timeout(int fd, int timeout)
int tty_close(int fd)
{
+ if (fd < 0) {
+ return -1;
+ }
+
tcflush(fd, TCIOFLUSH);
return close(fd);
}
@@ -1113,19 +1117,41 @@ static int sms_atcmd_init(int fd)
return 0;
}
+static char *lock_path;
+
+int sms_device_close(int fd)
+{
+ int ret;
+
+ ret = tty_close(fd);
+
+ if (lock_path) {
+ unlink(lock_path);
+ }
+
+ return ret;
+}
+
int sms_device_open(void)
{
int fd;
int tmp;
+ lock_path = device_lock(Global.core.device);
+ if (!lock_path) {
+ return -1;
+ }
+
fd = tty_open(Global.core.device, Global.core.baud_rate);
if (fd < 0) {
+ unlink(lock_path);
return -1;
}
tmp = tty_set_read_timeout(fd, Global.core.read_timeout);
if (tmp < 0) {
close(fd);
+ unlink(lock_path);
return tmp;
}
@@ -1133,6 +1159,7 @@ int sms_device_open(void)
tmp = sms_atcmd_init(fd);
if (tmp < 0) {
close(fd);
+ unlink(lock_path);
return tmp;
}
}
diff --git a/src/atcmd.h b/src/atcmd.h
index 8fbd240..d9a53a0 100644
--- a/src/atcmd.h
+++ b/src/atcmd.h
@@ -108,6 +108,7 @@ int atcmd_plus_cpbs_write(int fd, const char *name);
int atcmd_plus_cpbr_test(int fd, struct phonebook_store *store);
int atcmd_init(int fd, int read_timeout);
+int sms_device_close(int fd);
int sms_device_open(void);
#if __ATCMD_C
diff --git a/src/sms_delete.c b/src/sms_delete.c
index aee01dd..b45546e 100644
--- a/src/sms_delete.c
+++ b/src/sms_delete.c
@@ -167,7 +167,7 @@ int sms_delete(int argc, char **argv)
ret = do_delete(fd, argc, argv);
- tty_close(fd);
+ sms_device_close(fd);
return ret;
}
diff --git a/src/sms_list.c b/src/sms_list.c
index 4e815fc..0ccec15 100644
--- a/src/sms_list.c
+++ b/src/sms_list.c
@@ -488,7 +488,7 @@ int sms_list(int argc, char **argv)
ret = do_list(fd, cmd_type, argc, argv);
- tty_close(fd);
+ sms_device_close(fd);
return ret;
}
diff --git a/src/sms_main.c b/src/sms_main.c
index f319019..d1155b8 100644
--- a/src/sms_main.c
+++ b/src/sms_main.c
@@ -59,7 +59,7 @@ static int pb_list(int argc, char **argv)
ret = print_phonebook_list(fd, Global.core.pb_store);
- tty_close(fd);
+ sms_device_close(fd);
return ret;
}
@@ -75,7 +75,7 @@ static int sms_init(int argc, char **argv)
return false;
}
- tty_close(fd);
+ sms_device_close(fd);
return true;
}
diff --git a/src/sms_send.c b/src/sms_send.c
index 7099c06..abc642f 100644
--- a/src/sms_send.c
+++ b/src/sms_send.c
@@ -378,7 +378,7 @@ int sms_send(int argc, char **argv)
ret = do_send(fd, &options, argc, argv);
- tty_close(fd);
+ sms_device_close(fd);
return ret;
}
diff --git a/src/utils.c b/src/utils.c
index 9149319..a794f9b 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -30,12 +30,23 @@
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <fcntl.h>
#include <string.h>
#include <linux/limits.h>
#include "utils.h"
#include "log.h"
+char *basename(const char *path)
+{
+ const char *cp = strrchr(path, '/');
+ if(cp) {
+ return (char *) cp + 1;
+ } else {
+ return (char *) path;
+ }
+}
+
int tokcmp(const char *cmd, const char *pattern)
{
int len = strlen(cmd);
@@ -234,3 +245,33 @@ char *shell_path_expand(const char *path)
return strdup(line);
}
+
+char *device_lock(const char *path)
+{
+ int tmp;
+ char *name = basename(path);
+ int fd;
+ char str[32];
+ char *lock;
+
+ tmp = asprintf(&lock, "/var/lock/LCK..%s", name);
+ if (tmp < 0) {
+ log_error("out of memory");
+ 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;
+ }
+
+ snprintf(str, sizeof(str), "%10d\n", getpid());
+
+ full_write(fd, str, strlen(str));
+
+ close(fd);
+
+ return lock;
+}
diff --git a/src/utils.h b/src/utils.h
index 3254b3c..79b04ae 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -16,6 +16,7 @@ enum {
true = 1,
};
+char *basename(const char *path);
int tokcmp(const char *cmd, const char *pattern);
char *strrstrip(char *str);
@@ -34,6 +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);
+
#define YAML_INDENT 2
#define indentf(n, format, args...) \