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