Process management/respawn. Bug #1173. PID file pazpar2.pid generated
[pazpar2-moved-to-github.git] / src / pazpar2.c
1 /* $Id: pazpar2.c,v 1.87 2007-06-08 13:57:19 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #if HAVE_CONFIG_H
23 #include "cconfig.h"
24 #endif
25
26
27 #include <signal.h>
28 #include <assert.h>
29
30 #include "pazpar2.h"
31 #include "database.h"
32 #include "settings.h"
33
34 void child_handler(void *data)
35 {
36     yaz_log(YLOG_LOG, "child_handler");
37
38     start_proxy();
39     //start_zproxy();
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 int main(int argc, char **argv)
56 {
57     int ret;
58     char *arg;
59     const char *pidfile = "pazpar2.pid";
60
61     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
62         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
63
64     yaz_log_init_prefix("pazpar2");
65
66     //while ((ret = options("f:h:p:z:t:l:dX", argv, argc, &arg)) != -2)
67    while ((ret = options("f:h:p:t:l:dX", argv, argc, &arg)) != -2)
68     {
69         switch (ret)
70         {
71         case 'f':
72             if (!read_config(arg))
73                 exit(1);
74             break;
75         case 'h':
76             strcpy(global_parameters.listener_override, arg);
77             break;
78         case 'p':
79             strcpy(global_parameters.proxy_override, arg);
80             break;
81 /*        case 'z': */
82 /*             strcpy(global_parameters.zproxy_override, arg); */
83 /*             break; */
84         case 't':
85             strcpy(global_parameters.settings_path_override, arg);
86              break;
87         case 'd':
88             global_parameters.dump_records = 1;
89             break;
90         case 'l':
91             yaz_log_init_file(arg);
92             break;
93         case 'X':
94             global_parameters.debug_mode = 1;
95             break;
96         default:
97             fprintf(stderr, "Usage: pazpar2\n"
98                     "    -f configfile\n"
99                     "    -h [host:]port          (REST protocol listener)\n"
100                     "    -p hostname[:portno]    (HTTP proxy)\n"
101                     "    -z hostname[:portno]    (Z39.50 proxy)\n"
102                     "    -t settings\n"
103                     "    -d                      (show internal records)\n"
104                     "    -l file                 log to file\n"
105                     "    -X                      debug mode\n"
106                 );
107             exit(1);
108         }
109     }
110
111     if (!config)
112     {
113         yaz_log(YLOG_FATAL, "Load config with -f");
114         exit(1);
115     }
116     global_parameters.server = config->servers;
117
118     start_http_listener();
119     pazpar2_process(global_parameters.debug_mode, 0, child_handler, 0,
120                     pidfile, 0);
121     return 0;
122 }
123
124
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */