| 1 | /*
|
|---|
| 2 | c++ -Iinclude -o remote-leak-test -Wl,-rpath ~+/.libs remote-leak-test.cc .libs/libxapian-1.5.so
|
|---|
| 3 | ./remote-leak-test
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | #include <cstdio>
|
|---|
| 7 | #include <cstdlib>
|
|---|
| 8 | #include <iostream>
|
|---|
| 9 |
|
|---|
| 10 | #include <unistd.h>
|
|---|
| 11 |
|
|---|
| 12 | #include <xapian.h>
|
|---|
| 13 |
|
|---|
| 14 | using namespace std;
|
|---|
| 15 |
|
|---|
| 16 | #define DB "tmp.db"
|
|---|
| 17 | #define PORT 5050
|
|---|
| 18 | #define SERVER "localhost"
|
|---|
| 19 |
|
|---|
| 20 | #define STRINGIZE_(X) #X
|
|---|
| 21 | #define STRINGIZE(X) STRINGIZE_(X)
|
|---|
| 22 |
|
|---|
| 23 | static int get_first_unused_fd() {
|
|---|
| 24 | int fd = dup(1);
|
|---|
| 25 | close(fd);
|
|---|
| 26 | return fd;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | static int remote_leak() {
|
|---|
| 30 | try {
|
|---|
| 31 | auto wsdb = Xapian::Remote::open_writable(SERVER, PORT, 1000, 1000, Xapian::DB_CREATE_OR_OPEN);
|
|---|
| 32 | } catch (const Xapian::DatabaseLockError& exc) {
|
|---|
| 33 | // Expected
|
|---|
| 34 | return 0;
|
|---|
| 35 | }
|
|---|
| 36 | cerr << "Didn't get DatabaseLockError" << endl;
|
|---|
| 37 | return 1;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | int main() {
|
|---|
| 41 | system("rm -rf " DB);
|
|---|
| 42 | system("bin/xapian-tcpsrv --writable --one-shot --port " STRINGIZE(PORT) " --interface " SERVER " " DB "&");
|
|---|
| 43 | sleep(1);
|
|---|
| 44 | Xapian::WritableDatabase wdb(DB);
|
|---|
| 45 | int first_unused_fd_before = get_first_unused_fd();
|
|---|
| 46 | if (remote_leak() != 0) {
|
|---|
| 47 | return 1;
|
|---|
| 48 | }
|
|---|
| 49 | int first_unused_fd_after = get_first_unused_fd();
|
|---|
| 50 | if (first_unused_fd_before != first_unused_fd_after) {
|
|---|
| 51 | cerr << "First unused fd before= " << first_unused_fd_before
|
|---|
| 52 | << ", after = " << first_unused_fd_after << endl;
|
|---|
| 53 | return 1;
|
|---|
| 54 | }
|
|---|
| 55 | return 0;
|
|---|
| 56 | }
|
|---|