annotate src/lib/file-lock.c @ 1741:9df02b1533b3 HEAD

Removed most of the license comments from src/lib/*.c. It's just fine to keep them in a single COPYING.MIT file. Changed a few other comments as well.
author Timo Sirainen <tss@iki.fi>
date Wed, 27 Aug 2003 00:18:16 +0300
parents fc5d2e44cc5e
children 79790750c349
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1741
9df02b1533b3 Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents: 1161
diff changeset
1 /* Copyright (c) 2002-2003 Timo Sirainen */
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
532
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
4 #include "alarm-hup.h"
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "file-lock.h"
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6
532
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
7 #include <time.h>
490
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
8 #include <signal.h>
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
9
1161
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
10 int file_try_lock(int fd, int lock_type)
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
11 {
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
12 return file_wait_lock_full(fd, lock_type, 0, NULL, NULL);
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
13 }
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
14
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
15 int file_wait_lock(int fd, int lock_type)
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
16 {
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
17 return file_wait_lock_full(fd, lock_type, DEFAULT_LOCK_TIMEOUT,
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
18 NULL, NULL);
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
19 }
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
20
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
21 int file_wait_lock_full(int fd, int lock_type, unsigned int timeout,
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
22 void (*callback)(unsigned int secs_left, void *context),
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
23 void *context)
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24 {
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 struct flock fl;
674
b7aefd0d7611 Locking changes triggered a bit larger cleanup :) If we have to wait for a
Timo Sirainen <tss@iki.fi>
parents: 532
diff changeset
26 time_t timeout_time, now;
532
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
27
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
28 if (timeout == 0)
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
29 timeout_time = 0;
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
30 else {
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
31 alarm_hup_init();
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
32 timeout_time = time(NULL) + timeout;
3b53dd1280c6 I/O buffers now use real blocking instead of setting up a sub-ioloop to
Timo Sirainen <tss@iki.fi>
parents: 490
diff changeset
33 }
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 fl.l_type = lock_type;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 fl.l_whence = SEEK_SET;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37 fl.l_start = 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
38 fl.l_len = 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39
1161
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
40 while (fcntl(fd, timeout != 0 ? F_SETLKW : F_SETLK, &fl) < 0) {
fc5d2e44cc5e Locking code cleanups and small fixes
Timo Sirainen <tss@iki.fi>
parents: 953
diff changeset
41 if (timeout == 0 && (errno == EACCES || errno == EAGAIN))
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
42 return 0;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
43
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
44 if (errno != EINTR)
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
45 return -1;
490
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
46
674
b7aefd0d7611 Locking changes triggered a bit larger cleanup :) If we have to wait for a
Timo Sirainen <tss@iki.fi>
parents: 532
diff changeset
47 now = time(NULL);
b7aefd0d7611 Locking changes triggered a bit larger cleanup :) If we have to wait for a
Timo Sirainen <tss@iki.fi>
parents: 532
diff changeset
48 if (timeout != 0 && now >= timeout_time) {
490
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
49 errno = EAGAIN;
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
50 return 0;
fd0181ae25f6 file_wait_lock() now takes timeout-parameter so one badly behaving process
Timo Sirainen <tss@iki.fi>
parents: 222
diff changeset
51 }
674
b7aefd0d7611 Locking changes triggered a bit larger cleanup :) If we have to wait for a
Timo Sirainen <tss@iki.fi>
parents: 532
diff changeset
52
953
411006be3c66 Naming change for function typedefs.
Timo Sirainen <tss@iki.fi>
parents: 674
diff changeset
53 if (callback != NULL)
411006be3c66 Naming change for function typedefs.
Timo Sirainen <tss@iki.fi>
parents: 674
diff changeset
54 callback(timeout_time - now, context);
222
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
55 }
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
56
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
57 return 1;
cf4d065f2f85 lots of cleanups. also index/datafile is now capable of staying in memory,
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
58 }