File: lockfile.c

package info (click to toggle)
inn2 2.4.5-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,912 kB
  • ctags: 7,860
  • sloc: ansic: 85,104; perl: 11,427; sh: 9,863; makefile: 2,498; yacc: 1,563; python: 298; lex: 252; tcl: 7
file content (45 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download
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 6020 2002-12-20 00:19:58Z rra $
**
**  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 "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);
}