~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/flock.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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
 
 
26
APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type)
 
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;  /* 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)
 
39
            l.l_type = F_RDLCK;
 
40
        else
 
41
            l.l_type = F_WRLCK;
 
42
 
 
43
        fc = (type & APR_FLOCK_NONBLOCK) ? F_SETLK : F_SETLKW;
 
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
 
 
64
        if ((type & APR_FLOCK_TYPEMASK) == APR_FLOCK_SHARED)
 
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
 
 
85
APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile)
 
86
{
 
87
    int rc;
 
88
 
 
89
#if defined(HAVE_FCNTL_H)
 
90
    {
 
91
        struct flock l = { 0 };
 
92
 
 
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 */
 
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
}