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