File: lock.c

package info (click to toggle)
diablo 1.13-1
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 804 kB
  • ctags: 875
  • sloc: ansic: 8,308; perl: 1,908; sh: 186; csh: 81; makefile: 67
file content (70 lines) | stat: -rw-r--r-- 1,346 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

/*
 * LIB/LOCK.C
 *
 * (c)Copyright 1997, Matthew Dillon, All Rights Reserved.  Refer to
 *    the COPYRIGHT file in the base directory of this distribution 
 *    for specific rights granted.
 */

#include "defs.h"

Prototype int xflock(int fd, int flags);
Prototype int hflock(int fd, int offset, int flags);

/*
 * xflock() - a global lock.  
 *
 * NOTE: cannot be used in conjuction with hflock() due to odd overlap
 * interaction.  If we create other locks, they may wind up sticking
 * around while the unlocks take holes out of the original.
 */

int 
xflock(int fd, int flags)
{
    int r;
    struct flock fl = { 0 };

    fl.l_whence = SEEK_SET;

    switch(flags & 0x0F) {
    case XLOCK_SH:
	fl.l_type = F_RDLCK;
	break;
    case XLOCK_EX:
	fl.l_type = F_WRLCK;
	break;
    case XLOCK_UN:
	fl.l_type = F_UNLCK;
	break;
    }
    r = fcntl(fd, ((flags & XLOCK_NB) ? F_SETLK : F_SETLKW), &fl);
    return(r);
}

int 
hflock(int fd, int offset, int flags)
{
    int r;
    struct flock fl = { 0 };

    switch(flags & 0x0F) {
    case XLOCK_SH:
	fl.l_type = F_RDLCK;
	break;
    case XLOCK_EX:
	fl.l_type = F_WRLCK;
	break;
    case XLOCK_UN:
	fl.l_type = F_UNLCK;
	break;
    }
    fl.l_whence = SEEK_SET;
    fl.l_start = offset;
    fl.l_len = 4;

    r = fcntl(fd, ((flags & XLOCK_NB) ? F_SETLK : F_SETLKW), &fl);
    return(r);
}