00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "db_config.h"
00010
00011 #ifndef NO_SYSTEM_INCLUDES
00012 #include <sys/types.h>
00013
00014 #include <string.h>
00015 #endif
00016
00017 #include "db_int.h"
00018
00019 #define REGISTER_FILE "__db.register"
00020
00021 #define PID_EMPTY "X%23lu\n"
00022 #define PID_FMT "%24lu\n"
00023 #define PID_ISEMPTY(p) (p[0] == 'X')
00024 #define PID_LEN 25
00025
00026 #define REGISTRY_LOCK(dbenv, pos, nowait) \
00027 __os_fdlock(dbenv, (dbenv)->registry, (off_t)(pos), 1, nowait)
00028 #define REGISTRY_UNLOCK(dbenv, pos) \
00029 __os_fdlock(dbenv, (dbenv)->registry, (off_t)(pos), 0, 0)
00030 #define REGISTRY_EXCL_LOCK(dbenv, nowait) \
00031 REGISTRY_LOCK(dbenv, 1, nowait)
00032 #define REGISTRY_EXCL_UNLOCK(dbenv) \
00033 REGISTRY_UNLOCK(dbenv, 1)
00034
00035 static int __envreg_add __P((DB_ENV *, int *));
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 int
00102 __envreg_register(dbenv, db_home, need_recoveryp)
00103 DB_ENV *dbenv;
00104 const char *db_home;
00105 int *need_recoveryp;
00106 {
00107 pid_t pid;
00108 db_threadid_t tid;
00109 u_int32_t bytes, mbytes;
00110 int ret;
00111 char path[MAXPATHLEN];
00112
00113 *need_recoveryp = 0;
00114 dbenv->thread_id(dbenv, &pid, &tid);
00115
00116 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00117 __db_msg(dbenv, "%lu: register environment", (u_long)pid);
00118
00119
00120 (void)snprintf(path, sizeof(path), "%s/%s", db_home, REGISTER_FILE);
00121 if ((ret = __os_open(dbenv, path,
00122 DB_OSO_CREATE, __db_omode("rw-rw----"), &dbenv->registry)) != 0)
00123 goto err;
00124
00125
00126
00127
00128
00129
00130
00131
00132 if ((ret = REGISTRY_EXCL_LOCK(dbenv, 0)) != 0)
00133 goto err;
00134
00135
00136
00137
00138
00139
00140
00141 if ((ret = __os_ioinfo(
00142 dbenv, path, dbenv->registry, &mbytes, &bytes, NULL)) != 0)
00143 goto err;
00144 if (mbytes == 0 && bytes == 0) {
00145 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00146 __db_msg(dbenv,
00147 "%lu: creating %s", (u_long)pid, path);
00148 *need_recoveryp = 1;
00149 }
00150
00151
00152 if ((ret = __envreg_add(dbenv, need_recoveryp)) != 0)
00153 goto err;
00154
00155
00156
00157
00158
00159
00160 if (*need_recoveryp == 0 && (ret = REGISTRY_EXCL_UNLOCK(dbenv)) != 0)
00161 goto err;
00162
00163 if (0) {
00164 err: *need_recoveryp = 0;
00165
00166
00167
00168
00169
00170 (void)__os_closehandle(dbenv, dbenv->registry);
00171 dbenv->registry = NULL;
00172 }
00173
00174 return (ret);
00175 }
00176
00177
00178
00179
00180
00181 static int
00182 __envreg_add(dbenv, need_recoveryp)
00183 DB_ENV *dbenv;
00184 int *need_recoveryp;
00185 {
00186 pid_t pid;
00187 db_threadid_t tid;
00188 off_t end, pos;
00189 size_t nr, nw;
00190 u_int lcnt;
00191 u_int32_t bytes, mbytes;
00192 int need_recovery, ret;
00193 char *p, buf[256], pid_buf[256];
00194
00195 need_recovery = 0;
00196 COMPQUIET(p, NULL);
00197
00198
00199 dbenv->thread_id(dbenv, &pid, &tid);
00200 snprintf(pid_buf, sizeof(pid_buf), PID_FMT, (u_long)pid);
00201
00202 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00203 __db_msg(dbenv, "===== %lu: before add", (u_long)pid);
00204
00205
00206
00207
00208
00209
00210
00211 for (lcnt = 0;; ++lcnt) {
00212 if ((ret = __os_read(
00213 dbenv, dbenv->registry, buf, PID_LEN, &nr)) != 0)
00214 return (ret);
00215 if (nr == 0)
00216 break;
00217 if (nr != PID_LEN)
00218 goto corrupt;
00219
00220 if (FLD_ISSET(
00221 dbenv->verbose, DB_VERB_REGISTER) && PID_ISEMPTY(buf)) {
00222 __db_msg(dbenv, "%02u: EMPTY", lcnt);
00223 continue;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232 if (memcmp(buf, pid_buf, PID_LEN) == 0) {
00233 __db_err(dbenv,
00234 "DB_REGISTER limits each process to a single open DB_ENV handle");
00235 return (EINVAL);
00236 }
00237
00238 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER)) {
00239 for (p = buf; *p == ' ';)
00240 ++p;
00241 buf[nr - 1] = '\0';
00242 }
00243
00244 pos = (off_t)lcnt * PID_LEN;
00245 if (REGISTRY_LOCK(dbenv, pos, 1) == 0) {
00246 if ((ret = REGISTRY_UNLOCK(dbenv, pos)) != 0)
00247 return (ret);
00248
00249 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00250 __db_msg(dbenv, "%02u: %s: FAILED", lcnt, p);
00251
00252 need_recovery = 1;
00253 break;
00254 } else
00255 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00256 __db_msg(dbenv, "%02u: %s: LOCKED", lcnt, p);
00257 }
00258
00259
00260
00261
00262
00263
00264
00265
00266 if (need_recovery) {
00267
00268 if ((ret = __os_ioinfo(
00269 dbenv, NULL, dbenv->registry, &mbytes, &bytes, NULL)) != 0)
00270 return (ret);
00271 end = (off_t)mbytes * MEGABYTE + bytes;
00272
00273
00274 DB_ASSERT(end % PID_LEN == 0);
00275
00276
00277
00278
00279
00280 if ((ret = __os_seek(
00281 dbenv, dbenv->registry, 0, 0, 0, 0, DB_OS_SEEK_SET)) != 0)
00282 return (ret);
00283 snprintf(buf, sizeof(buf), PID_EMPTY, (u_long)0);
00284 for (lcnt = (u_int)end / PID_LEN; lcnt > 0; --lcnt)
00285 if ((ret = __os_write(
00286 dbenv, dbenv->registry, buf, PID_LEN, &nw)) != 0 ||
00287 nw != PID_LEN)
00288 goto corrupt;
00289 }
00290
00291
00292
00293
00294
00295 if ((ret = __os_seek(
00296 dbenv, dbenv->registry, 0, 0, 0, 0, DB_OS_SEEK_SET)) != 0)
00297 return (ret);
00298 for (lcnt = 0;; ++lcnt) {
00299 if ((ret = __os_read(
00300 dbenv, dbenv->registry, buf, PID_LEN, &nr)) != 0)
00301 return (ret);
00302 if (nr == PID_LEN && !PID_ISEMPTY(buf))
00303 continue;
00304 pos = (off_t)lcnt * PID_LEN;
00305 if (REGISTRY_LOCK(dbenv, pos, 1) == 0) {
00306 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00307 __db_msg(dbenv,
00308 "%lu: locking slot %02u at offset %lu",
00309 (u_long)pid, lcnt, (u_long)pos);
00310
00311 if ((ret = __os_seek(dbenv, dbenv->registry,
00312 0, 0, (u_int32_t)pos, 0, DB_OS_SEEK_SET)) != 0 ||
00313 (ret = __os_write(dbenv,
00314 dbenv->registry, pid_buf, PID_LEN, &nw)) != 0 ||
00315 nw != PID_LEN)
00316 return (ret);
00317 dbenv->registry_off = (u_int32_t)pos;
00318 break;
00319 }
00320 }
00321
00322 if (need_recovery)
00323 *need_recoveryp = 1;
00324
00325 if (0) {
00326 corrupt: __db_err(dbenv, "%s: file contents corrupted", REGISTER_FILE);
00327 return (ret == 0 ? EACCES : ret);
00328 }
00329
00330 return (ret);
00331 }
00332
00333
00334
00335
00336
00337
00338
00339 int
00340 __envreg_unregister(dbenv, recovery_failed)
00341 DB_ENV *dbenv;
00342 int recovery_failed;
00343 {
00344 size_t nw;
00345 int ret, t_ret;
00346 char buf[256];
00347
00348 ret = 0;
00349
00350
00351
00352
00353
00354
00355
00356 if (recovery_failed)
00357 goto err;
00358
00359
00360
00361
00362
00363
00364
00365
00366 snprintf(buf, sizeof(buf), PID_EMPTY, (u_long)0);
00367 if ((ret = __os_seek(dbenv, dbenv->registry,
00368 0, 0, dbenv->registry_off, 0, DB_OS_SEEK_SET)) != 0 ||
00369 (ret = __os_write(
00370 dbenv, dbenv->registry, buf, PID_LEN, &nw)) != 0 ||
00371 nw != PID_LEN)
00372 goto err;
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387 err: if ((t_ret =
00388 __os_closehandle(dbenv, dbenv->registry)) != 0 && ret == 0)
00389 ret = t_ret;
00390
00391 dbenv->registry = NULL;
00392 return (ret);
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 int
00402 __envreg_xunlock(dbenv)
00403 DB_ENV *dbenv;
00404 {
00405 pid_t pid;
00406 db_threadid_t tid;
00407 int ret;
00408
00409 dbenv->thread_id(dbenv, &pid, &tid);
00410
00411 if (FLD_ISSET(dbenv->verbose, DB_VERB_REGISTER))
00412 __db_msg(dbenv,
00413 "%lu: recovery completed, unlocking", (u_long)pid);
00414
00415 if ((ret = REGISTRY_EXCL_UNLOCK(dbenv)) == 0)
00416 return (ret);
00417
00418 __db_err(dbenv,
00419 "%s: exclusive file unlock: %s", REGISTER_FILE, db_strerror(ret));
00420 return (__db_panic(dbenv, ret));
00421 }