| @@ -1,109 +1,102 @@ | | | @@ -1,109 +1,102 @@ |
1 | $NetBSD: patch-af,v 1.1 2002/05/31 12:59:58 seb Exp $ | | 1 | $NetBSD: patch-af,v 1.2 2010/07/17 08:57:18 obache Exp $ |
2 | | | 2 | |
3 | --- skkserv/skkserv.c.orig Tue Jan 21 04:16:36 1997 | | 3 | --- skkserv/skkserv.c.orig 1997-01-20 19:16:36.000000000 +0000 |
4 | +++ skkserv/skkserv.c Mon Nov 6 00:12:57 2000 | | 4 | +++ skkserv/skkserv.c |
5 | @@ -100,14 +100,16 @@ | | 5 | @@ -100,8 +100,10 @@ |
6 | #define SERVER_NOT_FOUND "4" | | 6 | #define SERVER_NOT_FOUND "4" |
7 | #define SERVER_FULL "9" | | 7 | #define SERVER_FULL "9" |
8 | | | 8 | |
9 | +#define warn(m) \ | | 9 | +#define warn(m) \ |
10 | + {if (debug) fprintf(errout, "%s: %s\n", pgmnm, m);} | | 10 | + {if (debug) fprintf(errout, "%s: %s\n", pgmnm, m);} |
11 | #define err(m) \ | | 11 | #define err(m) \ |
12 | - {if (debug) fprintf(errout, "%s: %s\n", pgmnm, m); exit(1);} | | 12 | - {if (debug) fprintf(errout, "%s: %s\n", pgmnm, m); exit(1);} |
13 | + {warn(m); exit(1);} | | 13 | + {warn(m); exit(1);} |
14 | | | 14 | |
15 | /* | | 15 | /* |
16 | * Global Variables | | 16 | * Global Variables |
17 | */ | | 17 | @@ -121,6 +123,9 @@ int format; /* 0 (old-style) / non-0 (n |
18 | | | | |
19 | -char pgmver[] = "3.9.4 "; /* version number */ | | | |
20 | +char pgmver[] = "3.9.4nb1 "; /* version number */ | | | |
21 | | | | |
22 | char *pgmnm; /* program name */ | | | |
23 | char *jname; /* name of shared dictionary */ | | | |
24 | @@ -121,6 +123,9 @@ | | | |
25 | int jtab1[KANAMOJI]; /* index-table by 1st letter (1st part)*/ | | 18 | int jtab1[KANAMOJI]; /* index-table by 1st letter (1st part)*/ |
26 | int jtab2[KANAMOJI]; /* index-table by 1st letter (2nd part)*/ | | 19 | int jtab2[KANAMOJI]; /* index-table by 1st letter (2nd part)*/ |
27 | int initsock; /* socket for waiting requests */ | | 20 | int initsock; /* socket for waiting requests */ |
28 | +#ifdef PF_INET6 | | 21 | +#ifdef PF_INET6 |
29 | +int initsock6; /* IPv6 socket for waiting requests */ | | 22 | +int initsock6; /* IPv6 socket for waiting requests */ |
30 | +#endif | | 23 | +#endif |
31 | int clientsock[MAXCLNT]; /* socket for each client */ | | 24 | int clientsock[MAXCLNT]; /* socket for each client */ |
32 | int nclients; /* max index for active clients */ | | 25 | int nclients; /* max index for active clients */ |
33 | | | 26 | |
34 | @@ -130,8 +135,18 @@ | | 27 | @@ -130,8 +135,18 @@ char *argv[]; |
35 | int parg; | | 28 | int parg; |
36 | int setjisho = 0; | | 29 | int setjisho = 0; |
37 | int ctlterm; /* fildes for control terminal */ | | 30 | int ctlterm; /* fildes for control terminal */ |
38 | - void reread(); | | 31 | - void reread(); |
39 | + RETSIGTYPE req_reread(); | | 32 | + RETSIGTYPE req_reread(); |
40 | + | | 33 | + |
41 | +#ifdef SKKSERV_UID | | 34 | +#ifdef SKKSERV_UID |
42 | + gid_t gid = SKKSERV_GID; | | 35 | + gid_t gid = SKKSERV_GID; |
43 | | | 36 | |
44 | + /* revoke privilege if any */ | | 37 | + /* revoke privilege if any */ |
45 | + if (geteuid() == 0) { | | 38 | + if (geteuid() == 0) { |
46 | + setgroups(1, &gid); | | 39 | + setgroups(1, &gid); |
47 | + setgid(SKKSERV_GID); | | 40 | + setgid(SKKSERV_GID); |
48 | + setuid(SKKSERV_UID); | | 41 | + setuid(SKKSERV_UID); |
49 | + } | | 42 | + } |
50 | +#endif | | 43 | +#endif |
51 | pgmnm = argv[0]; | | 44 | pgmnm = argv[0]; |
52 | debug = 0; | | 45 | debug = 0; |
53 | errout = stderr; | | 46 | errout = stderr; |
54 | @@ -200,7 +215,10 @@ | | 47 | @@ -200,7 +215,10 @@ char *argv[]; |
55 | fclose(stderr); | | 48 | fclose(stderr); |
56 | | | 49 | |
57 | /* detach child process from control terminal */ | | 50 | /* detach child process from control terminal */ |
58 | -#ifdef HAVE_TIOCNOTTY | | 51 | -#ifdef HAVE_TIOCNOTTY |
59 | +#ifdef HAVE_SETSID | | 52 | +#ifdef HAVE_SETSID |
60 | + setsid(); | | 53 | + setsid(); |
61 | +#else | | 54 | +#else |
62 | +#ifdef TIOCNOTTY | | 55 | +#ifdef TIOCNOTTY |
63 | if ((ctlterm = open("/dev/tty", 2)) >= 0) { | | 56 | if ((ctlterm = open("/dev/tty", 2)) >= 0) { |
64 | ioctl(ctlterm, TIOCNOTTY, 0); | | 57 | ioctl(ctlterm, TIOCNOTTY, 0); |
65 | close(ctlterm); | | 58 | close(ctlterm); |
66 | @@ -215,6 +233,7 @@ | | 59 | @@ -215,6 +233,7 @@ char *argv[]; |
67 | signal(SIGHUP, SIG_IGN); | | 60 | signal(SIGHUP, SIG_IGN); |
68 | if (fork() != 0) exit(0); | | 61 | if (fork() != 0) exit(0); |
69 | #endif | | 62 | #endif |
70 | +#endif | | 63 | +#endif |
71 | } else { /* debug mode */ | | 64 | } else { /* debug mode */ |
72 | fprintf(errout, "SKK-JISYO is %s\n", jname); | | 65 | fprintf(errout, "SKK-JISYO is %s\n", jname); |
73 | fflush(errout); | | 66 | fflush(errout); |
74 | @@ -222,7 +241,7 @@ | | 67 | @@ -222,7 +241,7 @@ char *argv[]; |
75 | if (errout != stderr) fclose(stderr); | | 68 | if (errout != stderr) fclose(stderr); |
76 | } | | 69 | } |
77 | | | 70 | |
78 | - signal(SIGINT, reread); | | 71 | - signal(SIGINT, reread); |
79 | + signal(SIGINT, req_reread); | | 72 | + signal(SIGINT, req_reread); |
80 | /* 1993/6/5 by kam, re-read dictionary by "INT" signal */ | | 73 | /* 1993/6/5 by kam, re-read dictionary by "INT" signal */ |
81 | | | 74 | |
82 | nclients = 0; | | 75 | nclients = 0; |
83 | @@ -244,8 +263,11 @@ | | 76 | @@ -244,8 +263,11 @@ showusage() |
84 | mksock() | | 77 | mksock() |
85 | { | | 78 | { |
86 | struct sockaddr_in sin; | | 79 | struct sockaddr_in sin; |
87 | +#ifdef PF_INET6 | | 80 | +#ifdef PF_INET6 |
88 | + struct sockaddr_in6 sin6; | | 81 | + struct sockaddr_in6 sin6; |
89 | +#endif | | 82 | +#endif |
90 | struct servent *sp; | | 83 | struct servent *sp; |
91 | - int optbuf = 1; /* enable socket REUSEADDR */ | | 84 | - int optbuf = 1; /* enable socket REUSEADDR */ |
92 | + int optbuf; /* enable socket REUSEADDR */ | | 85 | + int optbuf; /* enable socket REUSEADDR */ |
93 | | | 86 | |
94 | bzero((char*)&sin, sizeof(sin)); | | 87 | bzero((char*)&sin, sizeof(sin)); |
95 | sin.sin_family = AF_INET; | | 88 | sin.sin_family = AF_INET; |
96 | @@ -262,16 +284,54 @@ | | 89 | @@ -262,16 +284,54 @@ mksock() |
97 | sin.sin_port = htons(portnum); | | 90 | sin.sin_port = htons(portnum); |
98 | | | 91 | |
99 | if ((initsock = socket(PF_INET, SOCK_STREAM, 0)) < 0) | | 92 | if ((initsock = socket(PF_INET, SOCK_STREAM, 0)) < 0) |
100 | - err("socket error; socket cannot be created"); | | 93 | - err("socket error; socket cannot be created"); |
101 | - if (setsockopt(initsock, SOL_SOCKET, SO_REUSEADDR, | | 94 | - if (setsockopt(initsock, SOL_SOCKET, SO_REUSEADDR, |
102 | + warn("socket error; socket cannot be created"); | | 95 | + warn("socket error; socket cannot be created"); |
103 | +#ifdef PF_INET6 | | 96 | +#ifdef PF_INET6 |
104 | + bzero((char*)&sin6, sizeof(sin6)); | | 97 | + bzero((char*)&sin6, sizeof(sin6)); |
105 | + sin6.sin6_family = AF_INET6; | | 98 | + sin6.sin6_family = AF_INET6; |
106 | + /* sin6.sin6_addr.s6_addr = IN6ADDR_ANY_INIT; ... all-zero pattern */ | | 99 | + /* sin6.sin6_addr.s6_addr = IN6ADDR_ANY_INIT; ... all-zero pattern */ |
107 | + sin6.sin6_port = htons(portnum); | | 100 | + sin6.sin6_port = htons(portnum); |
108 | + if ((initsock6 = socket(PF_INET6, SOCK_STREAM, 0)) < 0) | | 101 | + if ((initsock6 = socket(PF_INET6, SOCK_STREAM, 0)) < 0) |
109 | + warn("socket error; IPv6 socket cannot be created"); | | 102 | + warn("socket error; IPv6 socket cannot be created"); |
| @@ -144,73 +137,73 @@ $NetBSD: patch-af,v 1.1 2002/05/31 12:59 | | | @@ -144,73 +137,73 @@ $NetBSD: patch-af,v 1.1 2002/05/31 12:59 |
144 | err("listen error; something wrong happened with the socket"); | | 137 | err("listen error; something wrong happened with the socket"); |
145 | +#ifdef PF_INET6 | | 138 | +#ifdef PF_INET6 |
146 | + if (initsock6 >= 0 && listen(initsock6, MAXQUE) < 0) | | 139 | + if (initsock6 >= 0 && listen(initsock6, MAXQUE) < 0) |
147 | + err("listen error; something wrong happened with the IPv6 socket"); | | 140 | + err("listen error; something wrong happened with the IPv6 socket"); |
148 | +#endif | | 141 | +#endif |
149 | if (debug) { | | 142 | if (debug) { |
150 | fprintf(errout, "file descriptor for initsock is %d\n", initsock); | | 143 | fprintf(errout, "file descriptor for initsock is %d\n", initsock); |
151 | +#ifdef PF_INET6 | | 144 | +#ifdef PF_INET6 |
152 | + fprintf(errout, "file descriptor for initsock6 is %d\n", initsock6); | | 145 | + fprintf(errout, "file descriptor for initsock6 is %d\n", initsock6); |
153 | +#endif | | 146 | +#endif |
154 | fflush(errout); | | 147 | fflush(errout); |
155 | } | | 148 | } |
156 | } | | 149 | } |
157 | @@ -303,8 +363,19 @@ | | 150 | @@ -303,8 +363,19 @@ mkjtab() |
158 | mkoldjtab(buf); | | 151 | mkoldjtab(buf); |
159 | } | | 152 | } |
160 | | | 153 | |
161 | +/* using stdio in signal handler is unsafe */ | | 154 | +/* using stdio in signal handler is unsafe */ |
162 | +#ifdef __STDC__ | | 155 | +#ifdef __STDC__ |
163 | +volatile | | 156 | +volatile |
164 | +#endif | | 157 | +#endif |
165 | +int reread_requested; | | 158 | +int reread_requested; |
166 | + | | 159 | + |
167 | +RETSIGTYPE req_reread() | | 160 | +RETSIGTYPE req_reread() |
168 | +{ | | 161 | +{ |
169 | + reread_requested = 1; | | 162 | + reread_requested = 1; |
170 | +} | | 163 | +} |
171 | + | | 164 | + |
172 | /* 1993/6/5 by kam, re-read dictionary by "INT" signal */ | | 165 | /* 1993/6/5 by kam, re-read dictionary by "INT" signal */ |
173 | -RETSIGTYPE reread() | | 166 | -RETSIGTYPE reread() |
174 | +void reread() | | 167 | +void reread() |
175 | { | | 168 | { |
176 | if (fclose(jisho) < 0) { | | 169 | if (fclose(jisho) < 0) { |
177 | fprintf(stderr, | | 170 | fprintf(stderr, |
178 | @@ -455,15 +526,22 @@ | | 171 | @@ -455,15 +526,22 @@ mknewjtab() |
179 | main_loop() | | 172 | main_loop() |
180 | { | | 173 | { |
181 | fd_set readfds, writefds, exceptfds; | | 174 | fd_set readfds, writefds, exceptfds; |
182 | - fd_set getrfds(); | | 175 | - fd_set getrfds(); |
183 | + void getrfds(); | | 176 | + void getrfds(); |
184 | struct sockaddr_in from; | | 177 | struct sockaddr_in from; |
185 | +#ifdef PF_INET6 | | 178 | +#ifdef PF_INET6 |
186 | + struct sockaddr_in6 from6; | | 179 | + struct sockaddr_in6 from6; |
187 | +#endif | | 180 | +#endif |
188 | int len; | | 181 | int len; |
189 | register int i; | | 182 | register int i; |
190 | | | 183 | |
191 | FD_ZERO(&writefds); | | 184 | FD_ZERO(&writefds); |
192 | FD_ZERO(&exceptfds); | | 185 | FD_ZERO(&exceptfds); |
193 | for(;;) { /* infinite loop; waiting for client's request */ | | 186 | for(;;) { /* infinite loop; waiting for client's request */ |
194 | - readfds = getrfds(); | | 187 | - readfds = getrfds(); |
195 | + if (reread_requested) { | | 188 | + if (reread_requested) { |
196 | + reread(); | | 189 | + reread(); |
197 | + reread_requested = 0; | | 190 | + reread_requested = 0; |
198 | + } | | 191 | + } |
199 | + getrfds(&readfds); | | 192 | + getrfds(&readfds); |
200 | if (select(MAXDTAB, &readfds, &writefds, &exceptfds, NULL) < 0) { | | 193 | if (select(MAXDTAB, &readfds, &writefds, &exceptfds, NULL) < 0) { |
201 | if (errno == EINTR) /* if signal happens */ | | 194 | if (errno == EINTR) /* if signal happens */ |
202 | continue; | | 195 | continue; |
203 | @@ -474,16 +552,32 @@ | | 196 | @@ -474,16 +552,32 @@ main_loop() |
204 | fflush(errout); | | 197 | fflush(errout); |
205 | } | | 198 | } |
206 | | | 199 | |
207 | - if (FD_ISSET(initsock, &readfds)) { | | 200 | - if (FD_ISSET(initsock, &readfds)) { |
208 | + if (initsock >= 0 && FD_ISSET(initsock, &readfds)) { | | 201 | + if (initsock >= 0 && FD_ISSET(initsock, &readfds)) { |
209 | len = sizeof(from); | | 202 | len = sizeof(from); |
210 | if ((clientsock[nclients ++] = accept(initsock, &from, &len)) < 0) { | | 203 | if ((clientsock[nclients ++] = accept(initsock, &from, &len)) < 0) { |
211 | err("accept error; something wrong happened with the socket"); | | 204 | err("accept error; something wrong happened with the socket"); |
212 | } | | 205 | } |
213 | - if (nclients >= MAXDTAB - 3 - debug * 2) { | | 206 | - if (nclients >= MAXDTAB - 3 - debug * 2) { |
214 | + if (nclients >= MAXDTAB - 3 | | 207 | + if (nclients >= MAXDTAB - 3 |
215 | +#ifdef PF_INET6 | | 208 | +#ifdef PF_INET6 |
216 | + -1 | | 209 | + -1 |
| @@ -225,58 +218,58 @@ $NetBSD: patch-af,v 1.1 2002/05/31 12:59 | | | @@ -225,58 +218,58 @@ $NetBSD: patch-af,v 1.1 2002/05/31 12:59 |
225 | + len = sizeof(from6); | | 218 | + len = sizeof(from6); |
226 | + if ((clientsock[nclients ++] = accept(initsock6, &from6, &len)) < 0) { | | 219 | + if ((clientsock[nclients ++] = accept(initsock6, &from6, &len)) < 0) { |
227 | + err("accept error; something wrong happened with the socket"); | | 220 | + err("accept error; something wrong happened with the socket"); |
228 | + } | | 221 | + } |
229 | + if (nclients >= MAXDTAB - 4 - debug * 2) { | | 222 | + if (nclients >= MAXDTAB - 4 - debug * 2) { |
230 | + write(clientsock[--nclients], SERVER_FULL, 1); | | 223 | + write(clientsock[--nclients], SERVER_FULL, 1); |
231 | + close(clientsock[nclients]); | | 224 | + close(clientsock[nclients]); |
232 | + } | | 225 | + } |
233 | + } | | 226 | + } |
234 | +#endif | | 227 | +#endif |
235 | | | 228 | |
236 | /* naiive scheduling */ | | 229 | /* naiive scheduling */ |
237 | for (i = 0; i < nclients; i ++) | | 230 | for (i = 0; i < nclients; i ++) |
238 | @@ -511,16 +605,20 @@ | | 231 | @@ -511,16 +605,20 @@ main_loop() |
239 | * get bit pattern of read file descriptor | | 232 | * get bit pattern of read file descriptor |
240 | */ | | 233 | */ |
241 | | | 234 | |
242 | -fd_set getrfds() | | 235 | -fd_set getrfds() |
243 | +void getrfds(rfds) | | 236 | +void getrfds(rfds) |
244 | +fd_set *rfds; | | 237 | +fd_set *rfds; |
245 | { | | 238 | { |
246 | - fd_set rfds; | | 239 | - fd_set rfds; |
247 | register int i; | | 240 | register int i; |
248 | | | 241 | |
249 | - FD_ZERO(&rfds); | | 242 | - FD_ZERO(&rfds); |
250 | - FD_SET(initsock, &rfds); | | 243 | - FD_SET(initsock, &rfds); |
251 | + FD_ZERO(rfds); | | 244 | + FD_ZERO(rfds); |
252 | + if (initsock >= 0) | | 245 | + if (initsock >= 0) |
253 | + FD_SET(initsock, rfds); | | 246 | + FD_SET(initsock, rfds); |
254 | +#ifdef PF_INET6 | | 247 | +#ifdef PF_INET6 |
255 | + if (initsock6 >= 0) | | 248 | + if (initsock6 >= 0) |
256 | + FD_SET(initsock6, rfds); | | 249 | + FD_SET(initsock6, rfds); |
257 | +#endif | | 250 | +#endif |
258 | for (i = 0; i < nclients; i ++) | | 251 | for (i = 0; i < nclients; i ++) |
259 | - FD_SET(clientsock[i], &rfds); | | 252 | - FD_SET(clientsock[i], &rfds); |
260 | - return (rfds); | | 253 | - return (rfds); |
261 | + FD_SET(clientsock[i], rfds); | | 254 | + FD_SET(clientsock[i], rfds); |
262 | } | | 255 | } |
263 | | | 256 | |
264 | /* | | 257 | /* |
265 | @@ -724,6 +822,9 @@ | | 258 | @@ -724,6 +822,9 @@ set_hname() |
266 | exit(1); | | 259 | exit(1); |
267 | } | | 260 | } |
268 | hentry = gethostbyname(hname); | | 261 | hentry = gethostbyname(hname); |
269 | + if (hentry == NULL) { | | 262 | + if (hentry == NULL) { |
270 | + fprintf(errout, "%s: WARNING: gethostbyname(%s) failed\n", pgmnm, hname); | | 263 | + fprintf(errout, "%s: WARNING: gethostbyname(%s) failed\n", pgmnm, hname); |
271 | + } else { | | 264 | + } else { |
272 | #ifdef NO_ADDR_LIST | | 265 | #ifdef NO_ADDR_LIST |
273 | strcat(hname, ":"); | | 266 | strcat(hname, ":"); |
274 | strcat(hname, hentry->h_addr); | | 267 | strcat(hname, hentry->h_addr); |
275 | @@ -736,6 +837,7 @@ | | 268 | @@ -736,6 +837,7 @@ set_hname() |
276 | strcat(hname, inet_ntoa(*(struct in_addr *)*p++)); | | 269 | strcat(hname, inet_ntoa(*(struct in_addr *)*p++)); |
277 | } | | 270 | } |
278 | #endif | | 271 | #endif |
279 | + } | | 272 | + } |
280 | strcat(hname, ": "); | | 273 | strcat(hname, ": "); |
281 | } | | 274 | } |
282 | | | 275 | |