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