Fix attribute type checking
[yazpp-moved-to-github.git] / src / yaz-proxy-main.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-proxy-main.cpp,v 1.25 2003-10-23 13:49:58 adam Exp $
6  */
7
8 #include <signal.h>
9 #include <unistd.h>
10 #include <pwd.h>
11 #include <sys/types.h>
12
13 #include <yaz/log.h>
14 #include <yaz/options.h>
15
16 #include <yaz++/socket-manager.h>
17 #include <yaz++/pdu-assoc.h>
18 #include <yaz++/proxy.h>
19
20 void usage(char *prog)
21 {
22     fprintf (stderr, "%s: [-c config] [-a log] [-m num] [-v level] [-t target] [-i sec] "
23              "[-u uid] [-p pidfile] [-o optlevel] @:port\n", prog);
24     exit (1);
25 }
26
27 static char *pid_fname = 0;
28 static char *uid = 0;
29 static char *log_file = 0;
30
31 int args(Yaz_Proxy *proxy, int argc, char **argv)
32 {
33     char *addr = 0;
34     char *arg;
35     char *prog = argv[0];
36     int ret;
37
38     while ((ret = options("o:a:t:v:c:u:i:m:l:T:p:U:", argv, argc, &arg)) != -2)
39     {
40         int err;
41         switch (ret)
42         {
43         case 0:
44             if (addr)
45             {
46                 usage(prog);
47                 return 1;
48             }
49             addr = arg;
50             break;
51         case 'c':
52             err = proxy->set_config(arg);
53             if (err == -2)
54             {
55                 fprintf(stderr, "Config file support not enabled (proxy not compiled with libxml2 support)\n");
56                 exit(1);
57             }
58             else if (err == -1)
59             {
60                 fprintf(stderr, "Bad or missing file %s\n", arg);
61                 exit(1);
62             }
63             break;
64         case 'a':
65             proxy->set_APDU_log(arg);
66             break;
67         case 't':
68             proxy->set_default_target(arg);
69             break;
70         case 'U':
71             proxy->set_proxy_authentication(arg);
72             break;
73         case 'o':
74             proxy->option("optimize", arg);
75             break;
76         case 'v':
77             yaz_log_init_level (yaz_log_mask_str(arg));
78             break;
79         case 'l':
80             yaz_log_init_file (arg);
81             log_file = xstrdup(arg);
82             break;
83         case 'm':
84             proxy->set_max_clients(atoi(arg));
85             break;
86         case 'i':
87             proxy->set_client_idletime(atoi(arg));
88             break;
89         case 'T':
90             proxy->set_target_idletime(atoi(arg));
91             break;
92         case 'p':
93             if (!pid_fname)
94                 pid_fname = xstrdup(arg);
95             break;
96         case 'u':
97             if (!uid)
98                 uid = xstrdup(arg);
99             break;
100         default:
101             usage(prog);
102             return 1;
103         }
104     }
105     if (addr)
106     {
107         yaz_log(LOG_LOG, "Starting proxy pid=%ld", (long) getpid());
108         if (proxy->server(addr))
109         {
110             yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
111             exit(1);
112         }
113     }
114     else
115     {
116         usage(prog);
117         return 1;
118     }
119     return 0;
120 }
121
122 static Yaz_Proxy *static_yaz_proxy = 0;
123 static void sighup_handler(int num)
124 {
125     signal(SIGHUP, sighup_handler);
126     if (static_yaz_proxy)
127         static_yaz_proxy->reconfig();
128 }
129
130 int main(int argc, char **argv)
131 {
132     static int mk_pid = 0;
133     Yaz_SocketManager mySocketManager;
134     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
135
136     static_yaz_proxy = &proxy;
137
138     signal(SIGHUP, sighup_handler);
139
140     args(&proxy, argc, argv);
141
142     if (pid_fname)
143     {
144         FILE *f = fopen(pid_fname, "w");
145         if (!f)
146         {
147             yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
148             exit(0);
149         }
150         fprintf(f, "%ld", (long) getpid());
151         fclose(f);
152         xfree(pid_fname);
153     }
154     if (uid)
155     {
156         struct passwd *pw;
157
158         if (!(pw = getpwnam(uid)))
159         {
160             yaz_log(LOG_FATAL, "%s: Unknown user", uid);
161             exit(3);
162         }
163         if (log_file)
164         {
165             chown(log_file, pw->pw_uid,  pw->pw_gid);
166             xfree(log_file);
167         }
168
169         if (setuid(pw->pw_uid) < 0)
170         {
171             yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
172             exit(4);
173         }
174         xfree(uid);
175     }
176
177     while (mySocketManager.processEvent() > 0)
178         ;
179
180     exit (0);
181     return 0;
182 }