metaproxy  1.13.0
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 <unistd.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 sig_x_handler (int signo)
 
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
 
static pid_t process_group = 0
 
static int sig_received = 0
 
static pid_t my_pid = 0
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 297 of file metaproxy_prog.cpp.

References sc_main(), and sc_stop().

298 {
299  int ret;
300  yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
301 
302  ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
303 
304  yaz_sc_destroy(&s);
305  exit(ret);
306 }
static void sc_stop(yaz_sc_t s)
static int sc_main(yaz_sc_t s, int argc, char **argv)

Here is the call graph for this function:

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

Definition at line 115 of file metaproxy_prog.cpp.

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

Referenced by main().

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

Here is the call graph for this function:

static void sc_stop ( yaz_sc_t  s)
static

Definition at line 292 of file metaproxy_prog.cpp.

Referenced by main().

293 {
294  return;
295 }
static void set_log_prefix ( void  )
static

Definition at line 50 of file metaproxy_prog.cpp.

Referenced by sc_main(), and work_common().

51 {
52 #if HAVE_UNISTD_H
53  char str[80];
54 
55  sprintf(str, "%lld", (long long) getpid());
56  yaz_log_init_prefix(str);
57 #endif
58 }
static void sig_x_handler ( int  signo)
static

Definition at line 65 of file metaproxy_prog.cpp.

References my_pid, process_group, routerp, and sig_received.

Referenced by work_common().

66 {
67  if (sig_received)
68  return;
69  if ( getpid() != my_pid ) // can happen in unlikely cases
70  return; // when the sig hits a child just after fork(), before it
71  // clears its sighandler. MP-559.
72  sig_received = signo;
73  if (routerp)
74  routerp->stop(signo);
75  if (signo == SIGTERM)
76  kill(-process_group, SIGTERM); /* kill all children processes as well */
77 }
static pid_t my_pid
mp::RouterXML * routerp
static int sig_received
static pid_t process_group
static void work_common ( void *  data)
static

Definition at line 80 of file metaproxy_prog.cpp.

References my_pid, process_group, routerp, set_log_prefix(), and sig_x_handler().

Referenced by work_debug(), and work_normal().

81 {
83 #if HAVE_UNISTD_H
84  process_group = getpgid(0); // save process group ID
85  my_pid = getpid();
86 
87  signal(SIGTERM, sig_x_handler);
88  signal(SIGUSR1, sig_x_handler);
89 #endif
90  routerp = (mp::RouterXML*) data;
91  routerp->start();
92 
93  mp::Package pack;
94  pack.router(*routerp).move();
95  yaz_log(YLOG_LOG, "metaproxy stop");
96  delete routerp;
97  routerp = 0;
98  _exit(0);
99 }
static pid_t my_pid
static void sig_x_handler(int signo)
mp::RouterXML * routerp
static void set_log_prefix(void)
static pid_t process_group

Here is the call graph for this function:

static void work_debug ( void *  data)
static

Definition at line 101 of file metaproxy_prog.cpp.

References work_common().

Referenced by sc_main().

102 {
103  work_common(data);
104 }
static void work_common(void *data)

Here is the call graph for this function:

static void work_normal ( void *  data)
static

Definition at line 106 of file metaproxy_prog.cpp.

References work_common().

Referenced by sc_main().

107 {
108 #if HAVE_UNISTD_H
109  /* make the current working process group leader */
110  setpgid(0, 0);
111 #endif
112  work_common(data);
113 }
static void work_common(void *data)

Here is the call graph for this function:

Variable Documentation

pid_t my_pid = 0
static

Definition at line 63 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

pid_t process_group = 0
static

Definition at line 61 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

mp::RouterXML* routerp = 0

Definition at line 48 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

int sig_received = 0
static

Definition at line 62 of file metaproxy_prog.cpp.

Referenced by sig_x_handler().