Log level may be set (option -v)
[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 "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                     "    -X                      Debug mode\n"
159 #ifdef WIN32
160                     "    -install                Install windows service\n"
161                     "    -remove                 Remove windows service\n"
162 #endif
163                 );
164             return 1;
165         }
166     }
167     if (!config_fname)
168     {
169         yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
170         return 1;
171     }
172     config = config_create(config_fname, global_parameters.dump_records);
173     if (!config)
174         return 1;
175     sc_stop_config = config;
176     if (test_mode)
177     {
178         yaz_log(YLOG_LOG, "Configuration OK");
179         config_destroy(config);
180     }
181     else
182     {
183         yaz_log(YLOG_LOG, "Pazpar2 " VERSION  " "
184 #ifdef PAZPAR2_VERSION_SHA1
185                 PAZPAR2_VERSION_SHA1
186 #else
187                 "-"
188 #endif
189                 " started");
190         if (daemon && !log_file_in_use)
191         {
192             yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
193                     "mode");
194             return 1;
195         }
196         ret = config_start_listeners(config, listener_override);
197         if (ret)
198             return ret; /* error starting http listener */
199         
200         yaz_sc_running(s);
201         
202         yaz_daemon("pazpar2",
203                    (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
204                    (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
205                    child_handler, config /* child_data */,
206                    pidfile, uid);
207     }
208     return 0;
209 }
210
211
212 static void sc_stop(yaz_sc_t s)
213 {
214     config_stop_listeners(sc_stop_config);
215 }
216
217 int main(int argc, char **argv)
218 {
219     int ret;
220     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
221
222     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
223
224     yaz_sc_destroy(&s);
225     exit(ret);
226 }
227
228 /*
229  * Local variables:
230  * c-basic-offset: 4
231  * c-file-style: "Stroustrup"
232  * indent-tabs-mode: nil
233  * End:
234  * vim: shiftwidth=4 tabstop=8 expandtab
235  */
236