summaryrefslogtreecommitdiff
path: root/libgammu
diff options
context:
space:
mode:
authorMichal Čihař <nijel@debian.org>2015-08-20 11:45:11 +0200
committerMichal Čihař <nijel@debian.org>2015-08-20 11:45:11 +0200
commit7bc932d13fe000a6b895efcbf54105970e1773d0 (patch)
tree987c521a721ef3f051ddb4955634064e6f765d8f /libgammu
parentf435125b5219e4b6140534304115605279bf7ea4 (diff)
Imported Upstream version 1.36.4
Diffstat (limited to 'libgammu')
-rw-r--r--libgammu/device/serial/ser_unx.c16
-rw-r--r--libgammu/gsmstate.c3
2 files changed, 17 insertions, 2 deletions
diff --git a/libgammu/device/serial/ser_unx.c b/libgammu/device/serial/ser_unx.c
index 300639b..6755bf1 100644
--- a/libgammu/device/serial/ser_unx.c
+++ b/libgammu/device/serial/ser_unx.c
@@ -26,6 +26,8 @@
#include <termios.h>
#include <errno.h>
#include <assert.h>
+#include <unistd.h>
+#include <fcntl.h>
#ifdef HAVE_I_SETSIG
#include <stropts.h>
#endif
@@ -38,8 +40,6 @@
#endif
#ifdef __NetBSD__
-# define FNONBLOCK O_NONBLOCK
-
#ifndef B57600
# define B57600 0010001
#endif
@@ -158,6 +158,9 @@ static GSM_Error serial_close(GSM_StateMachine *s)
/* Restores old settings */
tcsetattr(d->hPhone, TCSANOW, &d->old_settings);
+ /* Remove advisory lock */
+ flock(d->hPhone, LOCK_UN);
+
/* Closes device */
close(d->hPhone);
@@ -188,6 +191,7 @@ static GSM_Error serial_open (GSM_StateMachine *s)
#ifdef TIOCEXCL
/* open() calls from other applications shall fail now */
+ /* this works only with CAP_SYS_ADMIN on Linux though */
ioctl(d->hPhone, TIOCEXCL, (char *) 0);
#endif
#ifdef HAVE_I_SETSIG
@@ -195,6 +199,14 @@ static GSM_Error serial_open (GSM_StateMachine *s)
ioctl(d->hPhone, I_SETSIG, (char *) 0);
#endif
+ /* Try advisory locks */
+ if (flock(d->hPhone, LOCK_EX | LOCK_NB) != 0) {
+ if (errno == EWOULDBLOCK) {
+ GSM_OSErrorInfo(s, "failed to lock device, probably opened by other process");
+ return ERR_DEVICEOPENERROR;
+ }
+ }
+
if (tcgetattr(d->hPhone, &d->old_settings) == -1) {
close(d->hPhone);
GSM_OSErrorInfo(s,"tcgetattr in serial_open");
diff --git a/libgammu/gsmstate.c b/libgammu/gsmstate.c
index 2d842fa..2742eaa 100644
--- a/libgammu/gsmstate.c
+++ b/libgammu/gsmstate.c
@@ -1627,6 +1627,9 @@ GSM_StateMachine *GSM_AllocStateMachine(void)
{
GSM_StateMachine *ret;
ret = (GSM_StateMachine *)calloc(1, sizeof(GSM_StateMachine));
+ if (ret == NULL) {
+ return ret;
+ }
ret->CurrentConfig = &(ret->Config[0]);
ret->Abort = FALSE;
return ret;