File: lockfile.c

package info (click to toggle)
inn2 2.5.2-2~squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,072 kB
  • ctags: 8,521
  • sloc: ansic: 91,418; sh: 13,249; perl: 12,311; makefile: 2,928; yacc: 868; python: 342; lex: 266
file content (45 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*  $Id: lockfile.c 7585 2006-11-21 09:37:51Z eagle $
**
**  Lock a file or a range in a file.
**
**  Provides inn_lock_file and inn_lock_range functions to lock or unlock a
**  file or ranges within a file with a more convenient syntax than fcntl.
**  Assume that fcntl is available.
*/

#include "config.h"
#include "clibrary.h"
#include <errno.h>
#include <fcntl.h>

#include "inn/libinn.h"

bool
inn_lock_file(int fd, enum inn_locktype type, bool block)
{
    return inn_lock_range(fd, type, block, 0, 0);
}

bool
inn_lock_range(int fd, enum inn_locktype type, bool block, off_t offset,
               off_t size)
{
    struct flock fl;
    int status;

    switch (type) {
        case INN_LOCK_READ:     fl.l_type = F_RDLCK;    break;
        case INN_LOCK_WRITE:    fl.l_type = F_WRLCK;    break;
        default:
        case INN_LOCK_UNLOCK:   fl.l_type = F_UNLCK;    break;
    }

    do {
	fl.l_whence = SEEK_SET;
	fl.l_start = offset;
	fl.l_len = size;

	status = fcntl(fd, block ? F_SETLKW : F_SETLK, &fl);
    } while (status == -1 && errno == EINTR);
    return (status != -1);
}