Remove support for option -t.
[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 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     config_read_settings(config);
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 = 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             config = config_create(arg);
119             if (!config)
120                 return 1;
121             sc_stop_config = config;
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             session_timeout = atoi(arg);
135             if (session_timeout < 9 || session_timeout > 86400)
136             {
137                 yaz_log(YLOG_FATAL, "Session timeout out of range 10..86400: %d",
138                         session_timeout);
139                 return 1;
140             }
141             global_parameters.session_timeout = session_timeout;
142             break;
143         case 'u':
144             uid = arg;
145             break;
146         case 'V':
147             show_version();
148         case 'X':
149             global_parameters.debug_mode = 1;
150             break;
151         default:
152             fprintf(stderr, "Usage: pazpar2\n"
153                     "    -d                      (show internal records)\n"
154                     "    -D                      Daemon mode (background)\n"
155                     "    -f configfile\n"
156                     "    -h [host:]port          (REST protocol listener)\n"
157                     "    -l file                 log to file\n"
158                     "    -p pidfile              PID file\n"
159                     "    -T session_timeout\n"
160                     "    -u uid\n"
161                     "    -V                      show version\n"
162                     "    -X                      debug mode\n"
163 #ifdef WIN32
164                     "    -install                install windows service\n"
165                     "    -remove                 remove windows service\n"
166 #endif
167                 );
168             return 1;
169         }
170     }
171
172     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
173     if (daemon && !log_file_in_use)
174     {
175         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
176                 "mode");
177         return 1;
178     }
179     if (!config)
180     {
181         yaz_log(YLOG_FATAL, "Load config with -f");
182         return 1;
183     }
184     ret = config_start_listeners(config, listener_override, proxy_override);
185     if (ret)
186         return ret; /* error starting http listener */
187
188     yaz_sc_running(s);
189
190     yaz_daemon("pazpar2",
191                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
192                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
193                child_handler, config /* child_data */,
194                pidfile, uid);
195     return 0;
196 }
197
198
199 static void sc_stop(yaz_sc_t s)
200 {
201     config_stop_listeners(sc_stop_config);
202 }
203
204 int main(int argc, char **argv)
205 {
206     int ret;
207     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
208
209     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
210
211     yaz_sc_destroy(&s);
212     exit(ret);
213 }
214
215 /*
216  * Local variables:
217  * c-basic-offset: 4
218  * c-file-style: "Stroustrup"
219  * indent-tabs-mode: nil
220  * End:
221  * vim: shiftwidth=4 tabstop=8 expandtab
222  */
223