aboutsummaryrefslogtreecommitdiff
path: root/src/lock.c
blob: e6762f429eff8a9fe1099ea0fddbcc3d06b65fb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* lock.c - Implement basic file locking for GDBM. */

/* This file is part of GDBM, the GNU data base manager.
   Copyright (C) 2008-2023 Free Software Foundation, Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

/* Include system configuration before all else. */
#include "autoconf.h"

#include "gdbmdefs.h"

#include <errno.h>

#if HAVE_FLOCK
# ifndef LOCK_SH
#  define LOCK_SH 1
# endif

# ifndef LOCK_EX
#  define LOCK_EX 2
# endif

# ifndef LOCK_NB
#  define LOCK_NB 4
# endif

# ifndef LOCK_UN
#  define LOCK_UN 8
# endif
#endif

#if defined(F_SETLK) && defined(F_RDLCK) && defined(F_WRLCK)
# define HAVE_FCNTL_LOCK 1
#else
# define HAVE_FCNTL_LOCK 0
#endif

/* Return values for try_lock_ functions: */
enum
  {
    TRY_LOCK_OK,    /* Locking succeeded */
    TRY_LOCK_FAIL,  /* File already locked by another process. */
    TRY_LOCK_NEXT   /* Another error (including locking mechanism not
		       available).  The caller should try next locking
		       mechanism. */
		       
  };

/*
 * Locking using flock().
 */
static int
try_lock_flock (GDBM_FILE dbf)
{
#if HAVE_FLOCK
  if (flock (dbf->desc,
	     ((dbf->read_write == GDBM_READER) ? LOCK_SH : LOCK_EX)
	     | LOCK_NB) == 0)
    {
      return TRY_LOCK_OK;
    }
  else if (errno == EWOULDBLOCK)
    {
      return TRY_LOCK_FAIL;
    }
#endif
  return TRY_LOCK_NEXT;
}

static void
unlock_flock (GDBM_FILE dbf)
{
#if HAVE_FLOCK
  flock (dbf->desc, LOCK_UN);
#endif
}

/*
 * Locking via lockf.
 */

static int
try_lock_lockf (GDBM_FILE dbf)
{
#if HAVE_LOCKF
  /*
   * NOTE: lockf will fail with EINVAL unless the database file was opened
   * with write-only permission (O_WRONLY) or with read/write permission
   * (O_RDWR).  This means that this locking mechanism will always fail for
   * databases opened with GDBM_READER,
   */
  if (dbf->read_write != GDBM_READER)
    {
      if (lockf (dbf->desc, F_TLOCK, (off_t)0L) == 0)
	return TRY_LOCK_OK;

      switch (errno)
	{
	case EACCES:
	case EAGAIN:
	case EDEADLK:
	  return TRY_LOCK_FAIL;
	  
	default:
	  /* try next locking method */
	  break;
	}
    }
#endif
  return TRY_LOCK_NEXT;
}

static void
unlock_lockf (GDBM_FILE dbf)
{
#if HAVE_LOCKF
  lockf (dbf->desc, F_ULOCK, (off_t)0L);
#endif
}

/*
 * Locking via fcntl().
 */

static int
try_lock_fcntl (GDBM_FILE dbf)
{
#if HAVE_FCNTL_LOCK
  struct flock fl;

  /* If we're still here, try fcntl. */
  if (dbf->read_write == GDBM_READER)
    fl.l_type = F_RDLCK;
  else
    fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = fl.l_len = (off_t)0L;
  if (fcntl (dbf->desc, F_SETLK, &fl) == 0)
    return TRY_LOCK_OK;

  switch (errno)
    {
    case EACCES:
    case EAGAIN:
    case EDEADLK:
      return TRY_LOCK_FAIL;

    default:
      /* try next locking method */
      break;
    }
  
#endif
  return TRY_LOCK_NEXT;
}

static void
unlock_fcntl (GDBM_FILE dbf)
{
#if HAVE_FCNTL_LOCK
  struct flock fl;

  fl.l_type = F_UNLCK;
  fl.l_whence = SEEK_SET;
  fl.l_start = fl.l_len = (off_t)0L;
  fcntl (dbf->desc, F_SETLK, &fl);
#endif
}

/* Try each supported locking mechanism. */
int
_gdbm_lock_file (GDBM_FILE dbf)
{
  int res;

  dbf->lock_type = LOCKING_NONE;
  if ((res = try_lock_flock (dbf)) == TRY_LOCK_OK)
    dbf->lock_type = LOCKING_FLOCK;
  else if (res == TRY_LOCK_NEXT)
    {
      if ((res = try_lock_lockf (dbf)) == TRY_LOCK_OK)
	dbf->lock_type = LOCKING_LOCKF;
      else if (res == TRY_LOCK_NEXT)
	{
	  if (try_lock_fcntl (dbf) == TRY_LOCK_OK)
	    dbf->lock_type = LOCKING_FCNTL;
	}
    }

  return dbf->lock_type == LOCKING_NONE ? -1 : 0;
}

void
_gdbm_unlock_file (GDBM_FILE dbf)
{
  void (*unlock_fn[]) (GDBM_FILE) = {
    [LOCKING_FLOCK] = unlock_flock,
    [LOCKING_LOCKF] = unlock_lockf,
    [LOCKING_FCNTL] = unlock_fcntl
  };

  if (dbf->lock_type != LOCKING_NONE)
    {
      unlock_fn[dbf->lock_type] (dbf);
      dbf->lock_type = LOCKING_NONE;
    }
}

Return to:

Send suggestions and report system problems to the System administrator.