autoheader generates config.h instead of cconfig.h.
[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
24
25 #include <signal.h>
26 #include <assert.h>
27
28 #include "pazpar2.h"
29 #include "database.h"
30 #include "settings.h"
31 #include <yaz/daemon.h>
32
33 void child_handler(void *data)
34 {
35     start_proxy();
36     init_settings();
37
38     if (*global_parameters.settings_path_override)
39         settings_read(global_parameters.settings_path_override);
40     else if (global_parameters.server->settings)
41         settings_read(global_parameters.server->settings);
42     else
43         yaz_log(YLOG_WARN, "No settings-directory specified");
44     global_parameters.odr_in = odr_createmem(ODR_DECODE);
45     global_parameters.odr_out = odr_createmem(ODR_ENCODE);
46
47
48     pazpar2_event_loop();
49
50 }
51
52 static void show_version(void)
53 {
54     char yaz_version_str[80];
55     printf("Pazpar2 " VERSION "\n");
56
57     yaz_version(yaz_version_str, 0);
58
59     printf("Configuration:");
60 #if HAVE_ICU
61     printf(" icu:?");
62 #endif
63     printf(" yaz:%s", yaz_version_str);
64     printf("\n");
65     exit(0);
66 }            
67
68 int main(int argc, char **argv)
69 {
70     int daemon = 0;
71     int ret;
72     int log_file_in_use = 0;
73     char *arg;
74     const char *pidfile = 0;
75     const char *uid = 0;
76
77     if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
78         yaz_log(YLOG_WARN|YLOG_ERRNO, "signal");
79
80     yaz_log_init_prefix("pazpar2");
81 #if YAZ_VERSIONL >= 0x03001B
82     yaz_log_xml_errors(0, YLOG_WARN);
83 #endif
84
85     while ((ret = options("dDf:h:l:p:t:u:VX", argv, argc, &arg)) != -2)
86     {
87         switch (ret)
88         {
89         case 'd':
90             global_parameters.dump_records = 1;
91             break;
92         case 'D':
93             daemon = 1;
94             break;
95         case 'f':
96             if (!read_config(arg))
97                 exit(1);
98             break;
99         case 'h':
100             strcpy(global_parameters.listener_override, arg);
101             break;
102         case 'l':
103             yaz_log_init_file(arg);
104             log_file_in_use = 1;
105             break;
106         case 'p':
107             pidfile = arg;
108             break;
109         case 't':
110             strcpy(global_parameters.settings_path_override, arg);
111             break;
112         case 'u':
113             uid = arg;
114             break;
115         case 'V':
116             show_version();
117         case 'X':
118             global_parameters.debug_mode = 1;
119             break;
120         default:
121             fprintf(stderr, "Usage: pazpar2\n"
122                     "    -d                      (show internal records)\n"
123                     "    -D                      Daemon mode (background)\n"
124                     "    -f configfile\n"
125                     "    -h [host:]port          (REST protocol listener)\n"
126                     "    -l file                 log to file\n"
127                     "    -p pidfile              PID file\n"
128                     "    -t settings\n"
129                     "    -u uid\n"
130                     "    -V                      show version\n"
131                     "    -X                      debug mode\n"
132                 );
133             exit(1);
134         }
135     }
136
137     yaz_log(YLOG_LOG, "Pazpar2 %s started", VERSION);
138     if (daemon && !log_file_in_use)
139     {
140         yaz_log(YLOG_FATAL, "Logfile must be given (option -l) for daemon "
141                 "mode");
142         exit(1);
143     }
144     if (!config)
145     {
146         yaz_log(YLOG_FATAL, "Load config with -f");
147         exit(1);
148     }
149     global_parameters.server = config->servers;
150
151     start_http_listener();
152     yaz_daemon("pazpar2",
153                (global_parameters.debug_mode ? YAZ_DAEMON_DEBUG : 0) +
154                (daemon ? YAZ_DAEMON_FORK : 0) + YAZ_DAEMON_KEEPALIVE,
155                child_handler, 0 /* child_data */,
156                pidfile, uid);
157     return 0;
158 }
159
160
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * indent-tabs-mode: nil
165  * End:
166  * vim: shiftwidth=4 tabstop=8 expandtab
167  */