0
|
1 /*
|
|
2 * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
|
|
3 * This file is part of FreeWnn.
|
|
4 *
|
|
5 * Copyright Kyoto University Research Institute for Mathematical Sciences
|
|
6 * 1987, 1988, 1989, 1990, 1991, 1992
|
|
7 * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
|
|
8 * Copyright ASTEC, Inc. 1987, 1988, 1989, 1990, 1991, 1992
|
|
9 * Copyright FreeWnn Project 1999, 2000, 2001, 2002, 2003
|
|
10 *
|
|
11 * Maintainer: FreeWnn Project <freewnn@tomo.gr.jp>
|
|
12 *
|
|
13 * This program is free software; you can redistribute it and/or modify
|
|
14 * it under the terms of the GNU General Public License as published by
|
|
15 * the Free Software Foundation; either version 2 of the License, or
|
|
16 * (at your option) any later version.
|
|
17 *
|
|
18 * This program is distributed in the hope that it will be useful,
|
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 * GNU General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU General Public License
|
|
24 * along with this program; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 */
|
|
27
|
|
28 /*
|
|
29 Jserver (Nihongo Daemon)
|
|
30 */
|
|
31 static char rcs_id[] = "$Id: de.c,v 1.36 2004/06/18 16:32:41 hiroo Exp $";
|
|
32
|
|
33 #if defined(HAVE_CONFIG_H)
|
|
34 # include <config.h>
|
|
35 #endif
|
|
36 #include "getopt.h"
|
|
37
|
|
38 #include <stdio.h>
|
|
39 #include <ctype.h>
|
|
40 #include <errno.h>
|
|
41 #include <signal.h>
|
|
42 #if STDC_HEADERS
|
|
43 # include <stdlib.h>
|
|
44 # include <string.h>
|
|
45 #else
|
|
46 # if HAVE_MALLOC_H
|
|
47 # include <malloc.h>
|
|
48 # endif
|
|
49 # if HAVE_STRINGS_H
|
|
50 # include <strings.h>
|
|
51 # endif
|
|
52 #endif /* STDC_HEADERS */
|
|
53 #include <sys/ioctl.h>
|
|
54 #include <sys/stat.h>
|
|
55 #if TIME_WITH_SYS_TIME
|
|
56 # include <sys/time.h>
|
|
57 # include <time.h>
|
|
58 #else
|
|
59 # if HAVE_SYS_TIME_H
|
|
60 # include <sys/time.h>
|
|
61 # else
|
|
62 # include <time.h>
|
|
63 # endif /* HAVE_SYS_TIME_H */
|
|
64 #endif /* TIME_WITH_SYS_TIME */
|
|
65 #if HAVE_UNISTD_H
|
|
66 # include <sys/types.h>
|
|
67 # include <unistd.h>
|
|
68 #endif
|
|
69 #ifdef HAVE_FCNTL_H
|
|
70 # include <fcntl.h>
|
|
71 #endif
|
|
72 #if HAVE_SYS_PARAM_H
|
|
73 # include <sys/param.h>
|
|
74 #endif
|
|
75
|
|
76 #include "getopt.h"
|
|
77
|
|
78 #include "commonhd.h"
|
|
79 #include "wnn_config.h"
|
|
80 #include "jd_sock.h"
|
|
81 #include "demcom.h"
|
|
82 #include "wnn_os.h"
|
|
83 #define GLOBAL_VALUE_DEFINE 1
|
|
84 #include "de_header.h"
|
|
85 #undef GLOBAL_VALUE_DEFINE
|
|
86 #include "msg.h"
|
|
87
|
|
88 #ifdef SOLARIS
|
|
89 #ifdef SO_DONTLINGER
|
|
90 #undef SO_DONTLINGER
|
|
91 #endif
|
|
92 #endif /* SOLARIS */
|
|
93
|
|
94 #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_NONBLOCK)
|
|
95 #define USE_SETSOCKOPT 1
|
|
96 #else
|
|
97 #undef USE_SETSOCKOPT
|
|
98 #endif
|
|
99
|
|
100 #ifndef min
|
|
101 #define min(x,y) ( (x)<(y) ? (x) : (y) )
|
|
102 #endif
|
|
103
|
|
104 #ifndef INET6
|
|
105 # define OPTIONARGS "Df:s:h:N:p:vu4"
|
|
106 #else
|
|
107 # define OPTIONARGS "Df:s:h:N:p:vu46"
|
|
108 #endif /* INET6 */
|
|
109
|
|
110 /* Accept Socket */
|
|
111 #ifdef INET6
|
|
112 #define MAX_ACCEPTS 3
|
|
113 #else
|
|
114 #define MAX_ACCEPTS 2
|
|
115 #endif
|
|
116 #define UNIX_ACPT 0
|
|
117 #define INET_ACPT 1
|
|
118 #ifdef INET6
|
|
119 #define INET6_ACPT 2
|
|
120 #endif
|
|
121
|
|
122 #define PROTO_ALL 0x1
|
|
123 #define PROTO_UN 0x2
|
|
124 #define PROTO_INET 0x4
|
|
125 #ifdef INET6
|
|
126 #define PROTO_INET6 0x8
|
|
127 #endif
|
|
128 static int listen_proto = PROTO_ALL;
|
|
129
|
|
130 jmp_buf client_dead;
|
|
131
|
|
132 static int port;
|
|
133 static int serverNO = 0;
|
|
134
|
|
135 struct cmblk
|
|
136 {
|
|
137 int sd; /** ¥½¥±¥Ã¥È¤Îfd **/
|
|
138 };
|
|
139 #define COMS_BLOCK struct cmblk
|
|
140
|
|
141 static COMS_BLOCK *cblk;
|
|
142 /** ¥¯¥é¥¤¥¢¥ó¥È¤´¤È¤Î¥½¥±¥Ã¥È¤ò´ÉÍý¤¹¤ë¥Æ¡¼¥Ö¥ë **/
|
|
143
|
|
144 static COMS_BLOCK accept_blk[MAX_ACCEPTS]; /*accept sock blocks */
|
|
145
|
|
146
|
|
147 /* Communication Buffers */
|
|
148 static char snd_buf[R_BUF_SIZ]; /** Á÷¿®¥Ð¥Ã¥Õ¥¡ **/
|
|
149 static int sbp; /** Á÷¿®¥Ð¥Ã¥Õ¥¡¥Ý¥¤¥ó¥¿ **/
|
|
150
|
|
151 static int rbc; /** ¼õ¿®¥Ð¥Ã¥Õ¥¡counter **/
|
|
152 static char rcv_buf[S_BUF_SIZ]; /** ¼õ¿®¥Ð¥Ã¥Õ¥¡ **/
|
|
153
|
|
154 #if defined(EAGAIN)
|
|
155 # if defined(EWOULDBLOCK)
|
|
156 # define ERRNO_CHECK(no) ((no) == EAGAIN || (no) == EWOULDBLOCK)
|
|
157 # else /* defined(EWOULDBLOCK) */
|
|
158 # define ERRNO_CHECK(no) ((no) == EAGAIN)
|
|
159 # endif /* defined(EWOULDBLOCK) */
|
|
160 #else /* defined(EAGAIN) */
|
|
161 # if defined(EWOULDBLOCK)
|
|
162 # define ERRNO_CHECK(no) ((no) == EWOULDBLOCK)
|
|
163 # else /* defined(EWOULDBLOCK) */
|
|
164 # define ERRNO_CHECK(no) (0)
|
|
165 # endif /* defined(EWOULDBLOCK) */
|
|
166 #endif /* defined(EAGAIN) */
|
|
167
|
|
168 /* Client Table */
|
|
169 int clientp; /** cblk¤Î͸ú¤Ê¥Ç¡¼¥¿¤ÎºÇ¸å¤òº¹¤·¤Æ¤¤¤ë **/
|
|
170
|
|
171 int cur_clp; /** ¸½ºß¤Î¥¯¥é¥¤¥¢¥ó¥È¤ÎÈÖ¹æ **/
|
|
172
|
|
173 static fd_set *all_socks; /** ¥Ó¥Ã¥È¥Ñ¥¿¡¼¥ó
|
|
174 which jserver to select ¤òÊÝ»ý **/
|
|
175 static fd_set *ready_socks; /** ¥Ç¡¼¥¿¤Î¤¤Æ¤¤¤ë¥½¥±¥Ã¥È¤Î
|
|
176 ¥Ó¥Ã¥È¥Ñ¥¿¡¼¥ó¤òÊÝ»ý **/
|
|
177 static fd_set *dummy1_socks, *dummy2_socks;
|
|
178 static int no_of_ready_socks;
|
|
179 static int nofile; /** No. of files **/
|
|
180 struct msg_cat *wnn_msg_cat;
|
|
181 struct msg_cat *js_msg_cat;
|
|
182
|
|
183 /* function prototypes */
|
|
184 static void daemon_main (void);
|
|
185 static void socket_disc_init (void);
|
|
186 static void sel_all (void);
|
|
187 static int get_client (void);
|
|
188 static void new_client (void);
|
|
189 static void daemon_init (void);
|
|
190 static void daemon_fin_un (int);
|
|
191 static void daemon_fin_in (int);
|
|
192 static int rcv_1_client (int);
|
|
193 static void snd_1_client (int, int);
|
|
194 static void socket_init_un (void);
|
|
195 static void socket_init_in (void);
|
|
196 static int socket_accept_un (void);
|
|
197 static int socket_accept_in (int);
|
|
198 static void xerror (char*);
|
|
199 static void get_options (int, char **);
|
|
200 static void usage (void);
|
|
201 static void print_version (void);
|
|
202 #ifdef DEBUG
|
|
203 static void dmp (char*, int);
|
|
204 #endif
|
|
205
|
|
206 static char cmd_name[16];
|
|
207
|
|
208 #if defined(HAVE_LIBWRAP)
|
|
209 int allow_severity;
|
|
210 int deny_severity;
|
|
211 # include <syslog.h>
|
|
212 # include <tcpd.h>
|
|
213 #endif /* HAVE_LIBWRAP */
|
|
214
|
|
215 /* No arguments are used. Only options. */
|
|
216 int
|
|
217 main (int argc, char *argv[])
|
|
218 {
|
|
219 int tmpttyfd;
|
|
220 char *cswidth_name;
|
|
221 extern char *get_cswidth_name ();
|
|
222 extern void set_cswidth ();
|
|
223
|
|
224 char nlspath[64];
|
|
225
|
|
226 strcpy (cmd_name, WNN_DAEMON_NAME);
|
|
227 strcpy (lang_dir, LANG_NAME);
|
|
228 strcpy (nlspath, LIBDIR);
|
|
229 strcat (nlspath, "/%L/%N");
|
|
230 js_msg_cat = msg_open (MESSAGE_FILE, nlspath, lang_dir);
|
|
231 wnn_msg_cat = msg_open ("libwnn.msg", nlspath, lang_dir);
|
|
232 if (wnn_msg_cat == NULL)
|
|
233 {
|
|
234 log_err ("cannot open message file libwnn.msg.");
|
|
235 }
|
|
236 if (cswidth_name = get_cswidth_name (LANG_NAME))
|
|
237 set_cswidth (create_cswidth (cswidth_name));
|
|
238
|
|
239 port = -1;
|
|
240 /* option default */
|
|
241 option_flag = (OPT_FORK & ~OPT_VERBOSE);
|
|
242
|
|
243 setuid (geteuid ());
|
|
244 get_options (argc, argv);
|
|
245 print_version();
|
|
246 log_debug("invoked as %s.", argv[0]);
|
|
247 if (option_flag & OPT_FORK)
|
|
248 {
|
|
249 if (fork ())
|
|
250 {
|
|
251 signal (SIGCHLD, _exit);
|
|
252 signal (SIGHUP, SIG_IGN);
|
|
253 signal (SIGINT, SIG_IGN);
|
|
254 signal (SIGQUIT, SIG_IGN);
|
|
255 #ifdef SIGTSTP
|
|
256 signal (SIGTSTP, SIG_IGN);
|
|
257 #endif
|
|
258 signal (SIGTERM, _exit);
|
|
259 pause ();
|
|
260 }
|
|
261 }
|
|
262
|
|
263 #if defined(HAVE_LIBWRAP)
|
|
264 allow_severity = LOG_INFO;
|
|
265 deny_severity = LOG_WARNING;
|
|
266 /* hosts_access_verbose = 2; */
|
|
267 #endif /* HAVE_LIBWRAP */
|
|
268
|
|
269 signal (SIGHUP, signal_hand);
|
|
270 signal (SIGINT, signal_hand);
|
|
271 signal (SIGQUIT, signal_hand);
|
|
272 signal (SIGTERM, terminate_hand);
|
|
273 if (option_flag & OPT_FORK)
|
|
274 {
|
|
275 #ifdef SIGTSTP
|
|
276 signal (SIGTSTP, SIG_IGN);
|
|
277 #endif /* SIGTSTP */
|
|
278 }
|
|
279 read_default ();
|
|
280 daemon_init ();
|
|
281
|
|
282 env_init ();
|
1
|
283 if (!file_init ())
|
0
|
284 {
|
|
285 exit (1);
|
|
286 }
|
|
287 dic_init ();
|
1
|
288 if (!get_kaiseki_area (LENGTHCONV + 1)) /* ÊÑ´¹²Äǽʸ»ú¿ô */
|
0
|
289 {
|
|
290 log_err ("get_kaiseki_area failed.");
|
|
291 exit (1);
|
|
292 }
|
|
293 init_work_areas ();
|
|
294 init_jmt ();
|
|
295
|
|
296 read_default_files ();
|
|
297
|
|
298 if (option_flag & OPT_FORK)
|
|
299 {
|
|
300 /* End of initialization, kill parent */
|
|
301 kill (getppid (), SIGTERM);
|
|
302 fclose (stdin);
|
|
303 fclose (stdout);
|
|
304 if (!(option_flag & OPT_VERBOSE))
|
|
305 {
|
|
306 #if !(defined(BSD) && (BSD >= 199306)) /* !4.4BSD-Lite by Taoka */
|
|
307 fclose (stderr);
|
|
308 #else /* 4.4BSD-Lite */
|
|
309 int fd = open ("/dev/null", O_WRONLY);
|
|
310 if (fd < 0)
|
|
311 {
|
|
312 xerror ("Cannot open /dev/null");
|
|
313 }
|
|
314 dup2 (fd, 2);
|
|
315 close (fd);
|
|
316 #endif /* 4.4BSD-Lite */
|
|
317 }
|
|
318
|
|
319 #ifdef SETPGRP_VOID
|
|
320 setpgrp ();
|
|
321 #else /* !SETPGRP_VOID */
|
|
322 # if !defined(TIOCNOTTY) && defined(SVR4)
|
|
323 # define TIOCNOTTY _IO('t', 113)
|
|
324 # endif /* !defined(TIOCNOTTY) && defined(SVR4) */
|
|
325 #ifndef HITACHI
|
|
326 if ((tmpttyfd = open ("/dev/tty", O_RDWR)) >= 0)
|
|
327 {
|
|
328 ioctl (tmpttyfd, TIOCNOTTY, 0);
|
|
329 close (tmpttyfd);
|
|
330 }
|
|
331 #endif /* HITACHI */
|
|
332 #endif /* SETPGRP_VOID */
|
|
333 }
|
|
334
|
|
335 daemon_main ();
|
|
336
|
|
337 daemon_fin ();
|
|
338 return (0); /* it is not reached. only for avoiding compiler warning. */
|
|
339 }
|
|
340
|
|
341 static void
|
|
342 daemon_main (void)
|
|
343 {
|
|
344 for (;;)
|
|
345 {
|
|
346 c_c = NULL; /* Added for logging: server section */
|
|
347 sel_all ();
|
|
348 new_client ();
|
|
349 for (;;)
|
|
350 {
|
|
351 if (get_client () == -1)
|
|
352 break;
|
|
353 c_c = &client[cur_clp];
|
|
354 rbc = 0;
|
|
355 sbp = 0;
|
|
356 /* if(rcv_1_client(cur_clp) == 0){ del_client(); continue; } */
|
|
357 if (setjmp (client_dead))
|
|
358 {
|
|
359 del_client ();
|
|
360 continue;
|
|
361 }
|
|
362 do_command (c_c);
|
|
363 }
|
|
364 }
|
|
365 }
|
|
366
|
|
367 /*
|
|
368 allocs area for selecting socketts
|
|
369 */
|
|
370 static void
|
|
371 socket_disc_init (void)
|
|
372 {
|
|
373 if (WNN_NFD <= FD_SETSIZE)
|
|
374 {
|
|
375 nofile = WNN_NFD;
|
|
376 }
|
|
377 else
|
|
378 {
|
|
379 nofile = FD_SETSIZE;
|
|
380 }
|
|
381 all_socks = (fd_set *) malloc (sizeof (fd_set));
|
|
382 FD_ZERO (all_socks);
|
|
383 ready_socks = (fd_set *) malloc (sizeof (fd_set));
|
|
384 dummy1_socks = (fd_set *) malloc (sizeof (fd_set));
|
|
385 dummy2_socks = (fd_set *) malloc (sizeof (fd_set));
|
|
386 }
|
|
387
|
|
388 /** Á´¤Æ¤Î¥½¥±¥Ã¥È¤Ë¤Ä¤¤¤ÆÂÔ¤Ä **/
|
|
389 static void
|
|
390 sel_all (void)
|
|
391 {
|
|
392 memcpy (ready_socks, all_socks, sizeof (fd_set));
|
|
393 bzero (dummy1_socks, sizeof (fd_set));
|
|
394 bzero (dummy2_socks, sizeof (fd_set));
|
|
395
|
|
396 top:
|
|
397 errno = 0;
|
|
398 if ((no_of_ready_socks = select (nofile, ready_socks, dummy1_socks, dummy2_socks, NULL)) == -1)
|
|
399 {
|
|
400 if (errno == EINTR)
|
|
401 goto top;
|
|
402 xerror ("select error");
|
|
403 }
|
|
404 #ifdef DEBUG
|
|
405 log_debug ("select OK, ready_socks[0]=%02X, n-r-s=%x\n", ready_socks[0], no_of_ready_socks);
|
|
406 #endif
|
|
407 }
|
|
408
|
|
409 /** ready_socks¤«¤éº£¤ä¤ë°ì¿Í¤ò¼è¤ê½Ð¤·¤ÆÊÖ¤¹(cur_clp¤Ë¤â¥»¥Ã¥È¤¹¤ë)
|
|
410 ï¤âµï¤Ê¤¤»þ¤Ï-1
|
|
411 ¥¹¥±¥¸¥å¡¼¥ê¥ó¥°¤Ï¥é¥¦¥ó¥É¥í¥Ó¥ó **/
|
|
412 static int
|
|
413 get_client (void)
|
|
414 {
|
|
415 int i;
|
|
416
|
|
417 if (no_of_ready_socks == 0)
|
|
418 return -1; /* no client waits service */
|
|
419
|
|
420 for (i = cur_clp;;)
|
|
421 {
|
|
422 if (no_of_ready_socks == 0)
|
|
423 return -1;
|
|
424 i++;
|
|
425 if (i >= clientp)
|
|
426 i = 0;
|
|
427 if (FD_ISSET (cblk[i].sd, ready_socks))
|
|
428 {
|
|
429 FD_CLR (cblk[i].sd, ready_socks);
|
|
430 no_of_ready_socks--;
|
|
431 return cur_clp = i;
|
|
432 }
|
|
433 }
|
|
434 }
|
|
435
|
|
436 /** ¿·¤·¤¤¥¯¥é¥¤¥¢¥ó¥È¤¬µï¤ë¤«Èݤ«¤òÄ´¤Ù¤ë
|
|
437 µï¤¿¾ì¹ç¤Ïcblk¤ËÅÐÏ¿¤¹¤ë **/
|
|
438 static void
|
|
439 new_client (void) /* NewClient */
|
|
440 {
|
|
441 int sd;
|
|
442 int full, i;
|
|
443 FILE *f[3];
|
|
444 char gomi[1024];
|
|
445 #ifdef HAVE_LIBWRAP
|
|
446 int is_internet_socket;
|
|
447 struct request_info tcpd_request;
|
|
448 #endif /* HAVE_LIBWRAP */
|
|
449 #ifdef AF_UNIX
|
|
450 if ((serverNO == 0) &&
|
|
451 (FD_ISSET (accept_blk[UNIX_ACPT].sd, ready_socks)))
|
|
452 {
|
|
453 FD_CLR (accept_blk[UNIX_ACPT].sd, ready_socks);
|
|
454 no_of_ready_socks--;
|
|
455 sd = socket_accept_un ();
|
|
456 #ifdef HAVE_LIBWRAP
|
|
457 is_internet_socket = 0;
|
|
458 #endif
|
|
459 }
|
|
460 else
|
|
461 #endif
|
|
462 #ifdef INET6
|
|
463 if (FD_ISSET (accept_blk[INET6_ACPT].sd, ready_socks))
|
|
464 {
|
|
465 FD_CLR (accept_blk[INET6_ACPT].sd, ready_socks);
|
|
466 no_of_ready_socks--;
|
|
467 sd = socket_accept_in (accept_blk[INET6_ACPT].sd);
|
|
468 #ifdef HAVE_LIBWRAP
|
|
469 is_internet_socket = 1;
|
|
470 #endif
|
|
471 }
|
|
472 else
|
|
473 #endif
|
|
474 if (FD_ISSET (accept_blk[INET_ACPT].sd, ready_socks))
|
|
475 {
|
|
476 FD_CLR (accept_blk[INET_ACPT].sd, ready_socks);
|
|
477 no_of_ready_socks--;
|
|
478 sd = socket_accept_in (accept_blk[INET_ACPT].sd);
|
|
479 #ifdef HAVE_LIBWRAP
|
|
480 is_internet_socket = 1;
|
|
481 #endif
|
|
482 }
|
|
483 else
|
|
484 {
|
|
485 return;
|
|
486 }
|
|
487 log_debug ("new client: sd = %d", sd);
|
|
488 /* reserve 2 fd */
|
|
489 for (full = i = 0; i < 2; i++)
|
|
490 {
|
|
491 if (NULL == (f[i] = fopen ("/dev/null", "r")))
|
|
492 {
|
|
493 full = 1;
|
|
494 }
|
|
495 }
|
|
496 for (i = 0; i < 2; i++)
|
|
497 {
|
|
498 if (NULL != f[i])
|
|
499 fclose (f[i]);
|
|
500 }
|
|
501
|
|
502 if (full || sd >= nofile || clientp >= max_client)
|
|
503 {
|
|
504 log_err ("no more client.");
|
|
505 #ifdef HAVE_RECV
|
|
506 recv (sd, gomi, 1024, 0);
|
|
507 #else
|
|
508 read (sd, gomi, 1024);
|
|
509 #endif
|
|
510 shutdown (sd, 2);
|
|
511 #ifdef HAVE_CLOSESOCKET
|
|
512 closesocket (sd);
|
|
513 #else
|
|
514 close (sd);
|
|
515 #endif
|
|
516 return;
|
|
517 }
|
|
518
|
|
519 #ifdef HAVE_LIBWRAP
|
|
520 if (is_internet_socket) {
|
|
521 request_init (&tcpd_request,RQ_DAEMON, WNN_DAEMON_NAME,
|
|
522 RQ_FILE, sd, NULL);
|
|
523 fromhost (&tcpd_request);
|
|
524 if (!hosts_access (&tcpd_request))
|
|
525 {
|
|
526 log_err ("reject client."); /* should be log_info? */
|
|
527 /* should we log IP address / hostname? */
|
|
528 #ifdef HAVE_RECV
|
|
529 recv (sd, gomi, 1024, 0);
|
|
530 #else
|
|
531 read (sd, gomi, 1024);
|
|
532 #endif
|
|
533 shutdown (sd, 2);
|
|
534 #ifdef HAVE_CLOSESOCKET
|
|
535 closesocket (sd);
|
|
536 #else
|
|
537 close (sd);
|
|
538 #endif
|
|
539 return;
|
|
540 }
|
|
541 }
|
|
542 #endif /* HAVE_LIBWRAP */
|
|
543
|
|
544 cblk[clientp].sd = sd;
|
|
545 FD_SET (sd, all_socks);
|
|
546 for (i = 0; i < WNN_MAX_ENV_OF_A_CLIENT; i++)
|
|
547 {
|
|
548 (client[clientp].env)[i] = -1;
|
|
549 }
|
|
550 clientp++;
|
|
551 }
|
|
552
|
|
553 /** ¥¯¥é¥¤¥¢¥ó¥È¤òcblk¤«¤éºï½ü¤¹¤ë **/
|
|
554 /* delete Client (please call from JS_CLOSE) */
|
|
555 void
|
|
556 del_client (void)
|
|
557 {
|
|
558 disconnect_all_env_of_client ();
|
|
559 FD_CLR (cblk[cur_clp].sd, all_socks);
|
|
560 #ifdef HAVE_CLOSESOCKET
|
|
561 closesocket (cblk[cur_clp].sd);
|
|
562 #else
|
|
563 close (cblk[cur_clp].sd);
|
|
564 #endif
|
|
565 /* logging here because c_c (used in log_debug) will be broken after
|
|
566 following section */
|
|
567 log_debug("Delete Client: cur_clp = %d\n", cur_clp);
|
|
568 cblk[cur_clp] = cblk[clientp - 1];
|
|
569 client[cur_clp] = client[clientp - 1];
|
|
570 /* Clear host/user name with zero - needed for logging */
|
|
571 client[clientp - 1].user_name[0] = '\0'; /* Should we use bzero()? */
|
|
572 client[clientp - 1].host_name[0] = '\0';
|
|
573 clientp--;
|
|
574 }
|
|
575
|
|
576
|
|
577 /** ¥µ¡¼¥Ð¤ò¥¤¥Ë¥·¥ã¥é¥¤¥º¤¹¤ë **/
|
|
578 static void
|
|
579 daemon_init (void) /* initialize Daemon */
|
|
580 {
|
|
581 /*
|
|
582 signal (SIGHUP, SIG_IGN);
|
|
583 signal (SIGINT, SIG_IGN);
|
|
584 signal (SIGQUIT, SIG_IGN);
|
|
585 */
|
|
586
|
|
587
|
|
588 if ((cblk = (COMS_BLOCK *) malloc (max_client * sizeof (COMS_BLOCK))) == NULL)
|
|
589 {
|
|
590 xerror ("daemon_init: ");
|
|
591 }
|
|
592 if ((client = (CLIENT *) malloc (max_client * sizeof (CLIENT))) == NULL)
|
|
593 {
|
|
594 xerror ("daemon_init: ");
|
|
595 }
|
|
596 SDRAND (time (NULL));
|
|
597 clientp = 0; /* V3.0 */
|
|
598 cur_clp = 0; /* V3.0 */
|
|
599 socket_disc_init ();
|
|
600 #ifdef INET6
|
|
601 if (listen_proto&(PROTO_ALL|PROTO_INET|PROTO_INET6))
|
|
602 socket_init_in ();
|
|
603 #else
|
|
604 if (listen_proto&(PROTO_ALL|PROTO_INET))
|
|
605 socket_init_in ();
|
|
606 #endif
|
|
607 #ifdef AF_UNIX
|
|
608 if (listen_proto&(PROTO_ALL|PROTO_UN))
|
|
609 socket_init_un ();
|
|
610 #endif /* AF_UNIX */
|
|
611 }
|
|
612
|
|
613 /** ¥µ¡¼¥Ð¤ò½ª¤ï¤ë **/
|
|
614 #ifdef AF_UNIX
|
|
615 static void
|
|
616 daemon_fin_un (int sock_d_un)
|
|
617 {
|
|
618 int trueFlag = 1;
|
|
619 struct sockaddr_un addr_un;
|
|
620 socklen_t addrlen;
|
|
621
|
|
622 if (serverNO == 0)
|
|
623 {
|
|
624 #ifndef SOLARIS
|
|
625 #if defined(FIONBIO)
|
|
626 ioctl (sock_d_un, FIONBIO, &trueFlag);
|
|
627 #endif
|
|
628 #else /* !SOLARIS */
|
|
629 fcntl (sock_d_un, F_SETFL, F_UNLCK);
|
|
630 #endif /* !SOLARIS */
|
|
631 for (;;)
|
|
632 {
|
|
633 addrlen = sizeof (addr_un);
|
|
634 if (accept (sock_d_un, (struct sockaddr *) &addr_un, &addrlen) < 0)
|
|
635 break;
|
|
636 /* EWOULDBLOCK EXPECTED, but we don't check */
|
|
637 }
|
|
638 shutdown (sock_d_un, 2);
|
|
639 close (sock_d_un);
|
|
640 }
|
|
641 }
|
|
642 #endif /* AF_UNIX */
|
|
643
|
|
644 static void
|
|
645 daemon_fin_in (int sock_d_in)
|
|
646 {
|
|
647 int trueFlag = 1;
|
|
648 struct sockaddr_in addr_in;
|
|
649 socklen_t addrlen;
|
|
650 #ifdef USE_SETSOCKOPT
|
|
651 int on = ~0;
|
|
652 #endif
|
|
653
|
|
654 #ifndef SOLARIS
|
|
655 #ifdef USE_SETSOCKOPT
|
|
656 setsockopt (sock_d_in, SOL_SOCKET, SO_NONBLOCK, &on, sizeof (int));
|
|
657 #else
|
|
658 #if defined(FIONBIO)
|
|
659 ioctl (sock_d_in, FIONBIO, &trueFlag);
|
|
660 #endif
|
|
661 #endif /* USE_SETSOCKOPT */
|
|
662 #else /* !SOLARIS */
|
|
663 fcntl (sock_d_in, F_SETFL, F_UNLCK);
|
|
664 #endif /* !SOLARIS */
|
|
665 for (;;)
|
|
666 {
|
|
667 addrlen = sizeof (addr_in);
|
|
668 if (accept (sock_d_in, (struct sockaddr *) &addr_in, &addrlen) < 0)
|
|
669 break;
|
|
670 /* EWOULDBLOCK EXPECTED, but we don't check */
|
|
671 }
|
|
672 shutdown (sock_d_in, 2);
|
|
673 #ifdef HAVE_CLOSESOCKET
|
|
674 closesocket (sock_d_in);
|
|
675 #else
|
|
676 close (sock_d_in);
|
|
677 #endif
|
|
678 }
|
|
679
|
|
680 void
|
|
681 daemon_fin (void)
|
|
682 {
|
|
683 int fd;
|
|
684 #ifdef AF_UNIX
|
|
685 int sock_d_un = accept_blk[UNIX_ACPT].sd;
|
|
686 #endif /* AF_UNIX */
|
|
687 int sock_d_in = accept_blk[INET_ACPT].sd;
|
|
688 #ifdef INET6
|
|
689 int sock_d_in6 = accept_blk[INET6_ACPT].sd;
|
|
690 #endif
|
|
691
|
|
692 /*
|
|
693 accept all pending connection from new clients,
|
|
694 avoiding kernel hangup.
|
|
695 */
|
|
696 #ifdef AF_UNIX
|
|
697 daemon_fin_un (sock_d_un);
|
|
698 #endif
|
|
699 daemon_fin_in (sock_d_in);
|
|
700 #ifdef INET6
|
|
701 daemon_fin_in (sock_d_in6);
|
|
702 #endif
|
|
703
|
|
704 for (fd = nofile - 1; fd >= 0; fd--)
|
|
705 {
|
|
706 if ((fd != sock_d_in) &&
|
|
707 #ifdef INET6
|
|
708 (fd != sock_d_in6) &&
|
|
709 #endif
|
|
710 #ifdef AF_UNIX
|
|
711 (fd != sock_d_un) &&
|
|
712 #endif /* AF_UNIX */
|
|
713 FD_ISSET (fd, all_socks))
|
|
714 {
|
|
715 shutdown (fd, 2);
|
|
716 #ifdef HAVE_CLOSESOCKET
|
|
717 closesocket (fd);
|
|
718 #else
|
|
719 close (fd);
|
|
720 #endif
|
|
721 }
|
|
722 }
|
|
723 }
|
|
724
|
|
725 /*------*/
|
|
726
|
|
727 /** **/
|
|
728 char *
|
|
729 gets_cur (char *buffer, size_t buffer_size)
|
|
730 {
|
|
731 char *b;
|
|
732
|
|
733 if (!buffer || !buffer_size)
|
|
734 return NULL;
|
|
735
|
|
736 b = buffer;
|
|
737
|
|
738 while (--buffer_size && (*b = getc_cur ()) != '\0')
|
|
739 b++;
|
|
740
|
|
741 if (!buffer_size)
|
|
742 {
|
|
743 *b = '\0';
|
|
744 while (getc_cur () != '\0')
|
|
745 ;
|
|
746 }
|
|
747
|
|
748 return buffer;
|
|
749 }
|
|
750
|
|
751 /** **/
|
|
752 w_char *
|
|
753 getws_cur (w_char *buffer, size_t buffer_size)
|
|
754 {
|
|
755 w_char *b;
|
|
756
|
|
757 if (!buffer || !buffer_size)
|
|
758 return NULL;
|
|
759
|
|
760 b = buffer;
|
|
761
|
|
762 while (--buffer_size && (*b = get2_cur ()) != 0)
|
|
763 b++;
|
|
764
|
|
765 if (!buffer_size)
|
|
766 {
|
|
767 *b = 0;
|
|
768 while (getc_cur () != 0)
|
|
769 ;
|
|
770 }
|
|
771
|
|
772 return buffer;
|
|
773 }
|
|
774
|
|
775 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤«¤é2¥Ð¥¤¥È¼è¤ë **/
|
|
776 int
|
|
777 get2_cur (void)
|
|
778 {
|
|
779 int x;
|
|
780 x = getc_cur ();
|
|
781 return (x << 8) | getc_cur ();
|
|
782 }
|
|
783
|
|
784 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤«¤é4¥Ð¥¤¥È¼è¤ë **/
|
|
785 int
|
|
786 get4_cur (void)
|
|
787 {
|
|
788 int x1, x2, x3;
|
|
789 x1 = getc_cur ();
|
|
790 x2 = getc_cur ();
|
|
791 x3 = getc_cur ();
|
|
792 return (x1 << (8 * 3)) | (x2 << (8 * 2)) | (x3 << (8 * 1)) | getc_cur ();
|
|
793 }
|
|
794
|
|
795 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤«¤é1¥Ð¥¤¥È¼è¤ë **/
|
|
796 int
|
|
797 getc_cur (void)
|
|
798 {
|
|
799 static int rbp;
|
|
800 if (rbc <= 0)
|
|
801 {
|
|
802 rbc = rcv_1_client (cur_clp);
|
|
803 rbp = 0;
|
|
804 }
|
|
805 rbc--;
|
|
806 return rcv_buf[rbp++] & 0xFF;
|
|
807 }
|
|
808
|
|
809 /** ¥¯¥é¥¤¥¢¥ó¥È¤«¤é1¥Ñ¥±¥Ã¥È¼è¤ë **/
|
|
810 static int
|
|
811 rcv_1_client (int clp) /* clp=¥¯¥é¥¤¥¢¥ó¥ÈÈÖ¹æ */
|
|
812 {
|
|
813 int cc = 0;
|
|
814 while (cc <= 0)
|
|
815 {
|
|
816 errno = 0;
|
|
817 #ifdef HAVE_RECV
|
|
818 cc = recv (cblk[clp].sd, rcv_buf, S_BUF_SIZ, 0);
|
|
819 #else
|
|
820 cc = read (cblk[clp].sd, rcv_buf, S_BUF_SIZ);
|
|
821 #endif
|
|
822 if (cc <= 0)
|
|
823 {
|
|
824 if (ERRNO_CHECK (errno))
|
|
825 {
|
|
826 continue;
|
|
827 }
|
|
828 else if (cc == 0)
|
|
829 { /* client dead */
|
|
830 longjmp (client_dead, 666);
|
|
831 }
|
|
832 else
|
|
833 { /* cc == -1 */
|
|
834 if (errno != EINTR)
|
|
835 longjmp (client_dead, 666);
|
|
836 continue;
|
|
837 }
|
|
838 }
|
|
839 }
|
|
840 #ifdef DEBUG
|
|
841 log_debug ("rcv: clp = %d, sd = %d, cc = %d", clp, cblk[clp].sd, cc);
|
|
842 dmp (rcv_buf, cc);
|
|
843 #endif
|
|
844 return cc;
|
|
845 }
|
|
846
|
|
847 /** ¥¯¥é¥¤¥¢¥ó¥È¤Ø1¥Ñ¥±¥Ã¥ÈÁ÷¤ë **/
|
|
848 static void
|
|
849 snd_1_client (int clp, /* clp: ¥¯¥é¥¤¥¢¥ó¥ÈÈÖ¹æ */
|
|
850 int n /* n : number of bytes to send */ )
|
|
851 {
|
|
852 int cc, x;
|
|
853 #ifdef DEBUG
|
|
854 log_debug ("snd: clp = %d, sd = %d", clp, cblk[clp].sd);
|
|
855 dmp (snd_buf, n);
|
|
856 #endif
|
|
857 for (cc = 0; cc < n;)
|
|
858 {
|
|
859 errno = 0;
|
|
860 #ifdef HAVE_SEND
|
|
861 x = send (cblk[clp].sd, &snd_buf[cc], n - cc, 0);
|
|
862 #else
|
|
863 x = write (cblk[clp].sd, &snd_buf[cc], n - cc);
|
|
864 #endif
|
|
865 if (x < 0)
|
|
866 {
|
|
867 if (ERRNO_CHECK (errno) || errno == EINTR)
|
|
868 {
|
|
869 errno = 0;
|
|
870 continue;
|
|
871 }
|
|
872 else
|
|
873 { /* client dead */
|
|
874 longjmp (client_dead, 666);
|
|
875 }
|
|
876 }
|
|
877 cc += x;
|
|
878 }
|
|
879 }
|
|
880
|
|
881 /** **/
|
|
882 void
|
|
883 puts_cur (char *p)
|
|
884 {
|
|
885 int c;
|
|
886 while (c = *p++)
|
|
887 putc_cur (c);
|
|
888 putc_cur (0);
|
|
889 }
|
|
890
|
|
891 /** **/
|
|
892 void
|
|
893 puts_n_cur (char *p, int n)
|
|
894 {
|
|
895 int c;
|
|
896 while ((c = *p++) && --n >= 0)
|
|
897 putc_cur (c);
|
|
898 putc_cur (0);
|
|
899 }
|
|
900
|
|
901 /** **/
|
|
902 void
|
|
903 putws_cur (w_char *p)
|
|
904 {
|
|
905 int c;
|
|
906 while (c = *p++)
|
|
907 put2_cur (c);
|
|
908 put2_cur (0);
|
|
909 }
|
|
910
|
|
911 /** **/
|
|
912 void
|
|
913 putnws_cur (w_char *p, int n)
|
|
914 {
|
|
915 int c;
|
|
916 for (; n > 0; n--)
|
|
917 {
|
|
918 if ((c = *p++) == 0)
|
|
919 break;
|
|
920 put2_cur (c);
|
|
921 }
|
|
922 put2_cur (0);
|
|
923 }
|
|
924
|
|
925 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤Ø2¥Ð¥¤¥ÈÁ÷¤ë **/
|
|
926 void
|
|
927 put2_cur (int c)
|
|
928 {
|
|
929 putc_cur (c >> (8 * 1));
|
|
930 putc_cur (c);
|
|
931 }
|
|
932
|
|
933 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤Ø4¥Ð¥¤¥ÈÁ÷¤ë **/
|
|
934 void
|
|
935 put4_cur (int c)
|
|
936 {
|
|
937 putc_cur (c >> (8 * 3));
|
|
938 putc_cur (c >> (8 * 2));
|
|
939 putc_cur (c >> (8 * 1));
|
|
940 putc_cur (c);
|
|
941 }
|
|
942
|
|
943 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤Ø1¥Ð¥¤¥ÈÁ÷¤ë **/
|
|
944 void
|
|
945 putc_cur (int c)
|
|
946 {
|
|
947 snd_buf[sbp++] = c;
|
|
948 if (sbp >= R_BUF_SIZ)
|
|
949 {
|
|
950 snd_1_client (cur_clp, R_BUF_SIZ);
|
|
951 sbp = 0;
|
|
952 }
|
|
953 }
|
|
954
|
|
955 /** ¥«¥ì¥ó¥È¡¦¥¯¥é¥¤¥¢¥ó¥È¤ÎÁ÷¿®¥Ð¥Ã¥Õ¥¡¤ò¥Õ¥é¥Ã¥·¥å¤¹¤ë **/
|
|
956 void
|
|
957 putc_purge (void)
|
|
958 {
|
|
959 if (sbp != 0)
|
|
960 {
|
|
961 snd_1_client (cur_clp, sbp);
|
|
962 sbp = 0;
|
|
963 }
|
|
964 }
|
|
965
|
|
966 /*-----*/
|
|
967
|
|
968 /** ¥½¥±¥Ã¥È¤Î¥¤¥Ë¥·¥ã¥é¥¤¥º **/
|
|
969 #ifdef AF_UNIX
|
|
970 static void
|
|
971 socket_init_un (void)
|
|
972 {
|
|
973 struct sockaddr_un saddr_un;
|
|
974 int sock_d_un;
|
|
975 if (serverNO == 0)
|
|
976 {
|
|
977 saddr_un.sun_family = AF_UNIX;
|
|
978 unlink (sockname);
|
|
979 strcpy (saddr_un.sun_path, sockname);
|
|
980 if ((sock_d_un = socket (AF_UNIX, SOCK_STREAM, 0)) == ERROR)
|
|
981 {
|
|
982 xerror ("could not create unix domain socket");
|
|
983 }
|
|
984 if (bind (sock_d_un, (struct sockaddr *) &saddr_un, strlen (saddr_un.sun_path) + 2) == ERROR)
|
|
985 {
|
|
986 shutdown (sock_d_un, 2);
|
|
987 xerror ("could not bind unix domain socket");
|
|
988 }
|
|
989 if (listen (sock_d_un, 5) == ERROR)
|
|
990 {
|
|
991 shutdown (sock_d_un, 2);
|
|
992 xerror ("could not listen unix domain socket");
|
|
993 }
|
|
994 chmod (sockname, 0777);
|
|
995 signal (SIGPIPE, SIG_IGN);
|
|
996 #ifdef DEBUG
|
|
997 log_debug ("sock_d_un = %d", sock_d_un);
|
|
998 #endif
|
|
999 accept_blk[UNIX_ACPT].sd = sock_d_un;
|
|
1000 FD_SET (sock_d_un, all_socks);
|
|
1001 }
|
|
1002 }
|
|
1003 #endif /* AF_UNIX */
|
|
1004
|
|
1005 /* Inet V3.0 */
|
|
1006 static void
|
|
1007 socket_init_in (void)
|
|
1008 {
|
|
1009 #ifndef SOLARIS
|
|
1010 int on = 1;
|
|
1011 #else /* SOLARIS */
|
|
1012 int on = 0;
|
|
1013 #endif /* SOLARIS */
|
|
1014 struct servent *sp;
|
|
1015 #if !defined(SO_DONTLINGER) && defined(SO_LINGER)
|
|
1016 struct linger linger;
|
|
1017 #endif
|
|
1018 #ifdef INET6
|
|
1019 struct addrinfo hints, *res, *res0;
|
|
1020 int error;
|
|
1021 char sport[6];
|
|
1022 #else
|
|
1023 struct sockaddr_in saddr_in;
|
|
1024 #endif
|
|
1025 int sock_d_in;
|
|
1026
|
|
1027 if (port < 0)
|
|
1028 {
|
|
1029 if ((sp = getservbyname (SERVERNAME, "tcp")) == NULL)
|
|
1030 {
|
|
1031 port = WNN_PORT_IN;
|
|
1032 }
|
|
1033 else
|
|
1034 {
|
|
1035 port = ntohs (sp->s_port);
|
|
1036 }
|
|
1037 }
|
|
1038
|
|
1039 port += serverNO;
|
|
1040
|
|
1041 #if DEBUG
|
|
1042 log_debug ("port=%x", port);
|
|
1043 #endif
|
|
1044 #ifdef INET6
|
|
1045 memset(&hints, 0, sizeof(hints));
|
|
1046 if (listen_proto&PROTO_INET && !(listen_proto&PROTO_INET6))
|
|
1047 hints.ai_family = PF_INET;
|
|
1048 else if (listen_proto&PROTO_INET6 && !(listen_proto&PROTO_INET))
|
|
1049 hints.ai_family = PF_INET6;
|
|
1050 else
|
|
1051 hints.ai_family = PF_UNSPEC;
|
|
1052 hints.ai_socktype = SOCK_STREAM;
|
|
1053 hints.ai_flags = AI_PASSIVE;
|
|
1054 sprintf(sport, "%d", port);
|
|
1055 error = getaddrinfo(NULL, sport, &hints, &res0);
|
|
1056 if (error)
|
|
1057 {
|
|
1058 xerror (gai_strerror(error));
|
|
1059 }
|
|
1060 for (res = res0; res; res = res->ai_next) {
|
|
1061 if (res->ai_family == AF_INET || res->ai_family == AF_INET6){
|
|
1062 if ((sock_d_in = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) == ERROR)
|
|
1063 #else
|
|
1064 saddr_in.sin_family = AF_INET;
|
|
1065 saddr_in.sin_port = htons (port);
|
|
1066 saddr_in.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
1067 if ((sock_d_in = socket (AF_INET, SOCK_STREAM, 0)) == ERROR)
|
|
1068 #endif
|
|
1069 {
|
|
1070 #ifdef INET6
|
|
1071 if (res->ai_family == AF_INET6)
|
|
1072 xerror ("could not create inet6 socket");
|
|
1073 else if (res->ai_family == AF_INET)
|
|
1074 #endif
|
|
1075 xerror ("could not create inet socket");
|
|
1076 }
|
|
1077 setsockopt (sock_d_in, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (int));
|
|
1078 #ifdef SO_DONTLINGER
|
|
1079 setsockopt (sock_d_in, SOL_SOCKET, SO_DONTLINGER, (char *) 0, 0);
|
|
1080 #else
|
|
1081 # ifdef SO_LINGER
|
|
1082 linger.l_onoff = 0;
|
|
1083 linger.l_linger = 0;
|
|
1084 setsockopt (sock_d_in, SOL_SOCKET, SO_LINGER, (char *) &linger, sizeof linger);
|
|
1085 # endif /* SO_LINGER */
|
|
1086 #endif /* SO_DONTLINGER */
|
|
1087
|
|
1088 #ifdef INET6
|
|
1089 if (bind (sock_d_in, res->ai_addr, res->ai_addrlen) == ERROR)
|
|
1090 #else
|
|
1091 if (bind (sock_d_in, (struct sockaddr *) &saddr_in, sizeof (saddr_in)) == ERROR)
|
|
1092 #endif
|
|
1093 {
|
|
1094 shutdown (sock_d_in, 2);
|
|
1095 #ifdef INET6
|
|
1096 if (res->ai_family == AF_INET6)
|
|
1097 xerror ("can't bind inet6 socket");
|
|
1098 else if (res->ai_family == AF_INET)
|
|
1099 #endif
|
|
1100 xerror ("can't bind inet socket");
|
|
1101 }
|
|
1102 if (listen (sock_d_in, 5) == ERROR)
|
|
1103 {
|
|
1104 shutdown (sock_d_in, 2);
|
|
1105 #ifdef INET6
|
|
1106 if (res->ai_family == AF_INET6)
|
|
1107 xerror ("can't listen inet6 socket");
|
|
1108 else if (res->ai_family == AF_INET)
|
|
1109 #endif
|
|
1110 xerror ("can't listen inet socket");
|
|
1111 }
|
|
1112 #if DEBUG
|
|
1113 log_debug ("sock_d_in = %d", sock_d_in);
|
|
1114 #endif
|
|
1115 FD_SET (sock_d_in, all_socks);
|
|
1116 #ifdef INET6
|
|
1117 if (res->ai_family == AF_INET)
|
|
1118 accept_blk[INET_ACPT].sd = sock_d_in;
|
|
1119 else if (res->ai_family == AF_INET6)
|
|
1120 accept_blk[INET6_ACPT].sd = sock_d_in;
|
|
1121 }
|
|
1122 }
|
|
1123 freeaddrinfo(res0);
|
|
1124 #else
|
|
1125 accept_blk[INET_ACPT].sd = sock_d_in;
|
|
1126 #endif
|
|
1127 }
|
|
1128
|
|
1129
|
|
1130 /** accept new client socket **/
|
|
1131 #ifdef AF_UNIX
|
|
1132 static int
|
|
1133 socket_accept_un (void)
|
|
1134 {
|
|
1135 struct sockaddr_un addr;
|
|
1136 socklen_t addrlen;
|
|
1137
|
|
1138 addrlen = sizeof (addr);
|
|
1139 return accept (accept_blk[UNIX_ACPT].sd, (struct sockaddr *) &addr, &addrlen);
|
|
1140 }
|
|
1141 #endif /* AF_UNIX */
|
|
1142
|
|
1143 static int
|
|
1144 socket_accept_in (int fd)
|
|
1145 {
|
|
1146 struct sockaddr_in addr;
|
|
1147 socklen_t addrlen;
|
|
1148
|
|
1149 addrlen = sizeof (addr);
|
|
1150 return accept (fd, (struct sockaddr *) &addr, &addrlen);
|
|
1151 }
|
|
1152
|
|
1153 static void
|
|
1154 xerror (char *s)
|
|
1155 {
|
|
1156 log_err ("%s (%s).", s, strerror(errno));
|
|
1157 exit (1);
|
|
1158 }
|
|
1159
|
|
1160 #if DEBUG
|
|
1161 static void
|
|
1162 dmp (char *p, int n)
|
|
1163 {
|
|
1164 int i, j;
|
|
1165
|
|
1166 for (i = 0; i < n; i += 16)
|
|
1167 {
|
|
1168 for (j = 0; j < 16; j++)
|
|
1169 {
|
|
1170 fprintf (stderr, "%02x ", p[i + j] & 0xFF);
|
|
1171 }
|
|
1172 fprintf (stderr, "n=%d\n", n);
|
|
1173 }
|
|
1174 }
|
|
1175 #endif
|
|
1176
|
|
1177 static void
|
|
1178 get_options (int argc, char **argv)
|
|
1179 {
|
|
1180 int c;
|
|
1181 int digit_optind = 0;
|
|
1182
|
|
1183 strcpy (jserverrcfile, LIBDIR); /* usr/local/lib/wnn */
|
|
1184 strcat (jserverrcfile, SERVER_INIT_FILE); /* ja_JP/jserverrc */
|
|
1185
|
|
1186 while (1)
|
|
1187 {
|
|
1188 int this_option_optind = optind ? optind : 1;
|
|
1189 int option_index = 0;
|
|
1190 static struct option long_options[] =
|
|
1191 {
|
|
1192 {"baseport", 1, NULL, 'p'},
|
|
1193 {"inet", 0, NULL, '4'},
|
|
1194 {"inet6", 0, NULL, '6'},
|
|
1195 {"jserverrc", 1, NULL, 'f'},
|
|
1196 {"version", 0, NULL, 'v'},
|
|
1197 {0, 0, 0, 0}
|
|
1198 };
|
|
1199
|
|
1200 c = getopt_long (argc, argv, OPTIONARGS,
|
|
1201 long_options, &option_index);
|
|
1202 if (c == -1)
|
|
1203 break;
|
|
1204
|
|
1205 switch (c)
|
|
1206 {
|
|
1207 case 'D': /* do not detach, not a daemon */
|
|
1208 option_flag &= ~OPT_FORK;
|
|
1209 break;
|
|
1210
|
|
1211 case 'f': /* --jserverrc FILENAME */
|
|
1212 strcpy (jserverrcfile, optarg);
|
|
1213 break;
|
|
1214
|
|
1215 case 's':
|
|
1216 /* should nuke noisy someday */
|
|
1217 noisy = 1; option_flag |= OPT_VERBOSE;
|
|
1218 if (strcmp ("-", optarg) != 0)
|
|
1219 {
|
|
1220 /** maybe FILE wnnerr = stderr; or wnnerr = open(optarg...) is better? or freopen is normal method? */
|
|
1221 /** take a look at daemon(3) */
|
|
1222 if (freopen (optarg, "a", stderr) == NULL)
|
|
1223 {
|
|
1224 /** fprintf to stderr? */
|
|
1225 printf ("Error in opening scriptfile %s.\n", optarg);
|
|
1226 exit (1);
|
|
1227 }
|
|
1228 }
|
|
1229 log_debug ("script started");
|
|
1230 break;
|
|
1231
|
|
1232 case 'h':
|
|
1233 /* var hinsi_file_name polluted */
|
|
1234 hinsi_file_name = optarg;
|
|
1235 break;
|
|
1236
|
|
1237 case 'N':
|
|
1238 serverNO = atoi (optarg);
|
|
1239 /* error handling needed */
|
|
1240 break;
|
|
1241
|
|
1242 case 'p':
|
|
1243 port = atoi (optarg);
|
|
1244 /* error handling needed */
|
|
1245 break;
|
|
1246
|
|
1247 case 'v':
|
|
1248 print_version();
|
|
1249 usage();
|
|
1250 break;
|
|
1251
|
|
1252 case 'u':
|
|
1253 listen_proto &= ~PROTO_ALL;
|
|
1254 listen_proto |= PROTO_UN;
|
|
1255 break;
|
|
1256
|
|
1257 case '4':
|
|
1258 listen_proto &= ~PROTO_ALL;
|
|
1259 listen_proto |= PROTO_INET;
|
|
1260 break;
|
|
1261
|
|
1262 #ifdef INET6
|
|
1263 case '6':
|
|
1264 listen_proto &= ~PROTO_ALL;
|
|
1265 listen_proto |= PROTO_INET6;
|
|
1266 break;
|
|
1267 #endif /* INET6 */
|
|
1268
|
|
1269 default:
|
|
1270 print_version();
|
|
1271 usage();
|
|
1272 break;
|
|
1273 }
|
|
1274 }
|
|
1275 }
|
|
1276
|
|
1277
|
|
1278 /*
|
|
1279 */
|
|
1280 void
|
|
1281 js_who (void)
|
|
1282 {
|
|
1283 int i, j;
|
|
1284
|
|
1285 put4_cur (clientp);
|
|
1286 for (i = 0; i < clientp; i++)
|
|
1287 {
|
|
1288 put4_cur (cblk[i].sd);
|
|
1289 puts_cur (client[i].user_name);
|
|
1290 puts_cur (client[i].host_name);
|
|
1291 for (j = 0; j < WNN_MAX_ENV_OF_A_CLIENT; j++)
|
|
1292 {
|
|
1293 put4_cur ((client[i].env)[j]);
|
|
1294 }
|
|
1295
|
|
1296 }
|
|
1297 putc_purge ();
|
|
1298 }
|
|
1299
|
|
1300 void
|
|
1301 js_kill (void)
|
|
1302 {
|
|
1303 if (clientp == 1)
|
|
1304 {
|
|
1305 put4_cur (0);
|
|
1306 putc_purge ();
|
|
1307 terminate_hand ();
|
|
1308 }
|
|
1309 else
|
|
1310 {
|
|
1311 put4_cur (clientp - 1);
|
|
1312 putc_purge ();
|
|
1313 }
|
|
1314 }
|
|
1315
|
|
1316 void
|
|
1317 usage (void)
|
|
1318 {
|
|
1319 fprintf(stderr,
|
|
1320 #ifdef INET6
|
|
1321 "usage: %s [-Du46][-f <init_file> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
|
|
1322 #else
|
|
1323 "usage: %s [-Du4][-f <init_file> -s <log_file(\"-\" for stderr)> -h <pos_file> -N <serverNO> -p <port_base>]\n",
|
|
1324 #endif
|
|
1325 cmd_name);
|
|
1326 fprintf(stderr,
|
|
1327 " %s -v\n",
|
|
1328 cmd_name);
|
|
1329 exit (1);
|
|
1330 }
|
|
1331
|
|
1332 void
|
|
1333 print_version (void)
|
|
1334 {
|
|
1335 #if defined(CHINESE)
|
|
1336 printf ("%s (%s) Chinese Multi Client Server\n", cmd_name, SER_VERSION);
|
|
1337 #elif defined(KOREAN)
|
|
1338 printf ("%s (%s) Korean Multi Client Server\n", cmd_name, SER_VERSION);
|
|
1339 #else
|
|
1340 printf ("%s (%s) Nihongo Multi Client Server\n", cmd_name, SER_VERSION);
|
|
1341 #endif /* CHINESE */
|
|
1342 }
|