summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/os/lock_file.c
blob: d57652004a499076d68f657cc9515f41309b9d1b (plain)
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
/*
 * $Source$
 * $Author$
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <krb5/mit-copyright.h>.
 *
 * libos: krb5_lock_file routine
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_lock_file_c [] =
"$Id$";
#endif	/* !lint & !SABER */

#include <krb5/copyright.h>

#include <krb5/krb5.h>
#include <krb5/krb5_err.h>
#include <krb5/libos.h>

#include <stdio.h>
#include <sys/file.h>

#include <sys/types.h>
#include <krb5/ext-proto.h>

extern int errno;

/*ARGSUSED*/
krb5_error_code
krb5_lock_file(filep, pathname, mode)
FILE *filep;
char *pathname;
int mode;
{
    int flock_flag = -1;

    switch (mode & ~KRB5_LOCKMODE_DONTBLOCK) {
    case KRB5_LOCKMODE_SHARED:
	flock_flag = LOCK_SH;
    case KRB5_LOCKMODE_EXCLUSIVE:
	flock_flag = LOCK_EX;
    case KRB5_LOCKMODE_UNLOCK:
	flock_flag = LOCK_UN;
    }
    if (flock_flag == -1)
	return(KRB5_LIBOS_BADLOCKFLAG);
    if (mode & KRB5_LOCKMODE_DONTBLOCK)
	flock_flag |= LOCK_NB;

    if (flock(fileno(filep), flock_flag) == -1)
	return(errno);
    return 0;
}