pazpar2  1.13.0
pazpar2.c
Go to the documentation of this file.
1 /* This file is part of Pazpar2.
2  Copyright (C) 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 #include <direct.h>
26 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 
31 #include <signal.h>
32 #include <assert.h>
33 
34 #include "parameters.h"
35 #include "session.h"
36 #include "ppmutex.h"
37 #include <yaz/daemon.h>
38 #include <yaz/log.h>
39 #include <yaz/options.h>
40 #include <yaz/sc.h>
41 #include <yaz/backtrace.h>
42 
43 // #define MTRACE
44 #ifdef MTRACE
45 #include <mcheck.h>
46 #endif
47 
48 static struct conf_config *sc_stop_config = 0;
49 
50 void child_handler(void *data)
51 {
52  struct conf_config *config = (struct conf_config *) data;
53 
54  config_process_events(config);
55 
56  config_destroy(config);
57 }
58 
59 static void show_version(void)
60 {
61  char yaz_version_str[20];
62  printf("Pazpar2 " PACKAGE_VERSION
64  " "
66 #endif
67 "\n");
68 
69  yaz_version(yaz_version_str, 0);
70 
71  printf("Configuration:");
72 #if YAZ_HAVE_ICU
73  printf(" icu:enabled");
74 #else
75  printf(" icu:disabled");
76 #endif
77  printf(" yaz:%s", yaz_version_str);
78  printf("\n");
79  exit(0);
80 }
81 
82 #ifdef WIN32
83 static int tcpip_init (void)
84 {
85  WORD requested;
86  WSADATA wd;
87 
88  requested = MAKEWORD(1, 1);
89  if (WSAStartup(requested, &wd))
90  return 0;
91  return 1;
92 }
93 #endif
94 
95 
96 static int sc_main(
97  yaz_sc_t s,
98  int argc, char **argv)
99 {
100  int daemon = 0;
101  int ret;
102  int log_file_in_use = 0;
103  char *arg;
104  const char *pidfile = 0;
105  const char *uid = 0;
106  const char *listener_override = 0;
107  const char *config_fname = 0;
108  const char *record_fname = 0;
109  struct conf_config *config = 0;
110  int test_mode = 0;
111 
112  xmlInitParser();
113  LIBXML_TEST_VERSION
114 
115 #ifndef WIN32
116  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
117  yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
118 #else
119  tcpip_init();
120 #endif
121 
122  yaz_log_init_prefix("pazpar2");
123  yaz_log_init_level(yaz_log_mask_str("post"));
124  yaz_log_xml_errors(0, YLOG_WARN);
125 
126  yaz_enable_panic_backtrace(argv[0]);
127 
128  while ((ret = options("dDf:h:l:m:p:R:tu:v:Vw:X", argv, argc, &arg)) != -2)
129  {
130  switch (ret)
131  {
132  case 'd':
134  break;
135  case 'D':
136  daemon = 1;
137  break;
138  case 'f':
139  config_fname = arg;
140  break;
141  case 'h':
142  listener_override = arg;
143  break;
144  case 'l':
145  yaz_log_init_file(arg);
146  log_file_in_use = 1;
147  break;
148  case 'm':
149  yaz_log_time_format(arg);
150  break;
151  case 'p':
152  pidfile = arg;
153  break;
154  case 'R':
155  if (strcmp(arg, "-"))
156  record_fname = arg;
158  break;
159  case 't':
160  test_mode = 1;
161  break;
162  case 'u':
163  uid = arg;
164  break;
165  case 'v':
166  yaz_log_init_level(yaz_log_mask_str(arg));
167  break;
168  case 'V':
169  show_version();
170  break;
171  case 'w':
172  if (
173 #ifdef WIN32
174  _chdir
175 #else
176  chdir
177 #endif
178  (arg))
179  {
180  yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", arg);
181  return 1;
182  }
183  break;
184  case 'X':
187  break;
188  default:
189  fprintf(stderr, "Usage: pazpar2\n"
190  " -d Show internal records\n"
191  " -D Daemon mode (background)\n"
192  " -f configfile Configuration\n"
193  " -h [host:]port Listener port\n"
194  " -l file Log to file\n"
195  " -m logformat log time format (strftime)\n"
196  " -p pidfile PID file\n"
197  " -R recfile HTTP recording file\n"
198  " -t Test configuration\n"
199  " -u uid Change user to uid\n"
200  " -V Show version\n"
201  " -v level Set log level\n"
202  " -w dir Working directory\n"
203  " -X Debug mode\n"
204 #ifdef WIN32
205  " -install Install windows service\n"
206  " -remove Remove windows service\n"
207 #endif
208  );
209  return 1;
210  }
211  }
212  if (!config_fname)
213  {
214  yaz_log(YLOG_FATAL, "Configuration must be given with option -f");
215  return 1;
216  }
218 
219  {
220  char yaz_version_str[20];
221  char yaz_sha1_str[41];
222  yaz_log(YLOG_LOG, "Pazpar2 %s " VERSION " "
225 #else
226  "-"
227 #endif
228  , test_mode ? "test" : "start");
229  yaz_version(yaz_version_str, yaz_sha1_str);
230  yaz_log(YLOG_LOG, "YAZ %s %s", yaz_version_str, yaz_sha1_str);
231  }
232 
233  config = config_create(config_fname);
234  if (!config)
235  return 1;
236  sc_stop_config = config;
237  ret = 0;
238  if (test_mode)
239  {
240  yaz_log(YLOG_LOG, "Configuration OK");
241  }
242  else
243  {
244  if (daemon && !log_file_in_use)
245  {
246  yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
247  "mode");
248  ret = 1;
249  }
250  if (!ret)
251  ret = config_start_listeners(config, listener_override,
252  record_fname);
253  if (!ret)
254  {
255  yaz_sc_running(s);
256  yaz_daemon("pazpar2",
257  (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
258  (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
259  child_handler, config /* child_data */,
260  pidfile, uid);
261  }
262  yaz_log(YLOG_LOG, "Pazpar2 stop");
263  }
264  config_destroy(config);
265  return ret;
266 }
267 
268 
269 static void sc_stop(yaz_sc_t s)
270 {
271  config_stop_listeners(sc_stop_config);
272 }
273 
274 int main(int argc, char **argv)
275 {
276  int ret;
277  yaz_sc_t s = yaz_sc_create("pazpar2", "Pazpar2");
278 
279 #ifdef MTRACE
280  mtrace();
281 #endif
282 
283  ret = yaz_sc_program(s, argc, argv, sc_main, sc_stop);
284 
285  yaz_sc_destroy(&s);
286 
287 #ifdef MTRACE
288  muntrace();
289 #endif
290 
291 
292  exit(ret);
293 }
294 
295 /*
296  * Local variables:
297  * c-basic-offset: 4
298  * c-file-style: "Stroustrup"
299  * indent-tabs-mode: nil
300  * End:
301  * vim: shiftwidth=4 tabstop=8 expandtab
302  */
303 
void config_destroy(struct conf_config *config)
void child_handler(void *data)
Definition: pazpar2.c:50
void config_stop_listeners(struct conf_config *conf)
#define VERSION
Definition: config.h:74
void pazpar2_mutex_init(void)
Definition: ppmutex.c:34
#define PAZPAR2_VERSION_SHA1
Definition: config.h:68
static struct conf_config * sc_stop_config
Definition: pazpar2.c:48
struct conf_config * config_create(const char *fname)
int predictable_sessions
Definition: parameters.h:29
static void show_version(void)
Definition: pazpar2.c:59
struct parameters global_parameters
Definition: session.c:86
static void sc_stop(yaz_sc_t s)
Definition: pazpar2.c:269
int dump_records
Definition: parameters.h:27
int debug_mode
Definition: parameters.h:28
void config_process_events(struct conf_config *conf)
int main(int argc, char **argv)
Definition: pazpar2.c:274
#define PACKAGE_VERSION
Definition: config.h:65
int config_start_listeners(struct conf_config *conf, const char *listener_override, const char *record_fname)
static int sc_main(yaz_sc_t s, int argc, char **argv)
Definition: pazpar2.c:96
control MUTEX debugging