| 1 | /** @file |
| 2 | * @brief tcp daemon for use with Xapian's remote backend |
| 3 | */ |
| 4 | /* Copyright 1999,2000,2001 BrightStation PLC |
| 5 | * Copyright 2001,2002 Ananova Ltd |
| 6 | * Copyright 2002,2003,2004,2006,2007,2008,2009,2010,2011,2013,2015,2023 Olly Betts |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 |
| 21 | * USA |
| 22 | */ |
| 23 | |
| 24 | #include <config.h>cxx . . $ `<file>/config.h`/ |
| 25 | |
| 26 | #include <cstdlib> |
| 27 | |
| 28 | #include <iostream> |
| 29 | #include <string> |
| 30 | |
| 31 | #include "gnu_getopt.h"cxx . . $ `<file>/common/gnu_getopt.h`/ |
| 32 | |
| 33 | #include "xapian/constants.h"cxx . . $ `<file>/include/xapian/constants.h`/ |
| 34 | #include "xapian/error.h"cxx . . $ `<file>/include/xapian/error.h`/ |
| 35 | #include "net/remotetcpserver.h"cxx . . $ `<file>/net/remotetcpserver.h`/ |
| 36 | #include "net/remoteserver.h"cxx . . $ `<file>/net/remoteserver.h`/ |
| 37 | #include "stringutils.h"cxx . . $ `<file>/common/stringutils.h`/ |
| 38 | |
| 39 | using namespace std; |
| 40 | |
| 41 | static void register_user_weighting_schemescxx . . $ register_user_weighting_schemes(adfd24afb86f8683).(RemoteTcpServercxx . . $ RemoteTcpServer# &server) { |
| 42 | Xapiancxx . . $ Xapian/::Registrycxx . . $ Xapian/Registry# regcxx . . $ Xapian/Registry#Registry(49f6e7a06ebc5aa8).; |
| 43 | // If you have defined your own weighting scheme, register it here |
| 44 | // like so: |
| 45 | // reg.register_weighting_scheme(FooWeight()); |
| 46 | server.set_registrycxx . . $ RemoteTcpServer#set_registry(fb836e43e31adf32).(reg); |
| 47 | } |
| 48 | |
| 49 | #define MSECS_IDLE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:49:9`! 60000 |
| 50 | #define MSECS_ACTIVE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:50:9`! 15000 |
| 51 | |
| 52 | #define PROG_NAMEcxx . . $ `bin/xapian-tcpsrv.cc:52:9`! "xapian-tcpsrv" |
| 53 | #define PROG_DESCcxx . . $ `bin/xapian-tcpsrv.cc:53:9`! "TCP daemon for use with Xapian's remote backend" |
| 54 | |
| 55 | #define OPT_HELPcxx . . $ `bin/xapian-tcpsrv.cc:55:9`! 1 |
| 56 | #define OPT_VERSIONcxx . . $ `bin/xapian-tcpsrv.cc:56:9`! 2 |
| 57 | |
| 58 | static const char * optscxx . . $ opts. = "I:p:a:i:t:oqw"; |
| 59 | static const struct option long_optscxx . . $ long_opts.[] = { |
| 60 | {"interface", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'I'}, |
| 61 | {"port", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'p'}, |
| 62 | {"active-timeout", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'a'}, |
| 63 | {"idle-timeout", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 'i'}, |
| 64 | {"timeout", required_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:63:9`!, 0, 't'}, |
| 65 | {"one-shot", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'o'}, |
| 66 | {"quiet", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'q'}, |
| 67 | {"writable", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, 'w'}, |
| 68 | {"help", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, OPT_HELPcxx . . $ `bin/xapian-tcpsrv.cc:55:9`!}, |
| 69 | {"version", no_argumentcxx . . $ `<external>/1746263805473775960/getopt_ext.h:62:9`!, 0, OPT_VERSIONcxx . . $ `bin/xapian-tcpsrv.cc:56:9`!}, |
| 70 | {NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, 0, 0, 0} |
| 71 | }; |
| 72 | |
| 73 | static void show_usagecxx . . $ show_usage(49f6e7a06ebc5aa8).() { |
| 74 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Usage: " PROG_NAMEcxx . . $ `bin/xapian-tcpsrv.cc:52:9`! " [OPTIONS] DATABASE_PATH...\n\n" |
| 75 | "Options:\n" |
| 76 | " --port PORTNUM listen on port PORTNUM for connections (no default)\n" |
| 77 | " --interface ADDRESS listen on the interface associated with name or\n" |
| 78 | " address ADDRESS (default is all interfaces)\n" |
| 79 | " --idle-timeout MSECS set timeout for idle connections (default: " |
| 80 | STRINGIZEcxx . . $ `common/stringutils.h:36:9`!(MSECS_IDLE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:49:9`!) "ms)\n" |
| 81 | " --active-timeout MSECS set timeout for active connections (default: " |
| 82 | STRINGIZEcxx . . $ `common/stringutils.h:36:9`!(MSECS_ACTIVE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:50:9`!) "ms)\n" |
| 83 | " --timeout MSECS set both timeout values\n" |
| 84 | " --one-shot serve a single connection and exit\n" |
| 85 | " --quiet disable information messages to stdout\n" |
| 86 | " --writable allow updates\n" |
| 87 | " --help display this help and exit\n" |
| 88 | " --version output version information and exit\n"; |
| 89 | } |
| 90 | |
| 91 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) { |
| 92 | stringcxx . . $ std/string# hostcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 93 | int port = 0; |
| 94 | double active_timeout = MSECS_ACTIVE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:50:9`! * 1e-3; |
| 95 | double idle_timeout = MSECS_IDLE_TIMEOUT_DEFAULTcxx . . $ `bin/xapian-tcpsrv.cc:49:9`! * 1e-3; |
| 96 | |
| 97 | bool one_shot = false; |
| 98 | bool verbose = true; |
| 99 | bool writable = false; |
| 100 | bool syntax_error = false; |
| 101 | |
| 102 | int c; |
| 103 | while ((c = gnu_getopt_longcxx . . $ gnu_getopt_long(41f665c090816971).(argc, argv, optscxx . . $ opts., long_optscxx . . $ long_opts., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)) != -1) { |
| 104 | switch (c) { |
| 105 | case OPT_HELPcxx . . $ `bin/xapian-tcpsrv.cc:55:9`!: |
| 106 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). PROG_NAMEcxx . . $ `bin/xapian-tcpsrv.cc:52:9`! " - " PROG_DESCcxx . . $ `bin/xapian-tcpsrv.cc:53:9`! "\n\n"; |
| 107 | show_usagecxx . . $ show_usage(49f6e7a06ebc5aa8).(); |
| 108 | exit(0); |
| 109 | case OPT_VERSIONcxx . . $ `bin/xapian-tcpsrv.cc:56:9`!: |
| 110 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). PROG_NAMEcxx . . $ `bin/xapian-tcpsrv.cc:52:9`! " - " PACKAGE_STRINGcxx . . $ `config.h:340:9`! "\n"; |
| 111 | exit(0); |
| 112 | case 'I': |
| 113 | host.assigncxx . . $ std/__cxx11/basic_string#assign(978ca1133b5f6e74).(optarg); |
| 114 | break; |
| 115 | case 'p': |
| 116 | port = atoi(optarg); |
| 117 | if (port <= 0 || port >= 65536) { |
| 118 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Error: must specify a valid port number " |
| 119 | "(between 1 and 65535). " |
| 120 | "We actually got " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). port <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n'; |
| 121 | exit(1); |
| 122 | } |
| 123 | break; |
| 124 | case 'a': |
| 125 | active_timeout = atoi(optarg) * 1e-3; |
| 126 | break; |
| 127 | case 'i': |
| 128 | idle_timeout = atoi(optarg) * 1e-3; |
| 129 | break; |
| 130 | case 't': |
| 131 | active_timeout = idle_timeout = atoi(optarg) * 1e-3; |
| 132 | break; |
| 133 | case 'o': |
| 134 | one_shot = true; |
| 135 | break; |
| 136 | case 'q': |
| 137 | verbose = false; |
| 138 | break; |
| 139 | case 'w': |
| 140 | writable = true; |
| 141 | break; |
| 142 | default: |
| 143 | syntax_error = true; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (syntax_error || argv[optind] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) { |
| 148 | show_usagecxx . . $ show_usage(49f6e7a06ebc5aa8).(); |
| 149 | exit(1); |
| 150 | } |
| 151 | |
| 152 | if (port == 0) { |
| 153 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Error: You must specify a port with --port\n"; |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | vectorcxx . . $ std/vector#<stringcxx . . $ std/string#> dbnamescxx . . $ std/vector#vector(13e0640cd3842f28).(argv + optind, argv + argc); |
| 158 | try { |
| 159 | if (!one_shot) { |
| 160 | // Try to open the database(s) so we report problems now instead of |
| 161 | // waiting for the first connection. |
| 162 | for (auto& dbname : dbnames) { |
| 163 | if (writable) { |
| 164 | Xapiancxx . . $ Xapian/::WritableDatabasecxx . . $ Xapian/WritableDatabase# dbcxx . . $ Xapian/WritableDatabase#WritableDatabase(a213b8c485bb0bf5).(dbname, |
| 165 | Xapiancxx . . $ Xapian/::DB_CREATE_OR_OPENcxx . . $ Xapian/DB_CREATE_OR_OPEN.); |
| 166 | } else { |
| 167 | Xapiancxx . . $ Xapian/::Databasecxx . . $ Xapian/Database# dbcxx . . $ Xapian/Database#Database(412d11940ded42ad).(dbname); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if (verbose) { |
| 173 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Starting"; |
| 174 | if (writable) |
| 175 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " writable"; |
| 176 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " server on"; |
| 177 | if (!host.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) |
| 178 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " host " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). host <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ","; |
| 179 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " port " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). port <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n'; |
| 180 | } |
| 181 | |
| 182 | RemoteTcpServercxx . . $ RemoteTcpServer# servercxx . . $ RemoteTcpServer#RemoteTcpServer(825c6fc307f885d0).(dbnames, host, port, active_timeout, |
| 183 | idle_timeout, writable, verbose); |
| 184 | |
| 185 | if (verbose) |
| 186 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Listening...\n" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). flushcxx . . $ std/flush(345bf97a19cc83a9).; |
| 187 | |
| 188 | register_user_weighting_schemescxx . . $ register_user_weighting_schemes(adfd24afb86f8683).(server); |
| 189 | |
| 190 | if (one_shot) { |
| 191 | server.run_oncecxx . . $ TcpServer#run_once(49f6e7a06ebc5aa8).(); |
| 192 | } else { |
| 193 | server.runcxx . . $ TcpServer#run(49f6e7a06ebc5aa8).(); |
| 194 | } |
| 195 | } catch (const Xapiancxx . . $ Xapian/::Errorcxx . . $ Xapian/Error# &e) { |
| 196 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(88df5bef833f7f83). e.get_descriptioncxx . . $ Xapian/Error#get_description(219584694fe2b248).() <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n'; |
| 197 | exit(1); |
| 198 | } catch (const exception &e) { |
| 199 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Caught standard exception: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). e.what() <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '\n'; |
| 200 | exit(1); |
| 201 | } catch (...) { |
| 202 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Caught unknown exception\n"; |
| 203 | exit(1); |
| 204 | } |
| 205 | } |