metaproxy daemon: option -t tests configuration
[metaproxy-moved-to-github.git] / src / metaproxy_prog.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 <iostream>
27 #include <stdexcept>
28 #include <libxml/xinclude.h>
29
30 #include <metaproxy/filter.hpp>
31 #include <metaproxy/package.hpp>
32 #include "router_flexml.hpp"
33 #include "factory_static.hpp"
34
35 #if HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <signal.h>
39 #ifdef WIN32
40 #include <direct.h>
41 #include <io.h>
42 #include <process.h>
43 #endif
44
45 namespace mp = metaproxy_1;
46
47 mp::RouterFleXML *routerp = 0;
48
49 #if HAVE_UNISTD_H
50 static pid_t process_group = 0;
51
52 static void sig_term_handler(int s)
53 {
54     kill(-process_group, SIGTERM); /* kill all children processes as well */
55     _exit(0);
56 }
57 #endif
58
59 static void handler_debug(void *data)
60 {
61 #if HAVE_UNISTD_H
62     process_group = getpgid(0); // save process group ID
63     
64     signal(SIGTERM, sig_term_handler);
65 #endif
66     routerp = (mp::RouterFleXML*) data;
67     routerp->start();
68
69     mp::Package pack;
70     pack.router(*routerp).move(); /* should never exit */
71 }
72     
73 static void handler_normal(void *data)
74 {
75 #if HAVE_UNISTD_H
76     /* make the current working process group leader */
77     setpgid(0, 0);
78 #endif
79     handler_debug(data);
80 }
81
82 static int sc_main(
83     yaz_sc_t s,
84     int argc, char **argv)
85 {
86     bool test_config = false;
87     const char *fname = 0;
88     int ret;
89     char *arg;
90     unsigned mode = 0;
91     const char *pidfile = 0;
92     const char *uid = 0;
93     
94     while ((ret = options("c{config}:Dh{help}l:p:tu:V{version}w:X", 
95                           argv, argc, &arg)) != -2)
96     {
97         switch (ret)
98         {
99         case 'c':
100             fname = arg;
101             break;
102         case 'D':
103             mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
104             break;
105         case 'h':
106             std::cerr << "metaproxy\n"
107                 " -h|--help     help\n"
108                 " -V|--version  version\n"
109                 " -c|--config f config filename\n"
110                 " -D            daemon and keepalive operation\n"
111                 " -l f          log file f\n"
112                 " -p f          pid file f\n"
113                 " -t            test configuration\n"
114                 " -u id         change uid to id\n"
115                 " -w dir        changes working directory to dir\n"
116                 " -X            debug mode (no fork/daemon mode)\n"
117 #ifdef WIN32
118                 " -install      install windows service\n"
119                 " -remove       remove windows service\n"
120 #endif
121                 
122                       << std::endl;
123             break;
124         case 'l':
125             yaz_log_init_file(arg);
126             break;
127         case 'p':
128             pidfile = arg;
129             break;
130         case 't':
131             test_config = true;
132             break;
133         case 'u':
134             uid = arg;
135             break;
136         case 'V':
137             std::cout << VERSION;
138 #ifdef VERSION_SHA1
139             std::cout << " " VERSION_SHA1;
140 #endif
141             std::cout << "\n";
142             return 0;
143             break;
144         case 'w':
145             if (
146 #ifdef WIN32
147                 _chdir(arg)
148 #else
149                 chdir(arg)
150 #endif
151                 ) 
152             {
153                 std::cerr << "chdir " << arg << " failed" << std::endl;
154                 return 1;
155             }
156         case 'X':
157             mode = YAZ_DAEMON_DEBUG;
158             break;
159         case -1:
160             std::cerr << "bad option: " << arg << std::endl;
161             return 1;
162         }
163     }
164     if (!fname)
165     {
166         std::cerr << "No configuration given; use -h for help\n";
167         return 1;
168     }
169     
170     yaz_log(YLOG_LOG, "Metaproxy start " VERSION
171 #ifdef VERSION_SHA1
172             " " VERSION_SHA1
173 #endif
174         );
175     
176     xmlDocPtr doc = xmlReadFile(fname,
177                                 NULL, 
178                                 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
179                                 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
180     
181     if (!doc)
182     {
183         yaz_log (YLOG_FATAL,"XML parsing failed");
184         return 1;
185     }
186     // and perform Xinclude then
187     int r = xmlXIncludeProcess(doc);
188     if (r == -1)
189     {
190         yaz_log(YLOG_FATAL, "XInclude processing failed");
191         return 1;
192     }
193     WRBUF base_path = wrbuf_alloc();
194     const char *last_p = strrchr(fname,
195 #ifdef WIN32
196                                  '\\'
197 #else
198                                  '/'
199 #endif
200         );
201     if (last_p)
202         wrbuf_write(base_path, fname, last_p - fname);
203
204     ret = 0;
205     try {
206         mp::FactoryStatic factory;
207         mp::RouterFleXML *router =
208             new mp::RouterFleXML(doc, factory, test_config, wrbuf_cstr(base_path));
209         if (!test_config)
210         {
211             
212             yaz_sc_running(s);
213             
214             yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
215                        handler_debug : handler_normal, router, pidfile, uid);
216         }
217     }
218     catch (std::logic_error &e) {
219         yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
220         ret = 1;
221     }
222     catch (std::runtime_error &e) {
223         yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
224         ret = 1;
225     }
226     catch ( ... ) {
227         yaz_log(YLOG_FATAL, "Unknown Exception");
228         ret = 1;
229     }
230     wrbuf_destroy(base_path);
231     xmlFreeDoc(doc);
232     return ret;
233 }
234
235 static void sc_stop(yaz_sc_t s)
236 {
237     return;
238 }
239
240 int main(int argc, char **argv)
241 {
242     int ret;
243     yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
244
245     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
246
247     yaz_sc_destroy(&s);
248     exit(ret);
249 }
250
251
252
253 /*
254  * Local variables:
255  * c-basic-offset: 4
256  * c-file-style: "Stroustrup"
257  * indent-tabs-mode: nil
258  * End:
259  * vim: shiftwidth=4 tabstop=8 expandtab
260  */
261