1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #include "test.h"
40 #include <db.h>
41 #include <sys/resource.h>
42
43 static int loader_flags = 0;
44 static const char *envdir = TOKU_TEST_FILENAME;
45
run_test(int ndb)46 static void run_test(int ndb) {
47 int r;
48
49 char rmcmd[32 + strlen(envdir)];
50 snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", envdir);
51 r = system(rmcmd); CKERR(r);
52 r = toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
53
54 DB_ENV *env;
55 r = db_env_create(&env, 0); CKERR(r);
56 int envflags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE;
57 r = env->open(env, envdir, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
58 env->set_errfile(env, stderr);
59
60 DB *dbs[ndb];
61 uint32_t db_flags[ndb];
62 uint32_t dbt_flags[ndb];
63 for (int i = 0; i < ndb; i++) {
64 db_flags[i] = DB_NOOVERWRITE;
65 dbt_flags[i] = 0;
66 r = db_create(&dbs[i], env, 0); CKERR(r);
67 char name[32];
68 sprintf(name, "db%d", i);
69 r = dbs[i]->open(dbs[i], NULL, name, NULL, DB_BTREE, DB_CREATE, 0666); CKERR(r);
70 }
71
72 DB_TXN *txn;
73 r = env->txn_begin(env, NULL, &txn, 0); CKERR(r);
74
75 DB_LOADER *loader;
76 r = env->create_loader(env, txn, &loader, ndb > 0 ? dbs[0] : NULL, ndb, dbs, db_flags, dbt_flags, loader_flags); CKERR(r);
77
78 struct rlimit current_nproc_limit;
79 r = getrlimit(RLIMIT_NPROC, ¤t_nproc_limit);
80 assert(r == 0);
81
82 struct rlimit new_nproc_limit = current_nproc_limit;
83 new_nproc_limit.rlim_cur = 0;
84 r = setrlimit(RLIMIT_NPROC, &new_nproc_limit);
85 assert(r == 0);
86
87 r = loader->close(loader);
88
89 if (loader_flags & LOADER_DISALLOW_PUTS)
90 CKERR(r);
91 else
92 CKERR2(r, EAGAIN);
93
94 r = setrlimit(RLIMIT_NPROC, ¤t_nproc_limit);
95 assert(r == 0);
96
97 r = txn->abort(txn); CKERR(r);
98
99 for (int i = 0; i < ndb; i++) {
100 r = dbs[i]->close(dbs[i], 0); CKERR(r);
101 }
102
103 r = env->close(env, 0); CKERR(r);
104 }
105
do_args(int argc,char * const argv[])106 static void do_args(int argc, char * const argv[]) {
107 int resultcode;
108 char *cmd = argv[0];
109 argc--; argv++;
110 while (argc>0) {
111 if (strcmp(argv[0], "-h")==0) {
112 resultcode=0;
113 do_usage:
114 fprintf(stderr, "Usage: %s -h -v -q -p\n", cmd);
115 exit(resultcode);
116 } else if (strcmp(argv[0], "-v")==0) {
117 verbose++;
118 } else if (strcmp(argv[0],"-q")==0) {
119 verbose--;
120 if (verbose<0) verbose=0;
121 } else if (strcmp(argv[0], "-p") == 0) {
122 loader_flags |= LOADER_DISALLOW_PUTS;
123 } else if (strcmp(argv[0], "-z") == 0) {
124 loader_flags |= LOADER_COMPRESS_INTERMEDIATES;
125 } else if (strcmp(argv[0], "-e") == 0) {
126 argc--; argv++;
127 if (argc > 0)
128 envdir = argv[0];
129 } else {
130 fprintf(stderr, "Unknown arg: %s\n", argv[0]);
131 resultcode=1;
132 goto do_usage;
133 }
134 argc--;
135 argv++;
136 }
137 }
138
test_main(int argc,char * const * argv)139 int test_main(int argc, char * const *argv) {
140 do_args(argc, argv);
141 run_test(1);
142 return 0;
143 }
144