Refactor MP main a little and clean up
[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     const char *fname = 0;
87     int ret;
88     char *arg;
89     unsigned mode = 0;
90     const char *pidfile = 0;
91     const char *uid = 0;
92     
93     while ((ret = options("c{config}:Dh{help}l:p:u:V{version}w:X", 
94                           argv, argc, &arg)) != -2)
95     {
96         switch (ret)
97         {
98         case 'c':
99             fname = arg;
100             break;
101         case 'D':
102             mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
103             break;
104         case 'h':
105             std::cerr << "metaproxy\n"
106                 " -h|--help     help\n"
107                 " -V|--version  version\n"
108                 " -c|--config f config filename\n"
109                 " -D            daemon and keepalive operation\n"
110                 " -l f          log file f\n"
111                 " -p f          pid file f\n"
112                 " -u id         change uid to id\n"
113                 " -w dir        changes working directory to dir\n"
114                 " -X            debug mode (no fork/daemon mode)\n"
115 #ifdef WIN32
116                 " -install      install windows service\n"
117                 " -remove       remove windows service\n"
118 #endif
119                 
120                       << std::endl;
121             break;
122         case 'l':
123             yaz_log_init_file(arg);
124             break;
125         case 'p':
126             pidfile = arg;
127             break;
128         case 'u':
129             uid = arg;
130             break;
131         case 'V':
132             std::cout << VERSION;
133 #ifdef VERSION_SHA1
134             std::cout << " " VERSION_SHA1;
135 #endif
136             std::cout << "\n";
137             return 0;
138             break;
139         case 'w':
140             if (
141 #ifdef WIN32
142                 _chdir(arg)
143 #else
144                 chdir(arg)
145 #endif
146                 ) 
147             {
148                 std::cerr << "chdir " << arg << " failed" << std::endl;
149                 return 1;
150             }
151         case 'X':
152             mode = YAZ_DAEMON_DEBUG;
153             break;
154         case -1:
155             std::cerr << "bad option: " << arg << std::endl;
156             return 1;
157         }
158     }
159     if (!fname)
160     {
161         std::cerr << "No configuration given; use -h for help\n";
162         return 1;
163     }
164     
165     yaz_log(YLOG_LOG, "Metaproxy start " VERSION
166 #ifdef VERSION_SHA1
167             " " VERSION_SHA1
168 #endif
169         );
170     
171     xmlDocPtr doc = xmlReadFile(fname,
172                                 NULL, 
173                                 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
174                                 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
175     
176     if (!doc)
177     {
178         yaz_log (YLOG_FATAL,"XML parsing failed");
179         return 1;
180     }
181     // and perform Xinclude then
182     int r = xmlXIncludeProcess(doc);
183     if (r == -1)
184     {
185         yaz_log(YLOG_FATAL, "XInclude processing failed");
186         return 1;
187     }
188     WRBUF base_path = wrbuf_alloc();
189     const char *last_p = strrchr(fname,
190 #ifdef WIN32
191                                  '\\'
192 #else
193                                  '/'
194 #endif
195         );
196     if (last_p)
197         wrbuf_write(base_path, fname, last_p - fname);
198
199     ret = 0;
200     try {
201         mp::FactoryStatic factory;
202         mp::RouterFleXML *router =
203             new mp::RouterFleXML(doc, factory, false, wrbuf_cstr(base_path));
204         wrbuf_destroy(base_path);
205         
206         yaz_sc_running(s);
207         
208         yaz_daemon("metaproxy", mode, mode == YAZ_DAEMON_DEBUG ?
209                    handler_debug : handler_normal, router, pidfile, uid);
210     }
211     catch (std::logic_error &e) {
212         yaz_log (YLOG_FATAL,"std::logic error: %s" , e.what() );
213         ret = 1;
214     }
215     catch (std::runtime_error &e) {
216         yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
217         ret = 1;
218     }
219     catch ( ... ) {
220         yaz_log(YLOG_FATAL, "Unknown Exception");
221         ret = 1;
222     }
223     wrbuf_destroy(base_path);
224     xmlFreeDoc(doc);
225     return ret;
226 }
227
228 static void sc_stop(yaz_sc_t s)
229 {
230     return;
231 }
232
233 int main(int argc, char **argv)
234 {
235     int ret;
236     yaz_sc_t s = yaz_sc_create("metaproxy", "metaproxy");
237
238     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
239
240     yaz_sc_destroy(&s);
241     exit(ret);
242 }
243
244
245
246 /*
247  * Local variables:
248  * c-basic-offset: 4
249  * c-file-style: "Stroustrup"
250  * indent-tabs-mode: nil
251  * End:
252  * vim: shiftwidth=4 tabstop=8 expandtab
253  */
254