2 $Id: locking.c,v 1.4 2010-01-05 19:05:52 franklahm Exp $
3 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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.
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.
18 * Netatalk utility functions
23 #endif /* HAVE_CONFIG_H */
27 #include <atalk/util.h>
30 * @def read_lock(fd, offset, whence, len)
31 * @brief place read lock on file
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)
38 * @returns 0 on success, -1 on failure with
39 * fcntl return value and errno
43 * @def write_lock(fd, offset, whence, len)
44 * @brief place write lock on file
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)
51 * @returns 0 on success, -1 on failure with
52 * fcntl return value and errno
56 * @def unlock(fd, offset, whence, len)
57 * @brief unlock a file
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)
64 * @returns 0 on success, -1 on failure with
65 * fcntl return value and errno
69 * @brief lock a file with fctnl
71 * This function is called via the macros:
72 * read_lock, write_lock, un_lock
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)
81 * @returns 0 on success, -1 on failure with
82 * fcntl return value and errno
84 * @sa read_lock, write_lock, unlock
86 int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
91 lock.l_start = offset;
92 lock.l_whence = whence;
95 return (fcntl(fd, cmd, &lock));