Remove more semi-unused globals
[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 static char *path_override = 0;
38
39 void child_handler(void *data)
40 {
41     struct conf_config *config = (struct conf_config *) data;
42     config_read_settings(config, path_override);
43
44     pazpar2_event_loop();
45 }
46
47 static void show_version(void)
48 {
49     char yaz_version_str[80];
50     printf("Pazpar2 " PACKAGE_VERSION 
51 #ifdef PAZPAR2_VERSION_SHA1
52            " "
53            PAZPAR2_VERSION_SHA1
54 #endif
55 "\n");
56
57     yaz_version(yaz_version_str, 0);
58
59     printf("Configuration:");
60 #if YAZ_HAVE_ICU
61     printf(" icu:?");
62 #endif
63     printf(" yaz:%s", yaz_version_str);
64     printf("\n");
65     exit(0);
66 }            
67
68 #ifdef WIN32
69 static int tcpip_init (void)
70 {
71     WORD requested;
72     WSADATA wd;
73
74     requested = MAKEWORD(1, 1);
75     if (WSAStartup(requested, &wd))
76         return 0;
77     return 1;
78 }
79 #endif
80
81
82 static int sc_main(
83     yaz_sc_t s, 
84     int argc, char **argv)
85 {
86     int daemon = 0;
87     int ret;
88     int log_file_in_use = 0;
89     char *arg;
90     const char *pidfile = 0;
91     const char *uid = 0;
92     int session_timeout = 60;
93     const char *listener_override = 0;
94     const char *proxy_override = 0;
95     struct conf_config *config;
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: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             config = read_config(arg);
119             if (!config)
120                 exit(1);
121             break;
122         case 'h':
123             listener_override = arg;
124             break;
125         case 'l':
126             yaz_log_init_file(arg);
127             log_file_in_use = 1;
128             break;
129         case 'p':
130             pidfile = arg;
131             break;
132         case 't':
133             path_override = arg;
134             break;
135         case 'T':
136             session_timeout = atoi(arg);
137             if (session_timeout < 9 || session_timeout > 86400)
138             {
139                 yaz_log(YLOG_FATAL, "Session timeout out of range 10..86400: %d",
140                         session_timeout);
141                 return 1;
142             }
143             global_parameters.session_timeout = session_timeout;
144             break;
145         case 'u':
146             uid = arg;
147             break;
148         case 'V':
149             show_version();
150         case 'X':
151             global_parameters.debug_mode = 1;
152             break;
153         default:
154             fprintf(stderr, "Usage: pazpar2\n"
155                     "    -d                      (show internal records)\n"
156                     "    -D                      Daemon mode (background)\n"
157                     "    -f configfile\n"
158                     "    -h [host:]port          (REST protocol listener)\n"
159                     "    -l file                 log to file\n"
160                     "    -p pidfile              PID file\n"
161                     "    -t settings\n"
162                     "    -T session_timeout\n"
163                     "    -u uid\n"
164                     "    -V                      show version\n"
165                     "    -X                      debug mode\n"
166 #ifdef WIN32
167                     "    -install                install windows service\n"
168                     "    -remove                 remove windows service\n"
169 #endif
170                 );
171             return 1;
172         }
173     }
174
175     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
176     if (daemon && !log_file_in_use)
177     {
178         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
179                 "mode");
180         return 1;
181     }
182     if (!config)
183     {
184         yaz_log(YLOG_FATAL, "Load config with -f");
185         return 1;
186     }
187     ret = start_http_listener(config, listener_override, proxy_override);
188     if (ret)
189         return ret; /* error starting http listener */
190
191     yaz_sc_running(s);
192
193     yaz_daemon("pazpar2",
194                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
195                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
196                child_handler, config /* child_data */,
197                pidfile, uid);
198     return 0;
199 }
200
201
202 static void sc_stop(yaz_sc_t s)
203 {
204     http_close_server();
205 }
206
207 int main(int argc, char **argv)
208 {
209     int ret;
210     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
211
212     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
213
214     yaz_sc_destroy(&s);
215     exit(ret);
216 }
217
218 /*
219  * Local variables:
220  * c-basic-offset: 4
221  * c-file-style: "Stroustrup"
222  * indent-tabs-mode: nil
223  * End:
224  * vim: shiftwidth=4 tabstop=8 expandtab
225  */
226