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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*-
* Public Domain 2014-2019 MongoDB, Inc.
* Public Domain 2008-2014 WiredTiger, Inc.
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "test_util.h"
#include <sys/wait.h>
#define HOME_SIZE 512
static char home[HOME_SIZE]; /* Program working dir lock file */
#define HOME_WR_SUFFIX ".WRNOLOCK" /* Writable dir copy no lock file */
static char home_wr[HOME_SIZE + sizeof(HOME_WR_SUFFIX)];
#define HOME_RD_SUFFIX ".RD" /* Read-only dir */
static char home_rd[HOME_SIZE + sizeof(HOME_RD_SUFFIX)];
#define HOME_RD2_SUFFIX ".RDNOLOCK" /* Read-only dir no lock file */
static char home_rd2[HOME_SIZE + sizeof(HOME_RD2_SUFFIX)];
static const char *saved_argv0; /* Program command */
static const char *const uri = "table:main";
#define ENV_CONFIG \
"create,log=(file_max=10M,archive=false,enabled)," \
"operation_tracking=(enabled=false),transaction_sync=(enabled,method=none)"
#define ENV_CONFIG_RD "operation_tracking=(enabled=false),readonly=true"
#define ENV_CONFIG_WR "operation_tracking=(enabled=false),readonly=false"
#define MAX_VAL 4096
#define MAX_KV 10000
#define EXPECT_ERR 1
#define EXPECT_SUCCESS 0
#define OP_READ 0
#define OP_WRITE 1
static void usage(void) WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));
static void
usage(void)
{
fprintf(stderr, "usage: %s [-h dir]\n", progname);
exit(EXIT_FAILURE);
}
static int
run_child(const char *homedir, int op, int expect)
{
WT_CONNECTION *conn;
WT_CURSOR *cursor;
WT_SESSION *session;
int i, ret;
const char *cfg;
/*
* We expect the read-only database will allow the second read-only handle to succeed because no
* one can create or set the lock file.
*/
if (op == OP_READ)
cfg = ENV_CONFIG_RD;
else
cfg = ENV_CONFIG_WR;
if ((ret = wiredtiger_open(homedir, NULL, cfg, &conn)) == 0) {
if (expect == EXPECT_ERR)
testutil_die(ret, "wiredtiger_open expected error, succeeded");
} else {
if (expect == EXPECT_SUCCESS)
testutil_die(ret, "wiredtiger_open expected success, error");
/*
* If we expect an error and got one, we're done.
*/
return (0);
}
/*
* Make sure we can read the data.
*/
testutil_check(conn->open_session(conn, NULL, NULL, &session));
testutil_check(session->open_cursor(session, uri, NULL, NULL, &cursor));
i = 0;
while ((ret = cursor->next(cursor)) == 0)
++i;
if (i != MAX_KV)
testutil_die(ret, "cursor walk");
testutil_check(conn->close(conn, NULL));
return (0);
}
/*
* Child process opens both databases readonly.
*/
static void open_dbs(int, const char *, const char *, const char *, const char *)
WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));
static void
open_dbs(int op, const char *dir, const char *dir_wr, const char *dir_rd, const char *dir_rd2)
{
int expect, ret;
/*
* The parent has an open connection to all directories. We expect opening the writeable homes
* to return an error. It is a failure if the child successfully opens that.
*/
expect = EXPECT_ERR;
if ((ret = run_child(dir, op, expect)) != 0)
testutil_die(ret, "wiredtiger_open readonly allowed");
if ((ret = run_child(dir_wr, op, expect)) != 0)
testutil_die(ret, "wiredtiger_open readonly allowed");
/*
* The parent must have a read-only connection open to the read-only databases. If the child is
* opening read-only too, we expect success. Otherwise an error if the child attempts to open
* read/write (permission error).
*/
if (op == OP_READ)
expect = EXPECT_SUCCESS;
if ((ret = run_child(dir_rd, op, expect)) != 0)
testutil_die(ret, "run child 1");
if ((ret = run_child(dir_rd2, op, expect)) != 0)
testutil_die(ret, "run child 2");
exit(EXIT_SUCCESS);
}
extern int __wt_optind;
extern char *__wt_optarg;
int
main(int argc, char *argv[])
{
WT_CONNECTION *conn, *conn2, *conn3, *conn4;
WT_CURSOR *cursor;
WT_ITEM data;
WT_SESSION *session;
uint64_t i;
uint8_t buf[MAX_VAL];
int ch, op, ret, status;
char cmd[512];
const char *working_dir;
bool child;
(void)testutil_set_progname(argv);
/*
* Needed unaltered for system command later.
*/
saved_argv0 = argv[0];
working_dir = "WT_RD";
child = false;
op = OP_READ;
while ((ch = __wt_getopt(progname, argc, argv, "Rh:W")) != EOF)
switch (ch) {
case 'R':
child = true;
op = OP_READ;
break;
case 'W':
child = true;
op = OP_WRITE;
break;
case 'h':
working_dir = __wt_optarg;
break;
default:
usage();
}
argc -= __wt_optind;
if (argc != 0)
usage();
/*
* Set up all the directory names.
*/
testutil_work_dir_from_path(home, sizeof(home), working_dir);
testutil_check(__wt_snprintf(home_wr, sizeof(home_wr), "%s%s", home, HOME_WR_SUFFIX));
testutil_check(__wt_snprintf(home_rd, sizeof(home_rd), "%s%s", home, HOME_RD_SUFFIX));
testutil_check(__wt_snprintf(home_rd2, sizeof(home_rd2), "%s%s", home, HOME_RD2_SUFFIX));
if (!child) {
testutil_make_work_dir(home);
testutil_make_work_dir(home_wr);
testutil_make_work_dir(home_rd);
testutil_make_work_dir(home_rd2);
} else
/*
* We are a child process, we just want to call the open_dbs with the directories we have.
* The child function will exit.
*/
open_dbs(op, home, home_wr, home_rd, home_rd2);
/*
* Parent creates a database and table. Then cleanly shuts down. Then copy database to read-only
* directory and chmod. Also copy database to read-only directory and remove the lock file. One
* read-only database will have a lock file in the file system and the other will not. Parent
* opens all databases with read-only configuration flag. Parent forks off child who tries to
* also open all databases with the read-only flag. It should error on the writeable directory,
* but allow it on the read-only directories. The child then confirms it can read all the data.
*/
/*
* Run in the home directory and create the table.
*/
testutil_check(wiredtiger_open(home, NULL, ENV_CONFIG, &conn));
testutil_check(conn->open_session(conn, NULL, NULL, &session));
testutil_check(session->create(session, uri, "key_format=Q,value_format=u"));
testutil_check(session->open_cursor(session, uri, NULL, NULL, &cursor));
/*
* Write data into the table and then cleanly shut down connection.
*/
memset(buf, 0, sizeof(buf));
data.data = buf;
data.size = MAX_VAL;
for (i = 0; i < MAX_KV; ++i) {
cursor->set_key(cursor, i);
cursor->set_value(cursor, &data);
testutil_check(cursor->insert(cursor));
}
testutil_check(conn->close(conn, NULL));
/*
* Copy the database. Remove any lock file from one copy and chmod the copies to be read-only
* permissions.
*/
testutil_check(__wt_snprintf(
cmd, sizeof(cmd), "cp -rp %s/* %s; rm -f %s/WiredTiger.lock", home, home_wr, home_wr));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
testutil_check(__wt_snprintf(cmd, sizeof(cmd),
"cp -rp %s/* %s; chmod 0555 %s; chmod -R 0444 %s/*", home, home_rd, home_rd, home_rd));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
testutil_check(__wt_snprintf(cmd, sizeof(cmd),
"cp -rp %s/* %s; rm -f %s/WiredTiger.lock; "
"chmod 0555 %s; chmod -R 0444 %s/*",
home, home_rd2, home_rd2, home_rd2, home_rd2));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
/*
* Run four scenarios. Sometimes expect errors, sometimes success.
* The writable database directories should always fail to allow the
* child to open due to the lock file. The read-only ones will only
* succeed when the child attempts read-only.
*
* 1. Parent has read-only handle to all databases. Child opens
* read-only also.
* 2. Parent has read-only handle to all databases. Child opens
* read-write.
* 3. Parent has read-write handle to writable databases and
* read-only to read-only databases. Child opens read-only.
* 4. Parent has read-write handle to writable databases and
* read-only to read-only databases. Child opens read-write.
*/
/*
* Open a connection handle to all databases.
*/
fprintf(stderr, " *** Expect several error messages from WT ***\n");
/*
* Scenario 1.
*/
if ((ret = wiredtiger_open(home, NULL, ENV_CONFIG_RD, &conn)) != 0)
testutil_die(ret, "wiredtiger_open original home");
if ((ret = wiredtiger_open(home_wr, NULL, ENV_CONFIG_RD, &conn2)) != 0)
testutil_die(ret, "wiredtiger_open write nolock");
if ((ret = wiredtiger_open(home_rd, NULL, ENV_CONFIG_RD, &conn3)) != 0)
testutil_die(ret, "wiredtiger_open readonly");
if ((ret = wiredtiger_open(home_rd2, NULL, ENV_CONFIG_RD, &conn4)) != 0)
testutil_die(ret, "wiredtiger_open readonly nolock");
/*
* Create a child to also open a connection handle to the databases.
* We cannot use fork here because using fork the child inherits the
* same memory image. Therefore the WT process structure is set in
* the child even though it should not be. So use 'system' to spawn
* an entirely new process.
*
* The child will exit with success if its test passes.
*/
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "%s -h %s -R", saved_argv0, working_dir));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
if (WEXITSTATUS(status) != 0)
testutil_die(WEXITSTATUS(status), "system: %s", cmd);
/*
* Scenario 2. Run child with writable config.
*/
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "%s -h %s -W", saved_argv0, working_dir));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
if (WEXITSTATUS(status) != 0)
testutil_die(WEXITSTATUS(status), "system: %s", cmd);
/*
* Reopen the two writable directories and rerun the child.
*/
testutil_check(conn->close(conn, NULL));
testutil_check(conn2->close(conn2, NULL));
if ((ret = wiredtiger_open(home, NULL, ENV_CONFIG_RD, &conn)) != 0)
testutil_die(ret, "wiredtiger_open original home");
if ((ret = wiredtiger_open(home_wr, NULL, ENV_CONFIG_RD, &conn2)) != 0)
testutil_die(ret, "wiredtiger_open write nolock");
/*
* Scenario 3. Child read-only.
*/
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "%s -h %s -R", saved_argv0, working_dir));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
if (WEXITSTATUS(status) != 0)
testutil_die(WEXITSTATUS(status), "system: %s", cmd);
/*
* Scenario 4. Run child with writable config.
*/
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "%s -h %s -W", saved_argv0, working_dir));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
if (WEXITSTATUS(status) != 0)
testutil_die(WEXITSTATUS(status), "system: %s", cmd);
/*
* Clean-up.
*/
testutil_check(conn->close(conn, NULL));
testutil_check(conn2->close(conn2, NULL));
testutil_check(conn3->close(conn3, NULL));
testutil_check(conn4->close(conn4, NULL));
/*
* We need to chmod the read-only databases back so that they can be removed by scripts.
*/
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "chmod 0777 %s %s", home_rd, home_rd2));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
testutil_check(__wt_snprintf(cmd, sizeof(cmd), "chmod -R 0666 %s/* %s/*", home_rd, home_rd2));
if ((status = system(cmd)) < 0)
testutil_die(status, "system: %s", cmd);
printf(" *** Readonly test successful ***\n");
return (EXIT_SUCCESS);
}
|