Timeout per-service, obsoletes -T
[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 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
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     const char *listener_override = 0;
94     const char *config_fname = 0;
95     struct conf_config *config = 0;
96     int test_mode = 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:tu: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_fname = arg;
120             break;
121         case 'h':
122             listener_override = arg;
123             break;
124         case 'l':
125             yaz_log_init_file(arg);
126             log_file_in_use = 1;
127             break;
128         case 'p':
129             pidfile = arg;
130             break;
131         case 't':
132             test_mode = 1;
133             break;
134         case 'u':
135             uid = arg;
136             break;
137         case 'V':
138             show_version();
139         case 'X':
140             global_parameters.debug_mode = 1;
141             break;
142         default:
143             fprintf(stderr, "Usage: pazpar2\n"
144                     "    -d                      Show internal records\n"
145                     "    -D                      Daemon mode (background)\n"
146                     "    -f configfile           Configuration\n"
147                     "    -h [host:]port          Listener port\n"
148                     "    -l file                 Log to file\n"
149                     "    -p pidfile              PID file\n"
150                     "    -t                      Test configuration\n"
151                     "    -u uid                  Change user to uid\n"
152                     "    -V                      Show version\n"
153                     "    -X                      Debug mode\n"
154 #ifdef WIN32
155                     "    -install                Install windows service\n"
156                     "    -remove                 Remove windows service\n"
157 #endif
158                 );
159             return 1;
160         }
161     }
162     if (!config_fname)
163     {
164         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
165         return 1;
166     }
167     config = config_create(config_fname, global_parameters.dump_records);
168     if (!config)
169         return 1;
170     sc_stop_config = config;
171     if (test_mode)
172     {
173         yaz_log(YLOG_LOG, "Configuration OK");
174         config_destroy(config);
175     }
176     else
177     {
178         yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
179         if (daemon && !log_file_in_use)
180         {
181             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
182                     "mode");
183             return 1;
184         }
185         ret = config_start_listeners(config, listener_override);
186         if (ret)
187             return ret; /* error starting http listener */
188         
189         yaz_sc_running(s);
190         
191         yaz_daemon("pazpar2",
192                    (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
193                    (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
194                    child_handler, config /* child_data */,
195                    pidfile, uid);
196     }
197     return 0;
198 }
199
200
201 static void sc_stop(yaz_sc_t s)
202 {
203     config_stop_listeners(sc_stop_config);
204 }
205
206 int main(int argc, char **argv)
207 {
208     int ret;
209     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
210
211     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
212
213     yaz_sc_destroy(&s);
214     exit(ret);
215 }
216
217 /*
218  * Local variables:
219  * c-basic-offset: 4
220  * c-file-style: "Stroustrup"
221  * indent-tabs-mode: nil
222  * End:
223  * vim: shiftwidth=4 tabstop=8 expandtab
224  */
225