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