Avoid conditions for YAZ features.. Require YAZ 3.0.29 or later.
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2008 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 "\n");
60
61     yaz_version(yaz_version_str, 0);
62
63     printf("Configuration:");
64 #if 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
97 #ifndef WIN32
98     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
99         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
100 #else
101     tcpip_init();
102 #endif
103
104     yaz_log_init_prefix("pazpar2");
105     yaz_log_xml_errors(0, YLOG_WARN);
106
107     while ((ret = options("dDf:h:l:p:t:u:VX", argv, argc, &arg)) != -2)
108     {
109         switch (ret)
110         {
111         case 'd':
112             global_parameters.dump_records = 1;
113             break;
114         case 'D':
115             daemon = 1;
116             break;
117         case 'f':
118             if (!read_config(arg))
119                 exit(1);
120             break;
121         case 'h':
122             strcpy(global_parameters.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             strcpy(global_parameters.settings_path_override, arg);
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\n"
147                     "    -h [host:]port          (REST protocol listener)\n"
148                     "    -l file                 log to file\n"
149                     "    -p pidfile              PID file\n"
150                     "    -t settings\n"
151                     "    -u 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
163     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
164     if (daemon && !log_file_in_use)
165     {
166         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
167                 "mode");
168         return 1;
169     }
170     if (!config)
171     {
172         yaz_log(YLOG_FATAL, "Load config with -f");
173         return 1;
174     }
175     global_parameters.server = config->servers;
176
177     ret = start_http_listener();
178     if (ret)
179         return ret; /* error starting http listener */
180
181     yaz_sc_running(s);
182
183     yaz_daemon("pazpar2",
184                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
185                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
186                child_handler, 0 /* child_data */,
187                pidfile, uid);
188     return 0;
189 }
190
191
192 static void sc_stop(yaz_sc_t s)
193 {
194     http_close_server();
195 }
196
197 int main(int argc, char **argv)
198 {
199     int ret;
200     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
201
202     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
203
204     yaz_sc_destroy(&s);
205     exit(ret);
206 }
207
208 /*
209  * Local variables:
210  * c-basic-offset: 4
211  * indent-tabs-mode: nil
212  * End:
213  * vim: shiftwidth=4 tabstop=8 expandtab
214  */