File: bhostparse.c

package info (click to toggle)
lam 7.1.2-1.4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 54,608 kB
  • ctags: 17,034
  • sloc: ansic: 156,264; sh: 9,976; cpp: 7,699; makefile: 5,589; perl: 476; fortran: 260; asm: 83
file content (596 lines) | stat: -rw-r--r-- 14,830 bytes parent folder | download | duplicates (4)
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
587
588
589
590
591
592
593
594
595
596
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: bhostparse.c,v 6.22 2004/01/01 23:59:08 jsquyres Exp $
 * 
 *	Function:	- vanilla boot schema parser
 *			- host file syntax version
 */


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <boot.h>
#include <lamnet.h>
#include <net.h>
#include <typical.h>
#include <etc_misc.h>


/*
 * constants
 */

#define LNDBLOCK	10	       /* array allocation block */


/*
 * local variables
 */

static char hostname_key[] = "hostname";
static char cpu_key[] = "cpu";
static char user_key[] = "user";
static char schedule_key[] = "schedule";


/* 
 * Compare function for LIST
 */

static int comp(ELEM *listelem1, ELEM *listelem2);


/*
 *	parse_host
 *
 *	Function:	- parse a host file
 *			- fill the network description
 *			- node IDs are [0, N-1]
 *	Accepts:	- input file stream
 *			- node description array (value returned)
 *			- array size (value returned)
 */
int
bhostparse(char *filename, struct lamnode **plamnet, int *pnlamnet)
{
  char *host;			/* host machine name */
  char *user;			/* user name */
  char *linecopy;		/* copy of current line */
  int4 ncpus;			/* number of CPUs */
  int nlndalloc;		/* current allocation size */
  int node;			/* current node ID */
  int i;			/* favorite counter */
  int found;			/* if found duplicate */
  int lineno;			/* line number in host file */
  struct lamnode *lamnet;	/* current node desc. array */
  FILE *fp;			/* pointer to host file */
  char linebuf[LAM_MAX_PARSE_LINE];
  LIST **kvlist;		/* list of key-value pairs */
  ELEM *keyval;			/* entry in list */
  ELEM cpu_in, *cpu_out;	/* cpu entry in list */
  char *cpu_count;              /* cpu count in string form for list value */
  int cpu_count_len;            /* cpu_count string length */
  int listalloc;                /* current allocation size for list */
  int list_index;               /* list pointer index */
  ELEM *listelem;               /* element of the keyval list */
  ELEM search;                  /* search key for the list */

  /* Open the system file */

  fp = fopen(filename, "r");
  if (fp == 0) {
    show_help(NULL, "open-file", "The boot SSI module", filename, NULL);
    return LAMERROR;
  }

  /* Loop till EOF reading and parsing line-by-line */

  lamnet = NULL;
  nlndalloc = 0;
  node = 0;
  lineno = 0;
  kvlist = NULL;
  listalloc = 0;

  while (!feof(fp)) {
    if (fgets(linebuf, LAM_MAX_PARSE_LINE, fp) == 0) 
      break;

    /* Parse the line for hostname, num CPUs, username and key-value pairs */

    lineno++;
    linecopy = strdup(linebuf);
    if (linecopy == NULL) {
      show_help(NULL, "library-call-fail", "strdup", NULL);
      fclose(fp);
      return LAMERROR;
    }
    i = strlen(linecopy);
    if (linecopy[i - 1] == '\n')
      linecopy[i - 1] = '\0';

    /* Allocate initial node list array */

    if (kvlist == NULL) {
      kvlist = (LIST **) malloc(LNDBLOCK * sizeof(LIST *));

      if (kvlist == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	fclose(fp);
	return LAMERROR;
      }

      listalloc = LNDBLOCK;
      
      /* Initialize the list pointers to 0 */
      for (list_index = 0; list_index < listalloc; ++list_index) {
	kvlist[list_index] = NULL;
      }
    }

    /* Extend node list array if required */

    if (listalloc <= node) {
      listalloc += LNDBLOCK;
      kvlist = (LIST **) realloc((char *)kvlist, listalloc * sizeof (LIST *));

      if (kvlist == NULL) {
	show_help(NULL, "system-call-fail", "realloc", NULL);
	fclose(fp);
	return LAMERROR;
      }

      /* Initialize the list pointers to 0 */

      for (list_index = node; list_index < listalloc; ++list_index) {
	kvlist[list_index] = NULL;
      }
    }

    /* initialize the list if not already done */

    if (kvlist[node] == NULL) {
      kvlist[node] = al_init(sizeof(ELEM), comp);

      if (kvlist[node] == NULL) {
	show_help(NULL, "library-call-fail", "al_init", NULL);
	fclose(fp);
	return LAMERROR;
      }
    }

    bhostparseline(linebuf, lineno, linecopy, kvlist[node]);

    free(linecopy);

    ncpus = 0;
    host = NULL;
    user = NULL;

    /* get hostname */

    search.key = hostname_key;

    keyval = al_find(kvlist[node], &search);
    if (keyval != NULL) {
      host = strdup(keyval->value);
      if (host == NULL) {
	show_help(NULL, "library-call-fail", "strdup", NULL);
	fclose(fp);
	return LAMERROR;
      }
    }

    /* get cpu count */

    search.key = cpu_key;
    keyval = al_find(kvlist[node], &search);
    if (keyval != NULL) {
      ncpus = atoi(keyval->value);
    }

    /* get username */

    search.key = user_key;
    keyval = al_find(kvlist[node], &search);
    if (keyval != NULL) {
      user = strdup(keyval->value);
      if (user == NULL) {
	show_help(NULL, "library-call-fail", "strdup", NULL);
	fclose(fp);
	return LAMERROR;
      }
    }

    /* If a machine name is not found, must be a blank line or comment */

    if (host == NULL) 
      continue;

    /* if hostname is already used, accumulate cpu count and add newly got
       entries in the list */
 
    for (found = i = 0; i < node; i++) {
      if (strcasecmp(host, lamnet[i].lnd_hname) == 0) {
	
	/* accumulate cpu count, delete and insert is used instead of 
	   modifying the list element value, since malloc is internally 
	   done in the list and assigning a new value may require more
	   memory space for the char pointer value */

	lamnet[i].lnd_ncpus += ncpus;
	search.key = cpu_key;

	/* remove the previous cpu entry from list */

	cpu_out = al_find(lamnet[i].lnd_keyval, &search);
	free(cpu_out->key);
	free(cpu_out->value);
	al_delete(lamnet[i].lnd_keyval, cpu_out);
       
	/* insert new cpu entry into the list */
	
	cpu_in.key = strdup(cpu_key);
	if (cpu_in.key == NULL) {
	  show_help(NULL, "system-call-fail", "malloc", NULL);
	  fclose(fp);
	  return LAMERROR;
	}
	
	cpu_count_len = 8 * sizeof(int4);
	cpu_count = (char *) malloc (cpu_count_len);
	if (cpu_count == NULL) {
	  show_help(NULL, "system-call-fail", "malloc", NULL);
	  fclose(fp);
	  return LAMERROR;
	}
	snprintf(cpu_count, cpu_count_len, "%d", lamnet[i].lnd_ncpus);
	cpu_in.value = cpu_count;

	al_insert(lamnet[i].lnd_keyval, &cpu_in);

	/* append new entries in the list to the previous list 
	   Keep previous entries intact even if the new list has
	   modified values -- we are not updating entries, considering
	   that its an error on user part */
	
	for (listelem = al_top(kvlist[node]); listelem != NULL; 
	     listelem = al_next(kvlist[node], listelem)) {

	  if ((strcasecmp(listelem->key, hostname_key) == 0) || 
	      (strcasecmp(listelem->key, cpu_key) == 0)) {
	    free(listelem->key);
	    free(listelem->value);
	    continue;
	  }
	  
	  if (al_find(lamnet[i].lnd_keyval, listelem) == NULL){
	    al_insert(lamnet[i].lnd_keyval, listelem);
	  } else {
	    free(listelem->key);
	    free(listelem->value);
	  }
	}

	al_free(kvlist[node]);
	kvlist[node] = NULL;

	found = 1;
	break;
      }
    }
	
    if (found) {
      if (host != NULL)
	free(host);
      if (user!= NULL)
	free(user);
      continue;
    }
    
	  
    /* Allocate initial node description array */

    if (lamnet == NULL) {
      lamnet = (struct lamnode *) malloc((unsigned)
					 LNDBLOCK *
					 sizeof(struct lamnode));
      if (lamnet == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	fclose(fp);
	return LAMERROR;
      }
      nlndalloc = LNDBLOCK;
    }

    /* Extend node description array */

    if (nlndalloc <= node) {
      nlndalloc += LNDBLOCK;
      lamnet = (struct lamnode *) realloc((char *) lamnet,
					  (unsigned) nlndalloc *
					  sizeof(struct lamnode));
      if (lamnet == NULL) {
	show_help(NULL, "system-call-fail", "realloc", NULL);
	fclose(fp);
	return LAMERROR;
      }
    }

    lamnet[node].lnd_nodeid = node;
    lamnet[node].lnd_type = NT_BOOT;
    
    search.key = schedule_key;
    keyval = al_find(kvlist[node], &search);
    if (keyval != NULL) {
      if (strcasecmp(keyval->value, "no") == 0) {
	lamnet[node].lnd_type |= NT_WASTE;
      } else {
	if (strcasecmp(keyval->value, "yes") == 0) 
	  /* if NT_WASTE, then clear it */
	  lamnet[node].lnd_type &= ~NT_WASTE;
      }
    }
    
    lamnet[node].lnd_ncpus = ncpus;
    lamnet[node].lnd_bootport = 0;
    lamnet[node].lnd_ssi = NULL;

    /* Set host name */

    lamnet[node].lnd_hname = malloc((unsigned) strlen(host) + 1);
    if (lamnet[node].lnd_hname == NULL) {
      show_help(NULL, "system-call-fail", "malloc", NULL);
      fclose(fp);
      return LAMERROR;
    }
    strcpy(lamnet[node].lnd_hname, host);

    free(host);

    /* Set optional user name */

    lamnet[node].lnd_uname = NULL;

    if (user != NULL) {
      lamnet[node].lnd_uname = malloc((unsigned) strlen(user) + 1);
      if (lamnet[node].lnd_uname == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	fclose(fp);
	return LAMERROR;
      }
      strcpy(lamnet[node].lnd_uname, user);

      free(user);
    }

    /* Set the list */

    lamnet[node].lnd_keyval = kvlist[node];

    node++;
  }

  *pnlamnet = node;
  *plamnet = lamnet;

  free(kvlist);
  fclose(fp);

  return 0;
}



/*
 *	parseline
 *
 *	Function:	- parses the line buffer for hostname and key-value
 *			  pairs 
 *			- Format: hostname [key=value]*
 *			- skip comments: everything after a # character
 *			- returns a LIST containing key-value pairs along with 
 *			  hostname
 *	Accepts:	- line buffer (for parsing)
 *			- line number (for error messages)
 *			- line contents (for error messages)
 *			- LIST of key-value pairs (returned)
 */

void
bhostparseline(char *linebuf, int lineno, char *linedata, LIST *list)
{
  char *p;			/* favourite pointer */
  char *q;			/* end of current token */
  char lineno_str[20];		/* string for lineno */
  int hostname;                 /* did we get the hostname yet? */
  char *equals;			/* equal character in token */
  char *forslash;               /* forward slash in token */
  char *key;			/* key in key-value pair */
  char *value;			/* value in key-value pair */
  char *token;			/* current token */
  int quotes;			/* is token enclosed in quotes */
  int blank;                    /* check for blank line */
  ELEM host_keyvalue;           /* stores the host entry for key-val list */
  ELEM keyvalue;		/* key-value token */
  ELEM search;			/* search key */
  
  linebuf[LAM_MAX_PARSE_LINE - 1] = '\0';

  snprintf(lineno_str, 19, "%d", lineno);
  lineno_str[19] = '\0';

  hostname = 0;
  blank = 1;

  /*
   * Skip comments by replacing the first '#' with a NULL.
   */
  for (p = linebuf; *p; ++p) {
    if (*p == '#') {
      *p = '\0';
      return;
    }
  }

  for (p = linebuf; *p; p = q) {
    quotes = 0;

    /* If *p is whitespace, just continue. */
    if (isspace((int) *p)) {
      q = p + 1;
      continue;
    }

    /* got the first non-whitespace character, hence not a blank line */
    blank = 0;

    /* Find end of token */
    if (*p == '"') 
      quotes = 1;

    if (quotes) {

      /* strip the quotes for token */
      token = p+1;

      for (q = p + 1; *q && (*q != '"'); ++q)
	continue;
      
      if (*q != '"') {
	show_help("bhost-parse", "imbalanced-quotes", lineno_str, linedata,
		  NULL);
	return;
      }
      
      *q = '\0';
      ++q;
    }
    else {
      token = p;
      for (q = p; *q && !isspace((int) *q); ++q)
	continue;

      if (*q != '\0') {
	*q = '\0';
	++q;
      }
    }

    /* first token should be hostname */
    equals = strchr(token, '=');
    forslash = strchr(token, '/');
	
    if (hostname == 0) {
      /* I am looking for valid hostname format -- checking for forward slash 
       here just to say that hostname format is valid if there is an equals
       in hostname along with forward slash before it -- in which case its a 
       globus hostname -- just for checking purpose, nothing else */
      if (((equals != NULL) && (forslash != NULL) && (equals < forslash)) ||
	  ((forslash == NULL) && (equals != NULL))) {
	show_help("bhost-parse", "hostname-must-be-first", lineno_str,
		  linedata, NULL);
	return;
      }

      host_keyvalue.key = strdup(hostname_key);
      if (host_keyvalue.key == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }

      host_keyvalue.value = strdup(token);
      if (host_keyvalue.value == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }
      
      /* Not inserting the hostname in the list now, so that if anything
	 is wrong with the rest of the line, the line can be ignored in
	 the calling function. Will insert hostname at the end, when 
	 everything is proper */

      hostname = 1;

    } else { /* key-value pair */
      
      if (equals == NULL) {
	/* not a valid key-value pair */
	show_help("bhost-parse", "invalid-key", lineno_str, linedata, NULL);
	return;
      } else if (isspace((int) *(equals - 1)) || isspace((int) *(equals + 1))) {
	/* not a valid key-value pair, cannot take whitespace before or after 
	   the equals sign -- this can happen only when enclosed in quotes */
	show_help("bhost-parse", "invalid-key", lineno_str, linedata, NULL);
	return;
      }
      *equals = '\0';
      key = token;
      value = equals + 1;
      
      /* add key value to the list */
      
      keyvalue.key = strdup(key);
      if (keyvalue.key == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }
      
      keyvalue.value = strdup(value);
      if (keyvalue.value == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }

      al_insert(list, &keyvalue);
    }
  }
  
  if (!blank) {

    /* fix the number of cpus if user has not set it already */

    search.key = cpu_key;
    if (al_find(list, &search) == NULL) {
      keyvalue.key = strdup(cpu_key);
      if (keyvalue.key == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }
    
      keyvalue.value = strdup("1");
      if (keyvalue.value == NULL) {
	show_help(NULL, "system-call-fail", "malloc", NULL);
	exit(errno);
      }
      al_insert(list, &keyvalue);
    }

    /* Everything looks right, insert the hostname now */
  
    al_insert(list, &host_keyvalue);
  }
}


static int
comp(ELEM *listelem1, ELEM *listelem2)
{
  return strcasecmp(listelem1->key, listelem2->key);
}