fcntl_lock.c 1.46 KB
/**    
    @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);
}