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