metaproxy  1.13.0
metaproxy_prog.cpp
Go to the documentation of this file.
1 /* This file is part of Metaproxy.
2  Copyright (C) Index Data
3 
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "config.hpp"
20 
21 #include <yaz/log.h>
22 #include <yaz/options.h>
23 #include <yaz/daemon.h>
24 
25 #include <yaz/sc.h>
26 #include <yaz/backtrace.h>
27 #include <iostream>
28 #include <stdexcept>
29 #include <libxml/xinclude.h>
30 
31 #include <metaproxy/filter.hpp>
32 #include <metaproxy/package.hpp>
33 #include <metaproxy/util.hpp>
34 #include <metaproxy/router_xml.hpp>
35 
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <signal.h>
40 #ifdef WIN32
41 #include <direct.h>
42 #include <io.h>
43 #include <process.h>
44 #endif
45 
46 namespace mp = metaproxy_1;
47 
48 mp::RouterXML *routerp = 0;
49 
50 static void set_log_prefix(void)
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 }
59 
60 #if HAVE_UNISTD_H
61 static pid_t process_group = 0;
62 static int sig_received = 0;
63 static pid_t my_pid = 0;
64 
65 static void sig_x_handler(int signo)
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 }
78 #endif
79 
80 static void work_common(void *data)
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 }
100 
101 static void work_debug(void *data)
102 {
103  work_common(data);
104 }
105 
106 static void work_normal(void *data)
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 }
114 
115 static int sc_main(
116  yaz_sc_t s,
117  int argc, char **argv)
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 }
291 
292 static void sc_stop(yaz_sc_t s)
293 {
294  return;
295 }
296 
297 int main(int argc, char **argv)
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 }
307 
308 /*
309  * Local variables:
310  * c-basic-offset: 4
311  * c-file-style: "Stroustrup"
312  * indent-tabs-mode: nil
313  * End:
314  * vim: shiftwidth=4 tabstop=8 expandtab
315  */
316 
static void work_normal(void *data)
static pid_t my_pid
int main(int argc, char **argv)
#define VERSION_SHA1
Definition: config.hpp:89
static void sig_x_handler(int signo)
mp::RouterXML * routerp
static void set_log_prefix(void)
static void work_debug(void *data)
static void work_common(void *data)
static int sig_received
static void sc_stop(yaz_sc_t s)
#define VERSION
Definition: config.hpp:86
static int sc_main(yaz_sc_t s, int argc, char **argv)
static pid_t process_group