Refactor PP2 charsets handling, use pazpar2_mutex.
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 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 "parameters.h"
31 #include "session.h"
32 #include "ppmutex.h"
33 #include <yaz/daemon.h>
34 #include <yaz/log.h>
35 #include <yaz/options.h>
36 #include <yaz/sc.h>
37
38 static struct conf_config *sc_stop_config = 0;
39
40 void child_handler(void *data)
41 {
42     struct conf_config *config = (struct conf_config *) data;
43
44     config_process_events(config);
45
46     config_destroy(config);
47 }
48
49 static void show_version(void)
50 {
51     char yaz_version_str[80];
52     printf("Pazpar2 " PACKAGE_VERSION 
53 #ifdef PAZPAR2_VERSION_SHA1
54            " "
55            PAZPAR2_VERSION_SHA1
56 #endif
57 "\n");
58
59     yaz_version(yaz_version_str, 0);
60
61     printf("Configuration:");
62 #if YAZ_HAVE_ICU
63     printf(" icu:enabled");
64 #else
65     printf(" icu:disabled");
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     const char *listener_override = 0;
97     const char *config_fname = 0;
98     struct conf_config *config = 0;
99     int test_mode = 0;
100
101 #ifndef WIN32
102     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
103         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
104 #else
105     tcpip_init();
106 #endif
107
108     yaz_log_init_prefix("pazpar2");
109     yaz_log_xml_errors(0, YLOG_WARN);
110
111     while ((ret = options("dDf:h:l:p:tu:v:VX", argv, argc, &arg)) != -2)
112     {
113         switch (ret)
114         {
115         case 'd':
116             global_parameters.dump_records = 1;
117             break;
118         case 'D':
119             daemon = 1;
120             break;
121         case 'f':
122             config_fname = arg;
123             break;
124         case 'h':
125             listener_override = arg;
126             break;
127         case 'l':
128             yaz_log_init_file(arg);
129             log_file_in_use = 1;
130             break;
131         case 'p':
132             pidfile = arg;
133             break;
134         case 't':
135             test_mode = 1;
136             break;
137         case 'u':
138             uid = arg;
139             break;
140         case 'v':
141             yaz_log_init_level(yaz_log_mask_str(arg));
142             break;
143         case 'V':
144             show_version();
145         case 'X':
146             global_parameters.debug_mode++;
147             break;
148         default:
149             fprintf(stderr, "Usage: pazpar2\n"
150                     "    -d                      Show internal records\n"
151                     "    -D                      Daemon mode (background)\n"
152                     "    -f configfile           Configuration\n"
153                     "    -h [host:]port          Listener port\n"
154                     "    -l file                 Log to file\n"
155                     "    -p pidfile              PID file\n"
156                     "    -t                      Test configuration\n"
157                     "    -u uid                  Change user to uid\n"
158                     "    -V                      Show version\n"
159                     "    -v level                Set log level\n"
160                     "    -X                      Debug mode\n"
161 #ifdef WIN32
162                     "    -install                Install windows service\n"
163                     "    -remove                 Remove windows service\n"
164 #endif
165                 );
166             return 1;
167         }
168     }
169     if (!config_fname)
170     {
171         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
172         return 1;
173     }
174     if (global_parameters.debug_mode > 1)
175         pazpar2_mutex_enable_debug(1);
176     
177     config = config_create(config_fname, global_parameters.dump_records);
178     if (!config)
179         return 1;
180     sc_stop_config = config;
181     if (test_mode)
182     {
183         yaz_log(YLOG_LOG, "Configuration OK");
184         config_destroy(config);
185     }
186     else
187     {
188         yaz_log(YLOG_LOG, "Pazpar2 " VERSION  " "
189 #ifdef PAZPAR2_VERSION_SHA1
190                 PAZPAR2_VERSION_SHA1
191 #else
192                 "-"
193 #endif
194                 " started");
195         if (daemon && !log_file_in_use)
196         {
197             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
198                     "mode");
199             return 1;
200         }
201         ret = config_start_listeners(config, listener_override);
202         if (ret)
203             return ret; /* error starting http listener */
204         
205         yaz_sc_running(s);
206         
207         yaz_daemon("pazpar2",
208                    (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
209                    (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
210                    child_handler, config /* child_data */,
211                    pidfile, uid);
212     }
213     return 0;
214 }
215
216
217 static void sc_stop(yaz_sc_t s)
218 {
219     config_stop_listeners(sc_stop_config);
220 }
221
222 int main(int argc, char **argv)
223 {
224     int ret;
225     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
226
227     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
228
229     yaz_sc_destroy(&s);
230     exit(ret);
231 }
232
233 /*
234  * Local variables:
235  * c-basic-offset: 4
236  * c-file-style: "Stroustrup"
237  * indent-tabs-mode: nil
238  * End:
239  * vim: shiftwidth=4 tabstop=8 expandtab
240  */
241