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