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