2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-proxy-main.cpp,v 1.30 2004-01-12 22:35:26 adam Exp $
11 #include <sys/types.h>
14 #include <yaz/options.h>
16 #include <yaz++/socket-manager.h>
17 #include <yaz++/pdu-assoc.h>
18 #include <yaz++/proxy.h>
20 void usage(char *prog)
22 fprintf (stderr, "%s: [-c config] [-l log] [-a log] [-v level] [-t target] "
23 "[-u uid] [-p pidfile] @:port\n", prog);
27 static char *pid_fname = 0;
29 static char *log_file = 0;
32 int args(Yaz_Proxy *proxy, int argc, char **argv)
39 while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:X",
40 argv, argc, &arg)) != -2)
54 err = proxy->set_config(arg);
57 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
62 fprintf(stderr, "Bad or missing file %s\n", arg);
67 proxy->set_APDU_log(arg);
70 proxy->set_default_target(arg);
73 proxy->set_proxy_authentication(arg);
76 proxy->option("optimize", arg);
79 yaz_log_init_level (yaz_log_mask_str(arg));
82 yaz_log_init_file (arg);
83 log_file = xstrdup(arg);
86 proxy->set_max_clients(atoi(arg));
89 proxy->set_client_idletime(atoi(arg));
92 proxy->set_target_idletime(atoi(arg));
99 pid_fname = xstrdup(arg);
112 if (proxy->server(addr))
114 yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
126 static Yaz_Proxy *static_yaz_proxy = 0;
127 static void sighup_handler(int num)
129 signal(SIGHUP, sighup_handler);
130 if (static_yaz_proxy)
131 static_yaz_proxy->reconfig();
135 static void child_run(Yaz_SocketManager *m, int run)
137 signal(SIGHUP, sighup_handler);
139 yaz_log(LOG_LOG, "0 proxy run=%d pid=%ld", run, (long) getpid());
142 FILE *f = fopen(pid_fname, "w");
145 yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
148 fprintf(f, "%ld", (long) getpid());
156 if (!(pw = getpwnam(uid)))
158 yaz_log(LOG_FATAL, "%s: Unknown user", uid);
163 chown(log_file, pw->pw_uid, pw->pw_gid);
167 if (setuid(pw->pw_uid) < 0)
169 yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
175 while (m->processEvent() > 0)
181 int main(int argc, char **argv)
190 Yaz_SocketManager mySocketManager;
191 Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
193 static_yaz_proxy = &proxy;
195 args(&proxy, argc, argv);
199 child_run(&mySocketManager, run);
207 yaz_log(LOG_FATAL|LOG_ERRNO, "fork");
212 child_run(&mySocketManager, run);
222 yaz_log(LOG_FATAL, "p1=%d != p=%d", p1, p);
225 if (WIFSIGNALED(status))
227 switch(WTERMSIG(status)) {
229 yaz_log(LOG_WARN, "Received SIGILL from child %ld", (long) p);
233 yaz_log(LOG_WARN, "Received SIGABRT from child %ld", (long) p);
237 yaz_log(LOG_WARN, "Received SIGSEGV from child %ld", (long) p);
241 yaz_log(LOG_LOG, "Received SIGTERM from child %ld",
246 yaz_log(LOG_WARN, "Received SIG %d from child %ld",
247 WTERMSIG(status), (long) p);
251 else if (status == 0)
255 yaz_log(LOG_LOG, "Exit %d from child %ld", status, (long) p);