Apache HTTPD
flock.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "apr_arch_file_io.h"
18
19#if APR_HAVE_FCNTL_H
20#include <fcntl.h>
21#endif
22#ifdef HAVE_SYS_FILE_H
23#include <sys/file.h>
24#endif
25
27{
28 int rc;
29
30#if defined(HAVE_FCNTL_H)
31 {
32 struct flock l = { 0 };
33 int fc;
34
35 l.l_whence = SEEK_SET; /* count l_start from start of file */
36 l.l_start = 0; /* lock from start of file */
37 l.l_len = 0; /* lock to end of file */
39 l.l_type = F_RDLCK;
40 else
41 l.l_type = F_WRLCK;
42
44
45 /* keep trying if fcntl() gets interrupted (by a signal) */
46 while ((rc = fcntl(thefile->filedes, fc, &l)) < 0 && errno == EINTR)
47 continue;
48
49 if (rc == -1) {
50 /* on some Unix boxes (e.g., Tru64), we get EACCES instead
51 * of EAGAIN; we don't want APR_STATUS_IS_EAGAIN() matching EACCES
52 * since that breaks other things, so fix up the retcode here
53 */
54 if (errno == EACCES) {
55 return EAGAIN;
56 }
57 return errno;
58 }
59 }
60#elif defined(HAVE_SYS_FILE_H)
61 {
62 int ltype;
63
65 ltype = LOCK_SH;
66 else
67 ltype = LOCK_EX;
68 if ((type & APR_FLOCK_NONBLOCK) != 0)
69 ltype |= LOCK_NB;
70
71 /* keep trying if flock() gets interrupted (by a signal) */
72 while ((rc = flock(thefile->filedes, ltype)) < 0 && errno == EINTR)
73 continue;
74
75 if (rc == -1)
76 return errno;
77 }
78#else
79#error No file locking mechanism is available.
80#endif
81
82 return APR_SUCCESS;
83}
84
86{
87 int rc;
88
89#if defined(HAVE_FCNTL_H)
90 {
91 struct flock l = { 0 };
92
93 l.l_whence = SEEK_SET; /* count l_start from start of file */
94 l.l_start = 0; /* lock from start of file */
95 l.l_len = 0; /* lock to end of file */
96 l.l_type = F_UNLCK;
97
98 /* keep trying if fcntl() gets interrupted (by a signal) */
99 while ((rc = fcntl(thefile->filedes, F_SETLKW, &l)) < 0
100 && errno == EINTR)
101 continue;
102
103 if (rc == -1)
104 return errno;
105 }
106#elif defined(HAVE_SYS_FILE_H)
107 {
108 /* keep trying if flock() gets interrupted (by a signal) */
109 while ((rc = flock(thefile->filedes, LOCK_UN)) < 0 && errno == EINTR)
110 continue;
111
112 if (rc == -1)
113 return errno;
114 }
115#else
116#error No file locking mechanism is available.
117#endif
118
119 return APR_SUCCESS;
120}
apr_redis_t * rc
Definition apr_redis.h:173
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_file_t * thefile
int type
#define APR_FLOCK_TYPEMASK
#define APR_FLOCK_NONBLOCK
#define APR_FLOCK_SHARED
apr_status_t apr_file_lock(apr_file_t *thefile, int type)
Definition flock.c:21
apr_status_t apr_file_unlock(apr_file_t *thefile)
Definition flock.c:33