Embeddable settings (repeatable).
[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     int session_timeout = 60;
94     const char *listener_override = 0;
95     const char *config_fname = 0;
96     struct conf_config *config = 0;
97     int test_mode = 0;
98
99 #ifndef WIN32
100     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
101         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
102 #else
103     tcpip_init();
104 #endif
105
106     yaz_log_init_prefix("pazpar2");
107     yaz_log_xml_errors(0, YLOG_WARN);
108
109     while ((ret = options("dDf:h:l:p:tT:u:VX", argv, argc, &arg)) != -2)
110     {
111         switch (ret)
112         {
113         case 'd':
114             global_parameters.dump_records = 1;
115             break;
116         case 'D':
117             daemon = 1;
118             break;
119         case 'f':
120             config_fname = arg;
121             break;
122         case 'h':
123             listener_override = arg;
124             break;
125         case 'l':
126             yaz_log_init_file(arg);
127             log_file_in_use = 1;
128             break;
129         case 'p':
130             pidfile = arg;
131             break;
132         case 't':
133             test_mode = 1;
134             break;
135         case 'T':
136             session_timeout = atoi(arg);
137             if (session_timeout < 9 || session_timeout > 86400)
138             {
139                 yaz_log(YLOG_FATAL, "Session timeout out of range 10..86400: %d",
140                         session_timeout);
141                 return 1;
142             }
143             global_parameters.session_timeout = session_timeout;
144             break;
145         case 'u':
146             uid = arg;
147             break;
148         case 'V':
149             show_version();
150         case 'X':
151             global_parameters.debug_mode = 1;
152             break;
153         default:
154             fprintf(stderr, "Usage: pazpar2\n"
155                     "    -d                      Show internal records\n"
156                     "    -D                      Daemon mode (background)\n"
157                     "    -f configfile           Configuration\n"
158                     "    -h [host:]port          Listener port\n"
159                     "    -l file                 Log to file\n"
160                     "    -p pidfile              PID file\n"
161                     "    -t                      Test configuration\n"
162                     "    -T session_timeout      Session timeout\n"
163                     "    -u uid                  Change user to uid\n"
164                     "    -V                      Show version\n"
165                     "    -X                      Debug mode\n"
166 #ifdef WIN32
167                     "    -install                Install windows service\n"
168                     "    -remove                 Remove windows service\n"
169 #endif
170                 );
171             return 1;
172         }
173     }
174     if (!config_fname)
175     {
176         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
177         return 1;
178     }
179     config = config_create(config_fname, global_parameters.dump_records);
180     if (!config)
181         return 1;
182     sc_stop_config = config;
183     if (test_mode)
184     {
185         yaz_log(YLOG_LOG, "Configuration OK");
186         config_destroy(config);
187     }
188     else
189     {
190         yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
191         if (daemon && !log_file_in_use)
192         {
193             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
194                     "mode");
195             return 1;
196         }
197         ret = config_start_listeners(config, listener_override);
198         if (ret)
199             return ret; /* error starting http listener */
200         
201         yaz_sc_running(s);
202         
203         yaz_daemon("pazpar2",
204                    (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
205                    (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
206                    child_handler, config /* child_data */,
207                    pidfile, uid);
208     }
209     return 0;
210 }
211
212
213 static void sc_stop(yaz_sc_t s)
214 {
215     config_stop_listeners(sc_stop_config);
216 }
217
218 int main(int argc, char **argv)
219 {
220     int ret;
221     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
222
223     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
224
225     yaz_sc_destroy(&s);
226     exit(ret);
227 }
228
229 /*
230  * Local variables:
231  * c-basic-offset: 4
232  * c-file-style: "Stroustrup"
233  * indent-tabs-mode: nil
234  * End:
235  * vim: shiftwidth=4 tabstop=8 expandtab
236  */
237