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