b3928aa4628efaa1c1b314603c713f5bd63edd3a
[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[20];
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     {
218         char yaz_version_str[20];
219         char yaz_sha1_str[41];
220         yaz_log(YLOG_LOG, "Pazpar2 %s " VERSION " "
221 #ifdef PAZPAR2_VERSION_SHA1
222                 PAZPAR2_VERSION_SHA1
223 #else
224                 "-"
225 #endif
226                 , test_mode ? "test" : "start");
227         yaz_version(yaz_version_str, yaz_sha1_str);
228         yaz_log(YLOG_LOG, "YAZ %s %s", yaz_version_str, yaz_sha1_str);
229     }
230
231     config = config_create(config_fname);
232     if (!config)
233         return 1;
234     sc_stop_config = config;
235     if (test_mode)
236     {
237         yaz_log(YLOG_LOG, "Configuration OK");
238         config_destroy(config);
239     }
240     else
241     {
242         ret = 0;
243         if (daemon && !log_file_in_use)
244         {
245             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
246                     "mode");
247             ret = 1;
248         }
249         if (!ret)
250             ret = config_start_listeners(config, listener_override,
251                                          record_fname);
252         if (!ret)
253         {
254             yaz_sc_running(s);
255             yaz_daemon("pazpar2",
256                        (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
257                        (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
258                        child_handler, config /* child_data */,
259                        pidfile, uid);
260         }
261         yaz_log(YLOG_LOG, "Pazpar2 stop");
262         return ret;
263     }
264     return 0;
265 }
266
267
268 static void sc_stop(yaz_sc_t s)
269 {
270     config_stop_listeners(sc_stop_config);
271 }
272
273 int main(int argc, char **argv)
274 {
275     int ret;
276     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
277
278 #ifdef MTRACE
279     mtrace();
280 #endif
281
282     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
283
284     yaz_sc_destroy(&s);
285
286 #ifdef MTRACE
287     muntrace();
288 #endif
289
290
291     exit(ret);
292 }
293
294 /*
295  * Local variables:
296  * c-basic-offset: 4
297  * c-file-style: "Stroustrup"
298  * indent-tabs-mode: nil
299  * End:
300  * vim: shiftwidth=4 tabstop=8 expandtab
301  */
302