1/* 2 $Id: locking.c,v 1.4 2010-01-05 19:05:52 franklahm Exp $ 3 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14*/ 15 16/*! 17 * @file 18 * Netatalk utility functions 19 */ 20 21#ifdef HAVE_CONFIG_H 22#include "config.h" 23#endif /* HAVE_CONFIG_H */ 24 25#include <unistd.h> 26#include <fcntl.h> 27#include <atalk/util.h> 28 29/*! 30 * @def read_lock(fd, offset, whence, len) 31 * @brief place read lock on file 32 * 33 * @param fd (r) File descriptor 34 * @param offset (r) byte offset relative to l_whence 35 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END 36 * @param len (r) no. of bytes (0 means to EOF) 37 * 38 * @returns 0 on success, -1 on failure with 39 * fcntl return value and errno 40 */ 41 42/*! 43 * @def write_lock(fd, offset, whence, len) 44 * @brief place write lock on file 45 * 46 * @param fd (r) File descriptor 47 * @param offset (r) byte offset relative to l_whence 48 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END 49 * @param len (r) no. of bytes (0 means to EOF) 50 * 51 * @returns 0 on success, -1 on failure with 52 * fcntl return value and errno 53 */ 54 55/*! 56 * @def unlock(fd, offset, whence, len) 57 * @brief unlock a file 58 * 59 * @param fd (r) File descriptor 60 * @param offset (r) byte offset relative to l_whence 61 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END 62 * @param len (r) no. of bytes (0 means to EOF) 63 * 64 * @returns 0 on success, -1 on failure with 65 * fcntl return value and errno 66 */ 67 68/*! 69 * @brief lock a file with fctnl 70 * 71 * This function is called via the macros: 72 * read_lock, write_lock, un_lock 73 * 74 * @param fd (r) File descriptor 75 * @param cmd (r) cmd to fcntl, only F_SETLK is usable here 76 * @param type (r) F_RDLCK, F_WRLCK, F_UNLCK 77 * @param offset (r) byte offset relative to l_whence 78 * @param whence (r) SEEK_SET, SEEK_CUR, SEEK_END 79 * @param len (r) no. of bytes (0 means to EOF) 80 * 81 * @returns 0 on success, -1 on failure with 82 * fcntl return value and errno 83 * 84 * @sa read_lock, write_lock, unlock 85 */ 86int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len) 87{ 88 struct flock lock; 89 90 lock.l_type = type; 91 lock.l_start = offset; 92 lock.l_whence = whence; 93 lock.l_len = len; 94 95 return (fcntl(fd, cmd, &lock)); 96} 97