fcntl_lock.c
1.46 KB
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
71
72
73
74
75
76
77
78
/**
@file fcntl_lock.c
@date 2009/9/30
@author 유영창 frog@falinux.com FALinux.Co.,Ltd.
@brief fcntl_lock 함수
Ver 0.1.0
@modify
@todo
@bug
@remark
@warning
*/
//----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/kdev_t.h>
#include <fcntl_lock.h>
int flock_create( char *key_file )
{
int fd;
fd = open( key_file, O_CREAT|O_RDWR );
if( fd < 0 ) return -1;
return fd;
}
int flock_free( int key_fd )
{
if( key_fd >= 0 ) close( key_fd );
}
int flock_lock( int key_fd )
{
struct flock lock;
if( key_fd < 0 ) return key_fd;
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
return fcntl(key_fd, F_SETLKW, &lock);
}
int flock_unlock( int key_fd )
{
struct flock lock;
if( key_fd < 0 ) return key_fd;
lock.l_type = F_UNLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
fcntl(key_fd, F_SETLK, &lock);
}