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