New PHP5 APC - version 3.0.19, using PHP5 5.2.0-8+etch11,
[php5-apc.git] / apc_fcntl.c
1 /*
2   +----------------------------------------------------------------------+
3   | APC                                                                  |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2008 The PHP Group                                     |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: George Schlossnagle <george@omniti.com>                     |
16   |          Rasmus Lerdorf <rasmus@php.net>                             |
17   +----------------------------------------------------------------------+
18
19    This software was contributed to PHP by Community Connect Inc. in 2002
20    and revised in 2005 by Yahoo! Inc. to add support for PHP 5.1.
21    Future revisions and derivatives of this source code must acknowledge
22    Community Connect Inc. as the original contributor of this module by
23    leaving this note intact in the source code.
24
25    All other licensing and usage conditions are those of the PHP Group.
26
27  */
28
29 /* $Id: apc_fcntl.c,v 3.25.2.1 2008/05/11 18:57:00 rasmus Exp $ */
30
31 #include "apc_fcntl.h"
32 #include "apc.h"
33 #include <unistd.h>
34 #include <fcntl.h>
35
36 int apc_fcntl_create(const char* pathname)
37 {
38     int fd;
39     if(pathname == NULL) {
40         char lock_path[] = "/tmp/.apc.XXXXXX";
41         mktemp(lock_path);
42         fd = open(lock_path, O_RDWR|O_CREAT, 0666);
43         if(fd > 0 ) {
44             unlink(lock_path);
45             return fd;
46         } else {
47             apc_eprint("apc_fcntl_create: open(%s, O_RDWR|O_CREAT, 0666) failed:", lock_path);
48             return -1;
49         }
50     }
51     fd = open(pathname, O_RDWR|O_CREAT, 0666);
52     if(fd > 0 ) {
53         unlink(pathname);
54         return fd;
55     }
56     apc_eprint("apc_fcntl_create: open(%s, O_RDWR|O_CREAT, 0666) failed:", pathname);
57     return -1;
58 }
59
60 void apc_fcntl_destroy(int fd)
61 {
62     close(fd);
63 }
64
65 static int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
66 {
67   int ret;
68   struct flock lock;
69
70   lock.l_type = type;
71   lock.l_start = offset;
72   lock.l_whence = whence;
73   lock.l_len = len;
74   lock.l_pid = 0;
75
76   do { ret = fcntl(fd, cmd, &lock) ; }
77   while(ret < 0 && errno == EINTR);
78   return(ret);
79 }
80
81 void apc_fcntl_lock(int fd)
82 {
83     if(lock_reg(fd, F_SETLKW, F_WRLCK, 0, SEEK_SET, 0) < 0) {
84         apc_eprint("apc_fcntl_lock failed:");
85     }
86 }
87
88 void apc_fcntl_rdlock(int fd)
89 {
90     if(lock_reg(fd, F_SETLKW, F_RDLCK, 0, SEEK_SET, 0) < 0) {
91         apc_eprint("apc_fcntl_rdlock failed:");
92     }
93 }
94
95 zend_bool apc_fcntl_nonblocking_lock(int fd)
96 {
97     if(lock_reg(fd, F_SETLK, F_WRLCK, 0, SEEK_SET, 0) < 0) {
98         if(errno==EACCES||errno==EAGAIN) return 0;
99         else apc_eprint("apc_fcntl_lock failed:");
100     }
101     return 1;
102 }
103
104 void apc_fcntl_unlock(int fd)
105 {
106     if(lock_reg(fd, F_SETLKW, F_UNLCK, 0, SEEK_SET, 0) < 0) {
107         apc_eprint("apc_fcntl_unlock failed:");
108     }
109 }
110
111 /*
112  * Local variables:
113  * tab-width: 4
114  * c-basic-offset: 4
115  * End:
116  * vim600: expandtab sw=4 ts=4 sts=4 fdm=marker
117  * vim<600: expandtab sw=4 ts=4 sts=4
118  */