Fix handling of bad config
[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 static char *path_override = 0;
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     config_read_settings(config, path_override);
44
45     pazpar2_event_loop();
46 }
47
48 static void show_version(void)
49 {
50     char yaz_version_str[80];
51     printf("Pazpar2 " PACKAGE_VERSION 
52 #ifdef PAZPAR2_VERSION_SHA1
53            " "
54            PAZPAR2_VERSION_SHA1
55 #endif
56 "\n");
57
58     yaz_version(yaz_version_str, 0);
59
60     printf("Configuration:");
61 #if YAZ_HAVE_ICU
62     printf(" icu:?");
63 #endif
64     printf(" yaz:%s", yaz_version_str);
65     printf("\n");
66     exit(0);
67 }            
68
69 #ifdef WIN32
70 static int tcpip_init (void)
71 {
72     WORD requested;
73     WSADATA wd;
74
75     requested = MAKEWORD(1, 1);
76     if (WSAStartup(requested, &wd))
77         return 0;
78     return 1;
79 }
80 #endif
81
82
83 static int sc_main(
84     yaz_sc_t s, 
85     int argc, char **argv)
86 {
87     int daemon = 0;
88     int ret;
89     int log_file_in_use = 0;
90     char *arg;
91     const char *pidfile = 0;
92     const char *uid = 0;
93     int session_timeout = 60;
94     const char *listener_override = 0;
95     const char *proxy_override = 0;
96     struct conf_config *config = 0;
97
98 #ifndef WIN32
99     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
100         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
101 #else
102     tcpip_init();
103 #endif
104
105     yaz_log_init_prefix("pazpar2");
106     yaz_log_xml_errors(0, YLOG_WARN);
107
108     while ((ret = options("dDf:h:l:p:t:T:u:VX", argv, argc, &arg)) != -2)
109     {
110         switch (ret)
111         {
112         case 'd':
113             global_parameters.dump_records = 1;
114             break;
115         case 'D':
116             daemon = 1;
117             break;
118         case 'f':
119             config = config_create(arg);
120             if (!config)
121                 return 1;
122             sc_stop_config = config;
123             break;
124         case 'h':
125             listener_override = arg;
126             break;
127         case 'l':
128             yaz_log_init_file(arg);
129             log_file_in_use = 1;
130             break;
131         case 'p':
132             pidfile = arg;
133             break;
134         case 't':
135             path_override = arg;
136             break;
137         case 'T':
138             session_timeout = atoi(arg);
139             if (session_timeout < 9 || session_timeout > 86400)
140             {
141                 yaz_log(YLOG_FATAL, "Session timeout out of range 10..86400: %d",
142                         session_timeout);
143                 return 1;
144             }
145             global_parameters.session_timeout = session_timeout;
146             break;
147         case 'u':
148             uid = arg;
149             break;
150         case 'V':
151             show_version();
152         case 'X':
153             global_parameters.debug_mode = 1;
154             break;
155         default:
156             fprintf(stderr, "Usage: pazpar2\n"
157                     "    -d                      (show internal records)\n"
158                     "    -D                      Daemon mode (background)\n"
159                     "    -f configfile\n"
160                     "    -h [host:]port          (REST protocol listener)\n"
161                     "    -l file                 log to file\n"
162                     "    -p pidfile              PID file\n"
163                     "    -t settings\n"
164                     "    -T session_timeout\n"
165                     "    -u uid\n"
166                     "    -V                      show version\n"
167                     "    -X                      debug mode\n"
168 #ifdef WIN32
169                     "    -install                install windows service\n"
170                     "    -remove                 remove windows service\n"
171 #endif
172                 );
173             return 1;
174         }
175     }
176
177     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
178     if (daemon && !log_file_in_use)
179     {
180         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
181                 "mode");
182         return 1;
183     }
184     if (!config)
185     {
186         yaz_log(YLOG_FATAL, "Load config with -f");
187         return 1;
188     }
189     ret = config_start_listeners(config, listener_override, proxy_override);
190     if (ret)
191         return ret; /* error starting http listener */
192
193     yaz_sc_running(s);
194
195     yaz_daemon("pazpar2",
196                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
197                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
198                child_handler, config /* child_data */,
199                pidfile, uid);
200     return 0;
201 }
202
203
204 static void sc_stop(yaz_sc_t s)
205 {
206     config_stop_listeners(sc_stop_config);
207 }
208
209 int main(int argc, char **argv)
210 {
211     int ret;
212     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
213
214     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
215
216     yaz_sc_destroy(&s);
217     exit(ret);
218 }
219
220 /*
221  * Local variables:
222  * c-basic-offset: 4
223  * c-file-style: "Stroustrup"
224  * indent-tabs-mode: nil
225  * End:
226  * vim: shiftwidth=4 tabstop=8 expandtab
227  */
228