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