122{
123 bool test_config = false;
124 const char *fname = 0;
125 int ret;
126 char *arg;
127 unsigned mode = 0;
128 const char *pidfile = 0;
129 const char *uid = 0;
130#if HAVE_GETRLIMIT
131 const char *socket_limit = 0;
132#endif
133
134 yaz_enable_panic_backtrace(argv[0]);
136
137 while ((ret = options("c{config}:Dh{help}l:m:p:s:tu:v:V{version}w:X",
138 argv, argc, &arg)) != -2)
139 {
140 switch (ret)
141 {
142 case 'c':
143 fname = arg;
144 break;
145 case 'D':
146 mode = YAZ_DAEMON_FORK|YAZ_DAEMON_KEEPALIVE;
147 break;
148 case 'h':
149 std::cerr << "metaproxy\n"
150 " -h|--help help\n"
151 " -V|--version version\n"
152 " -v level\n"
153 " -c|--config f config filename\n"
154 " -D daemon and keepalive operation\n"
155 " -l f log file f\n"
156 " -m logformat log time format (strftime)\n"
157 " -p f pid file f\n"
158 " -s n socket limit\n"
159 " -t test configuration\n"
160 " -u id change uid to id\n"
161 " -w dir changes working directory to dir\n"
162 " -X debug mode (no fork/daemon mode)\n"
163#ifdef WIN32
164 " -install install windows service\n"
165 " -remove remove windows service\n"
166#endif
167
168 << std::endl;
169 break;
170 case 'l':
171 yaz_log_init_file(arg);
172 break;
173 case 'm':
174 yaz_log_time_format(arg);
175 break;
176 case 'p':
177 pidfile = arg;
178 break;
179 case 's':
180#if HAVE_GETRLIMIT
181 socket_limit = arg;
182#else
183 yaz_log(YLOG_WARN, "Option -s unsuppoted on this platform");
184#endif
185 break;
186 case 't':
187 test_config = true;
188 break;
189 case 'u':
190 uid = arg;
191 break;
192 case 'v':
193 yaz_log_init_level(yaz_log_mask_str(arg));
194 break;
195 case 'V':
197#ifdef VERSION_SHA1
199#endif
200 std::cout << "\n";
201 return 0;
202 break;
203 case 'w':
204 if (
205#ifdef WIN32
206 _chdir(arg)
207#else
208 chdir(arg)
209#endif
210 )
211 {
212 std::cerr << "chdir " << arg << " failed" << std::endl;
213 return 1;
214 }
215 case 'X':
216 mode = YAZ_DAEMON_DEBUG;
217 break;
218 case -1:
219 std::cerr << "bad option: " << arg << std::endl;
220 return 1;
221 }
222 }
223 if (!fname)
224 {
225 std::cerr << "No configuration given; use -h for help\n";
226 return 1;
227 }
228
229 yaz_log(YLOG_LOG,
"metaproxy %s " VERSION
232#endif
233 , test_config ? "test" : "start"
234 );
235
236 char yaz_version_str[20];
237 char yaz_sha1_str[41];
238 yaz_version(yaz_version_str, yaz_sha1_str);
239 yaz_log(YLOG_LOG, "YAZ %s %s", yaz_version_str, yaz_sha1_str);
240
241 xmlInitParser();
242 LIBXML_TEST_VERSION
243
244 yaz_log_xml_errors(0, YLOG_LOG);
245 xmlDocPtr doc = xmlReadFile(fname,
246 NULL,
247 XML_PARSE_XINCLUDE + XML_PARSE_NOBLANKS
248 + XML_PARSE_NSCLEAN + XML_PARSE_NONET );
249
250 if (!doc)
251 {
252 yaz_log(YLOG_FATAL,"XML parsing failed");
253 return 1;
254 }
255
256 int r = xmlXIncludeProcess(doc);
257 if (r == -1)
258 {
259 yaz_log(YLOG_FATAL, "XInclude processing failed");
260 return 1;
261 }
262 mp::wrbuf base_path;
263 const char *last_p = strrchr(fname,
264#ifdef WIN32
265 '\\'
266#else
267 '/'
268#endif
269 );
270 if (last_p)
271 wrbuf_write(base_path, fname, last_p - fname);
272 else
273 wrbuf_puts(base_path, ".");
274 ret = 0;
275 try {
276 mp::RouterXML *router =
277 new mp::RouterXML(doc, test_config, wrbuf_cstr(base_path));
278 if (!test_config)
279 {
280#if HAVE_GETRLIMIT
281 if (socket_limit)
282 {
283 struct rlimit limit_data;
284 limit_data.rlim_cur = atoi(socket_limit);
285 limit_data.rlim_max = atoi(socket_limit);
286 int r = setrlimit(RLIMIT_NOFILE, &limit_data);
287 if (r)
288 {
289 yaz_log(YLOG_FATAL,"setrlimit: %s", strerror(errno));
290 return 1;
291 }
292
293 }
294#endif
295 yaz_sc_running(s);
296
297 yaz_daemon("metaproxy", mode | YAZ_DAEMON_LOG_REOPEN,
299 router, pidfile, uid);
300 }
301 delete router;
302 }
303 catch (std::logic_error &e) {
304 yaz_log(YLOG_FATAL,"std::logic error: %s" , e.what() );
305 ret = 1;
306 }
307 catch (std::runtime_error &e) {
308 yaz_log(YLOG_FATAL, "std::runtime error: %s" , e.what() );
309 ret = 1;
310 }
311 catch ( ... ) {
312 yaz_log(YLOG_FATAL, "Unknown Exception");
313 ret = 1;
314 }
315 xmlFreeDoc(doc);
316 if (test_config)
317 yaz_log(YLOG_LOG, "metaproxy test exit code %d", ret);
318 return ret;
319}