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