Dump configuration file during start PAZ-957
[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 #ifndef WIN32
112     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
113         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
114 #else
115     tcpip_init();
116 #endif
117
118     yaz_log_init_prefix("pazpar2");
119     yaz_log_xml_errors(0, YLOG_WARN);
120
121     while ((ret = options("dDf:h:l:m:p:R:tu:v:Vw:X", argv, argc, &arg)) != -2)
122     {
123         switch (ret)
124         {
125         case 'd':
126             global_parameters.dump_records++;
127             break;
128         case 'D':
129             daemon = 1;
130             break;
131         case 'f':
132             config_fname = arg;
133             break;
134         case 'h':
135             listener_override = arg;
136             break;
137         case 'l':
138             yaz_log_init_file(arg);
139             log_file_in_use = 1;
140             break;
141         case 'm':
142             yaz_log_time_format(arg);
143             break;
144         case 'p':
145             pidfile = arg;
146             break;
147         case 'R':
148             record_fname = arg;
149             global_parameters.predictable_sessions = 1;
150             break;
151         case 't':
152             test_mode = 1;
153             break;
154         case 'u':
155             uid = arg;
156             break;
157         case 'v':
158             yaz_log_init_level(yaz_log_mask_str(arg));
159             break;
160         case 'V':
161             show_version();
162             break;
163         case 'w':
164             if (
165 #ifdef WIN32
166               _chdir
167 #else
168               chdir
169 #endif
170                 (arg))
171             {
172                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", arg);
173                 return 1;
174             }
175             break;
176         case 'X':
177             global_parameters.debug_mode++;
178             global_parameters.predictable_sessions = 1;
179             break;
180         default:
181             fprintf(stderr, "Usage: pazpar2\n"
182                     "    -d                      Show internal records\n"
183                     "    -D                      Daemon mode (background)\n"
184                     "    -f configfile           Configuration\n"
185                     "    -h [host:]port          Listener port\n"
186                     "    -l file                 Log to file\n"
187                     "    -m logformat            log time format (strftime)\n"
188                     "    -p pidfile              PID file\n"
189                     "    -R recfile              HTTP recording file\n"
190                     "    -t                      Test configuration\n"
191                     "    -u uid                  Change user to uid\n"
192                     "    -V                      Show version\n"
193                     "    -v level                Set log level\n"
194                     "    -w dir                  Working directory\n"
195                     "    -X                      Debug mode\n"
196 #ifdef WIN32
197                     "    -install                Install windows service\n"
198                     "    -remove                 Remove windows service\n"
199 #endif
200                 );
201             return 1;
202         }
203     }
204     if (!config_fname)
205     {
206         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
207         return 1;
208     }
209     pazpar2_mutex_init();
210
211     if (!test_mode)
212     {
213         yaz_log(YLOG_LOG, "Pazpar2 start " VERSION " "
214 #ifdef PAZPAR2_VERSION_SHA1
215                 PAZPAR2_VERSION_SHA1
216 #else
217                 "-"
218 #endif
219             );
220     }
221     config = config_create(config_fname);
222     if (!config)
223         return 1;
224     sc_stop_config = config;
225     if (test_mode)
226     {
227         yaz_log(YLOG_LOG, "Configuration OK");
228         config_destroy(config);
229     }
230     else
231     {
232         ret = 0;
233         if (daemon && !log_file_in_use)
234         {
235             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
236                     "mode");
237             ret = 1;
238         }
239         if (!ret)
240             ret = config_start_listeners(config, listener_override,
241                                          record_fname);
242         if (!ret)
243         {
244             yaz_sc_running(s);
245             yaz_daemon("pazpar2",
246                        (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
247                        (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
248                        child_handler, config /* child_data */,
249                        pidfile, uid);
250         }
251         yaz_log(YLOG_LOG, "Pazpar2 stop");
252         return ret;
253     }
254     return 0;
255 }
256
257
258 static void sc_stop(yaz_sc_t s)
259 {
260     config_stop_listeners(sc_stop_config);
261 }
262
263 int main(int argc, char **argv)
264 {
265     int ret;
266     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
267
268 #ifdef MTRACE
269     mtrace();
270 #endif
271
272     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
273
274     yaz_sc_destroy(&s);
275
276 #ifdef MTRACE
277     muntrace();
278 #endif
279
280
281     exit(ret);
282 }
283
284 /*
285  * Local variables:
286  * c-basic-offset: 4
287  * c-file-style: "Stroustrup"
288  * indent-tabs-mode: nil
289  * End:
290  * vim: shiftwidth=4 tabstop=8 expandtab
291  */
292