Debian Codegraph @

libsrc/xcon/consubs.c

raw
1 /*===========================================================================
2 Copyright (C) 1994-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++ module consubs +++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module consubs
31 .AUTHOR K. Banse ESO - Garching
32 .KEYWORDS
33 connection, sockets
34 .COMMENTS
35 holds msetup, outmail
36 .ENVIRONMENT UNIX
37
38 .VERSION [1.00] 940426: pulled out from prepb.c
39
40 090407 last modif
41
42 ------------------------------------------------------------------------*/
43
44 #include <midas_def.h>
45 #include <stdio.h>
46 #include <signal.h>
47
48 #ifndef SIGUSR1
49 #define SIGUSR1 30
50 #endif
51
52 #ifndef SIGUSR2
53 #define SIGUSR2 31
54 #endif
55
56 #include <osyparms.h>
57 #include <xconndef.h>
58
59 struct BACK_STRUCT BKMIDAS[MAX_BACK];
60 struct XCONN_STRUCT XCONNECT;
61
62 #define WRITE 1
63 #define READ 0
64
65 static int YesSignal;
66
67
68
69 void intdum2()
70
71 {
72
73 YesSignal = 2;
74
75 }
76
77
78 /*
79
80 */
81
82 int msetup(flag,token1,token2,token3,token4)
83
84 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
85 .PURPOSE
86 setup data for background activities
87 flag = 0 DISCONNECT/BACK unit
88 = 1 CONNECT/BACK unit ...
89 .ALGORITHM
90 .RETURNS
91 = 0; all o.k.
92 = 4; problems with socket communication
93 = 34; BKMIDAS structure overflow
94 = 90; invalid unit (FORGRxy.PID not found)
95 = 100; invalid seconds
96
97 -----------------------------------------------------------------*/
98
99 int flag;
100
101 char *token1; /* IN: backgr. Midas unit */
102 char *token2; /* IN: `wait,seconds' string */
103 char *token3; /* IN: background char. */
104 char *token4; /* IN: connection method, `files' or `sockets' */
105
106
107 {
108 register int nr, mr;
109 int fid, iwc, kk, jj, reto;
110 int idum;
111
112 float rdum;
113
114 double ddum;
115
116 char save[4], cbuf[20], wstr[84];
117
118
119 if (flag == 0)
120 { /* clear background-Midas table */
121 if ((token1[0] == '?') || (token1[0] == '*'))
122 {
123 for (nr=0; nr<MAX_BACK; nr++)
124 {
125 for (mr=0; mr<MAX_BACK; mr++)
126 BKMIDAS[mr].ACTIVE = 'N'; /* deactivate all */
127 if (BKMIDAS[nr].UNIT[0] != ' ')
128 {
129 if (BKMIDAS[nr].METHOD == 's')
130 {
131 if (ClientClose(BKMIDAS[nr].CHAN,&reto) != 0)
132 goto bad_osxclose;
133 }
134 BKMIDAS[nr].UNIT[0] = ' ';
135 }
136 }
137 return (0);
138 }
139
140 else
141 {
142 for (nr=0; nr<MAX_BACK; nr++)
143 {
144 if ((token1[0] == BKMIDAS[nr].UNIT[0]) &&
145 (token1[1] == BKMIDAS[nr].UNIT[1]))
146 {
147 BKMIDAS[nr].ACTIVE = 'N'; /* deactivate it */
148 if (BKMIDAS[nr].METHOD == 's')
149 {
150 if (ClientClose(BKMIDAS[nr].CHAN,&reto) != 0)
151 goto bad_osxclose;
152 }
153 BKMIDAS[nr].UNIT[0] = ' ';
154 }
155 }
156 return (0);
157 }
158
159 bad_osxclose:
160 token1[0] = BKMIDAS[nr].UNIT[0];
161 token1[1] = BKMIDAS[nr].UNIT[1];
162 token1[2] = '\0';
163 BKMIDAS[nr].UNIT[0] = ' ';
164 return (4);
165 }
166
167 else if (flag == 1)
168 { /* work on background MIDAS table */
169 save[2] = CGN_LOWER(token2[0]); /* wait,secs */
170 cbuf[0] = CGN_LOWER(token3[0]); /* b_char. */
171 cbuf[1] = CGN_LOWER(token4[0]); /* method */
172
173 if (cbuf[1] != 's') cbuf[1] = 'f';
174
175 jj = CGN_INDEXC(token1,':'); /* check if remote host */
176 if (jj > 0)
177 { /* remote host */
178 token1[jj] = '\0'; /* ':' -> '\0' */
179 nr = jj + 1;
180 }
181 else
182 nr = 0;
183
184 save[0] = CGN_UPPER(token1[nr++]);
185 save[1] = CGN_UPPER(token1[nr]);
186
187 if (save[2] == 'w')
188 {
189 save[2] = 'Y';
190 kk = CGN_INDEXC(token2,',');
191 if (kk > 0)
192 {
193 iwc = CGN_CNVT(&token2[kk+1],1,1,&idum,&rdum,&ddum);
194 if (iwc == 1)
195 kk = idum;
196 else
197 kk = -1; /* that's an error */
198 if (kk < 0)
199 {
200 return (100);
201 }
202 }
203 else
204 kk = -1;
205 }
206 else
207 {
208 save[2] = 'N';
209 kk = 0;
210 }
211
212 for (nr=0; nr<MAX_BACK; nr++) /* see, if already same entry there */
213 {
214 if (jj > 0) /* remote MIDAS unit */
215 {
216 if (strcmp(BKMIDAS[nr].HOST,token1) == 0)
217 {
218 if ( (BKMIDAS[nr].UNIT[0] == save[0]) &&
219 (BKMIDAS[nr].UNIT[1] == save[1]) &&
220 (BKMIDAS[nr].COMC == cbuf[0]) )
221 {
222 iwc = nr;
223 goto get_backpid;
224 }
225 }
226 }
227 else
228 {
229 if (BKMIDAS[nr].HOST[0] == '\0')
230 {
231 if ( (BKMIDAS[nr].UNIT[0] == save[0]) &&
232 (BKMIDAS[nr].UNIT[1] == save[1]) &&
233 (BKMIDAS[nr].COMC == cbuf[0]) )
234 {
235 iwc = nr;
236 goto get_backpid;
237 }
238 }
239 }
240 }
241
242 for (nr=0; nr<MAX_BACK; nr++) /* look for free slot */
243 {
244 if (BKMIDAS[nr].UNIT[0] == ' ')
245 {
246 iwc = nr;
247 goto get_backpid;
248 }
249 }
250 return (34); /* no more space */
251
252 get_backpid:
253 BKMIDAS[iwc].UNIT[0] = save[0];
254 BKMIDAS[iwc].UNIT[1] = save[1];
255 BKMIDAS[iwc].WAIT = kk;
256 BKMIDAS[iwc].COMC = cbuf[0];
257 BKMIDAS[iwc].METHOD = cbuf[1];
258 BKMIDAS[iwc].FLAG = save[2];
259 BKMIDAS[iwc].ACTIVE = 'N';
260 BKMIDAS[iwc].PID = -1;
261 if (jj > 0)
262 (void)strcpy(BKMIDAS[iwc].HOST,token1);
263 else
264 BKMIDAS[iwc].HOST[0] = '\0';
265
266 if (BKMIDAS[iwc].METHOD != 's') /* for interrupts we need the PID */
267 {
268 kk = CGN_COPY(wstr,XCONNECT.SBOX);
269 kk -= 12; /* get rid of FORGRxy.SBOX */
270 (void)strcpy(&wstr[kk],"MIDAS .PID");
271 wstr[kk+5] = BKMIDAS[iwc].UNIT[0];
272 wstr[kk+6] = BKMIDAS[iwc].UNIT[1];
273 fid = osaopen(wstr,READ); /* open to read */
274 if (fid > 0)
275 {
276 kk = osaread(fid,wstr,40);
277 if (kk > 0) (void)sscanf(wstr,"%d",&BKMIDAS[iwc].PID);
278 (void)osaclose(fid);
279 }
280
281 if (BKMIDAS[iwc].PID == -1)
282 return (90); /* could not read MIDASxy.PID */
283 }
284 else
285 {
286 cbuf[0] = BKMIDAS[iwc].UNIT[0];
287 cbuf[1] = BKMIDAS[iwc].UNIT[1];
288 cbuf[2] = '\0';
289 kk = ClientInit(BKMIDAS[iwc].HOST,cbuf,&BKMIDAS[iwc].CHAN,&reto);
290 if (kk == -1) return (4); /* no connection ... */
291 }
292 }
293
294 return (0);
295 }
296
297 /*
298
299 */
300
301 int outmail(flag,comline,bgindx,retstat)
302
303 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
304 .PURPOSE
305 send single line via file "FORGRxy.SBOX" to a `background' MIDAS
306 and wait for receive_box
307 .ALGORITHM
308 .RETURNS
309 = 0; all o.k.
310 = 1; background is busy
311 = 2; timed out
312 = 3; background was busy + ignored last command sent
313 = 4; problems with osx, `oserror' in retstat[0]
314 = 5; problems with file/interrupts, specific error in retstat[0]
315 = 7; socket connection was closed by command sent
316 = 9; nothing to wait for (flag = 2)
317 -----------------------------------------------------------------*/
318
319 int flag; /* IN: 1 = send command with/without wait
320 2 = only wait
321 3 = check, if finished */
322 char *comline; /* IN: command line to send */
323 int bgindx; /* IN: index in BKMIDAS */
324 int retstat[2]; /* OUT: return status (2 vals) from Background Midas */
325
326 {
327 int tmout, fp, send_more, reto;
328 int nn, kk, jj, wasbusy;
329
330 float rdum;
331 double ddum;
332
333 char frname[80];
334
335
336
337 /* insert unit into relevant files */
338
339 if (BKMIDAS[bgindx].METHOD == 'f') /* files/interrupt service */
340 {
341 XCONNECT.SBOX[XCONNECT.OFFS] = BKMIDAS[bgindx].UNIT[0];
342 XCONNECT.SBOX[XCONNECT.OFFS+1] = BKMIDAS[bgindx].UNIT[1];
343 XCONNECT.RBOX[XCONNECT.OFFS] = XCONNECT.SBOX[XCONNECT.OFFS];
344 XCONNECT.RBOX[XCONNECT.OFFS+1] = XCONNECT.SBOX[XCONNECT.OFFS+1];
345 XCONNECT.RBOX[XCONNECT.OFFR] = XCONNECT.MYUNIT[0]; /* return address.. */
346 XCONNECT.RBOX[XCONNECT.OFFR+1] = XCONNECT.MYUNIT[1];
347 }
348 send_more = 0;
349 wasbusy = 0;
350 retstat[1] = 0;
351
352
353 /* for flag=2, we wait on given unit until finished */
354
355 if (flag == 2)
356 {
357 if (BKMIDAS[bgindx].ACTIVE != 'N')
358 {
359 if (BKMIDAS[bgindx].METHOD == 'f') /* files/interrupt service */
360 {
361 w_loop:
362 fp = osaopen(XCONNECT.RBOX,READ); /* so, see if already done */
363 if (fp == -1)
364 {
365 OSY_SLEEP(100,1); /* wait .1 sec. */
366 goto w_loop;
367 }
368 else
369 goto read_rbox;
370 }
371 else
372 {
373 jj = ClientWait(BKMIDAS[bgindx].CHAN,-1); /* wait forever */
374 if (jj == -1) return (4);
375
376 goto read_socket;
377 }
378 }
379 return (9); /* nothing there to wait for */
380 }
381
382
383 /* for flag=3, we just check on given unit */
384
385 else if (flag == 3)
386 {
387 if (BKMIDAS[bgindx].METHOD == 'f') /* files/interrupt service */
388 {
389 if (BKMIDAS[bgindx].ACTIVE != 'N')
390 {
391 fp = osaopen(XCONNECT.RBOX,READ); /* see if already done */
392 if (fp != -1)
393 {
394 kk = osaread(fp,frname,72); /* read a line */
395 (void)osaclose(fp);
396 if (kk < 1) return (5);
397
398 if (strncmp(frname,"BUSY",4) == 0) /* check for busy status */
399 return (1);
400 }
401 else
402 return (1);
403 }
404 }
405 else
406 {
407 jj = ClientWait(BKMIDAS[bgindx].CHAN,0); /* just check */
408 if ((BKMIDAS[bgindx].ACTIVE != 'N') && (jj == 0))
409 return (1); /* NODATA means it's busy */
410 if (jj == -1) return (4);
411 }
412 BKMIDAS[bgindx].ACTIVE = 'N';
413 return (0);
414 }
415
416
417 /* for flag=4, we read termination status of last command */
418
419 else if (flag == 4)
420 {
421 BKMIDAS[bgindx].ACTIVE = 'N'; /* always clear entry */
422
423 if (BKMIDAS[bgindx].METHOD == 'f') /* files/interrupt service */
424 {
425 fp = osaopen(XCONNECT.RBOX,READ); /* open for reading */
426 if (fp == -1) return (5);
427
428 kk = osaread(fp,frname,72); /* read a line */
429 (void)osaclose(fp);
430 if (kk < 1)
431 {
432 *retstat = -2;
433 return (5);
434 }
435
436 /* now pull out the status info */
437
438 kk = CGN_INDEXC(frname,'=') + 2; /* Status = `status' */
439 if (kk > 1)
440 {
441 if ((frname[kk] != '0') || (frname[kk+1] != '\0'))
442 {
443 nn = CGN_CNVT(&frname[kk],1,2,retstat,&rdum,&ddum);
444 if (nn < 2)
445 {
446 *retstat = -3;
447 return (5);
448 }
449 }
450 else
451 *retstat = 0;
452 }
453 else
454 {
455 *retstat = -3;
456 return (5); /* strange Receive Box */
457 }
458 }
459
460 else
461 {
462 kk = ClientRead(BKMIDAS[bgindx].CHAN,frname,&jj,&reto);
463 if (kk != 0)
464 {
465 *retstat = reto;
466 return (4);
467 }
468
469 *retstat = jj;
470 }
471 return (0);
472 }
473
474
475
476 /* for flag=1, we send a command
477 check, if we're still using same background Midas */
478
479 if (BKMIDAS[bgindx].ACTIVE != 'N')
480 {
481 send_more = 1; /* mark that we still have to send */
482
483 if (BKMIDAS[bgindx].METHOD == 'f') /* files/interrupt service */
484 {
485 fp = osaopen(XCONNECT.RBOX,READ); /* so, see if already done */
486 if (fp == -1) return (1);
487
488 goto read_rbox;
489 }
490 else
491 {
492 jj = ClientWait(BKMIDAS[bgindx].CHAN,0);
493 if (jj == -1)
494 return (4);
495 else if (jj == 0)
496 return (1);
497 else
498 goto read_socket;
499 }
500 }
501
502
503 /* o.k. it's an idle guy, so make this entry active
504 and send via `osx' or create FORGRbk.SBOX */
505
506 send_mail:
507 BKMIDAS[bgindx].ACTIVE = 'A';
508
509 if (BKMIDAS[bgindx].METHOD == 's')
510 {
511 nn = BKMIDAS[bgindx].CHAN;
512 kk = ClientWrite(nn,comline,&reto); /* send via osx + return */
513 if (kk != 0)
514 {
515 *retstat = reto;
516 return (4);
517 }
518 if (BKMIDAS[bgindx].FLAG == 'N')
519 {
520 *retstat = 0;
521 return (wasbusy); /* if we send without wait, that's it... */
522 }
523
524 jj = ClientWait(BKMIDAS[bgindx].CHAN,BKMIDAS[bgindx].WAIT);
525 if (jj != 1) return (2); /* we waited the whole time */
526
527 read_socket:
528 BKMIDAS[bgindx].ACTIVE = 'N'; /* always clear entry */
529
530 kk = ClientRead(BKMIDAS[bgindx].CHAN,frname,&jj,&reto);
531 if (kk == -1)
532 {
533 *retstat = reto;
534 return (4);
535 }
536 else if (jj == 99) /* connection was closed by the command we sent */
537 return (7);
538
539 if (send_more == 1)
540 {
541 send_more = 0; /* clear flag again */
542 goto send_mail; /* we still have to send our stuff */
543 }
544
545 *retstat = jj;
546 return (0);
547 }
548
549
550 /* ASCII file + interrupt section */
551
552 kk = osfdelete(XCONNECT.RBOX); /* try to delete own old receive box */
553
554 fp = osaopen("dummy",WRITE); /* create send_box */
555 if (fp == -1)
556 {
557 *retstat = -1;
558 return(5);
559 }
560
561 osawrite(fp,comline,(int)strlen(comline));
562 (void)sprintf(frname,"%d",XCONNECT.MYPID); /* write own Pid */
563 osawrite(fp,frname,(int)strlen(frname));
564 (void)osaclose(fp);
565 osfrename("dummy",XCONNECT.SBOX); /* move to real name */
566
567
568
569 if (BKMIDAS[bgindx].PID != -1) osssend(BKMIDAS[bgindx].PID,SIGUSR1);
570 osscatch(SIGUSR2,intdum2);
571 if (BKMIDAS[bgindx].FLAG == 'N')
572 {
573 *retstat = 0;
574 return (wasbusy); /* if we send without wait, that's it... */
575 }
576
577 if (BKMIDAS[bgindx].WAIT != -1)
578 tmout = BKMIDAS[bgindx].WAIT;
579 else
580 tmout = 1; /* initial timeout for osswait */
581
582
583 /* now we wait until Backgr. Midas finishes */
584
585 wait:
586 YesSignal = 0;
587 osswait(SIGUSR2,tmout); /* wait max. `tmout' secs */
588
589 fp = osaopen(XCONNECT.RBOX,READ); /* open for reading */
590 if (fp == -1)
591 {
592 if ((BKMIDAS[bgindx].WAIT != -1) && (YesSignal == 0))
593 {
594 (void)osscatch(SIGUSR2,intdum2); /* in case, signal comes later */
595 return (2); /* we waited the whole time */
596 }
597 else
598 {
599 tmout = 5; /* new timeout for osswait */
600 (void)osscatch(SIGUSR2,intdum2);
601 goto wait;
602 }
603 }
604
605
606 /* read RBOX and get the return status */
607
608 read_rbox:
609 BKMIDAS[bgindx].ACTIVE = 'N'; /* clear entry */
610
611 kk = osaread(fp,frname,72); /* read a line */
612 (void)osaclose(fp);
613 if (kk < 1)
614 {
615 *retstat = -2;
616 return (5);
617 }
618
619 if (strncmp(frname,"BUSY",4) == 0) /* background MIDAS was busy */
620 {
621 if (send_more == 1)
622 {
623 wasbusy = 3;
624 send_more = 0; /* clear flag again */
625 goto send_mail; /* we still have to send our stuff */
626 }
627 else
628 return (1);
629 }
630
631
632 if (send_more == 1)
633 {
634 send_more = 0; /* clear flag again */
635 goto send_mail; /* we still have to send our stuff */
636 }
637
638
639 /* pull out status info */
640
641 kk = CGN_INDEXC(frname,'=') + 2; /* Status = `status' */
642 if (kk > 1)
643 {
644 if ((frname[kk] != '0') || (frname[kk+1] != '\0'))
645 {
646 nn = CGN_CNVT(&frname[kk],1,2,retstat,&rdum,&ddum);
647 if (nn < 2)
648 {
649 *retstat = -3;
650 return (5);
651 }
652 }
653 else
654 *retstat = 0;
655 }
656 else
657 {
658 *retstat = -3;
659 return (5); /* strange Receive Box */
660 }
661
662
663 return (wasbusy);
664 }
665