1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#include "apr_arch_file_io.h"
22
#ifdef HAVE_SYS_FILE_H
26
APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type)
30
#if defined(HAVE_FCNTL_H)
32
struct flock l = { 0 };
35
l.l_whence = SEEK_SET; /* lock from current point */
36
l.l_start = 0; /* begin lock at this offset */
37
l.l_len = 0; /* lock to end of file */
38
if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
43
fc = (type & APR_FLOCK_NONBLOCK) ? F_SETLK : F_SETLKW;
45
/* keep trying if fcntl() gets interrupted (by a signal) */
46
while ((rc = fcntl(thefile->filedes, fc, &l)) < 0 && errno == EINTR)
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
54
if (errno == EACCES) {
60
#elif defined(HAVE_SYS_FILE_H)
64
if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
68
if ((type & APR_FLOCK_NONBLOCK) != 0)
71
/* keep trying if flock() gets interrupted (by a signal) */
72
while ((rc = flock(thefile->filedes, ltype)) < 0 && errno == EINTR)
79
#error No file locking mechanism is available.
85
APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile)
89
#if defined(HAVE_FCNTL_H)
91
struct flock l = { 0 };
93
l.l_whence = SEEK_SET; /* lock from current point */
94
l.l_start = 0; /* begin lock at this offset */
95
l.l_len = 0; /* lock to end of file */
98
/* keep trying if fcntl() gets interrupted (by a signal) */
99
while ((rc = fcntl(thefile->filedes, F_SETLKW, &l)) < 0
106
#elif defined(HAVE_SYS_FILE_H)
108
/* keep trying if flock() gets interrupted (by a signal) */
109
while ((rc = flock(thefile->filedes, LOCK_UN)) < 0 && errno == EINTR)
116
#error No file locking mechanism is available.