summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>2022-02-11 09:02:32 +0100
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>2022-02-11 09:02:32 +0100
commit130ca47b4d32863d65b677ea7cac2bbebf1ee5ec (patch)
tree930c4f56ebdd99fe709322fb71f5221d6698114f
parent0ccdf91d61e0cbff3e9686d19d7929f0aeda00e3 (diff)
parent34e7fe5296e9228b042f07551a9bebf8d95b3ac9 (diff)
downloadmicrocom-130ca47b4d32863d65b677ea7cac2bbebf1ee5ec.tar.gz
microcom-130ca47b4d32863d65b677ea7cac2bbebf1ee5ec.tar.xz
Merge branch 'flock' of https://github.com/lynxeye-dev/microcom into HEAD
While some tools still use uucp style locking (e.g. minicom) there isn't much much breakage exprected by dropping it. This only hurts if microcom (or another terminal program that relies on flock() only (e.g. picocom)) and a uucp-locking program are used on the same device, which should be rare. Also swiching completely away from uucp (instead of doing both uucp and flock() for some time) might give some incentive to other terminal programs to do the same move and maybe eventually get rid of uucp-locking completely. Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
-rw-r--r--serial.c69
1 files changed, 11 insertions, 58 deletions
diff --git a/serial.c b/serial.c
index 4039d79..07be8ed 100644
--- a/serial.c
+++ b/serial.c
@@ -21,13 +21,13 @@
****************************************************************************/
#include <limits.h>
+#include <sys/file.h>
#include <sys/ioctl.h>
#include <arpa/telnet.h>
#include "microcom.h"
static struct termios pots; /* old port termios settings to restore */
-static char *lockfile;
static void init_comm(struct termios *pts)
{
@@ -134,39 +134,23 @@ static int serial_send_break(struct ios_ops *ios)
return 0;
}
-/* unlink the lockfile */
-static void serial_unlock()
-{
- if (lockfile)
- unlink(lockfile);
-}
-
/* restore original terminal settings on exit */
static void serial_exit(struct ios_ops *ios)
{
tcsetattr(ios->fd, TCSANOW, &pots);
close(ios->fd);
free(ios);
- serial_unlock();
}
-#define BUFLEN 512
-
struct ios_ops * serial_init(char *device)
{
struct termios pts; /* termios settings on port */
struct ios_ops *ops;
- int fd;
- char *substring;
- long pid;
- int ret;
+ int fd, ret;
ops = malloc(sizeof(*ops));
if (!ops)
return NULL;
- lockfile = malloc(BUFLEN);
- if (!lockfile)
- return NULL;
ops->write = serial_write;
ops->read = serial_read;
@@ -177,51 +161,20 @@ struct ios_ops * serial_init(char *device)
ops->exit = serial_exit;
ops->istelnet = false;
- /* check lockfile */
- substring = strrchr(device, '/');
- if (substring)
- substring++;
- else
- substring = device;
-
- ret = snprintf(lockfile, BUFLEN, "/var/lock/LCK..%s", substring);
- if (ret >= BUFLEN) {
- printf("path to lockfile too long\n");
- exit(1);
- }
-
- fd = open(lockfile, O_RDONLY);
- if (fd >= 0 && !opt_force) {
- close(fd);
- main_usage(3, "lockfile for port exists", device);
- }
-
- if (fd >= 0 && opt_force) {
- close(fd);
- printf("lockfile for port exists, ignoring\n");
- serial_unlock();
- }
-
- fd = open(lockfile, O_RDWR | O_CREAT, 0444);
- if (fd < 0 && opt_force) {
- printf("cannot create lockfile. ignoring\n");
- lockfile = NULL;
- goto force;
- }
- if (fd < 0)
- main_usage(3, "cannot create lockfile", device);
- /* Kermit wants binary pid */
- pid = getpid();
- write(fd, &pid, sizeof(long));
- close(fd);
-force:
/* open the device */
fd = open(device, O_RDWR | O_NONBLOCK);
ops->fd = fd;
- if (fd < 0) {
- serial_unlock();
+ if (fd < 0)
main_usage(2, "cannot open device", device);
+
+ /* try to lock the device */
+ ret = flock(fd, LOCK_EX | LOCK_NB);
+ if (ret) {
+ if (!opt_force)
+ main_usage(3, "could not lock port", device);
+ else
+ printf("could not lock port, ignoring\n");
}
/* modify the port configuration */