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