Set signal again (otherwise Solaris stops)
[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.24 2003-10-23 13:00:35 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         if (proxy->server(addr))
108         {
109             yaz_log(LOG_FATAL|LOG_ERRNO, "listen %s", addr);
110             exit(1);
111         }
112     }
113     else
114     {
115         usage(prog);
116         return 1;
117     }
118     return 0;
119 }
120
121 static Yaz_Proxy *static_yaz_proxy = 0;
122 static void sighup_handler(int num)
123 {
124     signal(SIGHUP, sighup_handler);
125     if (static_yaz_proxy)
126         static_yaz_proxy->reconfig();
127 }
128
129 int main(int argc, char **argv)
130 {
131     static int mk_pid = 0;
132     Yaz_SocketManager mySocketManager;
133     Yaz_Proxy proxy(new Yaz_PDU_Assoc(&mySocketManager));
134
135     static_yaz_proxy = &proxy;
136
137     signal(SIGHUP, sighup_handler);
138
139     args(&proxy, argc, argv);
140
141     if (pid_fname)
142     {
143         FILE *f = fopen(pid_fname, "w");
144         if (!f)
145         {
146             yaz_log(LOG_ERRNO|LOG_FATAL, "Couldn't create %s", pid_fname);
147             exit(0);
148         }
149         fprintf(f, "%ld", (long) getpid());
150         fclose(f);
151         xfree(pid_fname);
152     }
153     if (uid)
154     {
155         struct passwd *pw;
156
157         if (!(pw = getpwnam(uid)))
158         {
159             yaz_log(LOG_FATAL, "%s: Unknown user", uid);
160             exit(3);
161         }
162         if (log_file)
163         {
164             chown(log_file, pw->pw_uid,  pw->pw_gid);
165             xfree(log_file);
166         }
167
168         if (setuid(pw->pw_uid) < 0)
169         {
170             yaz_log(LOG_FATAL|LOG_ERRNO, "setuid");
171             exit(4);
172         }
173         xfree(uid);
174     }
175
176     while (mySocketManager.processEvent() > 0)
177         ;
178
179     exit (0);
180     return 0;
181 }