metaproxy  1.21.0
Functions | Variables
metaproxy_prog.cpp File Reference
#include "config.hpp"
#include <sys/resource.h>
#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

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 326 of file metaproxy_prog.cpp.

327 {
328  int ret;
329  yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
330 
331  ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
332 
333  yaz_sc_destroy(&s);
334  exit(ret);
335 }
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 119 of file metaproxy_prog.cpp.

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

322 {
323  return;
324 }

Referenced by main().

◆ set_log_prefix()

static void set_log_prefix ( void  )
static

Definition at line 54 of file metaproxy_prog.cpp.

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

Referenced by sc_main(), and work_common().

◆ sig_x_handler()

static void sig_x_handler ( int  signo)
static

Definition at line 69 of file metaproxy_prog.cpp.

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

References my_pid, process_group, routerp, and sig_received.

Referenced by work_common().

◆ work_common()

static void work_common ( void *  data)
static

Definition at line 84 of file metaproxy_prog.cpp.

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

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

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 105 of file metaproxy_prog.cpp.

106 {
107  work_common(data);
108 }
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 110 of file metaproxy_prog.cpp.

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

References work_common().

Referenced by sc_main().

Here is the call graph for this function:

Variable Documentation

◆ my_pid

pid_t my_pid = 0
static

Definition at line 67 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

◆ process_group

pid_t process_group = 0
static

Definition at line 65 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

◆ routerp

mp::RouterXML* routerp = 0

Definition at line 52 of file metaproxy_prog.cpp.

Referenced by sig_x_handler(), and work_common().

◆ sig_received

int sig_received = 0
static

Definition at line 66 of file metaproxy_prog.cpp.

Referenced by sig_x_handler().