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 // This test verifies that the env->dbremove function returns an error rather than
40 // crash when the NOFILE resource limit is exceeded.
41
42 #include "test.h"
43 #include <db.h>
44 #include <sys/resource.h>
45
46 static const char *envdir = TOKU_TEST_FILENAME;
47
test_dbremove()48 static void test_dbremove() {
49 int r;
50
51 char rmcmd[32 + strlen(envdir)];
52 snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", envdir);
53 r = system(rmcmd); CKERR(r);
54 r = toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
55
56 DB_ENV *env;
57 r = db_env_create(&env, 0); CKERR(r);
58 int envflags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE;
59 r = env->open(env, envdir, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
60 env->set_errfile(env, stderr);
61
62 DB *db;
63 r = db_create(&db, env, 0); CKERR(r);
64 char fname[32];
65 sprintf(fname, "db%d", 0);
66 r = db->open(db, nullptr, fname, nullptr, DB_BTREE, DB_CREATE, 0666); CKERR(r);
67
68 r = db->close(db, 0); CKERR(r);
69
70 DB_TXN *txn;
71 r = env->txn_begin(env, nullptr, &txn, 0); CKERR(r);
72
73 struct rlimit current_limit;
74 r = getrlimit(RLIMIT_NOFILE, ¤t_limit);
75 assert(r == 0);
76
77 struct rlimit new_limit = current_limit;
78 new_limit.rlim_cur = 0;
79 r = setrlimit(RLIMIT_NOFILE, &new_limit);
80 assert(r == 0);
81
82 r = env->dbremove(env, txn, fname, nullptr, 0);
83 CKERR2(r, EMFILE);
84
85 r = setrlimit(RLIMIT_NOFILE, ¤t_limit);
86 assert(r == 0);
87
88 r = env->dbremove(env, txn, fname, nullptr, 0);
89 CKERR(r);
90
91 r = txn->commit(txn, 0); CKERR(r);
92
93 r = env->close(env, 0); CKERR(r);
94 }
95
do_args(int argc,char * const argv[])96 static void do_args(int argc, char * const argv[]) {
97 int resultcode;
98 char *cmd = argv[0];
99 argc--; argv++;
100 while (argc>0) {
101 if (strcmp(argv[0], "-h")==0) {
102 resultcode=0;
103 do_usage:
104 fprintf(stderr, "Usage: %s -h -v -q\n", cmd);
105 exit(resultcode);
106 } else if (strcmp(argv[0], "-v")==0) {
107 verbose++;
108 } else if (strcmp(argv[0],"-q")==0) {
109 verbose--;
110 if (verbose<0) verbose=0;
111 } else {
112 fprintf(stderr, "Unknown arg: %s\n", argv[0]);
113 resultcode=1;
114 goto do_usage;
115 }
116 argc--;
117 argv++;
118 }
119 }
120
test_main(int argc,char * const * argv)121 int test_main(int argc, char * const *argv) {
122 do_args(argc, argv);
123 test_dbremove();
124 return 0;
125 }
126