/* ******************************************************************************* * Copyright (c) 1996 Martin Poole * ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/lock_file.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:39 $ * $Revision: 1.2 $ * ******************************************************************************* * * Change History * * $Log: lock_file.c,v $ * Revision 1.2 2002/10/07 09:37:39 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:36:57 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/lock_file.c,v 1.2 2002/10/07 09:37:39 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include /* ------------------------------------------------------------------ defines ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ int lock_reg( int fd, int cmd, int type, off_t offset, int whence, off_t len ) { struct flock lock; lock.l_type = type; lock.l_start = offset; lock.l_whence = whence; lock.l_len = len; return( fcntl( fd, cmd, &lock ) ); } int lock_file( const char *filename ) { int fd,val; int rv = 0; char buf[PID_STR_LEN]; if ( (fd = open( filename, O_WRONLY | O_CREAT, 0644 )) >= 0) { gen_pid_str( buf, getpid() ); if ( (write_lock( fd, 0, SEEK_SET, 0) >= 0) && (ftruncate(fd, 0) >= 0) && (write(fd,buf,strlen(buf)) == strlen(buf)) && ( (val = fcntl(fd, F_GETFD, 0)) >= 0) ) { val |= FD_CLOEXEC; if (fcntl(fd, F_SETFD, val) >=0 ) rv=1; } if (rv==0) close(fd); } return(rv); } /* -- End of File -- */