metaproxy 1.22.1
Functions | Variables
metaproxy_prog.cpp File Reference
#include "config.hpp"
#include <yaz/log.h>
#include <yaz/options.h>
#include <yaz/daemon.h>
#include <yaz/sc.h>
#include <yaz/backtrace.h>
#include <iostream>
#include <stdexcept>
#include <libxml/xinclude.h>
#include <metaproxy/filter.hpp>
#include <metaproxy/package.hpp>
#include <metaproxy/util.hpp>
#include <metaproxy/router_xml.hpp>
#include <stdlib.h>
#include <signal.h>
Include dependency graph for metaproxy_prog.cpp:

Go to the source code of this file.

Functions

static void set_log_prefix (void)
 
static void work_common (void *data)
 
static void work_debug (void *data)
 
static void work_normal (void *data)
 
static int sc_main (yaz_sc_t s, int argc, char **argv)
 
static void sc_stop (yaz_sc_t s)
 
int main (int argc, char **argv)
 

Variables

mp::RouterXML * routerp = 0
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 327 of file metaproxy_prog.cpp.

328{
329 int ret;
330 yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
331
332 ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
333
334 yaz_sc_destroy(&s);
335 exit(ret);
336}
static int sc_main(yaz_sc_t s, int argc, char **argv)
static void sc_stop(yaz_sc_t s)

References sc_main(), and sc_stop().

Here is the call graph for this function:

◆ sc_main()

static int sc_main ( yaz_sc_t  s,
int  argc,
char **  argv 
)
static

Definition at line 120 of file metaproxy_prog.cpp.

123{
124 bool test_config = false;
125 const char *fname = 0;
126 int ret;
127 char *arg;
128 unsigned mode = 0;
129 const char *pidfile = 0;
130 const char *uid = 0;
131#if HAVE_GETRLIMIT
132 const char *socket_limit = 0;
133#endif
134
135 yaz_enable_panic_backtrace(argv[0]);
137
138 while ((ret = options("c{config}:Dh{help}l:m:p:s:tu:v:V{version}w:X",
139 argv, argc, &arg)) != -2)
140 {
141 switch (ret)
142 {
143 case 'c':
144 fname = arg;
145 break;
146 case 'D':
147 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
148 break;
149 case 'h':
150 std::cerr << "metaproxy\n"
151 " -h|--help help\n"
152 " -V|--version version\n"
153 " -v level\n"
154 " -c|--config f config filename\n"
155 " -D daemon and keepalive operation\n"
156 " -l f log file f\n"
157 " -m logformat log time format (strftime)\n"
158 " -p f pid file f\n"
159 " -s n socket limit\n"
160 " -t test configuration\n"
161 " -u id change uid to id\n"
162 " -w dir changes working directory to dir\n"
163 " -X debug mode (no fork/daemon mode)\n"
164#ifdef WIN32
165 " -install install windows service\n"
166 " -remove remove windows service\n"
167#endif
168
169 << std::endl;
170 break;
171 case 'l':
172 yaz_log_init_file(arg);
173 break;
174 case 'm':
175 yaz_log_time_format(arg);
176 break;
177 case 'p':
178 pidfile = arg;
179 break;
180 case 's':
181#if HAVE_GETRLIMIT
182 socket_limit = arg;
183#else
184 yaz_log(YLOG_WARN, "Option -s unsuppoted on this platform");
185#endif
186 break;
187 case 't':
188 test_config = true;
189 break;
190 case 'u':
191 uid = arg;
192 break;
193 case 'v':
194 yaz_log_init_level(yaz_log_mask_str(arg));
195 break;
196 case 'V':
197 std::cout << VERSION;
198#ifdef VERSION_SHA1
199 std::cout << " " VERSION_SHA1;
200#endif
201 std::cout << "\n";
202 return 0;
203 break;
204 case 'w':
205 if (
206#ifdef WIN32
207 _chdir(arg)
208#else
209 chdir(arg)
210#endif
211 )
212 {
213 std::cerr << "chdir " << arg << " failed" << std::endl;
214 return 1;
215 }
216 case 'X':
217 mode = YAZ_DAEMON_DEBUG;
218 break;
219 case -1:
220 std::cerr << "bad option: " << arg << std::endl;
221 return 1;
222 }
223 }
224 if (!fname)
225 {
226 std::cerr << "No configuration given; use -h for help\n";
227 return 1;
228 }
229
230 yaz_log(YLOG_LOG, "metaproxy %s " VERSION
231#ifdef VERSION_SHA1
232 " " VERSION_SHA1
233#endif
234 , test_config ? "test" : "start"
235 );
236
237 char yaz_version_str[20];
238 char yaz_sha1_str[41];
239 yaz_version(yaz_version_str, yaz_sha1_str);
240 yaz_log(YLOG_LOG, "YAZ %s %s", yaz_version_str, yaz_sha1_str);
241
242 xmlInitParser();
243 LIBXML_TEST_VERSION
244
245 yaz_log_xml_errors(0, YLOG_LOG);
246 xmlDocPtr doc = xmlReadFile(fname,
247 NULL,
248 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
249 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
250
251 if (!doc)
252 {
253 yaz_log(YLOG_FATAL,"XML parsing failed");
254 return 1;
255 }
256 // and perform Xinclude then
257 int r = xmlXIncludeProcess(doc);
258 if (r == -1)
259 {
260 yaz_log(YLOG_FATAL, "XInclude processing failed");
261 return 1;
262 }
263 mp::wrbuf base_path;
264 const char *last_p = strrchr(fname,
265#ifdef WIN32
266 '\\'
267#else
268 '/'
269#endif
270 );
271 if (last_p)
272 wrbuf_write(base_path, fname, last_p - fname);
273 else
274 wrbuf_puts(base_path, ".");
275 ret = 0;
276 try {
277 mp::RouterXML *router =
278 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
279 if (!test_config)
280 {
281#if HAVE_GETRLIMIT
282 if (socket_limit)
283 {
284 struct rlimit limit_data;
285 limit_data.rlim_cur = atoi(socket_limit);
286 limit_data.rlim_max = atoi(socket_limit);
287 int r = setrlimit(RLIMIT_NOFILE, &limit_data);
288 if (r)
289 {
290 yaz_log(YLOG_FATAL,"setrlimit: %s", strerror(errno));
291 return 1;
292 }
293
294 }
295#endif
296 yaz_sc_running(s);
297
298 yaz_daemon("metaproxy", mode | YAZ_DAEMON_LOG_REOPEN,
299 (mode & YAZ_DAEMON_FORK) ? work_normal : work_debug,
300 router, pidfile, uid);
301 }
302 delete router;
303 }
304 catch (std::logic_error &e) {
305 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
306 ret = 1;
307 }
308 catch (std::runtime_error &e) {
309 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
310 ret = 1;
311 }
312 catch ( ... ) {
313 yaz_log(YLOG_FATAL, "Unknown Exception");
314 ret = 1;
315 }
316 xmlFreeDoc(doc);
317 if (test_config)
318 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
319 return ret;
320}
#define VERSION_SHA1
Definition config.hpp:91
#define VERSION
Definition config.hpp:88
static void work_normal(void *data)
static void set_log_prefix(void)
static void work_debug(void *data)

References set_log_prefix(), VERSION, VERSION_SHA1, work_debug(), and work_normal().

Referenced by main().

Here is the call graph for this function:

◆ sc_stop()

static void sc_stop ( yaz_sc_t  s)
static

Definition at line 322 of file metaproxy_prog.cpp.

323{
324 return;
325}

Referenced by main().

◆ set_log_prefix()

static void set_log_prefix ( void  )
static

Definition at line 55 of file metaproxy_prog.cpp.

56{
57#if HAVE_UNISTD_H
58 char str[80];
59
60 sprintf(str, "%lld", (long long) getpid());
61 yaz_log_init_prefix(str);
62#endif
63}

Referenced by sc_main(), and work_common().

◆ work_common()

static void work_common ( void *  data)
static

Definition at line 85 of file metaproxy_prog.cpp.

86{
88#if HAVE_UNISTD_H
89 process_group = getpgid(0); // save process group ID
90 my_pid = getpid();
91
92 signal(SIGTERM, sig_x_handler);
93 signal(SIGUSR1, sig_x_handler);
94#endif
95 routerp = (mp::RouterXML*) data;
96 routerp->start();
97
98 mp::Package pack;
99 pack.router(*routerp).move();
100 yaz_log(YLOG_LOG, "metaproxy stop");
101 delete routerp;
102 routerp = 0;
103 _exit(0);
104}
mp::RouterXML * routerp

References routerp, and set_log_prefix().

Referenced by work_debug(), and work_normal().

Here is the call graph for this function:

◆ work_debug()

static void work_debug ( void *  data)
static

Definition at line 106 of file metaproxy_prog.cpp.

107{
108 work_common(data);
109}
static void work_common(void *data)

References work_common().

Referenced by sc_main().

Here is the call graph for this function:

◆ work_normal()

static void work_normal ( void *  data)
static

Definition at line 111 of file metaproxy_prog.cpp.

112{
113#if HAVE_UNISTD_H
114 /* make the current working process group leader */
115 setpgid(0, 0);
116#endif
117 work_common(data);
118}

References work_common().

Referenced by sc_main().

Here is the call graph for this function:

Variable Documentation

◆ routerp

mp::RouterXML* routerp = 0

Definition at line 53 of file metaproxy_prog.cpp.

Referenced by work_common().