Debian Codegraph @

xpa/client.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 /*
8 *----------------------------------------------------------------------------
9 *
10 *
11 * Private Routines and Data
12 *
13 *
14 *----------------------------------------------------------------------------
15 */
16
17 /* this is the head of the global list of client xpas */
18 static XPA xpaclienthead=NULL;
19
20 static char errbuf[SZ_LINE]; /* holds current error message */
21 static int id=0; /* id of current command */
22
23 #define DATA_CONNECT 1
24 #define DATA_ACCEPT 2
25 #define DATA_DATA 4
26
27 /* use of a double fork() call is used to prevent zombies which happen
28 if fork is a child of xpans started by an XPA server (i.e., for some
29 reason, the SIGCHLD signal does not get sent to xpans parent)
30 See Stevens, Advanced Programming in te Unix Environment, p. 202 */
31 #define USE_DOUBLE_FORK 1
32 #ifndef USE_DOUBLE_FORK
33 #ifdef ANSI_FUNC
34 void sig_chld(int signo)
35 #else
36 #endif
37 {
38 int stat;
39
40 while(waitpid(-1, &stat, WNOHANG) > 0){
41 ;
42 }
43 return;
44 }
45 #endif
46
47 /*
48 *----------------------------------------------------------------------------
49 *
50 * Routine: rdl
51 *
52 * Purpose: read characters up a new-line
53 *
54 * Returns: number of characters read
55 *
56 *----------------------------------------------------------------------------
57 */
58 #ifdef ANSI_FUNC
59 static int
60 rdl (int fd, char *buf, size_t len)
61 #else
62 static int rdl(fd, buf, len)
63 int fd;
64 char *buf;
65 int len;
66 #endif
67 {
68 int i=0;
69 int got;
70
71 /* start out clean */
72 *buf = '\0';
73
74 /* make sure we have a valid channel */
75 if( fd < 0 )
76 return(-1);
77
78 /* grab characters up to a new-line or max len */
79 while( i < (len-1) ){
80 got = read(fd, &(buf[i]), 1);
81 if( got < 1 )
82 break;
83 else if( buf[i++] == '\n' )
84 break;
85 }
86 buf[i] = '\0';
87 return(i);
88 }
89
90 /*
91 *----------------------------------------------------------------------------
92 *
93 * Routine: XPAProxyAccept
94 *
95 * Purpose: accept a connection from an XPA proxy server
96 *
97 * Return: fd of accepted connection or -1
98 *
99 *----------------------------------------------------------------------------
100 */
101 #ifdef ANSI_FUNC
102 static
103 int XPAProxyAccept(XPA xpa, char *method, char *xclass, char *name, int ifd,
104 unsigned int *rip, unsigned short *rport, char *rname)
105 #else
106 static
107 int XPAProxyAccept(xpa, method, xclass, name, ifd, rip, rport, rname)
108 XPA xpa;
109 char *method;
110 char *xclass;
111 char *name;
112 int ifd;
113 unsigned int *rip;
114 unsigned short *rport;
115 char *rname;
116 #endif
117 {
118 int sock;
119 int got;
120 int oum;
121 int ofd;
122 int niter;
123 int swidth=FD_SETSIZE;
124 int keep_alive=1;
125 int reuse_addr=1;
126 unsigned int ip;
127 unsigned short port;
128 char tbuf[SZ_LINE];
129 char amethod[SZ_LINE];
130 char *tptr;
131 socklen_t slen;
132 struct sockaddr_in sock_in;
133 #if HAVE_SYS_UN_H
134 struct sockaddr_un sock_un;
135 #endif
136 struct timeval tv;
137 struct timeval *tvp;
138 fd_set readfds;
139
140 /* initialize results */
141 if( rip ) *rip = 0;
142 if( rport ) *rport = 0;
143 if( rname ) *rname = '\0';
144
145 switch(XPAMethod(method)){
146 case XPA_INET:
147 if( !XPAParseIpPort(method, &ip, &port) ){
148 goto error;
149 }
150 /* open a socket for data connections */
151 if( (sock = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
152 PERROR(("xpaaccept socket"));
153 goto error;
154 }
155 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
156 (char *)&keep_alive, sizeof(keep_alive));
157 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
158 (char *)&reuse_addr, sizeof(reuse_addr));
159 memset((char *)&sock_in, 0, sizeof(sock_in));
160 sock_in.sin_family = AF_INET;
161 sock_in.sin_addr.s_addr = htonl(INADDR_ANY);
162 sock_in.sin_port = htons(port);
163 /* bind to the ip:port */
164 if( xbind(sock, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
165 PERROR(("xpaaccept bind"));
166 xclose(sock);
167 goto error;
168 }
169 snprintf(amethod, SZ_LINE, "%x:%d", ip, port);
170 break;
171 #if HAVE_SYS_UN_H
172 case XPA_UNIX:
173 ip = 0;
174 port = 0;
175 /* get filename part, composed of class and name and unique id */
176 snprintf(tbuf, SZ_LINE, "%s_%s.%d", xclass, name, (int)time(NULL));
177 /* change "/" to "_" for filename */
178 for(tptr = tbuf; *tptr != '\0'; tptr++){
179 if( *tptr == '/' ) *tptr = '_';
180 }
181 /* create full pathname */
182 snprintf(amethod, SZ_LINE, "%s/%s", XPATmpdir(), tbuf);
183 /* delete old copy */
184 unlink (amethod);
185 /* open a socket and fill in socket information */
186 if( (sock = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
187 goto error;
188 }
189 setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
190 (char *)&keep_alive, sizeof(keep_alive));
191 memset((char *)&sock_un, 0, sizeof(sock_un));
192 sock_un.sun_family = AF_UNIX;
193 strcpy(sock_un.sun_path, amethod);
194 /* unset umask so that everyone can read and write */
195 oum = umask(0);
196 /* bind to the file */
197 got = xbind(sock, (struct sockaddr *)&sock_un, sizeof(sock_un));
198 /* reset umask to previous */
199 umask(oum);
200 /* now check for bind error */
201 if( got < 0 ){
202 xclose(sock);
203 goto error;
204 }
205 break;
206 #endif
207 default:
208 goto error;
209 }
210
211 /* send port to client so they can connect */
212 /* first listen for the connection */
213 if( listen(sock, XPA_MAXLISTEN) < 0 ){
214 PERROR(("xpaaccept listen"));
215 xclose(sock);
216 goto error;
217 }
218
219 /* and tell the client that we are listening */
220 snprintf(tbuf, SZ_LINE, "xpaaccept %s (%s:%s %s)\n",
221 amethod, xclass, name, method);
222 FPRINTF((stderr, "%sXPAProxyAccept: sending command to %d:\n%s",
223 _sp, ifd, tbuf));
224 if( XPAPuts(NULL, ifd, tbuf, XPAShortTimeout()) <= 0 ){
225 PERROR(("client xpaaccept write"));
226 xclose(sock);
227 goto error;
228 }
229
230 /* we will iterate on xselect */
231 if( XPAShortTimeout() > 0 )
232 niter = XPAShortTimeout()*100;
233 else
234 niter = XPA_SHORT_TIMEOUT*100;
235 again:
236 /* this has to be able to time out */
237 tv.tv_sec = 0;
238 tv.tv_usec = 10000;
239 tvp = &tv;
240 /* wait for this socket and XPA sockets */
241 FD_ZERO(&readfds);
242 FD_SET(sock, &readfds);
243 XPAAddSelect(NULL, &readfds);
244 /* wait for the connection */
245 got = xselect(swidth, &readfds, NULL, NULL, tvp);
246 /* process results of select */
247 if( got > 0 ){
248 if( !FD_ISSET(sock, &readfds)){
249 XPAProcessSelect(&readfds, 0);
250 goto again;
251 }
252 switch(XPAMethod(method)){
253 case XPA_INET:
254 while( 1 ){
255 slen = sizeof(struct sockaddr_in);
256 if((ofd=xaccept(sock, (struct sockaddr *)&sock_in, &slen)) >= 0){
257 break;
258 }
259 else{
260 if( xerrno == EINTR )
261 continue;
262 else{
263 PERROR(("xpaaccept acccept"));
264 xclose(sock);
265 goto error;
266 }
267 }
268 }
269 break;
270 #if HAVE_SYS_UN_H
271 case XPA_UNIX:
272 while( 1 ){
273 slen = sizeof(struct sockaddr_un);
274 if((ofd=xaccept(sock, (struct sockaddr *)&sock_un, &slen)) >= 0){
275 break;
276 }
277 else{
278 if( xerrno == EINTR )
279 continue;
280 else{
281 PERROR(("xpaaccept acccept"));
282 xclose(sock);
283 goto error;
284 }
285 }
286 }
287 break;
288 #endif
289 default:
290 xclose(sock);
291 goto error;
292 }
293 }
294 /* timeout? */
295 else if( got == 0 ){
296 if( --niter > 0 ){
297 goto again;
298 }
299 else{
300 xclose(sock);
301 FPRINTF((stderr, "%sXPAProxyAccept: select timed out\n", _sp));
302 goto error;
303 }
304 }
305 /* error */
306 else{
307 if( xerrno == EINTR ){
308 PERROR(("xpaaccept select"));
309 goto again;
310 }
311 else{
312 xclose(sock);
313 goto error;
314 }
315 }
316 /* done with listening */
317 xclose(sock);
318
319 /* fill in return information */
320 if( rip ) *rip = ip;
321 if( rport ) *rport = port;
322 if( rname ){
323 strncpy(rname, amethod, SZ_LINE-1);
324 rname[SZ_LINE-1] = '\0';
325 }
326 return(ofd);
327
328 error:
329 return(-1);
330 }
331
332 /*
333 *----------------------------------------------------------------------------
334 *
335 * Routine: XPAClientNewInput
336 *
337 * Purpose: allocate a new input struct for reading data from stdin
338 *
339 * Return: input struct, or NULL on error
340 *
341 *----------------------------------------------------------------------------
342 */
343 #ifdef ANSI_FUNC
344 static XPAInput
345 XPAClientNewInput(XPA xpa)
346 #else
347 static XPAInput XPAClientNewInput(xpa)
348 XPA xpa;
349 #endif
350 {
351 XPAInput xnew, inp;
352
353 /* allocate a new record */
354 if( (xnew=(XPAInput)xcalloc(1, sizeof(XPAInputRec))) == NULL ){
355 return(NULL);
356 }
357 /* allocate the data buffer */
358 xnew->buf = (char *)xmalloc(XPA_BIOSIZE);
359 /* this buffer starts (and currently ends) at the current byte count */
360 xnew->start = xpa->inpbytes;
361 xnew->end = xpa->inpbytes;
362 xnew->bytes = 0;
363
364 /* add this input to end of list of input's */
365 if( xpa->inphead == NULL ){
366 xpa->inphead = xnew;
367 }
368 else{
369 for(inp=xpa->inphead; inp->next!=NULL; inp=inp->next)
370 ;
371 inp->next = xnew;
372 }
373
374 /* return the record struct */
375 return(xnew);
376 }
377
378 /*
379 *----------------------------------------------------------------------------
380 *
381 * Routine: XPAClientFreeInput
382 *
383 * Purpose: free a input buffer once its been sent to all targets
384 *
385 * Return: 0 on success, -1 on failure
386 *
387 *----------------------------------------------------------------------------
388 */
389 #ifdef ANSI_FUNC
390 static void
391 XPAClientFreeInput (XPA xpa, XPAInput inp)
392 #else
393 static void XPAClientFreeInput(xpa, inp)
394 XPA xpa;
395 XPAInput inp;
396 #endif
397 {
398 XPAInput cur;
399
400 if( !xpa || !inp )
401 return;
402
403 if( inp == xpa->inphead ){
404 xpa->inphead = inp->next;
405 }
406 else{
407 for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
408 if( cur->next == inp ){
409 cur->next = inp->next;
410 break;
411 }
412 }
413 }
414
415 /* free current record */
416 if( inp != NULL ){
417 if( inp->buf != NULL )
418 xfree(inp->buf );
419 xfree(inp);
420 }
421 }
422
423 /*
424 *----------------------------------------------------------------------------
425 *
426 * Routine: XPAClientFreeAllInputs
427 *
428 * Purpose: free remaining input buffers
429 *
430 * Return: 0 on success, -1 on failure
431 *
432 *----------------------------------------------------------------------------
433 */
434 #ifdef ANSI_FUNC
435 static void
436 XPAClientFreeAllInputs (XPA xpa)
437 #else
438 static void XPAClientFreeAllInputs(xpa)
439 XPA xpa;
440 #endif
441 {
442 XPAInput cur, tmp;
443
444 if( !xpa )
445 return;
446
447 for(cur=xpa->inphead; cur!=NULL; ){
448 tmp = cur->next;
449 XPAClientFreeInput(xpa, cur);
450 cur = tmp;
451 }
452 xpa->inpbytes = 0;
453 }
454
455 /*
456 *----------------------------------------------------------------------------
457 *
458 * Routine: XPAClientProcessInput
459 *
460 * Purpose: read input from stdin and store in an input struct
461 *
462 * Return: bytes read
463 *
464 *----------------------------------------------------------------------------
465 */
466 #ifdef ANSI_FUNC
467 static int
468 XPAClientProcessInput(XPA xpa)
469 #else
470 static int XPAClientProcessInput(xpa)
471 XPA xpa;
472 #endif
473 {
474 static XPAInput cur=NULL;
475 int get, got;
476
477 /* set up next buffer, if necessary */
478 for(cur=xpa->inphead; cur!=NULL; cur=cur->next){
479 if( cur->bytes < XPA_BIOSIZE )
480 break;
481 }
482 if( cur == NULL ){
483 cur = XPAClientNewInput(xpa);
484 }
485
486 /* read data from stdin */
487 get = MIN(XPA_IOSIZE, XPA_BIOSIZE - cur->bytes);
488 if( isatty(xpa->ifd) ){
489 got = rdl(xpa->ifd, &(cur->buf[cur->bytes]), get);
490 }
491 else{
492 got = read(xpa->ifd, &(cur->buf[cur->bytes]), get);
493 }
494 switch(got){
495 case -1:
496 if( XPAVerbosity() ){
497 PERROR(("XPA client read"));
498 }
499 return(0);
500 case 0:
501 xpa->ifd = -1;
502 FPRINTF((stderr, "%sXPAClientProcessInput: signalling EOF\n", _sp));
503 break;
504 default:
505 break;
506 }
507 cur->bytes += got;
508 cur->end += got;
509 xpa->inpbytes += got;
510 #ifdef FIXEDBYCYGWIN
511 #if HAVE_CYGWIN
512 /* on non-NT Windows machines, Cygwin select() does not work once a pipe
513 gets EOF. It should show the fd ready for reading (and read 0 bytes),
514 but does not, so we have to hack a manual check */
515 /* GetVersion is a Windows call */
516 if( GetVersion() >= 0x80000000L ){
517 if( got < get ){
518 xpa->ifd = -1;
519 }
520 }
521 #endif
522 #endif
523
524 /* verify to stdout, if necessary */
525 if( xpa->client_mode & XPA_CLIENT_VERIFY ){
526 fwrite(&(cur->buf[cur->bytes-got]), sizeof(char), got, stdout);
527 }
528
529 /* return the number of bytes just read */
530 return(got);
531 }
532
533 /*
534 *----------------------------------------------------------------------------
535 *
536 * Routine: XPAClientFree
537 *
538 * Purpose: free a client record and remove from list
539 *
540 * Returns: none
541 *
542 *----------------------------------------------------------------------------
543 */
544 #ifdef ANSI_FUNC
545 static void
546 XPAClientFree (XPA xpa, XPAClient client)
547 #else
548 static void XPAClientFree(xpa, client)
549 XPA xpa;
550 XPAClient client;
551 #endif
552 {
553 XPAClient cur;
554
555 /* remove from list of xpa's */
556 if( xpa->clienthead ){
557 if( xpa->clienthead == client ){
558 xpa->clienthead = client->next;
559 }
560 else{
561 for(cur=xpa->clienthead; cur!=NULL; cur=cur->next){
562 if( cur->next == client ){
563 cur->next = client->next;
564 break;
565 }
566 }
567 }
568 }
569
570 if( client->cmdfd >= 0 ){
571 #if HAVE_CYGWIN
572 shutdown(client->cmdfd, SHUT_RDWR);
573 #endif
574 xclose(client->cmdfd);
575 }
576 if( client->datafd >= 0 ){
577 #if HAVE_CYGWIN
578 shutdown(client->datafd, SHUT_RDWR);
579 #endif
580 xclose(client->datafd);
581 }
582 if( client->dataname ){
583 unlink(client->dataname);
584 xfree(client->dataname);
585 }
586 if( client->method )
587 xfree(client->method);
588 if( client->info )
589 xfree(client->info);
590 if( client->xtemplate )
591 xfree(client->xtemplate);
592 if( client->xclass )
593 xfree(client->xclass);
594 if( client->name )
595 xfree(client->name);
596 if( client->id )
597 xfree(client->id);
598 /* xpaget's fd mode has an alloc'ed bufptr and lenptr */
599 if( (client->type == 'g') && (client->mode & XPA_CLIENT_FD) ){
600 if( client->bufptr && *(client->bufptr) )
601 xfree(*(client->bufptr));
602 if( client->bufptr )
603 xfree(client->bufptr);
604 if( client->lenptr )
605 xfree(client->lenptr);
606 }
607 xfree(client);
608 }
609
610 /*
611 *----------------------------------------------------------------------------
612 *
613 * Routine: XPAClientDataSent
614 *
615 * Purpose: data is sent, so close data channel and change status to
616 * signal that we are waiting for the server
617 *
618 * Returns: none
619 *
620 *----------------------------------------------------------------------------
621 */
622 #ifdef ANSI_FUNC
623 static void
624 XPAClientDataSent (XPA xpa, XPAClient client)
625 #else
626 static void XPAClientDataSent(xpa, client)
627 XPA xpa;
628 XPAClient client;
629 #endif
630 {
631 FPRINTF((stderr, "%sXPAClientDataSent: for cmd %d data %d\n", _sp,
632 client->cmdfd, client->datafd));
633 /* close the data channel, which should trigger a result from the server */
634 if( client->datafd >= 0 ){
635 #if HAVE_CYGWIN
636 shutdown(client->datafd, SHUT_RDWR);
637 #endif
638 xclose(client->datafd);
639 client->datafd = -1;
640 }
641 /* we are now waiting for the server to complete the calllback */
642 client->status = XPA_CLIENT_WAITING;
643 }
644
645 /*
646 *----------------------------------------------------------------------------
647 *
648 * Routine: XPAClientEnd
649 *
650 * Purpose: finish up with this client
651 *
652 * Returns: error message or null
653 *
654 *----------------------------------------------------------------------------
655 */
656 #ifdef ANSI_FUNC
657 static char *
658 XPAClientEnd (XPA xpa, XPAClient client)
659 #else
660 static char *XPAClientEnd(xpa, client)
661 XPA xpa;
662 XPAClient client;
663 #endif
664 {
665 char *error=NULL;
666 char *eptr;
667
668 FPRINTF((stderr, "%sXPAClientEnd: for cmd %d data %d\n", _sp,
669 client->cmdfd, client->datafd));
670 /* always read the status line -- if we are not ack'ing, we'll get an
671 OK from the server before the calllback and we can exit quickly */
672 /* don't do this if client is xpainfo and we're not ack'ing */
673 if( !((client->type == 'i') && !(client->mode & XPA_CLIENT_ACK)) ){
674 retry:
675 if( XPAGets(NULL, client->cmdfd, errbuf, SZ_LINE, XPALongTimeout()) >0 ){
676 FPRINTF((stderr, "%sXPAClientEnd: read %s\n", _sp, errbuf));
677 eptr = errbuf;
678 /* this should never happen */
679 if( *eptr == '?' ){
680 snprintf(errbuf, SZ_LINE,
681 "XPA$WARNING: protocol mismatch - missing id\n%s", eptr);
682 error = NULL;
683 }
684 else{
685 /* make sure we are dealing with a proper message */
686 if( strncmp(eptr, client->id, strlen(client->id)) ){
687 if( XPAVerbosity() > 1 ){
688 fprintf(stderr,
689 "XPA$WARNING: ignoring out of sync server message:\n");
690 fprintf(stderr, "%s", errbuf);
691 }
692 goto retry;
693 }
694 /* go past id */
695 eptr += strlen(client->id);
696 while( isspace((int)*eptr) ) eptr++;
697 if( !strncmp(eptr, "XPA$OK", 6) ){
698 error = NULL;
699 }
700 else{
701 error = eptr;
702 }
703 }
704 }
705 else{
706 if( XPAVerbosity() > 1 ){
707 fprintf(stderr,
708 "XPA$WARNING: no reply from server callback (assuming OK)\n");
709 }
710 error = NULL;
711 }
712 }
713 else
714 error = NULL;
715
716 /* store the error return */
717 if( client->errptr )
718 *(client->errptr) = xstrdup(error);
719
720 /* remove this client if we are not meant to persist */
721 if( !xpa->persist ){
722 XPAClientFree(xpa, client);
723 }
724 /* otherwise mark as inactive */
725 else{
726 client->status = XPA_CLIENT_IDLE;
727 client->bytes = 0;
728 }
729
730 /* return error status */
731 return(error);
732 }
733
734 /*
735 *----------------------------------------------------------------------------
736 *
737 * Routine: XPAClientNew
738 *
739 * Purpose: allocate a new xpa client
740 *
741 * Returns: xpa client struct
742 *
743 *----------------------------------------------------------------------------
744 */
745 #ifdef ANSI_FUNC
746 static XPAClient
747 XPAClientNew (XPA xpa, char *mode, char *xtemplate, int type,
748 char *xclass, char *name, char *method, char *info)
749 #else
750 static XPAClient XPAClientNew(xpa, mode, xtemplate, type,
751 xclass, name, method, info)
752 XPA xpa;
753 char *mode;
754 char *xtemplate;
755 int type;
756 char *xclass;
757 char *name;
758 char *method;
759 char *info;
760 #endif
761 {
762 XPAClient xnew, client;
763 struct sockaddr_in sock_in;
764 #if HAVE_SYS_UN_H
765 struct sockaddr_un sock_un;
766 #endif
767 char xmode[SZ_LINE];
768 char tbuf[SZ_LINE];
769 char amethod[SZ_LINE];
770 char *s=NULL;
771 unsigned short port;
772 unsigned int ip=0;
773 int fd;
774 int pfd;
775 int tries=0;
776 int nsproxy=0;
777 int keep_alive=1;
778
779 FPRINTF((stderr, "%sXPAClientNew: entering with %s %s %s %s\n", _sp,
780 xclass, name, method, info));
781
782 /* no errors as yet */
783 *errbuf = '\0';
784
785 /* look for reuse of xpans fd (used in conjunction with the xpans proxy) */
786 *xmode = '\0';
787 if( mode ){
788 strncpy(xmode, mode, SZ_LINE-1);
789 xmode[SZ_LINE-1] = '\0';
790 }
791 if( keyword(xmode, "nsproxy", tbuf, SZ_LINE) ){
792 nsproxy = 1;
793 pfd = strtol(tbuf, &s, 0);
794 fd = XPAProxyAccept(xpa, XPANSMethod(NULL,2),
795 xclass, name, pfd, &ip, &port, amethod);
796 /* make sure we got a valid int fd */
797 if( fd < 0 ){
798 snprintf(errbuf, SZ_LINE,
799 "XPA$ERROR: no response from server on proxyaccept (%s:%s%s)\n",
800 xclass, name, XPATimestamp());
801 FPRINTF((stderr, "%sXPAClientNew: %s", _sp, errbuf));
802 PERROR(("XPAClientNew"));
803 return(NULL);
804 }
805 }
806 /* normal usage: connect to server */
807 else{
808 switch(XPAMethod(method)){
809 case XPA_INET:
810 again1:
811 if( !XPAParseIpPort(method, &ip, &port) )
812 return(NULL);
813 /* use $localhost over $host (we do not trust host to be correct) */
814 if( (ip == gethostip("$host")) && (tries == 0) )
815 ip = gethostip("$localhost");
816 /* connect to the server before we go further */
817 if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
818 return(NULL);
819 }
820 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
821 (char *)&keep_alive, sizeof(keep_alive));
822 memset((char *)&sock_in, 0, sizeof(sock_in));
823 sock_in.sin_family = AF_INET;
824 sock_in.sin_addr.s_addr = htonl(ip);
825 sock_in.sin_port = htons(port);
826 /* make the connection with the server */
827 if( connect(fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) <0 ){
828 xclose(fd);
829 /* if localhost doesn't work, make one try with the host ip */
830 /* we also try again just in case there was an odd error such
831 as "permission denied", which we have seen once or twice */
832 if( tries < 2 ){
833 tries++;
834 goto again1;
835 }
836 /* give up */
837 else{
838 snprintf(errbuf, SZ_LINE,
839 "XPA$ERROR: no response from server on connect (%s:%s%s)\n",
840 xclass, name, XPATimestamp());
841 PERROR(("XPAClientNew"));
842 return(NULL);
843 }
844 }
845 /* make sure we close on exec */
846 xfcntl(fd, F_SETFD, FD_CLOEXEC);
847 FPRINTF((stderr, "%sXPAClientNew: inet connect returns fd %d\n",
848 _sp, fd));
849 break;
850 #if HAVE_SYS_UN_H
851 case XPA_UNIX:
852 again2:
853 /* open a socket and fill in socket information */
854 if( (fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
855 return(NULL);
856 }
857 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
858 (char *)&keep_alive, sizeof(keep_alive));
859 memset((char *)&sock_un, 0, sizeof(sock_un));
860 sock_un.sun_family = AF_UNIX;
861 strcpy(sock_un.sun_path, method);
862 /* make the connection with the server */
863 if( connect(fd, (struct sockaddr *)&sock_un, sizeof(sock_un)) <0 ){
864 xclose(fd);
865 /* Unix sockets get ECONNREFUSED when the listen queue is full,
866 so we try a few times to give the server a chance to recover */
867 if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
868 tries++;
869 XPASleep(10);
870 goto again2;
871 }
872 /* give up */
873 else{
874 snprintf(errbuf, SZ_LINE,
875 "XPA$ERROR: no response from server on connect (%s:%s%s)\n",
876 xclass, name, XPATimestamp());
877 PERROR(("XPAClientNew"));
878 return(NULL);
879 }
880 }
881 /* make sure we close on exec */
882 xfcntl(fd, F_SETFD, FD_CLOEXEC);
883 FPRINTF((stderr, "%sXPAClientNew: unix connect returns fd %d\n",
884 _sp, fd));
885 break;
886 #endif
887 default:
888 return(NULL);
889 }
890 strncpy(amethod, method, SZ_LINE-1);
891 amethod[SZ_LINE-1] = '\0';
892 }
893
894 /* allocate new send record */
895 if( (xnew=(XPAClient)xcalloc(1, sizeof(XPAClientRec))) == NULL ){
896 xclose(fd);
897 return(NULL);
898 }
899
900 /* fill in the blanks */
901 xnew->xtemplate = xstrdup(xtemplate);
902 xnew->type = type;
903 xnew->cmdfd = fd;
904 xnew->datafd = -1;
905 xnew->xclass = xstrdup(xclass);
906 xnew->name = xstrdup(name);
907 xnew->method = xstrdup(amethod);
908 xnew->info = xstrdup(info);
909 xnew->ip = ip;
910 xnew->nsproxy = nsproxy;
911 xnew->status = XPA_CLIENT_ACTIVE;
912
913 /* now that we have a valid client, add to list */
914 if( xpa->clienthead == NULL ){
915 xpa->clienthead = xnew;
916 }
917 else{
918 for(client=xpa->clienthead; client->next!=NULL; client=client->next)
919 ;
920 client->next = xnew;
921 }
922 FPRINTF((stderr, "%sXPAClientNew: new fd %d\n", _sp, xnew->cmdfd));
923 /* return the good news */
924 return(xnew);
925 }
926
927 /*
928 *----------------------------------------------------------------------------
929 *
930 * Routine: XPAClientAddSelect
931 *
932 * Purpose: add one or more xpa client sockets to the select flags
933 *
934 * Return: number of clients that were added to the select flags
935 *
936 *----------------------------------------------------------------------------
937 */
938 #ifdef ANSI_FUNC
939 int
940 XPAClientAddSelect (XPA xpa, fd_set *readfdsptr, fd_set *writefdsptr)
941 #else
942 int XPAClientAddSelect(xpa, readfdsptr, writefdsptr)
943 XPA xpa;
944 fd_set *readfdsptr;
945 fd_set *writefdsptr;
946 #endif
947 {
948 XPAClient client;
949 int got=0;
950 int loop=0;
951
952 /* better have some place to set the flags */
953 if( readfdsptr == NULL )
954 return(0);
955
956 /* if no xpa is specified, do them all */
957 if( xpa == NULL ){
958 if( xpaclienthead == NULL ) return(0);
959 xpa = xpaclienthead;
960 loop = 1;
961 }
962
963 loop:
964 /* set select flags for all clients */
965 for(client=xpa->clienthead; client!=NULL; client=client->next){
966 /* if this client is processing */
967 if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd >= 0) ){
968 if( client->type == 'g' ){
969 FPRINTF((stderr, "%sXPAClientAddSelect(get): adding fd %d\n", _sp,
970 client->datafd));
971 FD_SET(client->datafd, readfdsptr);
972 got++;
973 }
974 else if( client->type == 's' ){
975 FPRINTF((stderr, "%sXPAClientAddSelect(set): adding fd %d\n", _sp,
976 client->datafd));
977 FD_SET(client->datafd, writefdsptr);
978 got++;
979 }
980 }
981 /* if this client is waiting */
982 else if( (client->status == XPA_CLIENT_WAITING) && (client->cmdfd >= 0) ){
983 FPRINTF((stderr, "%sXPAClientAddSelect(waiting): adding fd %d\n", _sp,
984 client->cmdfd));
985 FD_SET(client->cmdfd, readfdsptr);
986 got++;
987 }
988 }
989 /* loop if necessary */
990 if( loop && (xpa=xpa->next) ) goto loop;
991 /* return the news */
992 return(got);
993 }
994
995 /*
996 *----------------------------------------------------------------------------
997 *
998 * Routine: XPAClientGet
999 *
1000 * Purpose: process an xpaget request for a given client
1001 *
1002 * Return: 0 on success, -1 on failure
1003 *
1004 *----------------------------------------------------------------------------
1005 */
1006 #ifdef ANSI_FUNC
1007 static int
1008 XPAClientGet (XPA xpa, XPAClient client)
1009 #else
1010 static int XPAClientGet(xpa, client)
1011 XPA xpa;
1012 XPAClient client;
1013 #endif
1014 {
1015 int status;
1016 char tbuf[SZ_LINE];
1017
1018 /* allocate the first buffer, if necessary */
1019 if( *(client->bufptr) == NULL ){
1020 client->bufsize = XPA_IOSIZE;
1021 *(client->bufptr) = (char *)xmalloc(client->bufsize);
1022 *(client->lenptr) = 0;
1023 }
1024 if( (*(client->lenptr) + XPA_IOSIZE) > client->bufsize ){
1025 client->bufsize += (XPA_IOSIZE*10);
1026 *(client->bufptr) = (char *)xrealloc(*(client->bufptr), client->bufsize);
1027 }
1028
1029 /* now retrieve the data from the server */
1030 status = recv(client->datafd, *(client->bufptr) + *(client->lenptr),
1031 XPA_IOSIZE, 0);
1032 /* status < 0 means error */
1033 switch(status){
1034 /* error */
1035 case -1:
1036 /* socket would block */
1037 if((xerrno == EINPROGRESS) || (xerrno == EWOULDBLOCK)){
1038 return(0);
1039 }
1040 /* clean up after error */
1041 if( *(client->bufptr) ){
1042 xfree(*(client->bufptr));
1043 *(client->bufptr) = NULL;
1044 client->bufsize = 0;
1045 }
1046 *(client->lenptr) = 0;
1047 XPAClientDataSent(xpa, client);
1048 #ifdef OLD
1049 (void)XPAClientEnd(xpa, client);
1050 /* we need to flag some sort of error, if nothing came across */
1051 if( *(client->errptr) == NULL ){
1052 *(client->errptr) = xstrdup(
1053 "XPA$ERROR: incomplete transmission from server\n");
1054 }
1055 #endif
1056 break;
1057 /* eof */
1058 case 0:
1059 /* if we have multiple clients, we now need to write this one */
1060 if( client->mode & XPA_CLIENT_FD ){
1061 if( xpa->nclient > 1 ){
1062 snprintf(tbuf, SZ_LINE, "XPA$BEGIN %s:%s %s\n",
1063 client->xclass, client->name, client->method);
1064 write(client->fd, tbuf, strlen(tbuf));
1065 }
1066 write(client->fd, *(client->bufptr), *(client->lenptr));
1067 if( xpa->nclient > 1 ){
1068 snprintf(tbuf, SZ_LINE, "XPA$END %s:%s %s\n",
1069 client->xclass, client->name, client->method);
1070 write(client->fd, tbuf, strlen(tbuf));
1071 }
1072 /* we can free buf, since its not being passed back */
1073 if( *(client->bufptr) ){
1074 xfree(*(client->bufptr));
1075 *(client->bufptr) = NULL;
1076 client->bufsize = 0;
1077 }
1078 }
1079 else{
1080 /* set final buffer size and put a convenience null at the end */
1081 if( *(client->bufptr) ){
1082 client->bufsize = *(client->lenptr)+1;
1083 *(client->bufptr) = (char *)xrealloc(*(client->bufptr),client->bufsize);
1084 *(*(client->bufptr)+*(client->lenptr)) = '\0';
1085 }
1086 }
1087 /* for all clients, we need to clean up */
1088 XPAClientDataSent(xpa, client);
1089 #ifdef OLD
1090 (void)XPAClientEnd(xpa, client);
1091 #endif
1092 break;
1093 /* status > 0: bytes read */
1094 default:
1095 *(client->lenptr) += status;
1096 /* for single client fd mode, we write immediately -- this deals with
1097 the important case of one client with a large amount of data */
1098 if( (client->mode & XPA_CLIENT_FD) && (xpa->nclient == 1) ){
1099 write(client->fd, *(client->bufptr), *(client->lenptr));
1100 /* reset buf for next read */
1101 if( *(client->bufptr) ) xfree(*(client->bufptr));
1102 *(client->bufptr) = NULL;
1103 *(client->lenptr) = 0;
1104 }
1105 break;
1106 }
1107 return(status);
1108 }
1109
1110 /*
1111 *----------------------------------------------------------------------------
1112 *
1113 * Routine: XPAClientSet
1114 *
1115 * Purpose: process an xpaset request for a given client
1116 *
1117 * Return: 0 on success, -1 on failure
1118 *
1119 *----------------------------------------------------------------------------
1120 */
1121 #ifdef ANSI_FUNC
1122 static int
1123 XPAClientSet (XPA xpa, XPAClient client)
1124 #else
1125 static int XPAClientSet(xpa, client)
1126 XPA xpa;
1127 XPAClient client;
1128 #endif
1129 {
1130 int status;
1131 int left;
1132 int got;
1133 int len;
1134 XPAInput inp;
1135
1136 if( client->mode & XPA_CLIENT_BUF ){
1137 while( 1 ){
1138 len = MIN(XPA_IOSIZE, client->len - client->bytes);
1139 /* see if we have written it all */
1140 if( len == 0 ){
1141 status = 1;
1142 goto done;
1143 }
1144 /* write the next chunk */
1145 FPRINTF((stderr,
1146 "%sXPAClientSet: fd %d sending %lu bytes (%lu - %lu)\n", _sp,
1147 client->datafd, (unsigned long)len,
1148 (unsigned long)client->len, (unsigned long)client->bytes));
1149 got=send(client->datafd, &(client->buf[client->bytes]), len, 0);
1150 if( got >= 0 ){
1151 client->bytes += got;
1152 if( XPALevelGet() >0 )
1153 return(got);
1154 }
1155 else{
1156 PERROR(("XPAClientSet"));
1157 /* check for error */
1158 if( (xerrno != EWOULDBLOCK) && (xerrno != EAGAIN) ){
1159 status = -1;
1160 goto done;
1161 }
1162 /* write would block, so we return and wait the server */
1163 else{
1164 return(0);
1165 }
1166 }
1167 }
1168 }
1169 /* reading from stdin and writing to servers */
1170 else{
1171 /* find the input buf that contains the data we need */
1172 for(inp=xpa->inphead; inp!=NULL; inp=inp->next){
1173 if( (client->bytes >= inp->start) && (client->bytes < inp->end) ){
1174 break;
1175 }
1176 }
1177 /* if we can't find a buffer ... */
1178 if( !inp ){
1179 /* ... and we have all the input, we are done */
1180 if( xpa->ifd < 0 ){
1181 FPRINTF((stderr, "%sXPAClientSet: all data read\n", _sp));
1182 status = 1;
1183 goto done;
1184 }
1185 /* ... but there is more input to come, return */
1186 else{
1187 return(0);
1188 }
1189 }
1190 /* optimization: don't write a buffer until its full (or until eof) */
1191 if( (xpa->ifd >=0) && (inp->bytes < XPA_BIOSIZE) ){
1192 return(0);
1193 }
1194
1195 /* write bytes until we would block or until end of this buffer, etc */
1196 while( 1 ){
1197 len = MIN(XPA_IOSIZE, inp->end - client->bytes);
1198 FPRINTF((stderr, "%sXPAClientSet: has %lu=min(%d,(%lu-%lu)) [%d]\n",
1199 _sp, (unsigned long)len, XPA_IOSIZE,
1200 (unsigned long)inp->end, (unsigned long)client->bytes,
1201 client->status));
1202 /* if we are done with this buffer, just return */
1203 if( (client->status == XPA_CLIENT_PROCESSING) && (len <=0) ){
1204 /* see if everyone else is done with this buffer as well,
1205 in which case we can free it */
1206 left = 0;
1207 for(client=xpa->clienthead; client!=NULL; client=client->next){
1208 if( (client->type != 's') || !(client->mode & XPA_CLIENT_FD) )
1209 continue;
1210 /* in order to be totally written out, the following must be true:
1211 * 1. send->bytes must be past the end of this buffer
1212 * and
1213 * 2. this buffer must be filled or else we hit eof
1214 */
1215 FPRINTF((stderr,
1216 "%sXPAClientSet: %lu>=%lu && ((%lu>=%d) or %d<0)) .. ",
1217 _sp, client->bytes, (unsigned long)inp->end,
1218 (unsigned long)inp->bytes, XPA_BIOSIZE,
1219 xpa->ifd));
1220 if( (client->bytes >= inp->end) &&
1221 ((inp->bytes >= XPA_BIOSIZE) || (xpa->ifd < 0)) ){
1222 /* buffer complete written */
1223 FPRINTF((stderr, "%sEOF (%lu)\n",
1224 _sp, (unsigned long)xpa->inpbytes));
1225 ;
1226 }
1227 else{
1228 FPRINTF((stderr, "%s\n", _sp));
1229 /* buffer not complete written */
1230 left++;
1231 break;
1232 }
1233 }
1234 /* if nothing is left, we can free this input struct */
1235 if( !left ){
1236 XPAClientFreeInput(xpa, inp);
1237 }
1238 return(0);
1239 }
1240 /* write to the server */
1241 FPRINTF((stderr,
1242 "%sXPAClientSet: fd %d sending %lu bytes (%lu):\n", _sp,
1243 client->datafd,
1244 (unsigned long)len, (unsigned long)client->bytes));
1245 got = send(client->datafd, &(inp->buf[client->bytes-inp->start]),len,0);
1246 /* check for success */
1247 if( got >= 0 ){
1248 /* update the number of bytes we wrote */
1249 client->bytes += got;
1250 FPRINTF((stderr, "%sXPAClientSet: sent %lu bytes (total is %lu)\n",
1251 _sp, (unsigned long)got, (unsigned long)client->bytes));
1252 /* go back for more */
1253 if( XPALevelGet() >0 )
1254 return(got);
1255 else
1256 continue;
1257 }
1258 /* check for error */
1259 else{
1260 PERROR(("XPAClientSet"));
1261 /* anything but a "would block" error is bad */
1262 if( (xerrno != EWOULDBLOCK) && (xerrno != EAGAIN) ){
1263 status = -1;
1264 goto done;
1265 }
1266 /* write would block, so we return and wait the server */
1267 else{
1268 FPRINTF((stderr, "%sXPAClientSet: waiting for more data\n", _sp));
1269 return(0);
1270 }
1271 }
1272 }
1273 }
1274
1275 done:
1276 XPAClientDataSent(xpa, client);
1277 #ifdef OLD
1278 (void)XPAClientEnd(xpa, client);
1279 #endif
1280 return(status);
1281 }
1282
1283 /*
1284 *----------------------------------------------------------------------------
1285 *
1286 * Routine: XPAClientProcessSelect
1287 *
1288 * Purpose: process xpas that have pending reads or writes
1289 *
1290 * Return: number of xpas processed
1291 *
1292 *----------------------------------------------------------------------------
1293 */
1294 #ifdef ANSI_FUNC
1295 int
1296 XPAClientProcessSelect (XPA xpa,
1297 fd_set *readfdsptr, fd_set *writefdsptr, int maxreq)
1298 #else
1299 int XPAClientProcessSelect(xpa, readfdsptr, writefdsptr, maxreq)
1300 XPA xpa;
1301 fd_set *readfdsptr;
1302 fd_set *writefdsptr;
1303 int maxreq;
1304 #endif
1305 {
1306 int got=0;
1307 int loop=0;
1308 XPAClient client;
1309
1310 /* <= 0 means do all of them */
1311 if( maxreq < 0 ){
1312 maxreq = 0;
1313 }
1314
1315 /* if no xpa is specified, do them all */
1316 if( xpa == NULL ){
1317 if( xpaclienthead == NULL ) return(0);
1318 xpa = xpaclienthead;
1319 loop = 1;
1320 }
1321
1322 loop:
1323 /* first process any new input before we write output */
1324 if( xfd_isset_stdin(xpa->ifd, readfdsptr) ){
1325 xfd_clr_stdin(xpa->ifd, readfdsptr);
1326 XPAClientProcessInput(xpa);
1327 }
1328 /* look at all clients */
1329 again:
1330 for(client=xpa->clienthead; client!=NULL; client=client->next){
1331 /* if we are processing */
1332 if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd>=0) ){
1333 /* then handle new requests */
1334 if((client->type == 'g') && FD_ISSET(client->datafd, readfdsptr)){
1335 FD_CLR(client->datafd, readfdsptr);
1336 XPAClientGet(xpa, client);
1337 got++;
1338 if( maxreq && (got >= maxreq) ) return(got);
1339 goto again;
1340 }
1341 else if((client->type == 's') && FD_ISSET(client->datafd, writefdsptr)){
1342 FD_CLR(client->datafd, writefdsptr);
1343 /* if the return is > 0, we completed the send */
1344 if( XPAClientSet(xpa, client) > 0 )
1345 got++;
1346 if( maxreq && (got >= maxreq) ) return(got);
1347 goto again;
1348 }
1349 }
1350 /* if this client is waiting */
1351 else if( (client->status == XPA_CLIENT_WAITING) && (client->cmdfd >= 0) ){
1352 if( FD_ISSET(client->cmdfd, readfdsptr)){
1353 FD_CLR(client->cmdfd, readfdsptr);
1354 XPAClientEnd(xpa, client);
1355 got++;
1356 if( maxreq && (got >= maxreq) ) return(got);
1357 goto again;
1358 }
1359 }
1360 }
1361 /* loop if necessary */
1362 if( loop && (xpa=xpa->next) ) goto loop;
1363 /* return the news */
1364 return(got);
1365 }
1366
1367 /*
1368 *----------------------------------------------------------------------------
1369 *
1370 * Routine: XPAClientLoop
1371 *
1372 * Purpose: non-X programs event loop for handling XPA client events
1373 *
1374 * Returns: none
1375 *
1376 *----------------------------------------------------------------------------
1377 */
1378 #ifdef ANSI_FUNC
1379 static int
1380 XPAClientLoop (XPA xpa, int mode)
1381 #else
1382 static int XPAClientLoop(xpa, mode)
1383 XPA xpa;
1384 int mode;
1385 #endif
1386 {
1387 int got=0;
1388 int sgot;
1389 int doxpa=1;
1390 int ltimeout;
1391 char *s=NULL;
1392 fd_set readfds;
1393 fd_set writefds;
1394 static int width=0;
1395 struct timeval tv;
1396 struct timeval *tvp;
1397
1398 /* set width once */
1399 if( width == 0 ){
1400 width = FD_SETSIZE;
1401 }
1402 /* allow environment to turn off xpa server processing in client loop */
1403 if( (s=getenv("XPA_CLIENT_DOXPA")) && isfalse(s) ){
1404 doxpa = 0;
1405 }
1406 ltimeout = XPALongTimeout();
1407 FD_ZERO(&readfds);
1408 FD_ZERO(&writefds);
1409 while( XPAClientAddSelect(xpa, &readfds, &writefds) ){
1410 /* add other XPA's and process them as we process the client */
1411 if( (mode & XPA_CLIENT_SEL_XPA) && doxpa ){
1412 FPRINTF((stderr, "%sXPAClientLoop: will handle server reqs ...\n", _sp));
1413 XPAAddSelect(NULL, &readfds);
1414 }
1415 /* hopefully, a server will respond in a finite amount of time */
1416 if( ltimeout > 0 ){
1417 tv.tv_sec = ltimeout;
1418 tv.tv_usec = 0;
1419 tvp = &tv;
1420 }
1421 /* wait forever, if necessary */
1422 else{
1423 tvp = NULL;
1424 }
1425 /* add stdin to select, if there is one */
1426 if( xpa->ifd >= 0 ){
1427 xfd_set_stdin(xpa->ifd, &readfds);
1428 #if HAVE_MINGW32
1429 /* BUT: for windows, we can't add stdin to select and therefore we
1430 must set a short timeout and look manually */
1431 tv.tv_sec = 0;
1432 tv.tv_usec = 10000;
1433 /* this is the number of window iterations we will perform */
1434 if( ltimeout > 0 )
1435 ltimeout *= 100;
1436 tvp = &tv;
1437 #endif
1438 }
1439 /* wait for a server to respond */
1440 FPRINTF((stderr, "%sXPAClientLoop: waiting on select() ...\n", _sp));
1441 sgot = xselect(width, &readfds, &writefds, NULL, tvp);
1442 FPRINTF((stderr, "%sXPAClientLoop: select returns: %d\n", _sp, sgot));
1443 /* error -- what should we do? */
1444 if( sgot < 0 ){
1445 if( xerrno == EINTR ){
1446 FD_ZERO(&readfds);
1447 FD_ZERO(&writefds);
1448 continue;
1449 }
1450 if( XPAVerbosity() ){
1451 perror("XPAClientLoop() select");
1452 }
1453 exit(1);
1454 }
1455 /* timed out -- no one responded */
1456 else if( sgot == 0 ){
1457 #if HAVE_MINGW32
1458 if( xpa->ifd >= 0 ){
1459 if( ltimeout > 0 ){
1460 if( --ltimeout <= 0 )
1461 break;
1462 }
1463 }
1464 else{
1465 break;
1466 }
1467 #else
1468 break;
1469 #endif
1470 }
1471 else{
1472 got += XPAClientProcessSelect(xpa, &readfds, &writefds, 0);
1473 if( (mode & XPA_CLIENT_SEL_XPA) && doxpa ){
1474 got += XPAProcessSelect(&readfds, 0);
1475 }
1476 }
1477 FD_ZERO(&readfds);
1478 FD_ZERO(&writefds);
1479 }
1480 return(got);
1481 }
1482
1483 #if HAVE_MINGW32==0
1484 /*
1485 *----------------------------------------------------------------------------
1486 *
1487 * Routine: XPAClientLoopFork
1488 *
1489 * Purpose: non-X programs forked event loop for handling XPA client events
1490 *
1491 * Returns: number of clients "processed"
1492 *
1493 *----------------------------------------------------------------------------
1494 */
1495 #ifdef ANSI_FUNC
1496 static int
1497 XPAClientLoopFork (XPA xpa, int mode)
1498 #else
1499 static int XPAClientLoopFork(xpa, mode)
1500 XPA xpa;
1501 int mode;
1502 #endif
1503 {
1504 XPAClient client, tclient;
1505 pid_t pid;
1506 int got;
1507 int fd[2];
1508 char active=1;
1509 #ifndef USE_DOUBLE_FORK
1510 struct sigaction act;
1511 /* set up the signal handler to reap children (to avoid zombies) */
1512 act.sa_handler = sig_chld;
1513 sigemptyset(&act.sa_mask);
1514 act.sa_flags = 0;
1515 #ifdef SA_RESTART
1516 act.sa_flags |= SA_RESTART;
1517 #endif
1518 #ifdef SA_NOCLDWAIT
1519 act.sa_flags |= SA_NOCLDWAIT;
1520 #endif
1521 sigaction(SIGCHLD, &act, NULL);
1522 #endif
1523
1524 if( pipe(fd) < 0 ){
1525 got = 0;
1526 }
1527 else if( (pid = fork()) < 0 ){
1528 close(fd[0]);
1529 close(fd[1]);
1530 got=0;
1531 }
1532 else if( pid == 0 ){ /* child */
1533 /* child write to parent that he is active */
1534 close(fd[0]);
1535 write(fd[1], &active, 1);
1536 close(fd[1]);
1537 #ifdef USE_DOUBLE_FORK
1538 /* second fork prevents zombies:
1539 when child/parent exits, second child is inherited
1540 by init and thus is not a child of original parent */
1541 if( (pid = fork()) >= 0 ){
1542 /* child/parent exits */
1543 if( pid > 0 )
1544 exit(0);
1545 /* new child goes on under init ... */
1546 }
1547 #endif
1548 /* enter the main loop and process */
1549 XPAIOCallsXPA(0);
1550 XPAClientLoop(xpa, mode);
1551 exit(0);
1552 } else { /* parent */
1553 /* parent waits for child to wake up */
1554 close(fd[1]);
1555 read(fd[0], &active, 1);
1556 close(fd[0]);
1557 #ifdef USE_DOUBLE_FORK
1558 /* for double fork, also wait for intermediate process to exit */
1559 waitpid(pid, NULL, 0);
1560 #endif
1561 /* fake end of clients */
1562 for(got=0, client=xpa->clienthead; client!=NULL; ){
1563 got++;
1564 tclient = client->next;
1565 if( (client->status == XPA_CLIENT_PROCESSING) && (client->datafd >=0) ){
1566 #if HAVE_CYGWIN
1567 /* In Cygwin, we call shutdown (as well as close) to avoid Windows
1568 problems. The parent can't do this since the child is using the
1569 sockets, so we just close the sockets explicitly here */
1570 xclose(client->datafd);
1571 client->datafd = -1;
1572 if( !xpa->persist ){
1573 xclose(client->cmdfd);
1574 client->cmdfd = -1;
1575 }
1576 #endif
1577 client->errptr = NULL;
1578 /* remove this client if we are not meant to persist */
1579 if( !xpa->persist ){
1580 XPAClientFree(xpa, client);
1581 }
1582 /* otherwise mark as inactive */
1583 else{
1584 client->status = XPA_CLIENT_IDLE;
1585 client->bytes = 0;
1586 }
1587 }
1588 client = tclient;
1589 }
1590 }
1591 return(got);
1592 }
1593 #endif
1594
1595 /*
1596 *----------------------------------------------------------------------------
1597 *
1598 * Routine: XPAClientConnect
1599 *
1600 * Purpose: go to name service and get new names, merge with old,
1601 * and connect to servers
1602 *
1603 * Returns: number of connections
1604 *
1605 *----------------------------------------------------------------------------
1606 */
1607 #ifdef ANSI_FUNC
1608 static int
1609 XPAClientConnect (XPA xpa, char *mode, char *xtemplate, int type)
1610 #else
1611 static int XPAClientConnect(xpa, mode, xtemplate, type)
1612 XPA xpa;
1613 char *mode;
1614 char *xtemplate;
1615 int type;
1616 #endif
1617 {
1618 int i;
1619 int n;
1620 int got;
1621 int lp=0;
1622 int total=0;
1623 char **xclasses;
1624 char **names;
1625 char **methods;
1626 char **infos;
1627 char xtype[2];
1628 char xmode[SZ_LINE];
1629 char tbuf[SZ_LINE];
1630 char lbuf[SZ_LINE];
1631 XPAClient client;
1632
1633 /* do some initialization */
1634 XPAInitEnv();
1635
1636 /* make sure we have a target */
1637 if( !xtemplate || !*xtemplate )
1638 return(0);
1639
1640 /* make a string out of the type for the lookup */
1641 xtype[0] = type;
1642 xtype[1] = '\0';
1643
1644 /* reset the number of clients we are processing */
1645 xpa->nclient = 0;
1646
1647 /* look for specific proxy info */
1648 *xmode = '\0';
1649 if( mode ){
1650 strncpy(xmode, mode, SZ_LINE-1);
1651 xmode[SZ_LINE-1] = '\0';
1652 }
1653 if( keyword(xmode, "ns", lbuf, SZ_LINE) ){
1654 FPRINTF((stderr, "%sXPAClientConnect: using ns info: %s\n", _sp, lbuf));
1655 newdtable("(),");
1656 xclasses = (char **)xmalloc(sizeof(char *));
1657 names = (char **)xmalloc(sizeof(char *));
1658 methods = (char **)xmalloc(sizeof(char *));
1659 infos = (char **)xmalloc(sizeof(char *));
1660 if( word(lbuf, tbuf, &lp) )
1661 xclasses[0] = xstrdup(tbuf);
1662 if( word(lbuf, tbuf, &lp) )
1663 names[0] = xstrdup(tbuf);
1664 if( word(lbuf, tbuf, &lp) )
1665 methods[0] = xstrdup(tbuf);
1666 infos[0] = xstrdup(XPA_DEF_CLIENT_INFO);
1667 n = 1;
1668 freedtable();
1669 }
1670 /* else ask xpans for access points matching the template */
1671 else{
1672 n = XPANSLookup(xpa,
1673 xtemplate, xtype, &xclasses, &names, &methods, &infos);
1674 }
1675 /* mark existing clients who do not match this template */
1676 for(got=0, client=xpa->clienthead; client !=NULL; client=client->next){
1677 for(i=0; i<n; i++){
1678 if( (client->type == type) &&
1679 (!strcmp(client->xclass, xclasses[i])) &&
1680 (!strcmp(client->name, names[i])) &&
1681 (!strcmp(client->method, methods[i])) &&
1682 (!strcmp(client->info, infos[i])) ){
1683 got++;
1684 }
1685 }
1686 /* don't unmark if its a different type -- someone else might be active */
1687 if( !got && (client->type == type) ){
1688 client->status = XPA_CLIENT_IDLE;
1689 }
1690 }
1691 /* add new clients for this type */
1692 for(i=0; i<n; i++){
1693 for(got=0, client=xpa->clienthead; client !=NULL; client=client->next){
1694 if( (client->type == type) &&
1695 (!strcmp(client->xclass, xclasses[i])) &&
1696 (!strcmp(client->name, names[i])) &&
1697 (!strcmp(client->method, methods[i])) &&
1698 (!strcmp(client->info, infos[i])) ){
1699 /* might have to change the template */
1700 if( strcmp(client->xtemplate, xtemplate) ){
1701 xfree(client->xtemplate);
1702 client->xtemplate = xstrdup(xtemplate);
1703 }
1704 client->status = XPA_CLIENT_ACTIVE;
1705 got++;
1706 total++;
1707 FPRINTF((stderr, "%sXPAClientConnect: existing match: %s %s %s\n",
1708 _sp, xclasses[i], names[i], methods[i]));
1709 break;
1710 }
1711 }
1712 if( !got ){
1713 FPRINTF((stderr, "%sXPAClientConnect: calls XPAClientNew for %s:%s %s\n",
1714 _sp, xclasses[i], names[i], methods[i]));
1715 if( XPAClientNew(xpa, mode, xtemplate, type,
1716 xclasses[i], names[i], methods[i], infos[i]) )
1717 total++;
1718 }
1719 /* done with these strings */
1720 xfree(xclasses[i]);
1721 xfree(names[i]);
1722 xfree(methods[i]);
1723 xfree(infos[i]);
1724 }
1725 /* free up arrays alloc'ed by names server */
1726 if( n > 0 ){
1727 xfree(xclasses);
1728 xfree(names);
1729 xfree(methods);
1730 xfree(infos);
1731 }
1732 return(total);
1733 }
1734
1735 /*
1736 *----------------------------------------------------------------------------
1737 *
1738 * Routine: XPAClientStart
1739 *
1740 * Purpose: send init string to server and perform other authentication
1741 * tasks
1742 *
1743 * Returns: 0 if success, -1 otherwise
1744 *
1745 *----------------------------------------------------------------------------
1746 */
1747 #ifdef ANSI_FUNC
1748 static int
1749 XPAClientStart (XPA xpa, XPAClient client, char *paramlist, char *mode)
1750 #else
1751 static int XPAClientStart(xpa, client, paramlist, mode)
1752 XPA xpa;
1753 XPAClient client;
1754 char *paramlist;
1755 char *mode;
1756 #endif
1757 {
1758 int fd=0;
1759 int lp=0;
1760 int flags;
1761 int tries=0;
1762 int dmode=0;
1763 int keep_alive=1;
1764 unsigned int ip=0;
1765 unsigned short port;
1766 char tbuf[SZ_LINE];
1767 char tbuf2[SZ_LINE];
1768 char lbuf[SZ_LINE];
1769 char *cmd=NULL;
1770 char *method=NULL;
1771 struct sockaddr_in sock_in;
1772 #if HAVE_SYS_UN_H
1773 struct sockaddr_un sock_un;
1774 #endif
1775
1776 switch(client->type){
1777 case 'a':
1778 cmd = "xpaaccess";
1779 break;
1780 case 'g':
1781 cmd = "xpaget";
1782 break;
1783 case 'i':
1784 cmd = "xpainfo";
1785 break;
1786 case 's':
1787 cmd = "xpaset";
1788 break;
1789 }
1790
1791 /* get mode flags */
1792 XPAMode(mode, &(client->mode), "ack", XPA_CLIENT_ACK, 1);
1793 if( client->type == 's' )
1794 XPAMode(mode, &(xpa->client_mode), "verify", XPA_CLIENT_VERIFY, 0);
1795 /* package up and send the initialization message */
1796 strcpy(lbuf, cmd);
1797 /* set and save the id value */
1798 snprintf(tbuf, SZ_LINE, "%c%d", client->type, id++);
1799 if( client->id ) xfree(client->id);
1800 client->id = xstrdup(tbuf);
1801 /* if we are using the xpans proxy, we will want to call
1802 accept() (server calls connect()) for the data channel */
1803 if( client->nsproxy )
1804 strcat(lbuf, " -a");
1805 /* set the value of the client big-endian-ness */
1806 snprintf(tbuf, SZ_LINE, " -e %s", XPAEndian() ? "big" : "little");
1807 strcat(lbuf, tbuf);
1808 snprintf(tbuf, SZ_LINE, " -i %s", client->id);
1809 strcat(lbuf, tbuf);
1810 if( !(client->mode & XPA_CLIENT_ACK) )
1811 strcat(lbuf, " -n");
1812 if( strcmp(client->info, XPA_DEF_CLIENT_INFO) ){
1813 snprintf(tbuf, SZ_LINE, " -p %s", client->info);
1814 strcat(lbuf, tbuf);
1815 }
1816 snprintf(tbuf, SZ_LINE, " %s:%s", client->xclass, client->name);
1817 strcat(lbuf, tbuf);
1818 if( paramlist && *paramlist ){
1819 strcat(lbuf, " ");
1820 strncat(lbuf, paramlist, MAX(0,(int)(SZ_LINE-(int)strlen(lbuf)-2)));
1821 }
1822 strcat(lbuf, "\n");
1823 FPRINTF((stderr, "%sXPAClientStart: fd %d sends:\n%s",
1824 _sp, client->cmdfd, lbuf));
1825 if( XPAPuts(NULL, client->cmdfd, lbuf, XPAShortTimeout()) <= 0 ){
1826 goto error;
1827 }
1828
1829 /* if xpainfo and no ack'ing, we are basically done */
1830 if( (client->type == 'i') && !(client->mode & XPA_CLIENT_ACK) ){
1831 goto done;
1832 }
1833
1834 /* authentication */
1835 retry:
1836 lp = 0;
1837 if( XPAGets(NULL, client->cmdfd, lbuf, SZ_LINE, XPAShortTimeout()) >0 ){
1838 FPRINTF((stderr, "%sXPAClientStart: fd %d received cmd:\n%s", _sp,
1839 client->cmdfd, lbuf));
1840 /* this should never happen */
1841 if( !word(lbuf, tbuf, &lp) || (*tbuf == '?') ){
1842 snprintf(errbuf, SZ_LINE,
1843 "XPA$WARNING: Protocol mismatch: id\n%s", lbuf);
1844 goto error;
1845 }
1846 /* make sure we are dealing with a proper message */
1847 if( strcmp(tbuf, client->id) ){
1848 FPRINTF((stderr, "%sXPA$WARNING: ignoring out of sync message:\n", _sp));
1849 FPRINTF((stderr, "%s", lbuf));
1850 if( XPAVerbosity() > 1 ){
1851 fprintf(stderr, "XPA$WARNING: ignoring out of sync server message:\n");
1852 fprintf(stderr, "%s", lbuf);
1853 }
1854 goto retry;
1855 }
1856
1857 /* this should never happen */
1858 if( !word(lbuf, tbuf, &lp) ){
1859 snprintf(errbuf, SZ_LINE, "XPA$WARNING: missing BUF request\n%s", lbuf);
1860 goto error;
1861 }
1862 if( !strcmp(tbuf, "XPA$NODATA") || !strcmp(tbuf, "XPA$NOBUF") ){
1863 xpa->nclient += 1;
1864 goto started;
1865 }
1866 /* support 2.2 (DATA) and 2.0,2.1 (BUF) */
1867 else if( !strcmp(tbuf, "XPA$DATA") || !strcmp(tbuf, "XPA$BUF") ){
1868 if( !strcmp(tbuf, "XPA$DATA") ){
1869 dmode |= DATA_DATA;
1870 if( !word(lbuf, tbuf, &lp) ){
1871 snprintf(errbuf, SZ_LINE,
1872 "XPA$WARNING: missing DATA request type\n%s", lbuf);
1873 goto error;
1874 }
1875 if( !strcmp(tbuf, "connect") ){
1876 method = client->method;
1877 dmode |= DATA_CONNECT;
1878 }
1879 else if( !strcmp(tbuf, "accept") ){
1880 method = client->method;
1881 dmode |= DATA_ACCEPT;
1882 }
1883 else{
1884 snprintf(errbuf, SZ_LINE,
1885 "XPA$WARNING: invalid data connection request: %s (%s:%s)\n",
1886 tbuf, client->xclass, client->name);
1887 goto error;
1888 }
1889 }
1890 else if( !strcmp(tbuf, "XPA$BUF") ){
1891 if( !word(lbuf, tbuf, &lp) ){
1892 snprintf(errbuf, SZ_LINE,
1893 "XPA$ERROR: missing data buffer method (%s:%s%s)\n",
1894 client->xclass, client->name, XPATimestamp());
1895 goto error;
1896 }
1897 method = tbuf;
1898 dmode |= DATA_CONNECT;
1899 }
1900 /* handle connect-type requests */
1901 if( dmode & DATA_CONNECT ){
1902 switch(XPAMethod(method)){
1903 case XPA_INET:
1904 XPAParseIpPort(method, &ip, &port);
1905 /* connect to the server before we go further */
1906 if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){
1907 snprintf(errbuf, SZ_LINE,
1908 "XPA$ERROR: bad socket for data chan (%s:%s%s)\n",
1909 client->xclass, client->name, XPATimestamp());
1910 if( XPAVerbosity() ){
1911 perror("XPA client socket");
1912 }
1913 goto error;
1914 }
1915 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
1916 (char *)&keep_alive, sizeof(keep_alive));
1917 memset((char *)&sock_in, 0, sizeof(sock_in));
1918 sock_in.sin_family = AF_INET;
1919 /* connect using the same ip we used for the command channel
1920 (i.e., could be localhost) */
1921 sock_in.sin_addr.s_addr = htonl(client->ip);
1922 sock_in.sin_port = htons(port);
1923 FPRINTF((stderr,
1924 "%sXPAClientStart: attempting dchan connect: %s\n",
1925 _sp, method));
1926 if( connect(fd, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0 ){
1927 PERROR(("dchan connect"));
1928 snprintf(errbuf, SZ_LINE,
1929 "XPA$ERROR: can't connect to data chan (%s:%s%s)\n",
1930 client->xclass, client->name, XPATimestamp());
1931 if( XPAVerbosity() ){
1932 perror("XPA client connect");
1933 }
1934 xclose(fd);
1935 goto error;
1936 }
1937 break;
1938 #if HAVE_SYS_UN_H
1939 case XPA_UNIX:
1940 again:
1941 /* open a socket and fill in socket information */
1942 if( (fd = xsocket(AF_UNIX, SOCK_STREAM, 0)) < 0 ){
1943 snprintf(errbuf, SZ_LINE,
1944 "XPA$ERROR: bad socket for data chan (%s:%s%s)\n",
1945 client->xclass, client->name, XPATimestamp());
1946 if( XPAVerbosity() ){
1947 perror("XPA client socket");
1948 }
1949 goto error;
1950 }
1951 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
1952 (char *)&keep_alive, sizeof(keep_alive));
1953 memset((char *)&sock_un, 0, sizeof(sock_un));
1954 sock_un.sun_family = AF_UNIX;
1955 strcpy(sock_un.sun_path, method);
1956 FPRINTF((stderr,
1957 "%sXPAClientStart: attempting dchan connect: %s\n",
1958 _sp, method));
1959 if( connect(fd, (struct sockaddr *)&sock_un, sizeof(sock_un)) < 0 ){
1960 PERROR(("dchan connect"));
1961 xclose(fd);
1962 if( (xerrno == ECONNREFUSED) && (tries < XPA_RETRIES) ){
1963 tries++;
1964 xclose(fd);
1965 XPASleep(10);
1966 goto again;
1967 }
1968 /* give up */
1969 else{
1970 snprintf(errbuf, SZ_LINE,
1971 "XPA$ERROR: can't connect to data chan (%s:%s%s)\n",
1972 client->xclass, client->name, XPATimestamp());
1973 if( XPAVerbosity() ){
1974 perror("XPA client connect");
1975 }
1976 goto error;
1977 }
1978 }
1979 break;
1980 #endif
1981 default:
1982 snprintf(errbuf, SZ_LINE,
1983 "XPA$ERROR: unknown connection method (%s:%s%s)\n",
1984 client->xclass, client->name, XPATimestamp());
1985 goto error;
1986 }
1987 }
1988 /* handle "doaccept" requests */
1989 else if( dmode & DATA_ACCEPT ){
1990 fd = XPAProxyAccept(xpa, XPANSMethod(NULL,2),
1991 client->xclass, client->name, client->cmdfd,
1992 NULL, NULL, tbuf2);
1993 if( fd < 0 ){
1994 snprintf(errbuf, SZ_LINE,
1995 "XPA$ERROR: can't connect to proxy server (%s:%s%s)\n",
1996 client->xclass, client->name, XPATimestamp());
1997 goto error;
1998 }
1999 else if( *tbuf2 ){
2000 client->dataname = xstrdup(tbuf2);
2001 }
2002 }
2003
2004 /* common code for either type of data connect request */
2005 client->datafd = fd;
2006 /* make sure we close on exec */
2007 xfcntl(client->datafd, F_SETFD, FD_CLOEXEC);
2008 /* for senders, set to no block mode */
2009 if( client->type == 's' ){
2010 /* save state and set in non-blocking mode */
2011 xfcntl_nonblock(client->datafd, flags);
2012 }
2013 xpa->nclient += 1;
2014 goto started;
2015 }
2016 /* handle error message */
2017 else if( !strcmp(tbuf, "XPA$ERROR") ){
2018 snprintf(errbuf, SZ_LINE, "%s", &lbuf[lp]);
2019 goto error;
2020 }
2021 /* everything else is an error */
2022 else{
2023 snprintf(errbuf, SZ_LINE, "%s", &lbuf[lp]);
2024 goto error;
2025 }
2026 }
2027 else{
2028 snprintf(errbuf, SZ_LINE,
2029 "XPA$ERROR: no response from server during handshake (%s:%s%s)\n",
2030 client->xclass, client->name, XPATimestamp());
2031 goto error;
2032 }
2033
2034 error:
2035 FPRINTF((stderr, "Error in XPAClientStart: %s", errbuf));
2036 XPAClientFree(xpa, client);
2037 return(-1);
2038
2039 started:
2040 /* it is necessary to add this this extra hack/ack between the
2041 authentication ack and the error return (both from the server)
2042 to avoid getting Nagle buffered. if we already did an accept,
2043 however, we already sent a message and need not repeat it */
2044 if( !(dmode & DATA_ACCEPT) ){
2045 FPRINTF((stderr, "%sXPAClientStart: %d sending nagle\n",
2046 _sp, client->cmdfd));
2047 XPAPuts(NULL, client->cmdfd, "xpanagle\n", XPAShortTimeout());
2048 }
2049
2050 /* write a "data request" to the server on the data channel, supplying
2051 the arguments to allow the server to find the associated command */
2052 if( dmode & DATA_DATA ){
2053 if( !word(lbuf, tbuf, &lp) )
2054 strcpy(tbuf, "?");
2055 if( !word(lbuf, tbuf2, &lp) )
2056 strcpy(tbuf2, "?");
2057 snprintf(lbuf, SZ_LINE, "xpadata -f %s %s\n", tbuf, tbuf2);
2058 FPRINTF((stderr,
2059 "%sXPAClientStart: sending data channel %d request: %s", _sp,
2060 client->datafd, lbuf));
2061 if( XPAPuts(NULL, client->datafd, lbuf, XPAShortTimeout()) <0 ){
2062 snprintf(errbuf, SZ_LINE,
2063 "XPA$ERROR: unable to issue data request: %s (%s:%s%s)\n",
2064 lbuf, client->xclass, client->name, XPATimestamp());
2065 FPRINTF((stderr, "%sXPAClientStart: error returned is %s", _sp, errbuf));
2066 goto error;
2067 }
2068 }
2069
2070 done:
2071 /* mark as active and started */
2072 client->status = XPA_CLIENT_PROCESSING;
2073 return(0);
2074 }
2075
2076 /*
2077 *----------------------------------------------------------------------------
2078 *
2079 *
2080 * Public Routines
2081 *
2082 *
2083 *----------------------------------------------------------------------------
2084 */
2085
2086 /*
2087 *---------------------------------------------------------------------------
2088 *
2089 * Routine: XPAClientValid
2090 *
2091 * Purpose: see if the xpa client struct is valid
2092 *
2093 * Results: 1 on success, 0 for failure
2094 *
2095 *---------------------------------------------------------------------------
2096 */
2097 #ifdef ANSI_FUNC
2098 int
2099 XPAClientValid (XPA xpa)
2100 #else
2101 int XPAClientValid(xpa)
2102 XPA xpa;
2103 #endif
2104 {
2105 return(_XPAValid(xpaclienthead, xpa, "c"));
2106 }
2107
2108 /*
2109 *----------------------------------------------------------------------------
2110 *
2111 * Routine: XPAOpen
2112 *
2113 * Purpose: open a persistent XPA client connection
2114 *
2115 * Returns: XPA struct on success
2116 *
2117 *----------------------------------------------------------------------------
2118 */
2119 #ifdef ANSI_FUNC
2120 XPA
2121 XPAOpen (char *mode)
2122 #else
2123 XPA XPAOpen(mode)
2124 char *mode;
2125 #endif
2126 {
2127 XPA xpa;
2128
2129 /* allocate xpa struct */
2130 if( (xpa = (XPA)xcalloc(1, sizeof(XPARec))) == NULL )
2131 return(NULL);
2132 /* add version */
2133 xpa->version = xstrdup(XPA_VERSION);
2134 /* mark this as a client struct */
2135 xpa->type = xstrdup("c");
2136 /* mark as persistent so we don't destroy at the end of the transfer */
2137 xpa->persist = 1;
2138
2139 /* add this xpa to end of list of client xpas */
2140 XPAListAdd(&xpaclienthead, xpa);
2141
2142 return(xpa);
2143 }
2144
2145 /*
2146 *----------------------------------------------------------------------------
2147 *
2148 * Routine: XPAClose
2149 *
2150 * Purpose: close a persistent XPA client connection
2151 *
2152 * Returns: none
2153 *
2154 *----------------------------------------------------------------------------
2155 */
2156 #ifdef ANSI_FUNC
2157 void
2158 XPAClose (XPA xpa)
2159 #else
2160 void XPAClose(xpa)
2161 XPA xpa;
2162 #endif
2163 {
2164 XPAClient client, tclient;
2165 NS ns, tns;
2166
2167 /* ignore struct if its not a client struct */
2168 if( !XPAClientValid(xpa) )
2169 return;
2170
2171 /* remove from list of client xpas */
2172 XPAListDel(&xpaclienthead, xpa);
2173
2174 /* free each remaining client */
2175 for(client=xpa->clienthead; client!=NULL; ){
2176 tclient = client->next;
2177 XPAClientFree(xpa, client);
2178 client = tclient;
2179 }
2180
2181 /* close down the name server and all of the remotes for this xpa */
2182 for(ns=xpa->nshead; ns!=NULL; ){
2183 tns = ns->next;
2184 XPANSClose(xpa, ns);
2185 ns = tns;
2186 }
2187
2188 /* free string space */
2189 if( xpa->version )
2190 xfree(xpa->version);
2191 if( xpa->type )
2192 xfree(xpa->type);
2193 if( xpa )
2194 xfree(xpa);
2195 }
2196
2197 /*
2198 *----------------------------------------------------------------------------
2199 *
2200 * Routine: XPAGet
2201 *
2202 * Purpose: get XPA values
2203 *
2204 * Returns: 0 for success, -1 for failure
2205 * len bytes of data returned in buf
2206 *
2207 *----------------------------------------------------------------------------
2208 */
2209 #ifdef ANSI_FUNC
2210 int
2211 XPAGet (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2212 char **bufs, size_t *lens, char **names, char **messages, int n)
2213 #else
2214 int XPAGet(xpa, xtemplate, paramlist, mode, bufs, lens, names, messages, n)
2215 XPA xpa;
2216 char *xtemplate;
2217 char *paramlist;
2218 char *mode;
2219 char **bufs;
2220 size_t *lens;
2221 char **names;
2222 char **messages;
2223 int n;
2224 #endif
2225 {
2226 int i;
2227 int oldmode=0;
2228 int xmode=0;
2229 int type='g';
2230 int idef=1;
2231 int got=0;
2232 char tbuf[SZ_LINE];
2233 XPAClient client, tclient;
2234
2235 FPRINTF((stderr, "%sXPAGet: starting\n", _sp));
2236 /* if not persistent, we need a temp xpa struct;
2237 (also ignore passed struct if its not a client struct) */
2238 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2239 if( (xpa = XPAOpen(NULL)) == NULL )
2240 return(-1);
2241 /* mark this as not persistent */
2242 xpa->persist = 0;
2243 }
2244 /* save xpa mode -- this call might override */
2245 else{
2246 /* make sure we have a valid client handle */
2247 if( !XPAClientValid(xpa) ){
2248 if( XPAVerbosity() ){
2249 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2250 }
2251 return(-1);
2252 }
2253 oldmode = xpa->client_mode;
2254 }
2255
2256 /* these arrays are required */
2257 if( (bufs == NULL) || (lens == NULL) ){
2258 got = -1;
2259 goto done;
2260 }
2261
2262 /* flag that we don't read from stdin */
2263 xpa->ifd = -1;
2264
2265 /* zero out the return buffers */
2266 memset((char *)bufs, 0, ABS(n)*sizeof(char *));
2267 memset((char *)lens, 0, ABS(n)*sizeof(size_t));
2268 if( names != NULL )
2269 memset((char *)names, 0, ABS(n)*sizeof(char *));
2270 if( messages != NULL )
2271 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2272
2273 /* connect to clients and grab data */
2274 if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
2275 /* retrieve data from n active clients */
2276 for(client=xpa->clienthead; client!=NULL; ){
2277 tclient = client->next;
2278 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2279 (got<ABS(n)) ){
2280 if( names != NULL ){
2281 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2282 client->xclass, client->name, client->method);
2283 names[got] = xstrdup(tbuf);
2284 }
2285 if( XPAClientStart(xpa, client, paramlist, mode) >=0 ){
2286 /* we fill buffers */
2287 client->mode |= XPA_CLIENT_BUF;
2288 client->bufptr = &(bufs[got]);
2289 client->lenptr = &(lens[got]);
2290 if( names != NULL )
2291 client->nameptr = &(names[got]);
2292 if( messages != NULL )
2293 client->errptr = &(messages[got]);
2294 }
2295 else{
2296 if( messages != NULL )
2297 messages[got] = xstrdup(errbuf);
2298 }
2299 got++;
2300 }
2301 client = tclient;
2302 }
2303 /* if we have active clients */
2304 if( got ){
2305 #if HAVE_MINGW32==0
2306 /* check for loop modes */
2307 XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
2308 /* dofork implies don't do xpa */
2309 if( xmode & XPA_CLIENT_SEL_FORK )
2310 idef = 0;
2311 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2312 if( xmode & XPA_CLIENT_SEL_FORK ){
2313 XPAClientLoopFork(xpa, xmode);
2314 }
2315 else{
2316 /* enter the main loop and process */
2317 XPAClientLoop(xpa, xmode);
2318 }
2319 #else
2320 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2321 XPAClientLoop(xpa, xmode);
2322 #endif
2323 }
2324 }
2325
2326 done:
2327 /* look for clients who timed out */
2328 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
2329 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2330 (i<ABS(n)) ){
2331 i++;
2332 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
2333 snprintf(errbuf, SZ_LINE,
2334 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
2335 client->xclass, client->name, XPATimestamp());
2336 messages[i] = xstrdup(errbuf);
2337 }
2338 }
2339 }
2340 /* remove this xpa if we are not meant to persist */
2341 if( xpa && !xpa->persist )
2342 XPAClose(xpa);
2343 /* restore xpa mode -- this call might override */
2344 else
2345 xpa->client_mode = oldmode;
2346
2347 /* return number of clients processes (including errors) */
2348 return(got);
2349 }
2350
2351 /*
2352 *----------------------------------------------------------------------------
2353 *
2354 * Routine: XPAGetFd
2355 *
2356 * Purpose: get XPA values
2357 *
2358 * Returns: 0 for success, -1 for failure
2359 * len bytes of data returned in buf
2360 *
2361 *----------------------------------------------------------------------------
2362 */
2363 #ifdef ANSI_FUNC
2364 int
2365 XPAGetFd (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2366 int *fds, char **names, char **messages, int n)
2367 #else
2368 int XPAGetFd(xpa, xtemplate, paramlist, mode, fds, names, messages, n)
2369 XPA xpa;
2370 char *xtemplate;
2371 char *paramlist;
2372 int *fds;
2373 char *mode;
2374 char **names;
2375 char **messages;
2376 int n;
2377 #endif
2378 {
2379 int i;
2380 int oldmode=0;
2381 int xmode=0;
2382 int got=0;
2383 int type='g';
2384 int idef=1;
2385 char tbuf[SZ_LINE];
2386 XPAClient client, tclient;
2387
2388 FPRINTF((stderr, "%sXPAGetFd: starting\n", _sp));
2389 /* if not persistent, we need a temp xpa struct;
2390 (also ignore passed struct if its not a client struct) */
2391 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2392 if( (xpa = XPAOpen(NULL)) == NULL )
2393 return(-1);
2394 /* mark this as not persistent */
2395 xpa->persist = 0;
2396 }
2397 /* save xpa mode -- this call might override */
2398 else{
2399 /* make sure we have a valid client handle */
2400 if( !XPAClientValid(xpa) ){
2401 if( XPAVerbosity() ){
2402 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2403 }
2404 return(-1);
2405 }
2406 oldmode = xpa->client_mode;
2407 }
2408
2409 /* flag that we don't read from stdin */
2410 xpa->ifd = -1;
2411
2412 /* zero out the return buffers */
2413 if( names != NULL )
2414 memset((char *)names, 0, ABS(n)*sizeof(char *));
2415 if( messages != NULL )
2416 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2417
2418 /* connect to clients and grab data */
2419 if( XPAClientConnect(xpa, mode, xtemplate, type) > 0 ){
2420 /* retrieve data from n active clients */
2421 for(client=xpa->clienthead; client!=NULL; ){
2422 tclient = client->next;
2423 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2424 (got<ABS(n)) ){
2425 if( names != NULL ){
2426 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2427 client->xclass, client->name, client->method);
2428 names[got] = xstrdup(tbuf);
2429 }
2430 if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
2431 /* we write to an fd */
2432 client->mode |= XPA_CLIENT_FD;
2433 /* negative value => one channel for all clients */
2434 if( n < 0 )
2435 client->fd = fds[0];
2436 else
2437 client->fd = fds[got];
2438 client->bufptr = (char **)xcalloc(1, sizeof(char *));
2439 client->lenptr = (size_t *)xcalloc(1, sizeof(size_t));
2440 if( names != NULL )
2441 client->nameptr = &(names[got]);
2442 if( messages != NULL )
2443 client->errptr = &(messages[got]);
2444 }
2445 else{
2446 if( messages != NULL )
2447 messages[got] = xstrdup(errbuf);
2448 }
2449 got++;
2450 }
2451 client = tclient;
2452 }
2453 /* if we have active clients */
2454 if( got ){
2455 #if HAVE_MINGW32==0
2456 /* check for loop modes */
2457 XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
2458 /* dofork implies don't do xpa */
2459 if( xmode & XPA_CLIENT_SEL_FORK )
2460 idef = 0;
2461 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2462 if( xmode & XPA_CLIENT_SEL_FORK ){
2463 XPAClientLoopFork(xpa, xmode);
2464 }
2465 else{
2466 /* enter the main loop and process */
2467 XPAClientLoop(xpa, xmode);
2468 }
2469 #else
2470 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2471 XPAClientLoop(xpa, xmode);
2472 #endif
2473 }
2474 }
2475 /* look for clients who timed out */
2476 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
2477 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2478 (i<ABS(n)) ){
2479 i++;
2480 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
2481 snprintf(errbuf, SZ_LINE,
2482 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
2483 client->xclass, client->name, XPATimestamp());
2484 messages[i] = xstrdup(errbuf);
2485 }
2486 }
2487 }
2488 /* remove this xpa if we are not meant to persist */
2489 if( xpa && !xpa->persist )
2490 XPAClose(xpa);
2491 /* restore xpa mode -- this call might override */
2492 else
2493 xpa->client_mode = oldmode;
2494
2495 /* return number of clients processes (including errors) */
2496 return(got);
2497 }
2498
2499 /*
2500 *----------------------------------------------------------------------------
2501 *
2502 * Routine: XPASet
2503 *
2504 * Purpose: set XPA values
2505 *
2506 * Returns: 0 for success, -1 for failure
2507 *
2508 *----------------------------------------------------------------------------
2509 */
2510 #ifdef ANSI_FUNC
2511 int
2512 XPASet (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2513 char *buf, size_t len, char **names, char **messages, int n)
2514 #else
2515 int XPASet(xpa, xtemplate, paramlist, mode, buf, len, names, messages, n)
2516 XPA xpa;
2517 char *xtemplate;
2518 char *paramlist;
2519 char *mode;
2520 char *buf;
2521 size_t len;
2522 char **names;
2523 char **messages;
2524 int n;
2525 #endif
2526 {
2527 int i;
2528 int oldmode=0;
2529 int xmode=0;
2530 int got=0;
2531 int type='s';
2532 int idef=1;
2533 char tbuf[SZ_LINE];
2534 XPAClient client, tclient;
2535
2536 FPRINTF((stderr, "%sXPASet: starting\n", _sp));
2537 /* if not persistent, we need a temp xpa struct;
2538 (also ignore passed struct if its not a client struct) */
2539 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2540 if( (xpa = XPAOpen(NULL)) == NULL )
2541 return(-1);
2542 /* mark this as not persistent */
2543 xpa->persist = 0;
2544 }
2545 /* save xpa mode -- this call might override */
2546 else{
2547 /* make sure we have a valid client handle */
2548 if( !XPAClientValid(xpa) ){
2549 if( XPAVerbosity() ){
2550 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2551 }
2552 return(-1);
2553 }
2554 oldmode = xpa->client_mode;
2555 }
2556
2557 /* flag that we don't read from stdin */
2558 xpa->ifd = -1;
2559
2560 /* zero out the return buffers */
2561 if( names != NULL )
2562 memset((char *)names, 0, ABS(n)*sizeof(char *));
2563 if( messages != NULL )
2564 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2565
2566 /* connect to clients and grab data */
2567 if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
2568 /* retrieve data from n active clients */
2569 for(client=xpa->clienthead; client!=NULL; ){
2570 tclient = client->next;
2571 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2572 (got<ABS(n)) ){
2573 if( names != NULL ){
2574 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2575 client->xclass, client->name, client->method);
2576 names[got] = xstrdup(tbuf);
2577 }
2578 if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
2579 /* we fill buffers */
2580 client->mode |= XPA_CLIENT_BUF;
2581 client->buf = buf;
2582 client->len = len;
2583 if( names != NULL )
2584 client->nameptr = &(names[got]);
2585 if( messages != NULL )
2586 client->errptr = &(messages[got]);
2587 }
2588 else{
2589 if( messages != NULL )
2590 messages[got] = xstrdup(errbuf);
2591 }
2592 got++;
2593 }
2594 client = tclient;
2595 }
2596 /* if we have active clients */
2597 if( got ){
2598 #if HAVE_MINGW32==0
2599 /* check for loop modes */
2600 XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
2601 /* dofork implies don't do xpa */
2602 if( xmode & XPA_CLIENT_SEL_FORK )
2603 idef = 0;
2604 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2605 if( xmode & XPA_CLIENT_SEL_FORK ){
2606 XPAClientLoopFork(xpa, xmode);
2607 }
2608 else{
2609 /* enter the main loop and process */
2610 XPAClientLoop(xpa, xmode);
2611 }
2612 #else
2613 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2614 XPAClientLoop(xpa, xmode);
2615 #endif
2616 }
2617 }
2618 /* look for clients who timed out */
2619 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
2620 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2621 (i<ABS(n)) ){
2622 i++;
2623 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
2624 snprintf(errbuf, SZ_LINE,
2625 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
2626 client->xclass, client->name, XPATimestamp());
2627 messages[i] = xstrdup(errbuf);
2628 }
2629 }
2630 }
2631 /* remove this xpa if we are not meant to persist */
2632 if( xpa && !xpa->persist )
2633 XPAClose(xpa);
2634 /* restore xpa mode -- this call might override */
2635 else
2636 xpa->client_mode = oldmode;
2637
2638 /* return number of clients processes (including errors) */
2639 return(got);
2640 }
2641
2642 /*
2643 *----------------------------------------------------------------------------
2644 *
2645 * Routine: XPASetFd
2646 *
2647 * Purpose: set XPA values
2648 *
2649 * Returns: 0 for success, -1 for failure
2650 *
2651 *----------------------------------------------------------------------------
2652 */
2653 #ifdef ANSI_FUNC
2654 int
2655 XPASetFd (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2656 int fd, char **names, char **messages, int n)
2657 #else
2658 int XPASetFd(xpa, xtemplate, paramlist, mode, fd, names, messages, n)
2659 XPA xpa;
2660 char *xtemplate;
2661 char *paramlist;
2662 char *mode;
2663 int fd;
2664 char **names;
2665 char **messages;
2666 int n;
2667 #endif
2668 {
2669 int i;
2670 int oldmode=0;
2671 int xmode=0;
2672 int got=0;
2673 int got2=0;
2674 int type='s';
2675 int idef=1;
2676 int flags;
2677 char *s;
2678 char tbuf[SZ_LINE];
2679 XPAClient client, tclient;
2680
2681 FPRINTF((stderr, "%sXPASetFd: starting\n", _sp));
2682 /* if not persistent, we need a temp xpa struct;
2683 (also ignore passed struct if its not a client struct) */
2684 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2685 if( (xpa = XPAOpen(NULL)) == NULL )
2686 return(-1);
2687 /* mark this as not persistent */
2688 xpa->persist = 0;
2689 }
2690 /* save xpa mode -- this call might override */
2691 else{
2692 /* make sure we have a valid client handle */
2693 if( !XPAClientValid(xpa) ){
2694 if( XPAVerbosity() ){
2695 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2696 }
2697 return(-1);
2698 }
2699 oldmode = xpa->client_mode;
2700 }
2701
2702 /* Set non-blocking mode for the input fd, if its not a tty */
2703 xpa->ifd = fd;
2704 if( isatty(xpa->ifd) == 0 ){
2705 /* save state and set in non-blocking mode */
2706 xfcntl_nonblock(xpa->ifd, flags);
2707 }
2708 /* zero out the return buffers */
2709 if( names != NULL )
2710 memset((char *)names, 0, ABS(n)*sizeof(char *));
2711 if( messages != NULL )
2712 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2713
2714 /* connect to clients and grab data */
2715 if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
2716 /* open clients all at once */
2717 for(client=xpa->clienthead; client!=NULL; ){
2718 tclient = client->next;
2719 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2720 (got<ABS(n)) ){
2721 if( names != NULL ){
2722 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2723 client->xclass, client->name, client->method);
2724 names[got] = xstrdup(tbuf);
2725 }
2726 if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
2727 /* we fill buffers */
2728 client->mode |= XPA_CLIENT_FD;
2729 if( names != NULL )
2730 client->nameptr = &(names[got]);
2731 if( messages != NULL )
2732 client->errptr = &(messages[got]);
2733 }
2734 else{
2735 if( messages != NULL )
2736 messages[got] = xstrdup(errbuf);
2737 }
2738 got++;
2739 }
2740 client = tclient;
2741 }
2742 /* if we have active clients */
2743 if( got ){
2744 /* if fd is null, user did not want to send data, just the paramlist */
2745 if( fd < 0 ){
2746 for(client=xpa->clienthead; client!=NULL; ){
2747 tclient = client->next;
2748 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2749 (got<ABS(n)) ){
2750 XPAClientDataSent(xpa, client);
2751 s = XPAClientEnd(xpa, client);
2752 if( (messages != NULL) && (messages[got2] == NULL) )
2753 messages[got2] = xstrdup(s);
2754 got2++;
2755 }
2756 client = tclient;
2757 }
2758 }
2759 else{
2760 #if HAVE_MINGW32==0
2761 /* check for loop modes */
2762 XPAMode(mode, &xmode, "dofork", XPA_CLIENT_SEL_FORK, 0);
2763 /* dofork implies don't do xpa */
2764 if( xmode & XPA_CLIENT_SEL_FORK )
2765 idef = 0;
2766 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2767 if( xmode & XPA_CLIENT_SEL_FORK ){
2768 XPAClientLoopFork(xpa, xmode);
2769 }
2770 else{
2771 /* enter the main loop and process */
2772 XPAClientLoop(xpa, xmode);
2773 }
2774 #else
2775 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, idef);
2776 XPAClientLoop(xpa, xmode);
2777 #endif
2778 }
2779 }
2780 }
2781 /* look for clients who timed out */
2782 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
2783 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2784 (i<ABS(n)) ){
2785 i++;
2786 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
2787 snprintf(errbuf, SZ_LINE,
2788 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
2789 client->xclass, client->name, XPATimestamp());
2790 messages[i] = xstrdup(errbuf);
2791 }
2792 }
2793 }
2794 /* reset flags, if necessary */
2795 if( xpa->ifd >=0 && (isatty(xpa->ifd) ==0) ){
2796 xfcntl(xpa->ifd, F_SETFL, flags);
2797 }
2798 /* free all input structs */
2799 XPAClientFreeAllInputs(xpa);
2800 /* remove this xpa if we are not meant to persist */
2801 if( xpa && !xpa->persist )
2802 XPAClose(xpa);
2803 /* restore xpa mode -- this call might override */
2804 else
2805 xpa->client_mode = oldmode;
2806
2807 /* return number of clients processes (including errors) */
2808 return(got);
2809 }
2810
2811 /*
2812 *----------------------------------------------------------------------------
2813 *
2814 * Routine: XPAInfo
2815 *
2816 * Purpose: send XPA info
2817 *
2818 * Returns: 0 for success, -1 for failure
2819 *
2820 *----------------------------------------------------------------------------
2821 */
2822 #ifdef ANSI_FUNC
2823 int
2824 XPAInfo (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2825 char **names, char **messages, int n)
2826 #else
2827 int XPAInfo(xpa, xtemplate, paramlist, mode, names, messages, n)
2828 XPA xpa;
2829 char *xtemplate;
2830 char *paramlist;
2831 char *mode;
2832 char **names;
2833 char **messages;
2834 int n;
2835 #endif
2836 {
2837 int i;
2838 int oldmode=0;
2839 int got=0;
2840 char type='i';
2841 char *s;
2842 char tbuf[SZ_LINE];
2843 XPAClient client, tclient;
2844
2845 /* if not persistent, we need a temp xpa struct;
2846 (also ignore passed struct if its not a client struct) */
2847 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2848 if( (xpa = XPAOpen(NULL)) == NULL )
2849 return(-1);
2850 /* mark this as not persistent */
2851 xpa->persist = 0;
2852 }
2853 /* save xpa mode -- this call might override */
2854 else{
2855 /* make sure we have a valid client handle */
2856 if( !XPAClientValid(xpa) ){
2857 if( XPAVerbosity() ){
2858 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2859 }
2860 return(-1);
2861 }
2862 oldmode = xpa->client_mode;
2863 }
2864
2865 /* flag that we don't read from stdin */
2866 xpa->ifd = -1;
2867
2868 /* zero out the return buffers */
2869 if( names != NULL )
2870 memset((char *)names, 0, ABS(n)*sizeof(char *));
2871 if( messages != NULL )
2872 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2873
2874 /* connect to clients and grab data */
2875 if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
2876 /* retrieve data from n active clients */
2877 for(client=xpa->clienthead; client!=NULL; ){
2878 tclient = client->next;
2879 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2880 (got<ABS(n)) ){
2881 if( names != NULL ){
2882 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2883 client->xclass, client->name, client->method);
2884 names[got] = xstrdup(tbuf);
2885 }
2886 if( XPAClientStart(xpa, client, paramlist, mode) >= 0 ){
2887 XPAClientDataSent(xpa, client);
2888 s = XPAClientEnd(xpa, client);
2889 if( (messages != NULL) && (messages[got] == NULL) )
2890 messages[got] = xstrdup(s);
2891 }
2892 else{
2893 if( (messages != NULL) && (messages[got] == NULL) )
2894 messages[got] = xstrdup(errbuf);
2895 }
2896 got++;
2897 }
2898 client = tclient;
2899 }
2900 }
2901 /* look for clients who timed out */
2902 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
2903 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2904 (i<ABS(n)) ){
2905 i++;
2906 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
2907 snprintf(errbuf, SZ_LINE,
2908 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
2909 client->xclass, client->name, XPATimestamp());
2910 messages[i] = xstrdup(errbuf);
2911 }
2912 }
2913 }
2914 /* remove this xpa if we are not meant to persist */
2915 if( xpa && !xpa->persist )
2916 XPAClose(xpa);
2917 /* restore xpa mode -- this call might override */
2918 else
2919 xpa->client_mode = oldmode;
2920
2921 /* return number of clients processes (including errors) */
2922 return(got);
2923 }
2924
2925 /*
2926 *----------------------------------------------------------------------------
2927 *
2928 * Routine: XPAAccess
2929 *
2930 * Purpose: determine if XPA access point is available
2931 *
2932 * Returns: 0 for success, -1 for failure
2933 *
2934 *----------------------------------------------------------------------------
2935 */
2936 #ifdef ANSI_FUNC
2937 int
2938 XPAAccess (XPA xpa, char *xtemplate, char *paramlist, char *mode,
2939 char **names, char **messages, int n)
2940 #else
2941 int XPAAccess(xpa, xtemplate, paramlist, mode, names, messages, n)
2942 XPA xpa;
2943 char *xtemplate;
2944 char *paramlist;
2945 char *mode;
2946 char **names;
2947 char **messages;
2948 int n;
2949 #endif
2950 {
2951 int i;
2952 int oldmode=0;
2953 int xmode=0;
2954 int got=0;
2955 int type='a';
2956 char *s;
2957 char *ind1, *ind2;
2958 char tbuf[SZ_LINE];
2959 XPAClient client, tclient;
2960
2961 /* if not persistent, we need a temp xpa struct;
2962 (also ignore passed struct if its not a client struct) */
2963 if( (xpa == NULL) || strcmp(xpa->type, "c") ){
2964 if( (xpa = XPAOpen(NULL)) == NULL )
2965 return(-1);
2966 /* mark this as not persistent */
2967 xpa->persist = 0;
2968 }
2969 /* save xpa mode -- this call might override */
2970 else{
2971 /* make sure we have a valid client handle */
2972 if( !XPAClientValid(xpa) ){
2973 if( XPAVerbosity() ){
2974 fprintf(stderr, "XPA$ERROR: invalid xpa client handle\n");
2975 }
2976 return(-1);
2977 }
2978 oldmode = xpa->client_mode;
2979 }
2980
2981 /* flag that we don't read from stdin */
2982 xpa->ifd = -1;
2983
2984 /* zero out the return buffers */
2985 if( names != NULL )
2986 memset((char *)names, 0, ABS(n)*sizeof(char *));
2987 if( messages != NULL )
2988 memset((char *)messages, 0, ABS(n)*sizeof(char *));
2989
2990 /* connect to clients and grab data */
2991 if( XPAClientConnect(xpa, mode, xtemplate, type) >0 ){
2992 /* retrieve data from n active clients */
2993 for(client=xpa->clienthead; client!=NULL; ){
2994 tclient = client->next;
2995 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
2996 (got<ABS(n)) ){
2997 if( names != NULL ){
2998 snprintf(tbuf, SZ_LINE, "%s:%s %s",
2999 client->xclass, client->name, client->method);
3000 names[got] = xstrdup(tbuf);
3001 }
3002 if( XPAClientStart(xpa, client, paramlist, mode) >=0 ){
3003 XPAClientDataSent(xpa, client);
3004 s = XPAClientEnd(xpa, client);
3005 if( (messages != NULL) && (messages[got] == NULL) )
3006 messages[got] = xstrdup(s);
3007 }
3008 else{
3009 if( (messages != NULL) && (messages[got] == NULL) )
3010 messages[got] = xstrdup(errbuf);
3011 }
3012 /* might have to fix the name if was an explicit mach:port */
3013 if( names && names[got] && *errbuf &&
3014 !strncmp(names[got], "?:?", 3) &&
3015 (ind1=strrchr(errbuf, '(')) && (ind2=strrchr(errbuf, ')')) ){
3016 ind1++;
3017 strncpy(tbuf, ind1, ind2-ind1);
3018 tbuf[ind2-ind1] = '\0';
3019 xfree(names[got]);
3020 names[got] = xstrdup(tbuf);
3021 }
3022 got++;
3023 }
3024 client = tclient;
3025 }
3026 /* if we have active clients */
3027 if( got ){
3028 /* check for loop modes */
3029 XPAMode(mode, &xmode, "doxpa", XPA_CLIENT_SEL_XPA, 1);
3030 /* enter the main loop and process */
3031 XPAClientLoop(xpa, xmode);
3032 }
3033 }
3034 /* look for clients who timed out */
3035 for(i=0, client=xpa->clienthead; client!=NULL; client=client->next){
3036 if( (client->type == type) && (client->status != XPA_CLIENT_IDLE) &&
3037 (i<ABS(n)) ){
3038 i++;
3039 if( (client->status == XPA_CLIENT_PROCESSING) && ( messages != NULL) ){
3040 snprintf(errbuf, SZ_LINE,
3041 "XPA$ERROR: no response from server callback (%s:%s%s)\n",
3042 client->xclass, client->name, XPATimestamp());
3043 messages[i] = xstrdup(errbuf);
3044 }
3045 }
3046 }
3047 /* remove this xpa if we are not meant to persist */
3048 if( xpa && !xpa->persist )
3049 XPAClose(xpa);
3050 /* restore xpa mode -- this call might override */
3051 else
3052 xpa->client_mode = oldmode;
3053
3054 /* return number of clients processes (including errors) */
3055 return(got);
3056 }
3057