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