Skip to content

Commit 130ca47

Browse files
author
Uwe Kleine-König
committed
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>
2 parents 0ccdf91 + 34e7fe5 commit 130ca47

File tree

1 file changed

+11
-58
lines changed

1 file changed

+11
-58
lines changed

serial.c

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
****************************************************************************/
2222

2323
#include <limits.h>
24+
#include <sys/file.h>
2425
#include <sys/ioctl.h>
2526
#include <arpa/telnet.h>
2627

2728
#include "microcom.h"
2829

2930
static struct termios pots; /* old port termios settings to restore */
30-
static char *lockfile;
3131

3232
static void init_comm(struct termios *pts)
3333
{
@@ -134,39 +134,23 @@ static int serial_send_break(struct ios_ops *ios)
134134
return 0;
135135
}
136136

137-
/* unlink the lockfile */
138-
static void serial_unlock()
139-
{
140-
if (lockfile)
141-
unlink(lockfile);
142-
}
143-
144137
/* restore original terminal settings on exit */
145138
static void serial_exit(struct ios_ops *ios)
146139
{
147140
tcsetattr(ios->fd, TCSANOW, &pots);
148141
close(ios->fd);
149142
free(ios);
150-
serial_unlock();
151143
}
152144

153-
#define BUFLEN 512
154-
155145
struct ios_ops * serial_init(char *device)
156146
{
157147
struct termios pts; /* termios settings on port */
158148
struct ios_ops *ops;
159-
int fd;
160-
char *substring;
161-
long pid;
162-
int ret;
149+
int fd, ret;
163150

164151
ops = malloc(sizeof(*ops));
165152
if (!ops)
166153
return NULL;
167-
lockfile = malloc(BUFLEN);
168-
if (!lockfile)
169-
return NULL;
170154

171155
ops->write = serial_write;
172156
ops->read = serial_read;
@@ -177,51 +161,20 @@ struct ios_ops * serial_init(char *device)
177161
ops->exit = serial_exit;
178162
ops->istelnet = false;
179163

180-
/* check lockfile */
181-
substring = strrchr(device, '/');
182-
if (substring)
183-
substring++;
184-
else
185-
substring = device;
186-
187-
ret = snprintf(lockfile, BUFLEN, "/var/lock/LCK..%s", substring);
188-
if (ret >= BUFLEN) {
189-
printf("path to lockfile too long\n");
190-
exit(1);
191-
}
192-
193-
fd = open(lockfile, O_RDONLY);
194-
if (fd >= 0 && !opt_force) {
195-
close(fd);
196-
main_usage(3, "lockfile for port exists", device);
197-
}
198-
199-
if (fd >= 0 && opt_force) {
200-
close(fd);
201-
printf("lockfile for port exists, ignoring\n");
202-
serial_unlock();
203-
}
204-
205-
fd = open(lockfile, O_RDWR | O_CREAT, 0444);
206-
if (fd < 0 && opt_force) {
207-
printf("cannot create lockfile. ignoring\n");
208-
lockfile = NULL;
209-
goto force;
210-
}
211-
if (fd < 0)
212-
main_usage(3, "cannot create lockfile", device);
213-
/* Kermit wants binary pid */
214-
pid = getpid();
215-
write(fd, &pid, sizeof(long));
216-
close(fd);
217-
force:
218164
/* open the device */
219165
fd = open(device, O_RDWR | O_NONBLOCK);
220166
ops->fd = fd;
221167

222-
if (fd < 0) {
223-
serial_unlock();
168+
if (fd < 0)
224169
main_usage(2, "cannot open device", device);
170+
171+
/* try to lock the device */
172+
ret = flock(fd, LOCK_EX | LOCK_NB);
173+
if (ret) {
174+
if (!opt_force)
175+
main_usage(3, "could not lock port", device);
176+
else
177+
printf("could not lock port, ignoring\n");
225178
}
226179

227180
/* modify the port configuration */

0 commit comments

Comments
 (0)