Initialize Libxml2 - for threaded operation PAZ-961
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* This file is part of Pazpar2.
2    Copyright (C) Index Data
3
4 Pazpar2 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 Pazpar2 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
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #ifdef WIN32
24 #include <winsock.h>
25 #include <direct.h>
26 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #include <signal.h>
32 #include <assert.h>
33
34 #include "parameters.h"
35 #include "session.h"
36 #include "ppmutex.h"
37 #include <yaz/daemon.h>
38 #include <yaz/log.h>
39 #include <yaz/options.h>
40 #include <yaz/sc.h>
41
42 // #define MTRACE
43 #ifdef MTRACE
44 #include <mcheck.h>
45 #endif
46
47 static struct conf_config *sc_stop_config = 0;
48
49 void child_handler(void *data)
50 {
51     struct conf_config *config = (struct conf_config *) data;
52
53     config_process_events(config);
54
55     config_destroy(config);
56 }
57
58 static void show_version(void)
59 {
60     char yaz_version_str[80];
61     printf("Pazpar2 " PACKAGE_VERSION
62 #ifdef PAZPAR2_VERSION_SHA1
63            " "
64            PAZPAR2_VERSION_SHA1
65 #endif
66 "\n");
67
68     yaz_version(yaz_version_str, 0);
69
70     printf("Configuration:");
71 #if YAZ_HAVE_ICU
72     printf(" icu:enabled");
73 #else
74     printf(" icu:disabled");
75 #endif
76     printf(" yaz:%s", yaz_version_str);
77     printf("\n");
78     exit(0);
79 }
80
81 #ifdef WIN32
82 static int tcpip_init (void)
83 {
84     WORD requested;
85     WSADATA wd;
86
87     requested = MAKEWORD(1, 1);
88     if (WSAStartup(requested, &wd))
89         return 0;
90     return 1;
91 }
92 #endif
93
94
95 static int sc_main(
96     yaz_sc_t s,
97     int argc, char **argv)
98 {
99     int daemon = 0;
100     int ret;
101     int log_file_in_use = 0;
102     char *arg;
103     const char *pidfile = 0;
104     const char *uid = 0;
105     const char *listener_override = 0;
106     const char *config_fname = 0;
107     const char *record_fname = 0;
108     struct conf_config *config = 0;
109     int test_mode = 0;
110
111     xmlInitParser();
112     LIBXML_TEST_VERSION
113
114 #ifndef WIN32
115     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
116         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
117 #else
118     tcpip_init();
119 #endif
120
121     yaz_log_init_prefix("pazpar2");
122     yaz_log_xml_errors(0, YLOG_WARN);
123
124     while ((ret = options("dDf:h:l:m:p:R:tu:v:Vw:X", argv, argc, &arg)) != -2)
125     {
126         switch (ret)
127         {
128         case 'd':
129             global_parameters.dump_records++;
130             break;
131         case 'D':
132             daemon = 1;
133             break;
134         case 'f':
135             config_fname = arg;
136             break;
137         case 'h':
138             listener_override = arg;
139             break;
140         case 'l':
141             yaz_log_init_file(arg);
142             log_file_in_use = 1;
143             break;
144         case 'm':
145             yaz_log_time_format(arg);
146             break;
147         case 'p':
148             pidfile = arg;
149             break;
150         case 'R':
151             record_fname = arg;
152             global_parameters.predictable_sessions = 1;
153             break;
154         case 't':
155             test_mode = 1;
156             break;
157         case 'u':
158             uid = arg;
159             break;
160         case 'v':
161             yaz_log_init_level(yaz_log_mask_str(arg));
162             break;
163         case 'V':
164             show_version();
165             break;
166         case 'w':
167             if (
168 #ifdef WIN32
169               _chdir
170 #else
171               chdir
172 #endif
173                 (arg))
174             {
175                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", arg);
176                 return 1;
177             }
178             break;
179         case 'X':
180             global_parameters.debug_mode++;
181             global_parameters.predictable_sessions = 1;
182             break;
183         default:
184             fprintf(stderr, "Usage: pazpar2\n"
185                     "    -d                      Show internal records\n"
186                     "    -D                      Daemon mode (background)\n"
187                     "    -f configfile           Configuration\n"
188                     "    -h [host:]port          Listener port\n"
189                     "    -l file                 Log to file\n"
190                     "    -m logformat            log time format (strftime)\n"
191                     "    -p pidfile              PID file\n"
192                     "    -R recfile              HTTP recording file\n"
193                     "    -t                      Test configuration\n"
194                     "    -u uid                  Change user to uid\n"
195                     "    -V                      Show version\n"
196                     "    -v level                Set log level\n"
197                     "    -w dir                  Working directory\n"
198                     "    -X                      Debug mode\n"
199 #ifdef WIN32
200                     "    -install                Install windows service\n"
201                     "    -remove                 Remove windows service\n"
202 #endif
203                 );
204             return 1;
205         }
206     }
207     if (!config_fname)
208     {
209         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
210         return 1;
211     }
212     pazpar2_mutex_init();
213
214     if (!test_mode)
215     {
216         yaz_log(YLOG_LOG, "Pazpar2 start " VERSION " "
217 #ifdef PAZPAR2_VERSION_SHA1
218                 PAZPAR2_VERSION_SHA1
219 #else
220                 "-"
221 #endif
222             );
223     }
224     config = config_create(config_fname);
225     if (!config)
226         return 1;
227     sc_stop_config = config;
228     if (test_mode)
229     {
230         yaz_log(YLOG_LOG, "Configuration OK");
231         config_destroy(config);
232     }
233     else
234     {
235         ret = 0;
236         if (daemon && !log_file_in_use)
237         {
238             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
239                     "mode");
240             ret = 1;
241         }
242         if (!ret)
243             ret = config_start_listeners(config, listener_override,
244                                          record_fname);
245         if (!ret)
246         {
247             yaz_sc_running(s);
248             yaz_daemon("pazpar2",
249                        (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
250                        (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
251                        child_handler, config /* child_data */,
252                        pidfile, uid);
253         }
254         yaz_log(YLOG_LOG, "Pazpar2 stop");
255         return ret;
256     }
257     return 0;
258 }
259
260
261 static void sc_stop(yaz_sc_t s)
262 {
263     config_stop_listeners(sc_stop_config);
264 }
265
266 int main(int argc, char **argv)
267 {
268     int ret;
269     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
270
271 #ifdef MTRACE
272     mtrace();
273 #endif
274
275     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
276
277     yaz_sc_destroy(&s);
278
279 #ifdef MTRACE
280     muntrace();
281 #endif
282
283
284     exit(ret);
285 }
286
287 /*
288  * Local variables:
289  * c-basic-offset: 4
290  * c-file-style: "Stroustrup"
291  * indent-tabs-mode: nil
292  * End:
293  * vim: shiftwidth=4 tabstop=8 expandtab
294  */
295