Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/sp_spmd.c

raw
1 /*---------------------------------------------------------------------
2 * $Date: 2009-08-18 15:23:20 $ $Revision: 1.4 $
3 *---------------------------------------------------------------------
4 * Copyright (c) 1992, Visual Edge Software Ltd.
5 *
6 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
7 * distribute this software and its documentation for any purpose
8 * and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Visual Edge Software not be
12 * used in advertising or publicity pertaining to distribution of
13 * the software without specific, written prior permission. The year
14 * included in the notice is the year of the creation of the work.
15 *---------------------------------------------------------------------
16 * Motorola additions to make the subprocess management more amenable to
17 * an application doing it's own signal handling and/or child process
18 * creation (esp. in the "inocuous" cases of calls to popen(3S), system(3S)
19 * or getcwd(3C) are contained in "ifdef SPMD conditionals.
20
21 .VERSION
22 090818 last modif
23
24 *--------------------------------------------------------------------*/
25
26 #if defined(__FreeBSD__) || defined(__Darwin__) || defined(Cygwin)
27 #define ENOMSG 45
28 #endif
29
30
31 /*--- include files ---*/
32 #include <unistd.h>
33
34 #include <stdlib.h>
35 #include <limits.h>
36
37 #include "uxproto.h"
38 #include "version.h"
39 #include "subproc.h"
40 #include "sp_spmd.h"
41 #include "sp_pty.h"
42
43
44
45
46 extern void UxMarkFileCloseOnExec();
47
48 /*--- global variables ---*/
49
50 spmd_info_t UxSpmd = { -1, 0, 0, {0}};
51
52 /*--- file global variables ---*/
53 M_FILE_VERSION("$Header")
54 int UxSpmdSubprocCount;
55 spmd_pid_pty_t UxSpmdStates[MAX_SUBPROC];
56 long UxSpmdInterval = LONG_INTERVAL;
57 int MultiByteMode;
58 static int TimeOutId = -1;
59
60 /*--- forward declaration of static functions ---*/
61 static void SpmdCiao UXPROTO((int sig));
62 static void SpmdDeadProc UXPROTO((int i));
63 static void SpmdSigHandler UXPROTO((int sig));
64 static int SpmdReadPtys UXPROTO((void));
65 static void SpmdMain UXPROTO((void));
66 static void SpmdSendExitMsg UXPROTO((void));
67
68
69 /*----------------------------------------------------------------------
70 * NAME: <static int IsSpmdAlive()>
71 * DESCRIPTION: <Verifies if the Spmd is still alive>
72 * PARAMETERS: none
73 * RETURN: True/False
74 * EXT REFS: UxSpmd
75 * EXT EFFECTS:
76 * ASSUMPTIONS:
77 * REVISIONS: 25/04/93
78 *--------------------------------------------------------------------*/
79 static int IsSpmdAlive (tmsgp)
80 #if defined(__FreeBSD__) || defined(__Darwin__) || defined(Cygwin)
81 struct mymsg *tmsgp;
82 #else
83 struct msgbuf *tmsgp;
84 #endif
85
86 {
87 if (tmsgp && (tmsgp->mtype == SPMD_EXIT)) {
88 return False;
89 }
90
91 if ((UxSpmd.spmd_pc_msqid == -1) ||
92 (UxSpmd.spmd_cp_msqid == -1) ||
93 (UxSpmd.spmd_pid == -1)) {
94 return False;
95 }
96 return True;
97 }
98
99
100 /*----------------------------------------------------------------------
101 * NAME: <int UxExecSubprocess(sp, args)>
102 * DESCRIPTION: <Send a message to the daemon to create and execute the
103 process>
104 * PARAMETERS: SubprocInfo_t *sp - new process
105 char *args - command line
106 * RETURN: ERROR/NO_ERROR
107 * EXT REFS: UxSpmd
108 * EXT EFFECTS: UxSpArray via sp
109 * ASSUMPTIONS:
110 * REVISIONS: 05/01/93 fix3810 clean-up
111 * dd/mm/yy fix# fix_description
112 *--------------------------------------------------------------------*/
113 int UxExecSubprocess(sp, args)
114 SubprocInfo_t *sp;
115 char *args;
116
117 {
118 int cmdlen;
119 char *process;
120 spmd_create_req req;
121 spmd_create_rsp *rsp;
122
123 process = sp->process;
124 req.mtype = SPMD_CREATE;
125 req.echo = sp->echo;
126 req.cmd[0] = '\0'; /*EOS*/
127
128 if (args == NULL) args = "";
129 sprintf(req.cmd, "%s %s", process, args);
130
131 cmdlen = strlen(req.cmd); /* also used in msgsnd() */
132
133 if (-1 == msgsnd(UxSpmd.spmd_pc_msqid, &req,
134 cmdlen+sizeof(req.echo), IPC_NOWAIT))
135 {
136 perror("exec_subprocess: can not send SPMD_CREATE to daemon");
137 return(ERROR);
138 }
139
140 /* now we must wait for the daemon to do it's magic ... */
141 if (UxSpmdReadMsg(SPMD_RSP) == ERROR) {
142 return(ERROR);
143 }
144
145 rsp = (spmd_create_rsp *)UxSpmd.spmd_rmsgp;
146
147 if (rsp->response == NACK) {
148 UxStandardError("Cannot start child process %s \n", process);
149 return(ERROR);
150 }
151
152 sp->handle = rsp->handle;
153 sp->pid = rsp->pid;
154 return(NO_ERROR);
155 }
156
157
158
159 /*----------------------------------------------------------------------
160 * NAME: <int UxSpmdReadMsg (msgtyp)>
161 * DESCRIPTION: <check for and process any messages arriving from the
162 * the Sub-Process Manager daemon
163 *
164 * We arrange for this routine to be called ``periodically'' to see if
165 * work has arrived from the daemon. Each time it is called, we
166 * schedule it to be called again after an UxSpmdInterval.
167 * We use a longer UxSpmdInterval when no messages are found
168 * than when there are messages, assuming activity may be bursty.
169 *
170 * Also called from UxExecSubprocess explicitly waiting for SPMD_RSP
171 *
172 * Note: The timeout interval UxSpmdInterval could be adjusted in
173 * many ways, to account for number of processes, or rate of activity;
174 * instead of a timeout, XtWorkProcs could be used.
175 *
176 * The periodic use of this function is in the application (parent) process
177 * so we can use XtAppAddTimeOut to schedule the calls.>
178 * PARAMETERS: long msgtyp - message type we are waiting for
179 * RETURN: int - ERROR or 0
180 * EXT REFS: UxSpArray, UxSpmd
181 * EXT EFFECTS: UxSpmd
182 * ASSUMPTIONS:
183 * REVISIONS: 05/01/93 fix3810 clean-up
184 dd/mm/yy fix# fix_description
185 *--------------------------------------------------------------------*/
186 int UxSpmdReadMsg (msgtyp)
187 long msgtyp; /* zero -> called from timeout
188 * non-zero -> explicit rsp
189 */
190 {
191 int i, rc, msgflg;
192 extern int errno;
193 int msgfound = 0;
194
195 msgflg = msgtyp ? 0 : IPC_NOWAIT;
196
197 UxSpmd.spmd_rmsgtyp = 0; /* clear buffer */
198
199 while ((rc=msgrcv(UxSpmd.spmd_cp_msqid,
200 UxSpmd.spmd_rmsgp, MSGMAX, msgtyp, msgflg))
201 != -1)
202 {
203 msgfound = 1;
204 switch (UxSpmd.spmd_rmsgp->mtype)
205 {
206 case SPMD_OUTPUT: /* output from child process */
207 {
208 spmd_output_t *msg = (spmd_output_t *)UxSpmd.spmd_rmsgp;
209 UxSpmd.spmd_rmsgrp = msg->output;
210 UxSpmd.spmd_rmsgsiz = rc - sizeof(msg->pid);
211 for (i=0; i<MAX_SUBPROC; i++) {
212 if ((UxSpArray[i]) &&
213 (UxSpArray[i]->pid == msg->pid))
214 {
215 UxOutputHandler(UxSpArray[i]);
216 break;
217 }
218 }
219 ASSERT (i<MAX_SUBPROC); /* should not fall off loop */
220 break;
221 }
222 case SPMD_DIED: /* A managed process has died */
223 UX_CLOSE_COMMUNICATION();
224 break;
225 case SPMD_RSP:
226 #ifdef DEBUG
227 if (!msgtyp) {
228 UxInternalError(
229 "spmd_read_msg: timeout received RSP");
230 }
231 #endif /*DEBUG*/
232 return (0);
233 /*NOTREACHED*/
234 break;
235
236 default: /* Either garbage or wrong directional msg */
237 #ifdef DEBUG
238 UxInternalError("spmd_read_msg: protocol error");
239 #endif /*DEBUG*/
240 break;
241 }
242 }
243 if (rc == -1) {
244 if (!IsSpmdAlive(NULL)) {
245 SpmdCiao(0);
246 }
247
248 if (errno != ENOMSG) {
249 /* nop body; skip interrupts */ ;
250 perror("Subprocess manager: msgrcv failed");
251 #ifdef DEBUG
252 exit (errno);
253 /*NOTREACHED*/
254 #endif /*DEBUG*/
255 return(ERROR);
256 }
257 }
258
259 /*-----------------------------------------------------
260 * If called from the timer callback, re-arm.
261 *-----------------------------------------------------*/
262 if (!msgtyp) {
263 /* Assume that activity means there may be more.
264 */
265 UxSpmdInterval = msgfound ? SHORT_INTERVAL : LONG_INTERVAL;
266 TimeOutId = XtAppAddTimeOut(UxAppContext,
267 UxSpmdInterval,
268 (XtTimerCallbackProc)UxSpmdReadMsg, 0);
269 }
270 return (0);
271 }
272
273 /*----------------------------------------------------------------------
274 * NAME: <static int ResetSpmdSubprocs()>
275 * DESCRIPTION: <Resets UxSpmdStates structure>
276 * PARAMETERS: none
277 * RETURN: void
278 * EXT REFS: UxSpmdStates, UxSpmdSubprocCount
279 * EXT EFFECTS: UxSpmdStates, UxSpmdSubprocCount
280 * ASSUMPTIONS:
281 * REVISIONS: 25/04/93
282 *--------------------------------------------------------------------*/
283 static void ResetSpmdSubprocs ()
284
285 {
286 int i;
287
288 for (i=0;i<MAX_SUBPROC;i++) {
289 UxSpmdStates[i].pid = UxSpmdStates[i].pty =
290 UxSpmdStates[i].exit_status = -1;
291 }
292 UxSpmdSubprocCount = 0;
293 }
294 /*----------------------------------------------------------------------
295 * NAME: <static void SpmdMain()>
296 * DESCRIPTION: <This is the main routine of the manager daemon itself,
297 * called after the manager is fork/exec'd.>
298 * PARAMETERS: none
299 * RETURN: void
300 * EXT REFS: UxSpmdStates, UxSpmdSubprocCount, UxSpmd
301 * EXT EFFECTS: UxSpmdStates, UxSpmdSubprocCount, UxSpmd
302 * ASSUMPTIONS:
303 * REVISIONS: 05/01/93 fix3810 clean-up
304 * 25/03/93 fix4037 to deal with multibyte
305 * 29/04/93 fix4207
306 *--------------------------------------------------------------------*/
307 static void SpmdMain()
308
309 {
310 int i;
311 int rc = 0;
312
313 #if defined(__FreeBSD__) || defined(__Darwin__) || defined(Cygwin)
314 struct mymsg *tmsgp;
315
316 #else
317 struct msgbuf *tmsgp;
318 #endif
319
320 extern int errno;
321
322
323 #ifdef RUNTIME
324 /*-----------------------------------------------------
325 * In DESIGN_TIME, UIM/X properly treats all these signals.
326 *-----------------------------------------------------*/
327 signal(SIGINT, SpmdCiao); /* to handle Ctrl-C */
328 signal(SIGQUIT, SpmdCiao);
329 #endif
330 signal(SIGALRM, SpmdSigHandler); /* arm the signal catcher */
331
332
333 #if defined(__FreeBSD__) || defined(__Darwin__) || defined(Cygwin)
334 if ( !(tmsgp=(struct mymsg *)UxMalloc(SIZEOFMAXMSGBUF))) {
335 #else
336 if ( !(tmsgp=(struct msgbuf *)UxMalloc(SIZEOFMAXMSGBUF))) {
337 #endif
338
339 perror("Subprocess manager: memory allocation failed");
340 exit (errno);
341 }
342 /*---------------------------------------------------------
343 * Note: on sun4 MB_CUR_MAX doesn't work properly. It is
344 * O which is wrong but doesn't break this code. It
345 * works for those with proper international sun os's.
346 *---------------------------------------------------------*/
347 MultiByteMode = (MB_CUR_MAX > 1) ? 1 : 0; /* Set if in multibyte mode */
348
349 ResetSpmdSubprocs ();
350
351
352 /*
353 * Main processing loop is to wait on the message queue from the parent.
354 * Note: will be extended to select on this queue plus all managed
355 * sub-process pty's
356 */
357 for(;;) {
358
359 /* Read a message from the application
360 * (more frequently if we have subprocs)
361 */
362
363 alarm ((UxSpmdSubprocCount ? 1 : 5));
364 while (SpmdReadPtys() &&
365 (rc=msgrcv(UxSpmd.spmd_pc_msqid,tmsgp, MSGMAX, 0, 0))
366 == -1 &&
367 errno == EINTR)
368 {
369 if (!IsSpmdAlive(tmsgp)) {
370 SpmdCiao(0);
371 }
372 /* nop body; skip interrupts */ ;
373 }
374 alarm(0);
375 if (rc == -1) {
376 if (!IsSpmdAlive(tmsgp)) {
377 SpmdCiao(0);
378 }
379 perror("UIM/X: spmd_main: msgrcv failed");
380 #ifdef DEBUG
381 /* check out the error ... could be fatal */
382 exit (errno);
383 #endif /*DEBUG*/
384 }
385 if (!IsSpmdAlive(tmsgp)) {
386 SpmdCiao(0);
387 }
388
389 /*-----------------------------------------------------
390 * Process the message.
391 *-----------------------------------------------------*/
392 switch (tmsgp->mtype) {
393 case SPMD_CREATE: /* create a sub-process */
394 {
395 int new_handle, new_pid;
396 spmd_create_req *req = (spmd_create_req *)tmsgp;
397 spmd_create_rsp msg;
398 req->cmd[rc-sizeof(req->echo)] = '\0'; /* */
399
400 /* Disable the SIGCHLD signal while we are
401 creating the process because it could
402 terminate and the daemon wouldn't know
403 its pid.
404 */
405 signal(SIGCHLD, SIG_DFL); /* disable */
406 if((i=UX_SPMD_EXEC_SUBPROCESS(req->cmd, req->echo,
407 &new_handle, &new_pid))
408 == ERROR) {
409 msg.response = NACK;
410 } else {
411 msg.response = ACK;
412 for (i = 0; i < MAX_SUBPROC; i++)
413 {
414 if (UxSpmdStates[i].pid == -1)
415 {
416 break;
417 }
418 }
419 UxSpmdStates[i].pid = new_pid;
420 UxSpmdStates[i].pty = new_handle;
421 msg.handle = new_handle;
422 msg.pid = new_pid;
423 UxSpmdSubprocCount++;
424 }
425 /* Now that the process is created, we can rearm the
426 signal and catch all the children that died during
427 the creation of the process.
428 */
429 signal(SIGCHLD, SpmdSigHandler); /* arm the signal */
430
431 msg.mtype = SPMD_RSP;
432 while ( (msgsnd(UxSpmd.spmd_cp_msqid, &msg, sizeof(spmd_create_rsp)-sizeof(msg.mtype), 0) == -1) && (errno == EINTR) ) {
433 /* nop body */ ;
434 }
435 break;
436 }
437 case SPMD_INPUT: /* input for a process */
438 {
439 spmd_input_t *msg = (spmd_input_t *)tmsgp;
440 msg->input[rc-sizeof(msg->pid)] = '\0'; /* */
441 for (i=0; i < MAX_SUBPROC; i++) {
442 if (UxSpmdStates[i].pid == msg->pid) {
443 break;
444 }
445 }
446
447 if (i == MAX_SUBPROC) { /* App. gave us a bad pid */
448 #ifdef DEBUG
449 fprintf(stderr, ">>(%d)) [spmd_main] SPMD_INPUT received BUT %d is a BAD PID !\n", getpid(), msg->pid);
450 #endif /*DEBUG*/
451
452 break; /* silently ignore */
453 }
454 if (UxSpmdStates[i].exit_status == -1) {
455 /* not dead yet ? */
456 ASSERT (UxSpmdStates[i].pty >= 0);
457 write(UxSpmdStates[i].pty,
458 msg->input, rc-sizeof(msg->pid));
459 }
460
461 break;
462 }
463 default: /* garbage or wrong directional msg */
464 #ifdef DEBUG
465 fprintf(stderr,"UIM/X: spmd_main: protocol error\n");
466 #endif /*DEBUG*/
467
468 break;
469 }
470 tmsgp->mtype = 0; /* A.Nil. Legal value */
471 }
472 }
473
474
475 /*----------------------------------------------------------------------
476 * NAME: <int UxSpmdCreateDaemon()>
477 * DESCRIPTION: <Create the Sub-Process Manager Daemon.
478 * set up the messages queues and message buffers.>
479 * PARAMETERS: none
480 * RETURN: returns: 0 on success, non-0 on error/failure
481 * EXT REFS: UxSpmd
482 * EXT EFFECTS: spmd_info_t struct is filled in with relevant info.
483 UxSpmd
484 * ASSUMPTIONS:
485 * REVISIONS: 05/01/93 fix3810 clean-up
486 23/04/93 fix4181 handle Ctrl-C at runtime
487 dd/mm/yy fix# fix_description
488 *--------------------------------------------------------------------*/
489 int UxSpmdCreateDaemon()
490
491 {
492 void SpmdMain();
493
494 /* create directional message queues (one each direction) */
495 UxSpmd.spmd_pc_msqid = msgget(IPC_PRIVATE,IPC_CREAT|S_IRUSR|S_IWUSR);
496 UxSpmd.spmd_cp_msqid = msgget(IPC_PRIVATE,IPC_CREAT|S_IRUSR|S_IWUSR);
497
498 if ( (UxSpmd.spmd_cp_msqid == -1) || (UxSpmd.spmd_pc_msqid == -1) )
499 {
500 #ifdef DEBUG
501 perror("spmd_create_daemon: msgget failed");
502 #endif /*DEBUG*/
503 return(ERROR);
504 }
505
506 #ifdef DEBUG
507
508 fprintf(stderr,
509 "spmd_create_daemon c->p msqid = 0x%x, p->c msqid = 0x%x\n",
510 UxSpmd.spmd_cp_msqid, UxSpmd.spmd_pc_msqid);
511 #endif /*DEBUG*/
512
513 /* allocate the receive-message buffer */
514
515 #if defined(__FreeBSD__) || defined(__Darwin__) || defined(Cygwin)
516 if ( !(UxSpmd.spmd_rmsgp=(struct mymsg *)UxMalloc(SIZEOFMAXMSGBUF)) )
517 #else
518 if ( !(UxSpmd.spmd_rmsgp=(struct msgbuf *)UxMalloc(SIZEOFMAXMSGBUF)) )
519 #endif
520
521 {
522 #ifdef DEBUG
523 perror("spmd: spmd_create_daemon: UxMalloc failed");
524 #endif /*DEBUG*/
525
526 return(ERROR);
527 }
528
529 switch (UxSpmd.spmd_pid=fork()) {
530 case -1: /* forking error */
531 #ifdef DEBUG
532 perror("spmd: spmd_create_daemon: fork failed");
533 #endif /*DEBUG*/
534
535 return(ERROR);
536
537 case 0: /* child: the sub-process manager daemon */
538 SpmdMain();
539 /*NOTREACHED*/
540 break;
541
542 default: /* parent: the application */
543 /*-------------------------------------------
544 * Start reading the Spmd msg queue.
545 *-------------------------------------------*/
546 TimeOutId = XtAppAddTimeOut(UxAppContext,
547 UxSpmdInterval,
548 (XtTimerCallbackProc)UxSpmdReadMsg, 0);
549 break;
550 }
551
552 return (0);
553 }
554
555
556 /*----------------------------------------------------------------------
557 * NAME: <static void SpmdSigHandler(sig)>
558 * DESCRIPTION: <Handler for signals of interest to subproc manager.
559 * SIGCHLD: - mark dying process as dead in state table
560 * SIGALRM: - used to induce EINTR break from blocked msgrcv(),
561 * check that our parent process still owns us.>
562 * PARAMETERS: int sig -- signal being handled
563 * RETURN: void
564 * EXT REFS: UxSpmdStates, UxSpmdSubprocCount
565 * EXT EFFECTS: UxSpmdStates
566 * ASSUMPTIONS:
567 * REVISIONS: 05/01/93 fix3810 clean-up
568 dd/mm/yy fix# fix_description
569 *--------------------------------------------------------------------*/
570 static void SpmdSigHandler(sig)
571 int sig;
572
573 {
574 int status, pid, i;
575
576 switch (sig) {
577 case SIGCHLD:
578 while ( (pid=wait(&status)) == -1 && (errno == EINTR) ) {
579 /* nop body */ ;
580 }
581 if (pid == -1) {
582 perror("subprocess manager: wait failed");
583 }
584
585 for (i=0; i < MAX_SUBPROC; i++)
586 {
587 /* Mark the process dead. */
588 if (UxSpmdStates[i].pid == pid)
589 {
590 ASSERT (UxSpmdStates[i].exit_status == -1);
591 #ifdef DEBUG
592 fprintf(stderr,
593 "<<(%d) [spmd_sig_handler] pid %d died, status %d\n",
594 getpid(), pid, status);
595 #endif /*DEBUG*/
596 UxSpmdStates[i].exit_status = status;
597 break;
598 }
599 }
600
601 /*
602 * Don't check for falling off the end of the loop because
603 * a forked child would only be placed into the table if the
604 * handshake, pty's, exec, etc. all worked.
605 */
606 break;
607
608 case SIGALRM: /* used merely to break a blocked msgrcv(). */
609 if (getppid() == 1) { /* have we been adopted by init ? */
610 SpmdCiao(0);
611 }
612 alarm ((UxSpmdSubprocCount?1:5));
613 break;
614 default:
615 fprintf(stderr,"Unexpected signal caught. Impossible!\n");
616 break;
617 }
618
619 signal(sig, SpmdSigHandler); /* re-arm the signal catcher */
620 }
621
622
623
624 /*----------------------------------------------------------------------
625 * NAME: <static int SpmdReadPtys()>
626 * DESCRIPTION: <Read input from each of the ptys for active processes in turn,
627 * passing the input on to the application in a message.>
628 * PARAMETERS: none
629 * RETURN: int - always 1
630 * EXT REFS: UxSpmdSubprocCount, UxSpmdStates, UxSpmd
631 * EXT EFFECTS: globals or other things altered
632 * ASSUMPTIONS:
633 * REVISIONS: 05/01/93 fix3810 clean-up
634 * 02/02/93 fix3890 change size of message sent in msgsnd
635 * 25/03/93 fix4037 to deal with multibyte
636 * sept 93 fix4491 sun4 MB_CUR_MAX workaround.
637 *--------------------------------------------------------------------*/
638
639 static int SpmdReadPtys()
640
641 {
642 int i;
643 int n;
644 wchar_t wc_buffer[MB_LEN_MAX * MSGMAX];
645 void SpmdDeadProc();
646
647 if (!IsSpmdAlive(NULL)) {
648 SpmdCiao(0);
649 }
650
651 /* while there are children ... */
652
653 for (i=0; UxSpmdSubprocCount && (i < MAX_SUBPROC); i++)
654 {
655 if (UxSpmdStates[i].pid != -1)
656 {
657 /* slot is associated with a process */
658 int rec = 0, wanted, num;
659 spmd_output_t msg;
660 char *buf = msg.output;
661
662 /* subtract 1 to leave space for '\0' */
663 if (MultiByteMode)
664 {
665 #ifdef sun4
666 wanted = sizeof(msg.output) - (MB_CUR_MAX > 0 ? MB_CUR_MAX : 1) - 1;
667 #else
668 wanted = sizeof(msg.output) - MB_CUR_MAX - 1;
669 #endif
670
671 }
672 else
673 {
674 wanted = sizeof(msg.output) - 1;
675 }
676
677 /* read must be non-blocking */
678 buf[0] = '\0';
679
680 while ( wanted &&
681 (num=read(UxSpmdStates[i].pty, buf, wanted)) )
682 {
683 if (num==-1) {
684 if (errno == EINTR)
685 {
686 continue;
687 }
688 break;
689 }
690 wanted -= num;
691 buf[num] = '\0'; /* tail the string */
692 buf += num; /* buffer pointer bump */
693 rec += num; /* increment received counter */
694 }
695
696 /* In multibyte mode, be sure that we have a
697 * complete character, so that we pass meaningful
698 * strings along to the application.
699 */
700 if (MultiByteMode)
701 {
702 /* Keep reading 1 char at a time until
703 * we get a complete mbs string.
704 */
705 while (mbstowcs(wc_buffer, msg.output, rec)
706 == -1)
707 {
708 n = read(UxSpmdStates[i].pty, buf, 1);
709 if (n == -1)
710 {
711 if (errno == EINTR ||
712 errno == EAGAIN)
713 {
714 continue; /* while */
715 } else {
716 /* Read really failed.
717 * Give up.
718 */
719 break;
720 }
721 } else {
722 rec += n;
723 buf[n] = '\0';
724 buf += n;
725 }
726 }
727 }
728
729 /* send output from process to the application */
730 if (rec)
731 {
732 msg.mtype = SPMD_OUTPUT;
733 msg.pid = UxSpmdStates[i].pid;
734
735 /* send SPMD_OUTPUT message to application */
736
737 while ((msgsnd(UxSpmd.spmd_cp_msqid,
738 &msg,
739 rec + sizeof(msg.pid),
740 0) == -1)
741 && (errno == EINTR) )
742 {
743 /* nop body */ ;
744 }
745 }
746 if (UxSpmdStates[i].exit_status != -1)
747 {
748 /* Process has died. */
749 SpmdDeadProc(i);
750 }
751 }
752 }
753 return(1); /* spmd_main() relies on this always succeeding :-) */
754 }
755
756
757 /*----------------------------------------------------------------------
758 * NAME: <static void SpmdDeadProc(i)>
759 * DESCRIPTION: <The speciefied process has died.
760 * Send a message to the application to this effect.
761 * Remove the process from the subprocess table>
762 * PARAMETERS: int i -- subprocess table index
763 * RETURN: void
764 * EXT REFS: UxSpmdStates, UxSpmd, UxSpmdSubprocCount
765 * EXT EFFECTS: UxSpmdStates, UxSpmdSubprocCount
766 * ASSUMPTIONS:
767 * REVISIONS: 05/01/93 fix3810 clean-up
768 dd/mm/yy fix# fix_description
769 *--------------------------------------------------------------------*/
770 static void SpmdDeadProc(i)
771 int i;
772
773 {
774 spmd_dead_msg msg;
775
776 msg.mtype = SPMD_DIED;
777 ASSERT((UxSpmdStates[i].pid != -1)&&(UxSpmdStates[i].exit_status != -1));
778
779 msg.pid = UxSpmdStates[i].pid;
780 msg.status = UxSpmdStates[i].exit_status;
781
782 /* generate the SPMD_DIED message and send it to the application */
783 while ( (msgsnd(UxSpmd.spmd_cp_msqid, &msg,
784 sizeof(msg)-sizeof(msg.mtype), 0) == -1) &&
785 (errno == EINTR) )
786 {
787 /* nop body */ ;
788 }
789
790 /* Assume the PTY is done with.
791 * This guy is dead. Bury him and forget him !
792 */
793
794 (void) close(UxSpmdStates[i].pty);
795 UxSpmdStates[i].pid = UxSpmdStates[i].pty
796 = UxSpmdStates[i].exit_status = -1;
797 UxSpmdSubprocCount--;
798 ASSERT(UxSpmdSubprocCount >= 0);
799 }
800 /*----------------------------------------------------------------------
801 * NAME: <void SpmdSendExitMsg>
802 * DESCRIPTION: <Send a message to the SPMD for cleaning up and exiting>
803 * PARAMETERS: none
804 * RETURN: void
805 * EXT REFS: UxSpmd
806 * EXT EFFECTS:
807 * ASSUMPTIONS:
808 * REVISIONS: 23/04/93 fix4181 handle Ctrl-C at runtime
809 * dd/mm/yy fix# fix_description
810 *--------------------------------------------------------------------*/
811 static void SpmdSendExitMsg ()
812
813 {
814 int cmdlen;
815 spmd_dead_msg req;
816 int status, pid;
817
818 req.mtype = SPMD_EXIT;
819 req.pid = 0;
820 req.status = -1;
821 cmdlen = 1;
822
823 if (-1 == msgsnd(UxSpmd.spmd_pc_msqid, &req,
824 cmdlen+sizeof(req.pid), IPC_NOWAIT)) {
825 return;
826 }
827
828 /*-----------------------------------------------------
829 * Wait for the SPMD to die before handing control
830 * back to the main application.
831 *-----------------------------------------------------*/
832 while ( (pid=wait(&status)) == -1 && (errno == EINTR) ) {
833 /* nop body */ ;
834 }
835 }
836 /*----------------------------------------------------------------------
837 * NAME: <void UxDestroySpmd()>
838 * DESCRIPTION: <The application disappeared. We follow suit.
839 * Try and tidy up the message queues and destroy the SPMD deamon.>
840 * PARAMETERS: none
841 * RETURN: void
842 * EXT REFS: UxSpmd
843 * EXT EFFECTS: globals or other things altered
844 * ASSUMPTIONS:
845 * REVISIONS: 23/04/93 fix4181 handle Ctrl-C at runtime
846 * dd/mm/yy fix# fix_description
847 *--------------------------------------------------------------------*/
848 void UxDestroySpmd()
849
850 {
851 /*-----------------------------------------------------
852 * Now don't you just love looking at this code ?
853 * Look no further cause bugs you shall find none :-)
854 * but just in case you do, just send us the fix
855 * report instead of a bug report :-)
856 * After all, this code was mostly written by one of
857 * you collegues.
858 *-----------------------------------------------------*/
859
860 /*-----------------------------------------------------
861 * If the SPMD is not running, nothing to do. Just return.
862 *-----------------------------------------------------*/
863 if (!IsSpmdAlive(NULL)) {
864 return;
865 }
866
867 UxMarkFileCloseOnExec(1);
868
869 if (TimeOutId != -1) {
870 XtRemoveTimeOut (TimeOutId);
871 TimeOutId = -1;
872 }
873
874 /*-----------------------------------------------------
875 * When the parent process is about to die, send a msg
876 * to the SPMD so that it does its cleanup and exits.
877 *-----------------------------------------------------*/
878 SpmdSendExitMsg ();
879
880 /*-----------------------------------------------------
881 * In case the parent crashes or receives an interrupt
882 * signal, we are not guaranteed that the SPMD main loop
883 * will still be called so we force a cleanup of the msg
884 * queues at the application level.
885 *-----------------------------------------------------*/
886 if ((UxSpmd.spmd_pid != 0) && (UxSpmd.spmd_pid != -1)) {
887 SpmdCiao(0);
888 }
889
890 UxSpmd.spmd_pid = -1;
891
892 }
893 /*----------------------------------------------------------------------
894 * NAME: <static void SpmdCiao()>
895 * DESCRIPTION: <The application disappeared. We follow suit.
896 * Try and tidy up the message queues. We can't do anything if it fails so
897 * we don't even check the return status>
898 * PARAMETERS: none
899 * RETURN: void
900 * EXT REFS: UxSpmd
901 * EXT EFFECTS: globals or other things altered
902 * ASSUMPTIONS:
903 * REVISIONS: 05/01/93 fix3810 cleanup
904 * dd/mm/yy fix# fix_description
905 *--------------------------------------------------------------------*/
906 static void SpmdCiao(sig)
907 int sig;
908
909 {
910
911 /*-----------------------------------------------------
912 * Upon exiting, clean the message queues.
913 *-----------------------------------------------------*/
914
915 if ((UxSpmd.spmd_pid != -1) && (UxSpmd.spmd_pc_msqid != -1)) {
916 (void)msgctl(UxSpmd.spmd_pc_msqid, IPC_RMID,NULL);
917 UxSpmd.spmd_pc_msqid = -1;
918 }
919
920 if ((UxSpmd.spmd_pid != -1) && (UxSpmd.spmd_cp_msqid != -1)) {
921 (void)msgctl(UxSpmd.spmd_cp_msqid, IPC_RMID,NULL);
922 UxSpmd.spmd_cp_msqid = -1;
923 }
924
925 /*-----------------------------------------------------
926 * The child process will kill all remaining subbprocess
927 * and exit itself.
928 *-----------------------------------------------------*/
929 if (UxSpmd.spmd_pid == 0) {
930 UxMarkFileCloseOnExec(1);
931 UxKillAllSubprocs ();
932 UxSpmd.spmd_pid = -1;
933 exit(0);
934 }
935 }
936
937
938 /*----------------------------------------------------------------------
939 * NAME: <int UxWhatSpIsDead(sp_status)>
940 * DESCRIPTION: <Looking at the dead message, it finds out the subprocess
941 * that died.>
942 * PARAMETERS: int *sp_status - returned status of the subprocess
943 * RETURN: int - pid of dead subprocess
944 * EXT REFS: UxSpmd
945 * EXT EFFECTS: globals or other things altered
946 * ASSUMPTIONS:
947 * REVISIONS: 08/01/93 fix3810 creation
948 * dd/mm/yy fix# fix_description
949 *--------------------------------------------------------------------*/
950 int UxWhatSpIsDead(sp_status)
951 int *sp_status;
952
953 {
954 int pid;
955
956 spmd_dead_msg *msg = (spmd_dead_msg *)UxSpmd.spmd_rmsgp;
957 pid = msg->pid;
958 *sp_status = msg->status;
959 return(pid);
960 }