Incorporate Windos services (sc).
[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 #include <yaz/sc.h>
35
36 void child_handler(void *data)
37 {
38     start_proxy();
39     init_settings();
40
41     if (*global_parameters.settings_path_override)
42         settings_read(global_parameters.settings_path_override);
43     else if (global_parameters.server->settings)
44         settings_read(global_parameters.server->settings);
45     else
46         yaz_log(YLOG_WARN, "No settings-directory specified");
47     global_parameters.odr_in = odr_createmem(ODR_DECODE);
48     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
49
50
51     pazpar2_event_loop();
52
53 }
54
55 static void show_version(void)
56 {
57     char yaz_version_str[80];
58     printf("Pazpar2 " PACKAGE_VERSION "\n");
59
60     yaz_version(yaz_version_str, 0);
61
62     printf("Configuration:");
63 #if HAVE_ICU
64     printf(" icu:?");
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(yaz_sc_t s, int argc, char **argv)
86 {
87     int daemon = 0;
88     int ret;
89     int log_file_in_use = 0;
90     char *arg;
91     const char *pidfile = 0;
92     const char *uid = 0;
93
94 #ifndef WIN32
95     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
96         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
97 #else
98     tcpip_init();
99 #endif
100
101     yaz_log_init_prefix("pazpar2");
102 #if YAZ_VERSIONL >= 0x03001B
103     yaz_log_xml_errors(0, YLOG_WARN);
104 #endif
105
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             if (!read_config(arg))
119                 exit(1);
120             break;
121         case 'h':
122             strcpy(global_parameters.listener_override, arg);
123             break;
124         case 'l':
125             yaz_log_init_file(arg);
126             log_file_in_use = 1;
127             break;
128         case 'p':
129             pidfile = arg;
130             break;
131         case 't':
132             strcpy(global_parameters.settings_path_override, arg);
133             break;
134         case 'u':
135             uid = arg;
136             break;
137         case 'V':
138             show_version();
139         case 'X':
140             global_parameters.debug_mode = 1;
141             break;
142         default:
143             fprintf(stderr, "Usage: pazpar2\n"
144                     "    -d                      (show internal records)\n"
145                     "    -D                      Daemon mode (background)\n"
146                     "    -f configfile\n"
147                     "    -h [host:]port          (REST protocol listener)\n"
148                     "    -l file                 log to file\n"
149                     "    -p pidfile              PID file\n"
150                     "    -t settings\n"
151                     "    -u uid\n"
152                     "    -V                      show version\n"
153                     "    -X                      debug mode\n"
154                 );
155             exit(1);
156         }
157     }
158
159     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
160     if (daemon && !log_file_in_use)
161     {
162         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
163                 "mode");
164         exit(1);
165     }
166     if (!config)
167     {
168         yaz_log(YLOG_FATAL, "Load config with -f");
169         exit(1);
170     }
171     global_parameters.server = config->servers;
172
173     ret = start_http_listener();
174     if (ret)
175         return ret; /* error starting http listener */
176
177     yaz_sc_running(s);
178
179     yaz_daemon("pazpar2",
180                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
181                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
182                child_handler, 0 /* child_data */,
183                pidfile, uid);
184     return 0;
185 }
186
187
188 static void sc_stop(yaz_sc_t s)
189 {
190
191 }
192
193 int main(int argc, char **argv)
194 {
195     int ret;
196     yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar Metasearcher");
197
198     ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
199
200     yaz_sc_destroy(&s);
201     exit(ret);
202 }
203
204 /*
205  * Local variables:
206  * c-basic-offset: 4
207  * indent-tabs-mode: nil
208  * End:
209  * vim: shiftwidth=4 tabstop=8 expandtab
210  */