View | Details | Raw Unified | Return to bug 262738
Collapse All | Expand All

(-)lockf/Makefile (-1 / +1 lines)
Lines 1-5 Link Here
1
# $FreeBSD: stable/12/usr.bin/lockf/Makefile 90415 2002-02-08 22:31:43Z markm $
1
# $FreeBSD: stable/12/usr.bin/lockf/Makefile 90415 2002-02-08 22:31:43Z markm $
2
2
3
PROG=	lockf
3
PROG=	lockf
4
4
CFLAGS+=-g
5
.include <bsd.prog.mk>
5
.include <bsd.prog.mk>
(-)lockf/lockf.1 (-4 / +40 lines)
Lines 29-47 Link Here
29
.Os
29
.Os
30
.Sh NAME
30
.Sh NAME
31
.Nm lockf
31
.Nm lockf
32
.Nd execute a command while holding a file lock
32
.Nd execute a command while holding a file/fd lock
33
.Sh SYNOPSIS
33
.Sh SYNOPSIS
34
.Nm
34
.Nm
35
.Op Fl kns
35
.Op Fl kns
36
.Op Fl t Ar seconds
36
.Op Fl t Ar seconds
37
.Ar file
37
.Ar file|fd
38
.Ar command
38
.Ar command
39
.Op Ar arguments
39
.Op Ar arguments
40
.Nm
41
.Op Fl kns
42
.Op Fl t Ar seconds
43
.Ar fd
40
.Sh DESCRIPTION
44
.Sh DESCRIPTION
41
The
45
The
42
.Nm
46
.Nm
43
utility acquires an exclusive lock on a
47
utility acquires an exclusive lock on a
44
.Ar file ,
48
.Ar file|fd ,
45
creating it if necessary,
49
creating it if necessary,
46
.Bf Em
50
.Bf Em
47
and removing the file on exit unless explicitly told not to.
51
and removing the file on exit unless explicitly told not to.
Lines 64-70 Link Here
64
.Xr flock 2 ;
68
.Xr flock 2 ;
65
the mere existence of the
69
the mere existence of the
66
.Ar file
70
.Ar file
67
is not considered to constitute a lock.
71
is not considered to constitute a lock. If
72
.Ar file|fd
73
is an integer greater than 0 then it will be interpreted as
74
file descriptor number. This implicitly sets the
75
.Fl k
76
option. If a file descriptor is used then
77
.Ar command
78
is optional. This can be used to lock inside a shell script.
68
.Pp
79
.Pp
69
If the
80
If the
70
.Nm
81
.Nm
Lines 160-165 Link Here
160
did not exit normally,
171
did not exit normally,
161
but may have been signaled or stopped.
172
but may have been signaled or stopped.
162
.El
173
.El
174
.Sh EXAMPLES
175
Lock a file and run a script, return immediately if the lock is not
176
available. Do not delete the file afterward so lock order is
177
guaranteed.
178
.Pp
179
.Dl $ lockf -t 0 -k /tmp/my.lock myscript
180
.Pp
181
Protect a section of a shell script with a lock, wait up to 5 seconds
182
for it to become available. In this case
183
.Fl k
184
is implied.
185
.Pp
186
.Bd -literal -offset indent
187
(
188
	lockf -s -t 10 9
189
	if [ $? -ne 0 ]; then
190
		echo "Failed to obtain lock"
191
		exit 1
192
	fi
193
194
	echo Start
195
	# Do some stuff
196
	echo End
197
) 9>/tmp/my.lock
198
.Ed
163
.Sh SEE ALSO
199
.Sh SEE ALSO
164
.Xr flock 2 ,
200
.Xr flock 2 ,
165
.Xr lockf 3 ,
201
.Xr lockf 3 ,
(-)lockf/lockf.c (-11 / +34 lines)
Lines 50-55 Link Here
50
static const char *lockname;
50
static const char *lockname;
51
static int lockfd = -1;
51
static int lockfd = -1;
52
static int keep;
52
static int keep;
53
static int fdlock;
53
static volatile sig_atomic_t timed_out;
54
static volatile sig_atomic_t timed_out;
54
55
55
/*
56
/*
Lines 88-96 Link Here
88
			usage();
89
			usage();
89
		}
90
		}
90
	}
91
	}
91
	if (argc - optind < 2)
92
92
		usage();
93
	lockname = argv[optind++];
93
	lockname = argv[optind++];
94
95
	if (atoi(lockname) > 2)
96
		fdlock = 1;
97
	else
98
		fdlock = 0;
99
100
	if (argc - optind < 1 - fdlock)
101
		usage();
102
94
	argc -= optind;
103
	argc -= optind;
95
	argv += optind;
104
	argv += optind;
96
	if (waitsec > 0) {		/* Set up a timeout. */
105
	if (waitsec > 0) {		/* Set up a timeout. */
Lines 126-132 Link Here
126
	 */
135
	 */
127
	lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
136
	lockfd = acquire_lock(lockname, flags | O_NONBLOCK);
128
	while (lockfd == -1 && !timed_out && waitsec != 0) {
137
	while (lockfd == -1 && !timed_out && waitsec != 0) {
129
		if (keep)
138
		if (keep || fdlock)
130
			lockfd = acquire_lock(lockname, flags);
139
			lockfd = acquire_lock(lockname, flags);
131
		else {
140
		else {
132
			wait_for_lock(lockname);
141
			wait_for_lock(lockname);
Lines 140-146 Link Here
140
			exit(EX_TEMPFAIL);
149
			exit(EX_TEMPFAIL);
141
		errx(EX_TEMPFAIL, "%s: already locked", lockname);
150
		errx(EX_TEMPFAIL, "%s: already locked", lockname);
142
	}
151
	}
152
143
	/* At this point, we own the lock. */
153
	/* At this point, we own the lock. */
154
155
	/* Nothing else to do for FD lock, just exit */
156
	if (fdlock)
157
		return 0;
158
144
	if (atexit(cleanup) == -1)
159
	if (atexit(cleanup) == -1)
145
		err(EX_OSERR, "atexit failed");
160
		err(EX_OSERR, "atexit failed");
146
	if ((child = fork()) == -1)
161
	if ((child = fork()) == -1)
Lines 161-167 Link Here
161
}
176
}
162
177
163
/*
178
/*
164
 * Try to acquire a lock on the given file, creating the file if
179
 * Try to acquire a lock on the given file/fd, creating the file if
165
 * necessary.  The flags argument is O_NONBLOCK or 0, depending on
180
 * necessary.  The flags argument is O_NONBLOCK or 0, depending on
166
 * whether we should wait for the lock.  Returns an open file descriptor
181
 * whether we should wait for the lock.  Returns an open file descriptor
167
 * on success, or -1 on failure.
182
 * on success, or -1 on failure.
Lines 171-182 Link Here
171
{
186
{
172
	int fd;
187
	int fd;
173
188
174
	if ((fd = open(name, O_RDONLY|O_EXLOCK|flags, 0666)) == -1) {
189
	if ((fd = atoi(name)) > 0) {
175
		if (errno == EAGAIN || errno == EINTR)
190
		if (flock(fd, LOCK_EX | LOCK_NB) == -1) {
176
			return (-1);
191
			if (errno == EAGAIN || errno == EINTR)
177
		else if (errno == ENOENT && (flags & O_CREAT) == 0)
192
				return (-1);
178
			err(EX_UNAVAILABLE, "%s", name);
193
			err(EX_CANTCREAT, "cannot lock FD %d", fd);
179
		err(EX_CANTCREAT, "cannot open %s", name);
194
		}
195
	} else {
196
		if ((fd = open(name, O_RDONLY|O_EXLOCK|flags, 0666)) == -1) {
197
			if (errno == EAGAIN || errno == EINTR)
198
				return (-1);
199
			else if (errno == ENOENT && (flags & O_CREAT) == 0)
200
				err(EX_UNAVAILABLE, "%s", name);
201
			err(EX_CANTCREAT, "cannot open %s", name);
202
		}
180
	}
203
	}
181
	return (fd);
204
	return (fd);
182
}
205
}
Lines 188-194 Link Here
188
cleanup(void)
211
cleanup(void)
189
{
212
{
190
213
191
	if (keep)
214
	if (keep || lockfd)
192
		flock(lockfd, LOCK_UN);
215
		flock(lockfd, LOCK_UN);
193
	else
216
	else
194
		unlink(lockname);
217
		unlink(lockname);

Return to bug 262738