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