Pazpar2 -V also shows Git SHA1.
[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 void child_handler(void *data)
38 {
39     start_proxy();
40     init_settings();
41
42     if (*global_parameters.settings_path_override)
43         settings_read(global_parameters.settings_path_override);
44     else if (global_parameters.server->settings)
45         settings_read(global_parameters.server->settings);
46     else
47         yaz_log(YLOG_WARN, "No settings-directory specified");
48     global_parameters.odr_in = odr_createmem(ODR_DECODE);
49     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
50
51
52     pazpar2_event_loop();
53
54 }
55
56 static void show_version(void)
57 {
58     char yaz_version_str[80];
59     printf("Pazpar2 " PACKAGE_VERSION 
60 #ifdef PAZPAR2_VERSION_SHA1
61            " "
62            PAZPAR2_VERSION_SHA1
63 #endif
64 "\n");
65
66     yaz_version(yaz_version_str, 0);
67
68     printf("Configuration:");
69 #if YAZ_HAVE_ICU
70     printf(" icu:?");
71 #endif
72     printf(" yaz:%s", yaz_version_str);
73     printf("\n");
74     exit(0);
75 }            
76
77 #ifdef WIN32
78 static int tcpip_init (void)
79 {
80     WORD requested;
81     WSADATA wd;
82
83     requested = MAKEWORD(1, 1);
84     if (WSAStartup(requested, &wd))
85         return 0;
86     return 1;
87 }
88 #endif
89
90
91 static int sc_main(
92     yaz_sc_t s, 
93     int argc, char **argv)
94 {
95     int daemon = 0;
96     int ret;
97     int log_file_in_use = 0;
98     char *arg;
99     const char *pidfile = 0;
100     const char *uid = 0;
101
102 #ifndef WIN32
103     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
104         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
105 #else
106     tcpip_init();
107 #endif
108
109     yaz_log_init_prefix("pazpar2");
110     yaz_log_xml_errors(0, YLOG_WARN);
111
112     while ((ret = options("dDf:h:l:p:t:u:VX", argv, argc, &arg)) != -2)
113     {
114         switch (ret)
115         {
116         case 'd':
117             global_parameters.dump_records = 1;
118             break;
119         case 'D':
120             daemon = 1;
121             break;
122         case 'f':
123             if (!read_config(arg))
124                 exit(1);
125             break;
126         case 'h':
127             strcpy(global_parameters.listener_override, arg);
128             break;
129         case 'l':
130             yaz_log_init_file(arg);
131             log_file_in_use = 1;
132             break;
133         case 'p':
134             pidfile = arg;
135             break;
136         case 't':
137             strcpy(global_parameters.settings_path_override, arg);
138             break;
139         case 'u':
140             uid = 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\n"
152                     "    -h [host:]port          (REST protocol listener)\n"
153                     "    -l file                 log to file\n"
154                     "    -p pidfile              PID file\n"
155                     "    -t settings\n"
156                     "    -u 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
168     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
169     if (daemon && !log_file_in_use)
170     {
171         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
172                 "mode");
173         return 1;
174     }
175     if (!config)
176     {
177         yaz_log(YLOG_FATAL, "Load config with -f");
178         return 1;
179     }
180     global_parameters.server = config->servers;
181
182     ret = start_http_listener();
183     if (ret)
184         return ret; /* error starting http listener */
185
186     yaz_sc_running(s);
187
188     yaz_daemon("pazpar2",
189                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
190                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
191                child_handler, 0 /* child_data */,
192                pidfile, uid);
193     return 0;
194 }
195
196
197 static void sc_stop(yaz_sc_t s)
198 {
199     http_close_server();
200 }
201
202 int main(int argc, char **argv)
203 {
204     int ret;
205     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
206
207     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
208
209     yaz_sc_destroy(&s);
210     exit(ret);
211 }
212
213 /*
214  * Local variables:
215  * c-basic-offset: 4
216  * c-file-style: "Stroustrup"
217  * indent-tabs-mode: nil
218  * End:
219  * vim: shiftwidth=4 tabstop=8 expandtab
220  */
221