aboutsummaryrefslogtreecommitdiff
path: root/rpc_serial.c
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2016-08-23 12:27:44 -0400
committerPaul Selkirk <paul@psgd.org>2016-08-23 12:31:50 -0400
commitccdb3ab006dd46c125fc0277fa0ce2d3d7660147 (patch)
tree9f58521fff8c38cc2437eb494051ad5058f97387 /rpc_serial.c
parent99b022abb09ffd17fc54b4e479086444f0eeb79f (diff)
parent0166b1b370862ab34335af3d5710304dc3546499 (diff)
Merge branch 'master' of git.cryptech.is:sw/libhal
Diffstat (limited to 'rpc_serial.c')
-rw-r--r--rpc_serial.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/rpc_serial.c b/rpc_serial.c
index 728bbd8..0e0e6ff 100644
--- a/rpc_serial.c
+++ b/rpc_serial.c
@@ -35,6 +35,7 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/file.h>
#include <netinet/in.h>
#include <termios.h>
#include <unistd.h>
@@ -66,11 +67,17 @@ hal_error_t hal_serial_init(const char * const device, const uint32_t speed)
struct termios tty;
speed_t termios_speed;
+ /*
+ * Apparently Linux is too cool to need an atomic mechanism for
+ * locking an existing file, so we can't uses O_EXLOCK. Sigh.
+ */
+
fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
- if (fd == -1) {
- fprintf(stderr, "open %s: ", device);
- return perror(""), HAL_ERROR_RPC_TRANSPORT;
- }
+ if (fd == -1)
+ return perror(device), HAL_ERROR_RPC_TRANSPORT;
+
+ if (flock(fd, LOCK_EX) < 0)
+ return perror(device), HAL_ERROR_RPC_TRANSPORT;
if (tcgetattr (fd, &tty) != 0)
return perror("tcgetattr"), HAL_ERROR_RPC_TRANSPORT;