File: cons.c

package info (click to toggle)
pvm 3.4beta7-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 5,256 kB
  • ctags: 5,938
  • sloc: ansic: 66,147; makefile: 1,446; fortran: 631; sh: 424; csh: 70; asm: 37
file content (586 lines) | stat: -rw-r--r-- 12,297 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

static char rcsid[] =
	"$Id: cons.c,v 1.19 1998/01/28 23:03:40 pvmsrc Exp $";

/*
 *         PVM version 3.4:  Parallel Virtual Machine System
 *               University of Tennessee, Knoxville TN.
 *           Oak Ridge National Laboratory, Oak Ridge TN.
 *                   Emory University, Atlanta GA.
 *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
 *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
 *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
 *                   (C) 1997 All Rights Reserved
 *
 *                              NOTICE
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted
 * provided that the above copyright notice appear in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * Neither the Institutions (Emory University, Oak Ridge National
 * Laboratory, and University of Tennessee) nor the Authors make any
 * representations about the suitability of this software for any
 * purpose.  This software is provided ``as is'' without express or
 * implied warranty.
 *
 * PVM version 3 was funded in part by the U.S. Department of Energy,
 * the National Science Foundation and the State of Tennessee.
 */

/*
 *	cons.c
 *
 *	PVM-console task.  Gives the user a shell to manage the virtual
 *	machine and other tasks.
 *
$Log: cons.c,v $
 * Revision 1.19  1998/01/28  23:03:40  pvmsrc
 * Fixed tracing bogusness!
 * 	- host add / del notifies for tracing were getting scrunched by
 * 		console host add notify, etc...
 * 	- moved message / notify tag constants to job.h and USED THEM.
 * 	- initialized nextjob properly...
 * 	- added new "joboffset" global in cons.c for dumping more
 * 		reasonable job numbers to user.
 * (Spanker=kohl)
 *
 * Revision 1.18  1997/12/29  19:38:17  pvmsrc
 * 	Deleted linux/time.h include.  Don't need it on Redhat 4.2 or 5.0.
 * (Spanker=phil)
 *
 * Revision 1.17  1997/12/01  19:21:25  pvmsrc
 * Replaced #ifdef IMA_OS2 fd_set declarations:
 * 	- new #ifdef FDSETNOTSTRUCT.
 * 	- choose between "fd_set foo" and "struct fd_set foo"...
 * (Spanker=kohl)
 *
 * Revision 1.16  1997/11/04  23:14:45  pvmsrc
 * Cleaned up fd_set stuff (hopefully).
 * (Spanker=kohl)
 *
 * Revision 1.15  1997/07/09  13:21:04  pvmsrc
 * Fixed Author Header.
 *
 * Revision 1.14  1997/06/25  18:03:58  pvmsrc
 * WIN32 changes from Markus.
 *
 * Revision 1.13  1997/05/13  14:37:39  pvmsrc
 * Changed header file $includes:
 * 	- ../src/listmac.h -> listmac.h
 * 	- ../src/bfunc.h -> bfunc.h
 * 	- use -I$(PVMDIR)/src in Makefile.aimk instead.
 *
 * Revision 1.12  1997/05/01  15:49:44  pvmsrc
 * Oops...  exit_handler() must be int funct, not void.
 * 	- for pvm_addmhf()...
 *
 * Revision 1.11  1997/05/01  15:41:16  pvmsrc
 * SGI Compiler Warning Cleanup.
 *
 * Revision 1.10  1997/04/29  19:37:41  pvmsrc
 * Commented out signal( SIGTERM, SIGIGN ).
 * 	- not needed with PvmNoReset setopt now...
 *
 * Revision 1.9  1997/04/24  12:37:04  pvmsrc
 * Added NEEDSSELECTH switch to simplify ifdefs on IBM machines.
 *
 * Revision 1.8  1997/04/08  20:22:31  pvmsrc
 * Sonofabitch.  Typo.
 *
 * Revision 1.7  1997/04/08  20:15:28  pvmsrc
 * Spanked sscott's spank...  :-}
 *
 * Revision 1.6  1997/03/06  14:52:48  pvmsrc
 * Yanked out spank.
 * 	- removed Bob's test code for mbox & context...
 *
 * Revision 1.5  1997/02/17  19:51:54  pvmsrc
 * Added setting of PvmNoReset for pvm console.
 *
 * Revision 1.4  1997/01/28  19:13:18  pvmsrc
 * New Copyright Notice & Authors.
 *
 * Revision 1.3  1996/10/24  23:04:05  pvmsrc
 * Updated for new tracing facility.
 * 	- new init, select handling with checkmsgs(),
 * 		and notify event / trace / output handling.
 *
 * Revision 1.2  1996/10/08  18:29:40  pvmsrc
 * Renamed routines:
 * 	- pvm_put() -> pvm_putinfo().
 * 	- pvm_get() -> pvm_getinfo().
 *
 * Revision 1.1  1996/09/23  20:25:36  pvmsrc
 * Initial revision
 *
 * Revision 1.6  1995/05/30  16:51:30  manchek
 * added SP2MPI architecture
 *
 * Revision 1.5  1995/05/17  15:28:06  manchek
 * added FDSETISINT switch.
 * allow pvm_getfds() to fail silently (e.g. for shared memory)
 *
 * Revision 1.4  1994/10/15  18:40:09  manchek
 * don't compare add-host notify source to 0x80000000
 *
 * Revision 1.3  1994/06/03  20:01:52  manchek
 * version 3.3.0
 *
 * Revision 1.2  1993/09/16  21:33:40  manchek
 * moved notify(HostAdd) to before we read script file.
 * added include for linux/time.h
 *
 * Revision 1.1  1993/08/30  23:30:32  manchek
 * Initial revision
 *
 */

#include <stdio.h>
#include <sys/types.h>
/* #ifdef IMA_LINUX */
/* #include <linux/time.h> */
/* #endif */
#ifdef NEEDSSELECTH
#include <sys/select.h>
#endif
#ifdef	IMA_I860
#include <sys/socket.h>
#endif
#include <ctype.h>
#include <signal.h>
#include <pvm3.h>
#include <pvmtev.h>
#include "cmd.h"
#include "myalloc.h"
#include "listmac.h"
#include "bfunc.h"
#include "job.h"

#define	PVMERRMSG(n)	((n) <= 0 && (n) > -pvm_nerr \
						? pvm_errlist[-(n)] : "Unknown Error")

char *getenv();

#ifdef WIN32
extern int nAlert;
WSADATA WSAData;
#endif			 		

extern char *pvm_errlist[];
extern int pvm_nerr;

extern struct job *joblist;				/* from job.c */

int mytid = -1;
int nhosts = 0;
int narchs = 0;
struct pvmhostinfo *hostlist = 0;
struct alias *aliases = 0;

int joboffset = FirstJobTag - 1;
int nextjob = FirstJobTag;

#ifdef WIN32
int system_loser_win = FALSE;
#endif


int
exit_handler(mid)
	int mid;
{
	printf("Console: exit handler called\n");
	pvm_freebuf(mid);
	return 0;
}


main(argc, argv)
	int argc;
	char **argv;
{
	static struct timeval more_msgs = { 0, 0 };
	char *prompt = "pvm> ";
	char cmd[1024];
	int cc;
	int i;
	int src, cod, len;
	char *p;
	FILE *ff;
#ifdef FDSETNOTSTRUCT
	fd_set rfds, fds;
#else
	struct fd_set rfds, fds;
#endif
	int nfds;
	int n;
	int *np = 0;
	struct pvmminfo minfo;
	struct timeval *timeout;
	int more;

#ifdef WIN32

	/* WSAStartup has to be called before any socket command */
	/* can be executed. Why ? Ask Bill */

	HANDLE hStdin;
	struct timeval zerotimeout = { 0, 0 };

	if ( WSAStartup( 0x0101, &WSAData ) != 0 ) {
		printf("\nWSAStartup() failed\n");
		ExitProcess( 1 );
	} 

#endif

	pvm_setopt(PvmResvTids, 1);

	aliases = TALLOC(1, struct alias, "alias");
	BZERO((char*)aliases, sizeof(struct alias));
	aliases->a_link = aliases->a_rlink = aliases;

	if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'd')
		pvm_setopt(PvmDebugMask, pvmstrtoi(argv[1] + 2));

	pvm_setopt(PvmRoute, PvmDontRoute);

	pvm_setopt(PvmSelfTraceTid, -1);
	pvm_setopt(PvmSelfOutputTid, -1);

	i = pvm_setopt(PvmAutoErr, 0);
	cc = pvm_start_pvmd(argc - 1, argv + 1, 1);
	if (cc < 0) {
		if (cc != PvmDupHost) {
			pvm_perror("Console");
			exit(1);
		}
		printf("pvmd already running.\n");
	}
	pvm_setopt(PvmAutoErr, i);

	if ((mytid = pvm_mytid()) < 0)
		exit(1);

	(void)signal(SIGINT, SIG_IGN);
	/* (void)signal(SIGTERM, SIG_IGN); now PvmNoReset */

	pvm_setopt( PvmNoReset, 1 );

	pvm_setopt( PvmTraceOptions, PvmTraceFull );
	pvm_setopt( PvmTraceBuffer, 0 );

	job_init();

	pvm_notify(PvmHostAdd, HostsAddedTag, -1, (int*)0);

	BZERO(&minfo, sizeof(minfo));
	minfo.src = -1;
	minfo.ctx = -1;
	minfo.tag = MyExitTag;
	pvm_addmhf(minfo.src, minfo.tag, minfo.ctx, exit_handler);
	pvm_notify(PvmTaskExit, MyExitTag, 1, &mytid);

	if (!(p = getenv("HOME")))
		p = ".";
	sprintf(cmd, "%s/.pvmrc", p);
	if (ff = fopen(cmd, "r")) {
		while (fgets(cmd, sizeof(cmd)-1, ff))
			docmd(cmd);
		(void)fclose(ff);
	}

	FD_ZERO(&rfds);

#ifndef WIN32
	FD_SET(0, &rfds);
#endif

	nfds = 1;
	i = pvm_setopt(PvmAutoErr, 0);
	if (pvm_getfds(&np) > 0) {
		FD_SET(np[0], &rfds);
		nfds = np[0] + 1;
	}
	pvm_setopt(PvmAutoErr, i);

	trc_init();

	printf(prompt);
	fflush(stdout);

#ifdef WIN32
	hStdin = GetStdHandle(STD_INPUT_HANDLE);
#endif

	while (1) {

	/*
	* flush task stdout and trace events
	*/

		if (mytid > 0)
			more = checkmsgs();
		else
			more = 0;

		if ( more )
			timeout = &more_msgs;

		else
#ifndef WIN32
			timeout = (struct timeval *) NULL;
#else
			timeout = &zerotimeout;
#endif

	/*
	* wait for command or more output
	*/
		fds = rfds;
		if ((n = select(nfds,
#ifdef	FDSETISINT
				(int *)&fds, (int *)0, (int *)0,
#else
				(fd_set *)&fds, (fd_set *)0, (fd_set *)0,
#endif
				timeout))
		== -1) {
			perror("select");
#ifndef WIN32
			continue;
#endif
		} 

#ifdef WIN32
		else 
			if (n != 0) printf("select: returned untouched data \n");
#endif

#ifdef WIN32
		if (WAIT_TIMEOUT == WaitForSingleObject(hStdin,2000))
			continue; /* no input */
#else	
		if (n > 0 && FD_ISSET(0, &fds)) {
#endif
			if ((n = read(0, cmd, sizeof(cmd)-1)) < 1) {
				printf("quit");
				quit_cmd();
			}
			cmd[n] = 0;
			docmd(cmd);
			printf(prompt);
			fflush(stdout);
#ifndef WIN32
		}
#endif
	}
}


/*	acav()
*
*	Parse a string into words separated by whitespace.
*	Max number of words is original value of *acp.
*
*	Trashes out the original string.
*	Returns 0 with av[0]..av[*acp - 1] pointing to the words.
*	Returns 1 if too many words.
*	Returns -1 if unbalanced quote.
*/

int
acav(s, acp, av)
	char *s;			/* the string to parse */
	int *acp;			/* max num words in, num words found out */
	char **av;			/* pointers to words */
{
	int ac = 0;			/* number of words found */
	char *p = s;		/* input scanner */
	char *q;			/* output */
	int n = *acp;		/* max number of words allowed */
	int mode = 0;		/* quote mode */

	while (*p) {
		while (isspace(*p)) p++;
		if (*p) {
			if (*p == '#')
				break;
			if (ac >= n) {
				*acp = ac;
				return 1;
			}
			q = p;
			av[ac++] = p;
			while (*p) {
				if (mode) {
					if (mode == '\\') {
						*q++ = *p;
						mode = 0;

					} else if (mode == *p) {
						mode = 0;

					} else
						*q++ = *p;

				} else {
					if (isspace(*p))
						break;

					switch (*p) {

					case '"':
					case '\'':
					case '\\':
						mode = *p;
						break;

					default:
						*q++ = *p;
						break;
					}
				}
				p++;
			}
			if (*p)
				p++;
			if (*q)
				*q = 0;
			if (mode) {
				printf("unmatched %c\n", (char)mode);
				return -1;
			}
		}
	}
	*acp = ac;
	return 0;
}


/*	xtoi()
*
*	Yet another version of ascii hex to integer
*/

xtoi(p)
	char *p;
{
	int i = 0;
	char c;

	while (isxdigit(c = *p++)) {
		i = (i << 4) + c - (isdigit(c) ? '0' : (isupper(c) ? 'A' : 'a') - 10);
	}
	return i;
}


/*	axtoi()
*
*	ascii hex or decimal to integer.
*/

axtoi(p)
	char *p;
{
	if (p[0] == '0' && p[1] == 'x')
		return xtoi(p + 2);
	else
		return atoi(p);
}


int
tidtoi(p)
	char *p;
{
	if (*p == 't')
		p++;
	return xtoi(p);
}


checkmsgs()
{
	int cc;
	int len, cod, src;
	int n;
	struct job *jp;
	int *dtids;
	int i, j;
	int more, tmp;
	struct job *jpnext;

	/* Shit, really need context here to keep old pvm_nrecv(-1,-1)... */
	/* Should use 2 contexts, one for original console msgs and one   */
	/*     for tracer stuff...                                        */

	/* Get Host Add Notifies */
	while ((cc = pvm_nrecv(-1, HostsAddedTag)) > 0) {
		pvm_upkint(&n, 1, 1);
		dtids = TALLOC(n, int, "");
		pvm_upkint(dtids, n, 1);
		printf("\nConsole: %d new host%s added\n", n, (n == 1 ? "" : "s"));
		pvm_freebuf(cc);
		if (!pvm_config(&nhosts, &narchs, &hostlist)) {
			fputs("                    HOST     DTID     ARCH   SPEED\n",
					stdout);
			for (j = n; j-- > 0; )
				for (i = nhosts; i-- > 0; ) {
					if (dtids[j] == hostlist[i].hi_tid) {
						printf("%24s %8x %8s%8d\n",
								hostlist[i].hi_name,
								hostlist[i].hi_tid,
								hostlist[i].hi_arch,
								hostlist[i].hi_speed);
						break;
					}
				}
		}
		MY_FREE(dtids);
	}

	/* Poll Jobs...  This Sux...  Needs Context... */

	jp = joblist->j_link;

	more = 0;

	while (jp != joblist) {

		jpnext = jp->j_link;

		/* Trace Job */
		if (jp->j_flag & JOB_TRACE) {
			if (trc_recv_messages(jp->j_trcid, 10, &tmp) > 0 ) {
				fflush(jp->j_trcid->trace_out);
			}
			if ( tmp )
				more++;
			if ( jp->j_trcid->complete ) {
				printf("[%d] finished\n", jp->j_jid - joboffset);
				job_free(jp);
			}
		}

		/* Output Job */
		else {
			while ((cc = pvm_nrecv(-1, jp->j_jid)) > 0) {
				pvm_bufinfo(cc, &len, &cod, &src);
				checkoutput(jp, cc, len, cod, src);
			}
		}

		jp = jpnext;
	}

	if (cc < 0)
		exit(1);

	return( more );
}