For bad timeout value, use yaz_log and halt Pazpar2
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 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 "pazpar2.h"
31 #include "database.h"
32 #include "settings.h"
33 #include <yaz/daemon.h>
34
35 #include <yaz/sc.h>
36
37 void child_handler(void *data)
38 {
39     start_proxy();
40     init_settings();
41
42     if (*global_parameters.settings_path_override)
43         settings_read(global_parameters.settings_path_override);
44     else if (global_parameters.server->settings)
45         settings_read(global_parameters.server->settings);
46     else
47         yaz_log(YLOG_WARN, "No settings-directory specified");
48     global_parameters.odr_in = odr_createmem(ODR_DECODE);
49     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
50
51
52     pazpar2_event_loop();
53
54 }
55
56 static void show_version(void)
57 {
58     char yaz_version_str[80];
59     printf("Pazpar2 " PACKAGE_VERSION 
60 #ifdef PAZPAR2_VERSION_SHA1
61            " "
62            PAZPAR2_VERSION_SHA1
63 #endif
64 "\n");
65
66     yaz_version(yaz_version_str, 0);
67
68     printf("Configuration:");
69 #if YAZ_HAVE_ICU
70     printf(" icu:?");
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     int session_timeout = 60;
102
103 #ifndef WIN32
104     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
105         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
106 #else
107     tcpip_init();
108 #endif
109
110     yaz_log_init_prefix("pazpar2");
111     yaz_log_xml_errors(0, YLOG_WARN);
112
113     while ((ret = options("dDf:h:l:p:t:T:u:VX", argv, argc, &arg)) != -2)
114     {
115         switch (ret)
116         {
117         case 'd':
118             global_parameters.dump_records = 1;
119             break;
120         case 'D':
121             daemon = 1;
122             break;
123         case 'f':
124             if (!read_config(arg))
125                 exit(1);
126             break;
127         case 'h':
128             strcpy(global_parameters.listener_override, arg);
129             break;
130         case 'l':
131             yaz_log_init_file(arg);
132             log_file_in_use = 1;
133             break;
134         case 'p':
135             pidfile = arg;
136             break;
137         case 't':
138             strcpy(global_parameters.settings_path_override, arg);
139             break;
140         case 'T':
141             session_timeout = atoi(arg);
142             if (session_timeout < 9 || session_timeout > 86400)
143             {
144                 yaz_log(YLOG_FATAL, "Session timeout out of range 10..86400: %d",
145                         session_timeout);
146                 return 1;
147             }
148             global_parameters.session_timeout = session_timeout;
149             break;
150         case 'u':
151             uid = arg;
152             break;
153         case 'V':
154             show_version();
155         case 'X':
156             global_parameters.debug_mode = 1;
157             break;
158         default:
159             fprintf(stderr, "Usage: pazpar2\n"
160                     "    -d                      (show internal records)\n"
161                     "    -D                      Daemon mode (background)\n"
162                     "    -f configfile\n"
163                     "    -h [host:]port          (REST protocol listener)\n"
164                     "    -l file                 log to file\n"
165                     "    -p pidfile              PID file\n"
166                     "    -t settings\n"
167                     "    -T session_timeout\n"
168                     "    -u uid\n"
169                     "    -V                      show version\n"
170                     "    -X                      debug mode\n"
171 #ifdef WIN32
172                     "    -install                install windows service\n"
173                     "    -remove                 remove windows service\n"
174 #endif
175                 );
176             return 1;
177         }
178     }
179
180     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
181     if (daemon && !log_file_in_use)
182     {
183         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
184                 "mode");
185         return 1;
186     }
187     if (!config)
188     {
189         yaz_log(YLOG_FATAL, "Load config with -f");
190         return 1;
191     }
192     global_parameters.server = config->servers;
193
194     ret = start_http_listener();
195     if (ret)
196         return ret; /* error starting http listener */
197
198     yaz_sc_running(s);
199
200     yaz_daemon("pazpar2",
201                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
202                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
203                child_handler, 0 /* child_data */,
204                pidfile, uid);
205     return 0;
206 }
207
208
209 static void sc_stop(yaz_sc_t s)
210 {
211     http_close_server();
212 }
213
214 int main(int argc, char **argv)
215 {
216     int ret;
217     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
218
219     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
220
221     yaz_sc_destroy(&s);
222     exit(ret);
223 }
224
225 /*
226  * Local variables:
227  * c-basic-offset: 4
228  * c-file-style: "Stroustrup"
229  * indent-tabs-mode: nil
230  * End:
231  * vim: shiftwidth=4 tabstop=8 expandtab
232  */
233