Databases per-service.
[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
39 void child_handler(void *data)
40 {
41     start_proxy();
42
43     config_read_settings(path_override);
44
45     global_parameters.odr_in = odr_createmem(ODR_DECODE);
46     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
47
48     pazpar2_event_loop();
49 }
50
51 static void show_version(void)
52 {
53     char yaz_version_str[80];
54     printf("Pazpar2 " PACKAGE_VERSION 
55 #ifdef PAZPAR2_VERSION_SHA1
56            " "
57            PAZPAR2_VERSION_SHA1
58 #endif
59 "\n");
60
61     yaz_version(yaz_version_str, 0);
62
63     printf("Configuration:");
64 #if YAZ_HAVE_ICU
65     printf(" icu:?");
66 #endif
67     printf(" yaz:%s", yaz_version_str);
68     printf("\n");
69     exit(0);
70 }            
71
72 #ifdef WIN32
73 static int tcpip_init (void)
74 {
75     WORD requested;
76     WSADATA wd;
77
78     requested = MAKEWORD(1, 1);
79     if (WSAStartup(requested, &wd))
80         return 0;
81     return 1;
82 }
83 #endif
84
85
86 static int sc_main(
87     yaz_sc_t s, 
88     int argc, char **argv)
89 {
90     int daemon = 0;
91     int ret;
92     int log_file_in_use = 0;
93     char *arg;
94     const char *pidfile = 0;
95     const char *uid = 0;
96     int session_timeout = 60;
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             if (!read_config(arg))
120                 exit(1);
121             break;
122         case 'h':
123             strcpy(global_parameters.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             path_override = arg;
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\n"
158                     "    -h [host:]port          (REST protocol listener)\n"
159                     "    -l file                 log to file\n"
160                     "    -p pidfile              PID file\n"
161                     "    -t settings\n"
162                     "    -T session_timeout\n"
163                     "    -u 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
175     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
176     if (daemon && !log_file_in_use)
177     {
178         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
179                 "mode");
180         return 1;
181     }
182     if (!config)
183     {
184         yaz_log(YLOG_FATAL, "Load config with -f");
185         return 1;
186     }
187     global_parameters.server = config->servers;
188
189     ret = start_http_listener();
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, 0 /* child_data */,
199                pidfile, uid);
200     return 0;
201 }
202
203
204 static void sc_stop(yaz_sc_t s)
205 {
206     http_close_server();
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