Debian Codegraph @

liblsd/hostlist.c

raw
1 /*****************************************************************************\
2 * $Id: hostlist.c 9378 2009-12-16 05:34:32Z grondo $
3 *****************************************************************************
4 * Copyright (C) 2002 The Regents of the University of California.
5 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
6 * Written by Mark Grondona <[email protected]>
7 * UCRL-CODE-2002-040.
8 *
9 * This file is part of SLURM, a resource management program.
10 * For details, see <http://www.llnl.gov/linux/slurm/>.
11 *
12 * SLURM is free software; you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with SLURM; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 \*****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 # if HAVE_STRING_H
30 # include <string.h>
31 # endif
32 # if HAVE_PTHREAD_H
33 # include <pthread.h>
34 # endif
35 #else /* !HAVE_CONFIG_H */
36 # include <string.h>
37 # include <pthread.h>
38 #endif /* HAVE_CONFIG_H */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <assert.h>
44 #include <errno.h>
45 #include <ctype.h>
46 #include <sys/param.h>
47 #include <unistd.h>
48
49 #include "hostlist.h"
50
51 /*
52 * lsd_fatal_error : fatal error macro
53 */
54 #ifdef WITH_LSD_FATAL_ERROR_FUNC
55 # undef lsd_fatal_error
56 extern void lsd_fatal_error(char *file, int line, char *mesg);
57 #else /* !WITH_LSD_FATAL_ERROR_FUNC */
58 # ifndef lsd_fatal_error
59 # define lsd_fatal_error(file, line, mesg) \
60 do { \
61 fprintf(stderr, "ERROR: [%s:%d] %s: %s\n", \
62 file, line, mesg, strerror(errno)); \
63 } while (0)
64 # endif /* !lsd_fatal_error */
65 #endif /* !WITH_LSD_FATAL_ERROR_FUNC */
66
67 /*
68 * lsd_nomem_error
69 */
70 #ifdef WITH_LSD_NOMEM_ERROR_FUNC
71 # undef lsd_nomem_error
72 extern void * lsd_nomem_error(char *file, int line, char *mesg);
73 #else /* !WITH_LSD_NOMEM_ERROR_FUNC */
74 # ifndef lsd_nomem_error
75 # define lsd_nomem_error(file, line, mesg) (NULL)
76 # endif /* !lsd_nomem_error */
77 #endif /* !WITH_LSD_NOMEM_ERROR_FUNC */
78
79 /*
80 * OOM helper function
81 * Automatically call lsd_nomem_error with appropriate args
82 * and set errno to ENOMEM
83 */
84 #define out_of_memory(mesg) \
85 do { \
86 errno = ENOMEM; \
87 return(lsd_nomem_error(__FILE__, __LINE__, mesg)); \
88 } while (0)
89
90 /*
91 * Some constants and tunables:
92 */
93
94 /* number of elements to allocate when extending the hostlist array */
95 #define HOSTLIST_CHUNK 16
96
97 /* max host range: anything larger will be assumed to be an error */
98 #define MAX_RANGE 16384 /* 16K Hosts */
99
100 /* max host suffix value */
101 #define MAX_HOST_SUFFIX 1<<25
102
103 /* max number of ranges that will be processed between brackets */
104 #define MAX_RANGES 10240 /* 10K Ranges */
105
106 /* size of internal hostname buffer (+ some slop), hostnames will probably
107 * be truncated if longer than MAXHOSTNAMELEN */
108 #ifndef MAXHOSTNAMELEN
109 #define MAXHOSTNAMELEN 64
110 #endif
111
112 /* max size of internal hostrange buffer */
113 #define MAXHOSTRANGELEN 1024
114
115 /* ----[ Internal Data Structures ]---- */
116
117 /* hostname type: A convenience structure used in parsing single hostnames */
118 struct hostname_components {
119 char *hostname; /* cache of initialized hostname */
120 char *prefix; /* hostname prefix */
121 unsigned long num; /* numeric suffix */
122
123 /* string representation of numeric suffix
124 * points into `hostname' */
125 char *suffix;
126 };
127
128 typedef struct hostname_components *hostname_t;
129
130 /* hostrange type: A single prefix with `hi' and `lo' numeric suffix values */
131 struct hostrange_components {
132 char *prefix; /* alphanumeric prefix: */
133
134 /* beginning (lo) and end (hi) of suffix range */
135 unsigned long lo, hi;
136
137 /* width of numeric output format
138 * (pad with zeros up to this width) */
139 int width;
140
141 /* If singlehost is 1, `lo' and `hi' are invalid */
142 unsigned singlehost:1;
143 };
144
145 typedef struct hostrange_components *hostrange_t;
146
147 /* The hostlist type: An array based list of hostrange_t's */
148 struct hostlist {
149 #ifndef NDEBUG
150 #define HOSTLIST_MAGIC 57005
151 int magic;
152 #endif
153 #if WITH_PTHREADS
154 pthread_mutex_t mutex;
155 #endif /* WITH_PTHREADS */
156
157 /* current number of elements available in array */
158 int size;
159
160 /* current number of ranges stored in array */
161 int nranges;
162
163 /* current number of hosts stored in hostlist */
164 int nhosts;
165
166 /* pointer to hostrange array */
167 hostrange_t *hr;
168
169 /* list of iterators */
170 struct hostlist_iterator *ilist;
171
172 };
173
174
175 /* a hostset is a wrapper around a hostlist */
176 struct hostset {
177 hostlist_t hl;
178 };
179
180 struct hostlist_iterator {
181 #ifndef NDEBUG
182 int magic;
183 #endif
184 /* hostlist we are traversing */
185 hostlist_t hl;
186
187 /* current index of iterator in hl->hr[] */
188 int idx;
189
190 /* current hostrange object in list hl, i.e. hl->hr[idx] */
191 hostrange_t hr;
192
193 /* current depth we've traversed into range hr */
194 int depth;
195
196 /* next ptr for lists of iterators */
197 struct hostlist_iterator *next;
198 };
199
200
201 /* ---- ---- */
202
203 /* ------[ static function prototypes ]------ */
204
205 static void _error(char *file, int line, char *mesg, ...);
206 static char * _next_tok(char *, char **);
207 static int _zero_padded(unsigned long, int);
208 static int _width_equiv(unsigned long, int *, unsigned long, int *);
209
210 static int host_prefix_end(const char *);
211 static hostname_t hostname_create(const char *);
212 static void hostname_destroy(hostname_t);
213 static int hostname_suffix_is_valid(hostname_t);
214 static int hostname_suffix_width(hostname_t);
215
216 static hostrange_t hostrange_new(void);
217 static hostrange_t hostrange_create_single(const char *);
218 static hostrange_t hostrange_create(char *, unsigned long, unsigned long, int);
219 static unsigned long hostrange_count(hostrange_t);
220 static hostrange_t hostrange_copy(hostrange_t);
221 static void hostrange_destroy(hostrange_t);
222 static hostrange_t hostrange_delete_host(hostrange_t, unsigned long);
223 static int hostrange_cmp(hostrange_t, hostrange_t);
224 static int hostrange_prefix_cmp(hostrange_t, hostrange_t);
225 static int hostrange_within_range(hostrange_t, hostrange_t);
226 static int hostrange_width_combine(hostrange_t, hostrange_t);
227 static int hostrange_empty(hostrange_t);
228 static char * hostrange_pop(hostrange_t);
229 static char * hostrange_shift(hostrange_t);
230 static int hostrange_join(hostrange_t, hostrange_t);
231 static hostrange_t hostrange_intersect(hostrange_t, hostrange_t);
232 static int hostrange_hn_within(hostrange_t, hostname_t);
233 static size_t hostrange_to_string(hostrange_t hr, size_t, char *, char *);
234 static size_t hostrange_numstr(hostrange_t, size_t, char *);
235
236 static hostlist_t hostlist_new(void);
237 static hostlist_t _hostlist_create_bracketed(const char *, char *, char *);
238 static int hostlist_resize(hostlist_t, size_t);
239 static int hostlist_expand(hostlist_t);
240 static int hostlist_push_range(hostlist_t, hostrange_t);
241 static int hostlist_push_hr(hostlist_t, char *, unsigned long,
242 unsigned long, int);
243 static int hostlist_insert_range(hostlist_t, hostrange_t, int);
244 static void hostlist_delete_range(hostlist_t, int n);
245 static void hostlist_coalesce(hostlist_t hl);
246 static void hostlist_collapse(hostlist_t hl);
247 static hostlist_t _hostlist_create(const char *, char *, char *);
248 static void hostlist_shift_iterators(hostlist_t, int, int, int);
249 static int _attempt_range_join(hostlist_t, int);
250 static int _is_bracket_needed(hostlist_t, int);
251
252 static hostlist_iterator_t hostlist_iterator_new(void);
253 static void _iterator_advance(hostlist_iterator_t);
254 static void _iterator_advance_range(hostlist_iterator_t);
255
256 static int hostset_find_host(hostset_t, const char *);
257
258 /* ------[ macros ]------ */
259
260 #ifdef WITH_PTHREADS
261 # define mutex_init(mutex) \
262 do { \
263 int e = pthread_mutex_init(mutex, NULL); \
264 if (e) { \
265 errno = e; \
266 lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex init:"); \
267 abort(); \
268 } \
269 } while (0)
270
271 # define mutex_lock(mutex) \
272 do { \
273 int e = pthread_mutex_lock(mutex); \
274 if (e) { \
275 errno = e; \
276 lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex lock:"); \
277 abort(); \
278 } \
279 } while (0)
280
281 # define mutex_unlock(mutex) \
282 do { \
283 int e = pthread_mutex_unlock(mutex); \
284 if (e) { \
285 errno = e; \
286 lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex unlock:"); \
287 abort(); \
288 } \
289 } while (0)
290
291 # define mutex_destroy(mutex) \
292 do { \
293 int e = pthread_mutex_destroy(mutex); \
294 if (e) { \
295 errno = e; \
296 lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex destroy:"); \
297 abort(); \
298 } \
299 } while (0)
300
301 #else /* !WITH_PTHREADS */
302
303 # define mutex_init(mutex)
304 # define mutex_lock(mutex)
305 # define mutex_unlock(mutex)
306 # define mutex_destroy(mutex)
307
308 #endif /* WITH_PTHREADS */
309
310 #define LOCK_HOSTLIST(_hl) \
311 do { \
312 assert(_hl != NULL); \
313 mutex_lock(&(_hl)->mutex); \
314 assert((_hl)->magic == HOSTLIST_MAGIC); \
315 } while (0)
316
317 #define UNLOCK_HOSTLIST(_hl) \
318 do { \
319 mutex_unlock(&(_hl)->mutex); \
320 } while (0)
321
322 #define seterrno_ret(_errno, _rc) \
323 do { \
324 errno = _errno; \
325 return _rc; \
326 } while (0)
327
328 /* ------[ Function Definitions ]------ */
329
330 /* ----[ general utility functions ]---- */
331
332
333 /*
334 * Varargs capable error reporting via lsd_fatal_error()
335 */
336 static void _error(char *file, int line, char *msg, ...)
337 {
338 va_list ap;
339 char buf[1024];
340 int len = 0;
341 va_start(ap, msg);
342
343 len = vsnprintf(buf, 1024, msg, ap);
344 if ((len < 0) || (len > 1024))
345 buf[1023] = '\0';
346
347 lsd_fatal_error(file, line, buf);
348
349 va_end(ap);
350 return;
351 }
352
353 static int _advance_past_brackets (char *tok, char **str)
354 {
355 /* if _single_ opening bracket exists b/w tok and str, push str
356 * past first closing bracket to next seperator */
357 if ( memchr(tok, '[', *str - tok) != NULL
358 && memchr(tok, ']', *str - tok) == NULL ) {
359 char *q = strchr(*str, ']');
360 if (q && memchr(*str, '[', q - *str) == NULL) {
361 *str = q + 1;
362 return (1);
363 }
364 }
365
366 return 0;
367 }
368
369 /*
370 * Helper function for host list string parsing routines
371 * Returns a pointer to the next token; additionally advance *str
372 * to the next separator.
373 *
374 * next_tok was taken directly from pdsh courtesy of Jim Garlick.
375 * (with modifications to support bracketed hostlists, i.e.:
376 * xxx[xx,xx,xx] is a single token)
377 *
378 */
379 static char * _next_tok(char *sep, char **str)
380 {
381 char *tok;
382
383 /* push str past any leading separators */
384 while (**str != '\0' && strchr(sep, **str) != '\0')
385 (*str)++;
386
387 if (**str == '\0')
388 return NULL;
389
390 /* assign token ptr */
391 tok = *str;
392
393 /*
394 * Advance str past any separators, but if a separator occurs between
395 * brackets, e.g. foo[0-3,5], then advance str past closing brackets and
396 * try again.
397 */
398 do {
399 /* push str past token and leave pointing to first separator */
400 while (**str != '\0' && strchr(sep, **str) == '\0')
401 (*str)++;
402 } while (_advance_past_brackets (tok, str));
403
404 /* nullify consecutive separators and push str beyond them */
405 while (**str != '\0' && strchr(sep, **str) != '\0')
406 *(*str)++ = '\0';
407
408 return tok;
409 }
410
411
412 /* return the number of zeros needed to pad "num" to "width"
413 */
414 static int _zero_padded(unsigned long num, int width)
415 {
416 int n = 1;
417 while (num /= 10L)
418 n++;
419 return width > n ? width - n : 0;
420 }
421
422 /* test whether two format `width' parameters are "equivalent"
423 * The width arguments "wn" and "wm" for integers "n" and "m"
424 * are equivalent if:
425 *
426 * o wn == wm OR
427 *
428 * o applying the same format width (either wn or wm) to both of
429 * 'n' and 'm' will not change the zero padding of *either* 'm' nor 'n'.
430 *
431 * If this function returns 1 (or true), the appropriate width value
432 * (either 'wm' or 'wn') will have been adjusted such that both format
433 * widths are equivalent.
434 */
435 static int _width_equiv(unsigned long n, int *wn, unsigned long m, int *wm)
436 {
437 int npad, nmpad, mpad, mnpad;
438
439 if (wn == wm)
440 return 1;
441
442 npad = _zero_padded(n, *wn);
443 nmpad = _zero_padded(n, *wm);
444 mpad = _zero_padded(m, *wm);
445 mnpad = _zero_padded(m, *wn);
446
447 if (npad != nmpad && mpad != mnpad)
448 return 0;
449
450 if (npad != nmpad) {
451 if (mpad == mnpad) {
452 *wm = *wn;
453 return 1;
454 } else
455 return 0;
456 } else { /* mpad != mnpad */
457 if (npad == nmpad) {
458 *wn = *wm;
459 return 1;
460 } else
461 return 0;
462 }
463
464 /* not reached */
465 }
466
467
468 /* ----[ hostname_t functions ]---- */
469
470 /*
471 * return the location of the last char in the hostname prefix
472 */
473 static int host_prefix_end(const char *hostname)
474 {
475 int idx = strlen(hostname) - 1;
476
477 while (idx >= 0 && isdigit((char) hostname[idx]))
478 idx--;
479 return idx;
480 }
481
482 static hostname_t hostname_create_with_suffix (const char *hostname, int idx)
483 {
484 hostname_t hn = NULL;
485 char *p = '\0';
486
487 assert(hostname != NULL);
488
489 if (!(hn = (hostname_t) malloc(sizeof(*hn))))
490 out_of_memory("hostname create");
491
492 if (!(hn->hostname = strdup(hostname))) {
493 free(hn);
494 out_of_memory("hostname create");
495 }
496
497 hn->num = 0;
498 hn->prefix = NULL;
499 hn->suffix = NULL;
500
501 if (idx == strlen(hostname) - 1) {
502 if ((hn->prefix = strdup(hostname)) == NULL) {
503 hostname_destroy(hn);
504 out_of_memory("hostname prefix create");
505 }
506 return hn;
507 }
508
509 hn->suffix = hn->hostname + idx + 1;
510 hn->num = strtoul(hn->suffix, &p, 10);
511
512 if ((*p == '\0') && (hn->num <= MAX_HOST_SUFFIX)) {
513 if (!(hn->prefix = malloc((idx + 2) * sizeof(char)))) {
514 hostname_destroy(hn);
515 out_of_memory("hostname prefix create");
516 }
517 memcpy(hn->prefix, hostname, idx + 1);
518 hn->prefix[idx + 1] = '\0';
519 } else {
520 if (!(hn->prefix = strdup(hostname))) {
521 hostname_destroy(hn);
522 out_of_memory("hostname prefix create");
523 }
524 hn->suffix = NULL;
525 }
526
527 return hn;
528
529 }
530
531
532 /*
533 * create a hostname_t object from a string hostname
534 */
535 static hostname_t hostname_create(const char *hostname)
536 {
537 int idx = host_prefix_end (hostname);
538 return hostname_create_with_suffix (hostname, idx);
539 }
540
541 /* free a hostname object
542 */
543 static void hostname_destroy(hostname_t hn)
544 {
545 if (hn == NULL)
546 return;
547 hn->suffix = NULL;
548 if (hn->hostname)
549 free(hn->hostname);
550 if (hn->prefix)
551 free(hn->prefix);
552 free(hn);
553 }
554
555 /* return true if the hostname has a valid numeric suffix
556 */
557 static int hostname_suffix_is_valid(hostname_t hn)
558 {
559 return hn->suffix != NULL;
560 }
561
562 /* return the width (in characters) of the numeric part of the hostname
563 */
564 static int hostname_suffix_width(hostname_t hn)
565 {
566 assert(hn->suffix != NULL);
567 return (int) strlen(hn->suffix);
568 }
569
570
571 /* ----[ hostrange_t functions ]---- */
572
573 /* allocate a new hostrange object
574 */
575 static hostrange_t hostrange_new(void)
576 {
577 hostrange_t new = (hostrange_t) malloc(sizeof(*new));
578 if (!new)
579 out_of_memory("hostrange create");
580 return new;
581 }
582
583 /* Create a hostrange_t containing a single host without a valid suffix
584 * hr->prefix will represent the entire hostname.
585 */
586 static hostrange_t hostrange_create_single(const char *prefix)
587 {
588 hostrange_t new;
589
590 assert(prefix != NULL);
591
592 if ((new = hostrange_new()) == NULL)
593 goto error1;
594
595 if ((new->prefix = strdup(prefix)) == NULL)
596 goto error2;
597
598 new->singlehost = 1;
599 new->lo = 0L;
600 new->hi = 0L;
601 new->width = 0;
602
603 return new;
604
605 error2:
606 free(new);
607 error1:
608 out_of_memory("hostrange create single");
609 }
610
611
612 /* Create a hostrange object with a prefix, hi, lo, and format width
613 */
614 static hostrange_t
615 hostrange_create(char *prefix, unsigned long lo, unsigned long hi, int width)
616 {
617 hostrange_t new;
618
619 assert(prefix != NULL);
620
621 if ((new = hostrange_new()) == NULL)
622 goto error1;
623
624 if ((new->prefix = strdup(prefix)) == NULL)
625 goto error2;
626
627 new->lo = lo;
628 new->hi = hi;
629 new->width = width;
630
631 new->singlehost = 0;
632
633 return new;
634
635 error2:
636 free(new);
637 error1:
638 out_of_memory("hostrange create");
639 }
640
641
642 /* Return the number of hosts stored in the hostrange object
643 */
644 static unsigned long hostrange_count(hostrange_t hr)
645 {
646 assert(hr != NULL);
647 if (hr->singlehost)
648 return 1;
649 else
650 return hr->hi - hr->lo + 1;
651 }
652
653 /* Copy a hostrange object
654 */
655 static hostrange_t hostrange_copy(hostrange_t hr)
656 {
657 assert(hr != NULL);
658
659 if (hr->singlehost)
660 return hostrange_create_single(hr->prefix);
661 else
662 return hostrange_create(hr->prefix, hr->lo, hr->hi,
663 hr->width);
664 }
665
666
667 /* free memory allocated by the hostrange object
668 */
669 static void hostrange_destroy(hostrange_t hr)
670 {
671 if (hr == NULL)
672 return;
673 if (hr->prefix)
674 free(hr->prefix);
675 free(hr);
676 }
677
678 /* hostrange_delete_host() deletes a specific host from the range.
679 * If the range is split into two, the greater range is returned,
680 * and `hi' of the lesser range is adjusted accordingly. If the
681 * highest or lowest host is deleted from a range, NULL is returned
682 * and the hostrange hr is adjusted properly.
683 */
684 static hostrange_t hostrange_delete_host(hostrange_t hr, unsigned long n)
685 {
686 hostrange_t new = NULL;
687
688 assert(hr != NULL);
689 assert(n >= hr->lo && n <= hr->hi);
690
691 if (n == hr->lo)
692 hr->lo++;
693 else if (n == hr->hi)
694 hr->hi--;
695 else {
696 if (!(new = hostrange_copy(hr)))
697 out_of_memory("hostrange copy");
698 hr->hi = n - 1;
699 new->lo = n + 1;
700 }
701
702 return new;
703 }
704
705 /* hostrange_cmp() is used to sort hostrange objects. It will
706 * sort based on the following (in order):
707 * o result of strcmp on prefixes
708 * o if widths are compatible, then:
709 * sort based on lowest suffix in range
710 * else
711 * sort based on width */
712 static int hostrange_cmp(hostrange_t h1, hostrange_t h2)
713 {
714 int retval;
715
716 assert(h1 != NULL);
717 assert(h2 != NULL);
718
719 if ((retval = hostrange_prefix_cmp(h1, h2)) == 0)
720 retval = hostrange_width_combine(h1, h2) ?
721 h1->lo - h2->lo : h1->width - h2->width;
722
723 return retval;
724 }
725
726
727 /* compare the prefixes of two hostrange objects.
728 * returns:
729 * < 0 if h1 prefix is less than h2 OR h1 == NULL.
730 *
731 * 0 if h1's prefix and h2's prefix match,
732 * UNLESS, either h1 or h2 (NOT both) do not have a valid suffix.
733 *
734 * > 0 if h1's prefix is greater than h2's OR h2 == NULL. */
735 static int hostrange_prefix_cmp(hostrange_t h1, hostrange_t h2)
736 {
737 int retval;
738 if (h1 == NULL)
739 return 1;
740 if (h2 == NULL)
741 return -1;
742
743 retval = strcmp(h1->prefix, h2->prefix);
744 return retval == 0 ? h2->singlehost - h1->singlehost : retval;
745 }
746
747 /* returns true if h1 and h2 would be included in the same bracketed hostlist.
748 * h1 and h2 will be in the same bracketed list iff:
749 *
750 * 1. h1 and h2 have same prefix
751 * 2. neither h1 nor h2 are singlet hosts (i.e. invalid suffix)
752 *
753 * (XXX: Should incompatible widths be placed in the same bracketed list?
754 * There's no good reason not to, except maybe aesthetics)
755 */
756 static int hostrange_within_range(hostrange_t h1, hostrange_t h2)
757 {
758 if (hostrange_prefix_cmp(h1, h2) == 0)
759 return h1->singlehost || h2->singlehost ? 0 : 1;
760 else
761 return 0;
762 }
763
764
765 /* compare two hostrange objects to determine if they are width
766 * compatible, returns:
767 * 1 if widths can safely be combined
768 * 0 if widths cannot be safely combined
769 */
770 static int hostrange_width_combine(hostrange_t h0, hostrange_t h1)
771 {
772 assert(h0 != NULL);
773 assert(h1 != NULL);
774
775 return _width_equiv(h0->lo, &h0->width, h1->lo, &h1->width);
776 }
777
778
779 /* Return true if hostrange hr contains no hosts, i.e. hi < lo
780 */
781 static int hostrange_empty(hostrange_t hr)
782 {
783 assert(hr != NULL);
784 return ((hr->hi < hr->lo) || (hr->hi == (unsigned long) -1));
785 }
786
787 /* return the string representation of the last host in hostrange hr
788 * and remove that host from the range (i.e. decrement hi if possible)
789 *
790 * Returns NULL if malloc fails OR there are no more hosts left
791 */
792 static char *hostrange_pop(hostrange_t hr)
793 {
794 size_t size = 0;
795 char *host = NULL;
796
797 assert(hr != NULL);
798
799 if (hr->singlehost) {
800 hr->lo++; /* effectively set count == 0 */
801 host = strdup(hr->prefix);
802 } else if (hostrange_count(hr) > 0) {
803 size = strlen(hr->prefix) + hr->width + 16;
804 if (!(host = (char *) malloc(size * sizeof(char))))
805 out_of_memory("hostrange pop");
806 snprintf(host, size, "%s%0*lu", hr->prefix,
807 hr->width, hr->hi--);
808 }
809
810 return host;
811 }
812
813 /* Same as hostrange_pop(), but remove host from start of range */
814 static char *hostrange_shift(hostrange_t hr)
815 {
816 size_t size = 0;
817 char *host = NULL;
818
819 assert(hr != NULL);
820
821 if (hr->singlehost) {
822 hr->lo++;
823 if (!(host = strdup(hr->prefix)))
824 out_of_memory("hostrange shift");
825 } else if (hostrange_count(hr) > 0) {
826 size = strlen(hr->prefix) + hr->width + 16;
827 if (!(host = (char *) malloc(size * sizeof(char))))
828 out_of_memory("hostrange shift");
829 snprintf(host, size, "%s%0*lu", hr->prefix,
830 hr->width, hr->lo++);
831 }
832
833 return host;
834 }
835
836
837 /* join two hostrange objects.
838 *
839 * returns:
840 *
841 * -1 if ranges do not overlap (including incompatible zero padding)
842 * 0 if ranges join perfectly
843 * >0 number of hosts that were duplicated in h1 and h2
844 *
845 * h2 will be coalesced into h1 if rc >= 0
846 *
847 * it is assumed that h1->lo <= h2->lo, i.e. hr1 <= hr2
848 *
849 */
850 static int hostrange_join(hostrange_t h1, hostrange_t h2)
851 {
852 int duplicated = -1;
853
854 assert(h1 != NULL);
855 assert(h2 != NULL);
856 assert(hostrange_cmp(h1, h2) <= 0);
857
858 if (hostrange_prefix_cmp(h1, h2) == 0 &&
859 hostrange_width_combine(h1, h2)) {
860
861 if (h1->singlehost && h2->singlehost) { /* matching singlets */
862 duplicated = 1;
863 } else if (h1->hi == h2->lo - 1) { /* perfect join */
864 h1->hi = h2->hi;
865 duplicated = 0;
866 } else if (h1->hi >= h2->lo) { /* some duplication */
867 if (h1->hi < h2->hi) {
868 duplicated = h1->hi - h2->lo + 1;
869 h1->hi = h2->hi;
870 } else
871 duplicated = hostrange_count(h2);
872 }
873 }
874
875 return duplicated;
876 }
877
878 /* hostrange intersect returns the intersection (common hosts)
879 * of hostrange objects h1 and h2. If there is no intersection,
880 * NULL is returned.
881 *
882 * It is assumed that h1 <= h2 (i.e. h1->lo <= h2->lo)
883 */
884 static hostrange_t hostrange_intersect(hostrange_t h1, hostrange_t h2)
885 {
886 hostrange_t new = NULL;
887
888 assert(h1 != NULL);
889 assert(h2 != NULL);
890
891 if (h1->singlehost || h2->singlehost)
892 return NULL;
893
894 assert(hostrange_cmp(h1, h2) <= 0);
895
896 if ((hostrange_prefix_cmp(h1, h2) == 0)
897 && (h1->hi > h2->lo)
898 && (hostrange_width_combine(h1, h2))) {
899
900 if (!(new = hostrange_copy(h1)))
901 return NULL;
902 new->lo = h2->lo;
903 new->hi = h2->hi < h1->hi ? h2->hi : h1->hi;
904 }
905
906 return new;
907 }
908
909 /* return offset of hn if it is in the hostlist or
910 * -1 if not.
911 */
912 static int hostrange_hn_within(hostrange_t hr, hostname_t hn)
913 {
914 int len_hr;
915 int len_hn;
916 int width;
917
918 if (hr->singlehost) {
919 /*
920 * If the current hostrange [hr] is a `singlehost' (no valid
921 * numeric suffix (lo and hi)), then the hostrange [hr]
922 * stores just one host with name == hr->prefix.
923 *
924 * Thus the full hostname in [hn] must match hr->prefix, in
925 * which case we return true. Otherwise, there is no
926 * possibility that [hn] matches [hr].
927 */
928 if (strcmp (hn->hostname, hr->prefix) == 0)
929 return 0;
930 else
931 return -1;
932 }
933
934 /*
935 * Now we know [hr] is not a "singlehost", so hostname
936 * better have a valid numeric suffix, or there is no
937 * way we can match
938 */
939 if (!hostname_suffix_is_valid (hn))
940 return -1;
941
942 len_hn = strlen (hn->prefix);
943
944 /*
945 * If hostrange and hostname prefixes don't match to at least
946 * the length of the hostname object (which will have the min
947 * possible prefix length), then there is no way the hostname
948 * falls within the range [hr].
949 */
950 if (strncmp (hr->prefix, hn->prefix, len_hn) != 0)
951 return -1;
952
953 /*
954 * Now we know hostrange and hostname prefixes match up to the
955 * length of the hostname prefix. If the hostrange and hostname
956 * prefix lengths do not match (specifically if the hostname prefix
957 * length is less than the hostrange prefix length) and the
958 * hostrange prefix contains trailing digits, then it might be
959 * the case that the hostrange was created by forcing the prefix
960 * to contain digits a la f00[1-2]. So we try adjusting the
961 * hostname with the longer prefix and calling this function
962 * again with the new hostname. (Yes, this is ugly, sorry)
963 */
964 len_hr = strlen (hr->prefix);
965 width = hostname_suffix_width (hn);
966
967 if ((len_hn < len_hr)
968 && (width > 1)
969 && (isdigit (hr->prefix [len_hr - 1]))
970 && (hr->prefix [len_hn] == hn->suffix[0]) ) {
971 int rc;
972 /*
973 * Create new hostname object with its prefix offset by one
974 */
975 hostname_t h = hostname_create_with_suffix (hn->hostname, len_hn);
976 /*
977 * Recursive call :-o
978 */
979 rc = hostrange_hn_within (hr, h);
980 hostname_destroy (h);
981 return rc;
982 }
983
984
985 /*
986 * Finally, check whether [hn], with a valid numeric suffix,
987 * falls within the range of [hr].
988 */
989 if (hn->num <= hr->hi && hn->num >= hr->lo) {
990 int width = hostname_suffix_width (hn);
991 if (!_width_equiv(hr->lo, &hr->width, hn->num, &width))
992 return -1;
993 return (hn->num - hr->lo);
994 }
995
996 return -1;
997 }
998
999
1000 /* copy a string representation of the hostrange hr into buffer buf,
1001 * writing at most n chars including NUL termination
1002 */
1003 static size_t
1004 hostrange_to_string(hostrange_t hr, size_t n, char *buf, char *separator)
1005 {
1006 unsigned long i;
1007 int truncated = 0;
1008 int len = 0;
1009 char sep = separator == NULL ? ',' : separator[0];
1010
1011 if (n == 0)
1012 return 0;
1013
1014 if (hr->singlehost)
1015 return snprintf(buf, n, "%s", hr->prefix);
1016
1017 for (i = hr->lo; i <= hr->hi; i++) {
1018 size_t m = (n - len) <= n ? n - len : 0; /* check for < 0 */
1019 int ret = snprintf(buf + len, m, "%s%0*lu",
1020 hr->prefix, hr->width, i);
1021 if (ret < 0 || ret >= m) {
1022 len = n;
1023 truncated = 1;
1024 break;
1025 }
1026 len+=ret;
1027 buf[len++] = sep;
1028 }
1029
1030 if (truncated) {
1031 buf[n-1] = '\0';
1032 return -1;
1033 } else {
1034 /* back up over final separator */
1035 buf[--len] = '\0';
1036 return len;
1037 }
1038 }
1039
1040 /* Place the string representation of the numeric part of hostrange into buf
1041 * writing at most n chars including NUL termination.
1042 */
1043 static size_t hostrange_numstr(hostrange_t hr, size_t n, char *buf)
1044 {
1045 int len = 0;
1046
1047 assert(buf != NULL);
1048
1049 if (hr->singlehost || n == 0)
1050 return 0;
1051
1052 len = snprintf(buf, n, "%0*lu", hr->width, hr->lo);
1053
1054 if ((len >= 0) && (len < n) && (hr->lo < hr->hi)) {
1055 int len2 = snprintf(buf+len, n-len, "-%0*lu", hr->width, hr->hi);
1056 if (len2 < 0)
1057 len = -1;
1058 else
1059 len += len2;
1060 }
1061
1062 return len;
1063 }
1064
1065
1066 /* ----[ hostlist functions ]---- */
1067
1068 /* Create a new hostlist object.
1069 * Returns an empty hostlist, or NULL if memory allocation fails.
1070 */
1071 static hostlist_t hostlist_new(void)
1072 {
1073 int i;
1074 hostlist_t new = (hostlist_t) malloc(sizeof(*new));
1075 if (!new)
1076 goto fail1;
1077
1078 assert(new->magic = HOSTLIST_MAGIC);
1079 mutex_init(&new->mutex);
1080
1081 new->hr = (hostrange_t *) malloc(HOSTLIST_CHUNK * sizeof(hostrange_t));
1082 if (!new->hr)
1083 goto fail2;
1084
1085 /* set entries in hostrange array to NULL */
1086 for (i = 0; i < HOSTLIST_CHUNK; i++)
1087 new->hr[i] = NULL;
1088
1089 new->size = HOSTLIST_CHUNK;
1090 new->nranges = 0;
1091 new->nhosts = 0;
1092 new->ilist = NULL;
1093 return new;
1094
1095 fail2:
1096 free(new);
1097 fail1:
1098 out_of_memory("hostlist_create");
1099 }
1100
1101
1102 /* Resize the internal array used to store the list of hostrange objects.
1103 *
1104 * returns 1 for a successful resize,
1105 * 0 if call to _realloc fails
1106 *
1107 * It is assumed that the caller has the hostlist hl locked
1108 */
1109 static int hostlist_resize(hostlist_t hl, size_t newsize)
1110 {
1111 int i;
1112 size_t oldsize;
1113 assert(hl != NULL);
1114 assert(hl->magic == HOSTLIST_MAGIC);
1115 oldsize = hl->size;
1116 hl->size = newsize;
1117 hl->hr = realloc((void *) hl->hr, hl->size*sizeof(hostrange_t));
1118 if (!(hl->hr))
1119 return 0;
1120
1121 for (i = oldsize; i < newsize; i++)
1122 hl->hr[i] = NULL;
1123
1124 return 1;
1125 }
1126
1127 /* Resize hostlist by one HOSTLIST_CHUNK
1128 * Assumes that hostlist hl is locked by caller
1129 */
1130 static int hostlist_expand(hostlist_t hl)
1131 {
1132 if (!hostlist_resize(hl, hl->size + HOSTLIST_CHUNK))
1133 return 0;
1134 else
1135 return 1;
1136 }
1137
1138 /* Push a hostrange object onto hostlist hl
1139 * Returns the number of hosts successfully pushed onto hl
1140 * or -1 if there was an error allocating memory
1141 */
1142 static int hostlist_push_range(hostlist_t hl, hostrange_t hr)
1143 {
1144 hostrange_t tail;
1145 int retval;
1146
1147 assert(hr != NULL);
1148 LOCK_HOSTLIST(hl);
1149
1150 tail = (hl->nranges > 0) ? hl->hr[hl->nranges-1] : hl->hr[0];
1151
1152 if (hl->size == hl->nranges && !hostlist_expand(hl))
1153 goto error;
1154
1155 if (hl->nranges > 0
1156 && hostrange_prefix_cmp(tail, hr) == 0
1157 && tail->hi == hr->lo - 1
1158 && hostrange_width_combine(tail, hr)) {
1159 tail->hi = hr->hi;
1160 } else {
1161 if ((hl->hr[hl->nranges++] = hostrange_copy(hr)) == NULL)
1162 goto error;
1163 }
1164
1165 retval = hl->nhosts += hostrange_count(hr);
1166
1167 UNLOCK_HOSTLIST(hl);
1168
1169 return retval;
1170
1171 error:
1172 UNLOCK_HOSTLIST(hl);
1173 return -1;
1174 }
1175
1176
1177
1178 /* Same as hostlist_push_range() above, but prefix, lo, hi, and width
1179 * are passed as args
1180 */
1181 static int
1182 hostlist_push_hr(hostlist_t hl, char *prefix, unsigned long lo,
1183 unsigned long hi, int width)
1184 {
1185 hostrange_t hr = hostrange_create(prefix, lo, hi, width);
1186 int retval = hostlist_push_range(hl, hr);
1187 hostrange_destroy(hr);
1188 return retval;
1189 }
1190
1191 /* Insert a range object hr into position n of the hostlist hl
1192 * Assumes that hl->mutex is already held by calling process
1193 */
1194 static int hostlist_insert_range(hostlist_t hl, hostrange_t hr, int n)
1195 {
1196 int i;
1197 hostrange_t tmp;
1198 hostlist_iterator_t hli;
1199
1200 assert(hl != NULL);
1201 assert(hl->magic == HOSTLIST_MAGIC);
1202 assert(hr != NULL);
1203
1204 if (n > hl->nranges)
1205 return 0;
1206
1207 if (hl->size == hl->nranges && !hostlist_expand(hl))
1208 return 0;
1209
1210 /* copy new hostrange into slot "n" in array */
1211 tmp = hl->hr[n];
1212 hl->hr[n] = hostrange_copy(hr);
1213
1214 /* push remaining hostrange entries up */
1215 for (i = n + 1; i < hl->nranges + 1; i++) {
1216 hostrange_t last = hl->hr[i];
1217 hl->hr[i] = tmp;
1218 tmp = last;
1219 }
1220 hl->nranges++;
1221
1222 /* adjust hostlist iterators if needed */
1223 for (hli = hl->ilist; hli; hli = hli->next) {
1224 if (hli->idx >= n)
1225 hli->hr = hli->hl->hr[++hli->idx];
1226 }
1227
1228 return 1;
1229 }
1230
1231 /* Delete the range at position n in the range array
1232 * Assumes the hostlist lock is already held.
1233 */
1234 static void hostlist_delete_range(hostlist_t hl, int n)
1235 {
1236 int i;
1237 hostrange_t old;
1238
1239 assert(hl != NULL);
1240 assert(hl->magic == HOSTLIST_MAGIC);
1241 assert(n < hl->nranges && n >= 0);
1242
1243 old = hl->hr[n];
1244 for (i = n; i < hl->nranges - 1; i++)
1245 hl->hr[i] = hl->hr[i + 1];
1246 hl->nranges--;
1247 hl->hr[hl->nranges] = NULL;
1248 hostlist_shift_iterators(hl, n, 0, 1);
1249
1250 /* XXX caller responsible for adjusting nhosts */
1251 /* hl->nhosts -= hostrange_count(old) */
1252
1253 hostrange_destroy(old);
1254 }
1255
1256 #if WANT_RECKLESS_HOSTRANGE_EXPANSION
1257
1258 /* The reckless hostrange expansion function.
1259 * See comment in hostlist.h:hostlist_create() for more info on
1260 * the different choices for hostlist notation.
1261 */
1262 hostlist_t _hostlist_create(const char *hostlist, char *sep, char *r_op)
1263 {
1264 char *str, *orig;
1265 char *tok, *cur;
1266 int high, low, fmt = 0;
1267 char prefix[256] = "";
1268 int pos = 0;
1269 int error = 0;
1270 char range_op = r_op[0];/* XXX support > 1 char range ops in future? */
1271
1272 hostlist_t new = hostlist_new();
1273
1274 orig = str = strdup(hostlist);
1275
1276 /* return an empty list if an empty string was passed in */
1277 if (str == NULL || strlen(str) == 0)
1278 goto done;
1279
1280 /* Use hostlist_create_bracketed if we see "[" */
1281 if (strchr(str, '[') != NULL)
1282 return _hostlist_create_bracketed(hostlist, sep, r_op);
1283
1284 while ((tok = _next_tok(sep, &str)) != NULL) {
1285
1286 /* save the current string for error messages */
1287 cur = tok;
1288
1289 high = low = 0;
1290
1291 /* find end of alpha part
1292 * do this by finding last occurence of range_op in str */
1293 pos = strlen(tok) - 1;
1294 if (strstr(tok, r_op) != '\0') {
1295 while (pos >= 0 && (char) tok[pos] != range_op)
1296 pos--;
1297 }
1298
1299 /* now back up past any digits */
1300 while (pos >= 0 && isdigit((char) tok[--pos])) {;}
1301
1302 /* Check for valid x-y range (x must be a digit)
1303 * Reset pos if the range is not valid */
1304 if (!isdigit((char) tok[++pos]))
1305 pos = strlen(tok) - 1;
1306
1307 /* create prefix string
1308 * if prefix will be zero length, but prefix already exists
1309 * use the previous prefix and fmt
1310 */
1311 if ((pos > 0) || (prefix[0] == '\0')) {
1312 memcpy(prefix, tok, (size_t) pos * sizeof(char));
1313 prefix[pos] = '\0';
1314
1315 /* push pointer past prefix */
1316 tok += pos;
1317
1318 /* count number of digits for ouput fmt */
1319 for (fmt = 0; isdigit(tok[fmt]); ++fmt) {;}
1320
1321 if (fmt == 0)
1322 error = 1;
1323
1324 } else
1325 tok += pos;
1326
1327 /* get lower bound */
1328 low = strtoul(tok, (char **) &tok, 10);
1329
1330 if (*tok == range_op) { /* now get range upper bound */
1331 /* push pointer past range op */
1332 ++tok;
1333
1334 /* find length of alpha part */
1335 for (pos = 0; tok[pos] && !isdigit(tok[pos]); ++pos) {;}
1336
1337 /* alpha part must match prefix or error
1338 * this could mean we've got something like "rtr1-a2"
1339 * so just record an error
1340 */
1341 if (pos > 0) {
1342 if (pos != strlen(prefix) ||
1343 strncmp(prefix, tok, pos) != 0)
1344 error = 1;
1345 }
1346
1347 if (*tok != '\0')
1348 tok += pos;
1349
1350 /* make sure we have digits to the end */
1351 for (pos = 0; tok[pos] && isdigit((char) tok[pos]); ++pos) {;}
1352
1353 if (pos > 0) { /* we have digits to process */
1354 high = strtoul(tok, (char **) &tok, 10);
1355 } else { /* bad boy, no digits */
1356 error = 1;
1357 }
1358
1359 if ((low > high) || (high - low > MAX_RANGE))
1360 error = 1;
1361
1362 } else { /* single value */
1363 high = 0; /* special case, ugh. */
1364 }
1365
1366 /* error if:
1367 * 1. we are not at end of string
1368 * 2. upper bound equals lower bound
1369 */
1370 if (*tok != '\0' || high == low)
1371 error = 1;
1372
1373 if (error) { /* assume this is not a range on any error */
1374 hostlist_push_host(new, cur);
1375 } else {
1376 if (high < low)
1377 high = low;
1378 hostlist_push_hr(new, prefix, low, high, fmt);
1379 }
1380
1381 error = 0;
1382 }
1383
1384 done:
1385 free(orig);
1386
1387 return new;
1388 }
1389
1390 #else /* !WANT_RECKLESS_HOSTRANGE_EXPANSION */
1391
1392 hostlist_t _hostlist_create(const char *hostlist, char *sep, char *r_op)
1393 {
1394 return _hostlist_create_bracketed(hostlist, sep, r_op);
1395 }
1396
1397 #endif /* WANT_RECKLESS_HOSTRANGE_EXPANSION */
1398
1399 struct _range {
1400 unsigned long lo, hi;
1401 int width;
1402 };
1403
1404 /* Grab a single range from str
1405 * returns 1 if str contained a valid number or range,
1406 * 0 if conversion of str to a range failed.
1407 */
1408 static int _parse_single_range(const char *str, struct _range *range)
1409 {
1410 char *p, *q;
1411 char *orig = strdup(str);
1412 if (!orig)
1413 seterrno_ret(ENOMEM, 0);
1414
1415 if ((p = strchr(str, '-'))) {
1416 *p++ = '\0';
1417 if (*p == '-') /* do NOT allow negative numbers */
1418 goto error;
1419 }
1420 range->lo = strtoul(str, &q, 10);
1421 if (q == str)
1422 goto error;
1423
1424 range->hi = (p && *p) ? strtoul(p, &q, 10) : range->lo;
1425
1426 if (q == p || *q != '\0')
1427 goto error;
1428
1429 if (range->lo > range->hi)
1430 goto error;
1431
1432 if (range->hi - range->lo + 1 > MAX_RANGE ) {
1433 _error(__FILE__, __LINE__, "Too many hosts in range `%s'", orig);
1434 free(orig);
1435 seterrno_ret(ERANGE, 0);
1436 }
1437
1438 free(orig);
1439 range->width = strlen(str);
1440 return 1;
1441
1442 error:
1443 _error(__FILE__, __LINE__, "Invalid range: `%s'", orig);
1444 free(orig);
1445 seterrno_ret(EINVAL, 0);
1446 }
1447
1448
1449 /*
1450 * Convert 'str' containing comma separated digits and ranges into an array
1451 * of struct _range types (max 'len' elements).
1452 *
1453 * Return number of ranges created, or -1 on error.
1454 */
1455 static int _parse_range_list(char *str, struct _range *ranges, int len)
1456 {
1457 char *p;
1458 int count = 0;
1459
1460 while (str) {
1461 if (count == len)
1462 return -1;
1463 if ((p = strchr(str, ',')))
1464 *p++ = '\0';
1465 if (!_parse_single_range(str, &ranges[count++]))
1466 return -1;
1467 str = p;
1468 }
1469 return count;
1470 }
1471
1472 static void
1473 _push_range_list(hostlist_t hl, char *pfx, struct _range *rng,
1474 int n)
1475 {
1476 int i;
1477 for (i = 0; i < n; i++) {
1478 hostlist_push_hr(hl, pfx, rng->lo, rng->hi, rng->width);
1479 rng++;
1480 }
1481 }
1482
1483 static void
1484 _push_range_list_with_suffix(hostlist_t hl, char *pfx, char *sfx,
1485 struct _range *rng, int n)
1486 {
1487 int i;
1488 unsigned long j;
1489 for (i = 0; i < n; i++) {
1490 for (j = rng->lo; j <= rng->hi; j++) {
1491 char host[4096];
1492 hostrange_t hr;
1493 snprintf (host, 4096, "%s%0*lu%s", pfx, rng->width, j, sfx);
1494 hr = hostrange_create_single (host);
1495 hostlist_push_range (hl, hr);
1496 /*
1497 * hr is copied in hostlist_push_range. Need to free here.
1498 */
1499 hostrange_destroy (hr);
1500 }
1501 rng++;
1502 }
1503 }
1504
1505 /*
1506 * Create a hostlist from a string with brackets '[' ']' to aid
1507 * detection of ranges and compressed lists
1508 */
1509 static hostlist_t
1510 _hostlist_create_bracketed(const char *hostlist, char *sep, char *r_op)
1511 {
1512 hostlist_t new = hostlist_new();
1513 struct _range ranges[MAX_RANGES];
1514 int nr, err;
1515 char *p, *tok, *str, *orig;
1516 char cur_tok[1024];
1517
1518 if (hostlist == NULL)
1519 return new;
1520
1521 if (!(orig = str = strdup(hostlist))) {
1522 hostlist_destroy(new);
1523 return NULL;
1524 }
1525
1526 while ((tok = _next_tok(sep, &str)) != NULL) {
1527 strncpy(cur_tok, tok, 1024);
1528
1529 if ((p = strchr(tok, '[')) != NULL) {
1530 char *q, *prefix = tok;
1531 *p++ = '\0';
1532
1533 if ((q = strchr(p, ']'))) {
1534 *q = '\0';
1535 nr = _parse_range_list(p, ranges, MAX_RANGES);
1536 if (nr < 0)
1537 goto error;
1538
1539 if (*(++q) != '\0')
1540 _push_range_list_with_suffix (new, prefix, q, ranges, nr);
1541 else
1542 _push_range_list(new, prefix, ranges, nr);
1543
1544
1545 } else
1546 hostlist_push_host(new, cur_tok);
1547
1548 } else
1549 hostlist_push_host(new, cur_tok);
1550 }
1551
1552 free(orig);
1553 return new;
1554
1555 error:
1556 err = errno;
1557 hostlist_destroy(new);
1558 free(orig);
1559 seterrno_ret(err, NULL);
1560 }
1561
1562
1563
1564 hostlist_t hostlist_create(const char *str)
1565 {
1566 return _hostlist_create(str, "\t, ", "-");
1567 }
1568
1569
1570 hostlist_t hostlist_copy(const hostlist_t hl)
1571 {
1572 int i;
1573 hostlist_t new;
1574
1575 if (hl == NULL)
1576 return NULL;
1577
1578 LOCK_HOSTLIST(hl);
1579 if (!(new = hostlist_new()))
1580 goto done;
1581
1582 new->nranges = hl->nranges;
1583 new->nhosts = hl->nhosts;
1584 if (new->nranges > new->size)
1585 hostlist_resize(new, new->nranges);
1586
1587 for (i = 0; i < hl->nranges; i++)
1588 new->hr[i] = hostrange_copy(hl->hr[i]);
1589
1590 done:
1591 UNLOCK_HOSTLIST(hl);
1592 return new;
1593 }
1594
1595
1596 void hostlist_destroy(hostlist_t hl)
1597 {
1598 int i;
1599 if (hl == NULL)
1600 return;
1601 LOCK_HOSTLIST(hl);
1602 while (hl->ilist) {
1603 mutex_unlock(&hl->mutex);
1604 hostlist_iterator_destroy(hl->ilist);
1605 mutex_lock(&hl->mutex);
1606 }
1607 for (i = 0; i < hl->nranges; i++)
1608 hostrange_destroy(hl->hr[i]);
1609 free(hl->hr);
1610 assert(hl->magic = 0x1);
1611 UNLOCK_HOSTLIST(hl);
1612 mutex_destroy(&hl->mutex);
1613 free(hl);
1614 }
1615
1616
1617 int hostlist_push(hostlist_t hl, const char *hosts)
1618 {
1619 hostlist_t new;
1620 int retval;
1621 if (hosts == NULL)
1622 return 0;
1623 new = hostlist_create(hosts);
1624 if (!new)
1625 return 0;
1626 mutex_lock(&new->mutex);
1627 retval = new->nhosts;
1628 mutex_unlock(&new->mutex);
1629 hostlist_push_list(hl, new);
1630 hostlist_destroy(new);
1631 return retval;
1632 }
1633
1634 int hostlist_push_host(hostlist_t hl, const char *str)
1635 {
1636 hostrange_t hr;
1637 hostname_t hn;
1638
1639 if (str == NULL)
1640 return 0;
1641
1642 hn = hostname_create(str);
1643
1644 if (hostname_suffix_is_valid(hn)) {
1645 hr = hostrange_create(hn->prefix, hn->num, hn->num,
1646 hostname_suffix_width(hn));
1647 } else
1648 hr = hostrange_create_single(str);
1649
1650 hostlist_push_range(hl, hr);
1651
1652 hostrange_destroy(hr);
1653 hostname_destroy(hn);
1654
1655 return 1;
1656 }
1657
1658 int hostlist_push_list(hostlist_t h1, hostlist_t h2)
1659 {
1660 int i, n = 0;
1661
1662 if (h2 == NULL)
1663 return 0;
1664
1665 LOCK_HOSTLIST(h2);
1666
1667 for (i = 0; i < h2->nranges; i++)
1668 n += hostlist_push_range(h1, h2->hr[i]);
1669
1670 UNLOCK_HOSTLIST(h2);
1671
1672 return n;
1673 }
1674
1675
1676 char *hostlist_pop(hostlist_t hl)
1677 {
1678 char *host = NULL;
1679
1680 LOCK_HOSTLIST(hl);
1681 if (hl->nhosts > 0) {
1682 hostrange_t hr = hl->hr[hl->nranges - 1];
1683 host = hostrange_pop(hr);
1684 hl->nhosts--;
1685 if (hostrange_empty(hr)) {
1686 hostrange_destroy(hl->hr[--hl->nranges]);
1687 hl->hr[hl->nranges] = NULL;
1688 }
1689 }
1690 UNLOCK_HOSTLIST(hl);
1691 return host;
1692 }
1693
1694 /* find all iterators affected by a shift (or deletion) at
1695 * hl->hr[idx], depth, with the deletion of n ranges */
1696 static void
1697 hostlist_shift_iterators(hostlist_t hl, int idx, int depth, int n)
1698 {
1699 hostlist_iterator_t i;
1700 for (i = hl->ilist; i; i = i->next) {
1701 if (n == 0) {
1702 if (i->idx == idx && i->depth >= depth)
1703 i->depth = i->depth > -1 ? i->depth - 1 : -1;
1704 } else {
1705 if (i->idx >= idx) {
1706 if ((i->idx -= n) >= 0)
1707 i->hr = i->hl->hr[i->idx];
1708 else
1709 hostlist_iterator_reset(i);
1710 }
1711 }
1712 }
1713 }
1714
1715 char *hostlist_shift(hostlist_t hl)
1716 {
1717 char *host = NULL;
1718
1719 LOCK_HOSTLIST(hl);
1720
1721 if (hl->nhosts > 0) {
1722 hostrange_t hr = hl->hr[0];
1723
1724 host = hostrange_shift(hr);
1725 hl->nhosts--;
1726
1727 if (hostrange_empty(hr)) {
1728 hostlist_delete_range(hl, 0);
1729 /* hl->nranges--; */
1730 } else
1731 hostlist_shift_iterators(hl, 0, 0, 0);
1732 }
1733
1734 UNLOCK_HOSTLIST(hl);
1735
1736 return host;
1737 }
1738
1739
1740 char *hostlist_pop_range(hostlist_t hl)
1741 {
1742 int i;
1743 char buf[MAXHOSTRANGELEN + 1];
1744 hostlist_t hltmp;
1745 hostrange_t tail;
1746
1747 LOCK_HOSTLIST(hl);
1748 if (hl->nranges < 1 || !(hltmp = hostlist_new())) {
1749 UNLOCK_HOSTLIST(hl);
1750 return NULL;
1751 }
1752
1753 i = hl->nranges - 2;
1754 tail = hl->hr[hl->nranges - 1];
1755 while (i >= 0 && hostrange_within_range(tail, hl->hr[i]))
1756 i--;
1757
1758 for (i++; i < hl->nranges; i++) {
1759 hostlist_push_range(hltmp, hl->hr[i]);
1760 hostrange_destroy(hl->hr[i]);
1761 hl->hr[i] = NULL;
1762 }
1763 hl->nhosts -= hltmp->nhosts;
1764 hl->nranges -= hltmp->nranges;
1765
1766 UNLOCK_HOSTLIST(hl);
1767 hostlist_ranged_string(hltmp, MAXHOSTRANGELEN, buf);
1768 hostlist_destroy(hltmp);
1769 return strdup(buf);
1770 }
1771
1772
1773 char *hostlist_shift_range(hostlist_t hl)
1774 {
1775 int i;
1776 char buf[1024];
1777 hostlist_t hltmp = hostlist_new();
1778 if (!hltmp)
1779 return NULL;
1780
1781 LOCK_HOSTLIST(hl);
1782
1783 if (hl->nranges == 0) {
1784 hostlist_destroy(hltmp);
1785 UNLOCK_HOSTLIST(hl);
1786 return NULL;
1787 }
1788
1789 i = 0;
1790 do {
1791 hostlist_push_range(hltmp, hl->hr[i]);
1792 hostrange_destroy(hl->hr[i]);
1793 } while ( (++i < hl->nranges)
1794 && hostrange_within_range(hltmp->hr[0], hl->hr[i]) );
1795
1796 hostlist_shift_iterators(hl, i, 0, hltmp->nranges);
1797
1798 /* shift rest of ranges back in hl */
1799 for (; i < hl->nranges; i++) {
1800 hl->hr[i - hltmp->nranges] = hl->hr[i];
1801 hl->hr[i] = NULL;
1802 }
1803 hl->nhosts -= hltmp->nhosts;
1804 hl->nranges -= hltmp->nranges;
1805
1806 UNLOCK_HOSTLIST(hl);
1807
1808 hostlist_ranged_string(hltmp, 1024, buf);
1809 hostlist_destroy(hltmp);
1810
1811 return strdup(buf);
1812 }
1813
1814 /* XXX: Note: efficiency improvements needed */
1815 int hostlist_delete(hostlist_t hl, const char *hosts)
1816 {
1817 int n = 0;
1818 char *hostname = NULL;
1819 hostlist_t hltmp;
1820
1821 if (!(hltmp = hostlist_create(hosts)))
1822 seterrno_ret(EINVAL, 0);
1823
1824 while ((hostname = hostlist_pop(hltmp)) != NULL) {
1825 n += hostlist_delete_host(hl, hostname);
1826 free(hostname);
1827 }
1828 hostlist_destroy(hltmp);
1829
1830 return n;
1831 }
1832
1833
1834 /* XXX watch out! poor implementation follows! (fix it at some point) */
1835 int hostlist_delete_host(hostlist_t hl, const char *hostname)
1836 {
1837 int n = hostlist_find(hl, hostname);
1838 if (n >= 0)
1839 hostlist_delete_nth(hl, n);
1840 return n >= 0 ? 1 : 0;
1841 }
1842
1843
1844 static char *
1845 _hostrange_string(hostrange_t hr, int depth)
1846 {
1847 char buf[MAXHOSTNAMELEN + 16];
1848 int len = snprintf(buf, MAXHOSTNAMELEN + 15, "%s", hr->prefix);
1849
1850 if (!hr->singlehost)
1851 snprintf(buf+len, MAXHOSTNAMELEN+15 - len, "%0*lu",
1852 hr->width, hr->lo + depth);
1853 return strdup(buf);
1854 }
1855
1856 char * hostlist_nth(hostlist_t hl, int n)
1857 {
1858 char *host = NULL;
1859 int i, count;
1860
1861 LOCK_HOSTLIST(hl);
1862 count = 0;
1863 for (i = 0; i < hl->nranges; i++) {
1864 int num_in_range = hostrange_count(hl->hr[i]);
1865
1866 if (n <= (num_in_range - 1 + count)) {
1867 host = _hostrange_string(hl->hr[i], n - count);
1868 break;
1869 } else
1870 count += num_in_range;
1871 }
1872
1873 UNLOCK_HOSTLIST(hl);
1874
1875 return host;
1876 }
1877
1878
1879 int hostlist_delete_nth(hostlist_t hl, int n)
1880 {
1881 int i, count;
1882
1883 LOCK_HOSTLIST(hl);
1884 assert(n >= 0 && n <= hl->nhosts);
1885
1886 count = 0;
1887
1888 for (i = 0; i < hl->nranges; i++) {
1889 int num_in_range = hostrange_count(hl->hr[i]);
1890 hostrange_t hr = hl->hr[i];
1891
1892 if (n <= (num_in_range - 1 + count)) {
1893 unsigned long num = hr->lo + n - count;
1894 hostrange_t new;
1895
1896 if (hr->singlehost) { /* this wasn't a range */
1897 hostlist_delete_range(hl, i);
1898 } else if ((new = hostrange_delete_host(hr, num))) {
1899 hostlist_insert_range(hl, new, i + 1);
1900 hostrange_destroy(new);
1901 } else if (hostrange_empty(hr))
1902 hostlist_delete_range(hl, i);
1903
1904 goto done;
1905 } else
1906 count += num_in_range;
1907
1908 }
1909
1910 done:
1911 hl->nhosts--;
1912 UNLOCK_HOSTLIST(hl);
1913 return 1;
1914 }
1915
1916 int hostlist_count(hostlist_t hl)
1917 {
1918 int retval;
1919 LOCK_HOSTLIST(hl);
1920 retval = hl->nhosts;
1921 UNLOCK_HOSTLIST(hl);
1922 return retval;
1923 }
1924
1925 int hostlist_find(hostlist_t hl, const char *hostname)
1926 {
1927 int i, count, ret = -1;
1928 hostname_t hn;
1929
1930 if (!hostname)
1931 return -1;
1932
1933 hn = hostname_create(hostname);
1934
1935 LOCK_HOSTLIST(hl);
1936
1937 for (i = 0, count = 0; i < hl->nranges; i++) {
1938 int offset = hostrange_hn_within(hl->hr[i], hn);
1939 if (offset >= 0) {
1940 ret = count + offset;
1941 break;
1942 }
1943 else
1944 count += hostrange_count(hl->hr[i]);
1945 }
1946
1947 UNLOCK_HOSTLIST(hl);
1948 hostname_destroy(hn);
1949 return ret;
1950 }
1951
1952 /* hostrange compare with void * arguments to allow use with
1953 * libc qsort()
1954 */
1955 int _cmp(const void *hr1, const void *hr2)
1956 {
1957 hostrange_t *h1 = (hostrange_t *) hr1;
1958 hostrange_t *h2 = (hostrange_t *) hr2;
1959 return hostrange_cmp((hostrange_t) * h1, (hostrange_t) * h2);
1960 }
1961
1962
1963 void hostlist_sort(hostlist_t hl)
1964 {
1965 hostlist_iterator_t i;
1966 LOCK_HOSTLIST(hl);
1967
1968 if (hl->nranges <= 1) {
1969 UNLOCK_HOSTLIST(hl);
1970 return;
1971 }
1972
1973 qsort(hl->hr, hl->nranges, sizeof(hostrange_t), &_cmp);
1974
1975 /* reset all iterators */
1976 for (i = hl->ilist; i; i = i->next)
1977 hostlist_iterator_reset(i);
1978
1979 UNLOCK_HOSTLIST(hl);
1980
1981 hostlist_coalesce(hl);
1982
1983 }
1984
1985
1986 /* search through hostlist for ranges that can be collapsed
1987 * does =not= delete any hosts
1988 */
1989 static void hostlist_collapse(hostlist_t hl)
1990 {
1991 int i;
1992
1993 LOCK_HOSTLIST(hl);
1994 for (i = hl->nranges - 1; i > 0; i--) {
1995 hostrange_t hprev = hl->hr[i - 1];
1996 hostrange_t hnext = hl->hr[i];
1997
1998 if (hostrange_prefix_cmp(hprev, hnext) == 0 &&
1999 hprev->hi == hnext->lo - 1 &&
2000 hostrange_width_combine(hprev, hnext)) {
2001 hprev->hi = hnext->hi;
2002 hostlist_delete_range(hl, i);
2003 }
2004 }
2005 UNLOCK_HOSTLIST(hl);
2006 }
2007
2008 /* search through hostlist (hl) for intersecting ranges
2009 * split up duplicates and coalesce ranges where possible
2010 */
2011 static void hostlist_coalesce(hostlist_t hl)
2012 {
2013 int i, j;
2014 hostrange_t new;
2015
2016 LOCK_HOSTLIST(hl);
2017
2018 for (i = hl->nranges - 1; i > 0; i--) {
2019
2020 new = hostrange_intersect(hl->hr[i - 1], hl->hr[i]);
2021
2022 if (new) {
2023 hostrange_t hprev = hl->hr[i - 1];
2024 hostrange_t hnext = hl->hr[i];
2025 j = i;
2026
2027 if (new->hi < hprev->hi)
2028 hnext->hi = hprev->hi;
2029
2030 hprev->hi = new->lo;
2031 hnext->lo = new->hi;
2032
2033 if (hostrange_empty(hprev))
2034 hostlist_delete_range(hl, i);
2035
2036 while (new->lo <= new->hi) {
2037 hostrange_t hr = hostrange_create( new->prefix,
2038 new->lo, new->lo,
2039 new->width );
2040
2041 if (new->lo > hprev->hi)
2042 hostlist_insert_range(hl, hr, j++);
2043
2044 if (new->lo < hnext->lo)
2045 hostlist_insert_range(hl, hr, j++);
2046
2047 hostrange_destroy(hr);
2048
2049 new->lo++;
2050 }
2051 i = hl->nranges;
2052 hostrange_destroy(new);
2053 }
2054 }
2055 UNLOCK_HOSTLIST(hl);
2056
2057 hostlist_collapse(hl);
2058
2059 }
2060
2061 /* attempt to join ranges at loc and loc-1 in a hostlist */
2062 /* delete duplicates, return the number of hosts deleted */
2063 /* assumes that the hostlist hl has been locked by caller */
2064 /* returns -1 if no range join occurred */
2065 static int _attempt_range_join(hostlist_t hl, int loc)
2066 {
2067 int ndup;
2068 assert(hl != NULL);
2069 assert(hl->magic == HOSTLIST_MAGIC);
2070 assert(loc > 0);
2071 assert(loc < hl->nranges);
2072 ndup = hostrange_join(hl->hr[loc - 1], hl->hr[loc]);
2073 if (ndup >= 0) {
2074 hostlist_delete_range(hl, loc);
2075 hl->nhosts -= ndup;
2076 }
2077 return ndup;
2078 }
2079
2080 void hostlist_uniq(hostlist_t hl)
2081 {
2082 int i = 1;
2083 hostlist_iterator_t hli;
2084 LOCK_HOSTLIST(hl);
2085 if (hl->nranges <= 1) {
2086 UNLOCK_HOSTLIST(hl);
2087 return;
2088 }
2089 qsort(hl->hr, hl->nranges, sizeof(hostrange_t), &_cmp);
2090
2091 while (i < hl->nranges) {
2092 if (_attempt_range_join(hl, i) < 0) /* No range join occurred */
2093 i++;
2094 }
2095
2096 /* reset all iterators */
2097 for (hli = hl->ilist; hli; hli = hli->next)
2098 hostlist_iterator_reset(hli);
2099
2100 UNLOCK_HOSTLIST(hl);
2101 }
2102
2103
2104 ssize_t hostlist_deranged_string(hostlist_t hl, size_t n, char *buf)
2105 {
2106 int i;
2107 int len = 0;
2108 int truncated = 0;
2109
2110 LOCK_HOSTLIST(hl);
2111 for (i = 0; i < hl->nranges; i++) {
2112 size_t m = (n - len) <= n ? n - len : 0;
2113 int ret = hostrange_to_string(hl->hr[i], m, buf + len, ",");
2114 if (ret < 0 || ret > m) {
2115 len = n;
2116 truncated = 1;
2117 break;
2118 }
2119 len+=ret;
2120 buf[len++] = ',';
2121 }
2122 UNLOCK_HOSTLIST(hl);
2123
2124 buf[len > 0 ? --len : 0] = '\0';
2125 if (len == n)
2126 truncated = 1;
2127
2128 return truncated ? -1 : len;
2129 }
2130
2131 /* return true if a bracket is needed for the range at i in hostlist hl */
2132 static int _is_bracket_needed(hostlist_t hl, int i)
2133 {
2134 hostrange_t h1 = hl->hr[i];
2135 hostrange_t h2 = i < hl->nranges - 1 ? hl->hr[i + 1] : NULL;
2136 return hostrange_count(h1) > 1 || hostrange_within_range(h1, h2);
2137 }
2138
2139 /* write the next bracketed hostlist, i.e. prefix[n-m,k,...]
2140 * into buf, writing at most n chars including the terminating '\0'
2141 *
2142 * leaves start pointing to one past last range object in bracketed list,
2143 * and returns the number of bytes written into buf.
2144 *
2145 * Assumes hostlist is locked.
2146 */
2147 static int
2148 _get_bracketed_list(hostlist_t hl, int *start, const size_t n, char *buf)
2149 {
2150 hostrange_t *hr = hl->hr;
2151 int i = *start;
2152 int m, len = 0;
2153 int bracket_needed = _is_bracket_needed(hl, i);
2154
2155 len = snprintf(buf, n, "%s", hr[i]->prefix);
2156
2157 if ((len < 0) || (len > n))
2158 return n; /* truncated, buffer filled */
2159
2160 if (bracket_needed && len < n && len >= 0)
2161 buf[len++] = '[';
2162
2163 do {
2164 m = (n - len) <= n ? n - len : 0;
2165 len += hostrange_numstr(hr[i], m, buf + len);
2166 if (len >= n)
2167 break;
2168 if (bracket_needed) /* Only need commas inside brackets */
2169 buf[len++] = ',';
2170 } while (++i < hl->nranges && hostrange_within_range(hr[i], hr[i-1]));
2171
2172 if (bracket_needed && len < n && len > 0) {
2173
2174 /* Add trailing bracket (change trailing "," from above to "]" */
2175 buf[len - 1] = ']';
2176
2177 /* NUL terminate for safety, but do not add terminator to len */
2178 buf[len] = '\0';
2179
2180 } else if (len >= n) {
2181 if (n > 0)
2182 buf[n-1] = '\0';
2183
2184 } else {
2185 /* If len is > 0, NUL terminate (but do not add to len) */
2186 buf[len > 0 ? len : 0] = '\0';
2187 }
2188
2189 *start = i;
2190 return len;
2191 }
2192
2193 ssize_t hostlist_ranged_string(hostlist_t hl, size_t n, char *buf)
2194 {
2195 int i = 0;
2196 int len = 0;
2197 int truncated = 0;
2198
2199 LOCK_HOSTLIST(hl);
2200 while (i < hl->nranges && len < n) {
2201 len += _get_bracketed_list(hl, &i, n - len, buf + len);
2202 if ((len > 0) && (len < n) && (i < hl->nranges))
2203 buf[len++] = ',';
2204 }
2205 UNLOCK_HOSTLIST(hl);
2206
2207 /* NUL terminate */
2208 if (len >= n) {
2209 truncated = 1;
2210 if (n > 0)
2211 buf[n-1] = '\0';
2212 } else
2213 buf[len > 0 ? len : 0] = '\0';
2214
2215 return truncated ? -1 : len;
2216 }
2217
2218 /* ----[ hostlist iterator functions ]---- */
2219
2220 static hostlist_iterator_t hostlist_iterator_new(void)
2221 {
2222 hostlist_iterator_t i = (hostlist_iterator_t) malloc(sizeof(*i));
2223 if (!i)
2224 return NULL;
2225 i->hl = NULL;
2226 i->hr = NULL;
2227 i->idx = 0;
2228 i->depth = -1;
2229 i->next = i;
2230 assert(i->magic = HOSTLIST_MAGIC);
2231 return i;
2232 }
2233
2234 hostlist_iterator_t hostlist_iterator_create(hostlist_t hl)
2235 {
2236 hostlist_iterator_t i;
2237
2238 if (!(i = hostlist_iterator_new()))
2239 out_of_memory("hostlist_iterator_create");
2240
2241 LOCK_HOSTLIST(hl);
2242 i->hl = hl;
2243 i->hr = hl->hr[0];
2244 i->next = hl->ilist;
2245 hl->ilist = i;
2246 UNLOCK_HOSTLIST(hl);
2247 return i;
2248 }
2249
2250 hostlist_iterator_t hostset_iterator_create(hostset_t set)
2251 {
2252 return hostlist_iterator_create(set->hl);
2253 }
2254
2255 void hostlist_iterator_reset(hostlist_iterator_t i)
2256 {
2257 assert(i != NULL);
2258 assert(i->magic == HOSTLIST_MAGIC);
2259 i->idx = 0;
2260 i->hr = i->hl->hr[0];
2261 i->depth = -1;
2262 return;
2263 }
2264
2265 void hostlist_iterator_destroy(hostlist_iterator_t i)
2266 {
2267 hostlist_iterator_t *pi;
2268 if (i == NULL)
2269 return;
2270 assert(i != NULL);
2271 assert(i->magic == HOSTLIST_MAGIC);
2272 LOCK_HOSTLIST(i->hl);
2273 for (pi = &i->hl->ilist; *pi; pi = &(*pi)->next) {
2274 assert((*pi)->magic == HOSTLIST_MAGIC);
2275 if (*pi == i) {
2276 *pi = (*pi)->next;
2277 break;
2278 }
2279 }
2280 UNLOCK_HOSTLIST(i->hl);
2281 assert(i->magic = 0x1);
2282 free(i);
2283 }
2284
2285 static void _iterator_advance(hostlist_iterator_t i)
2286 {
2287 assert(i != NULL);
2288 assert(i->magic == HOSTLIST_MAGIC);
2289 if (i->idx > i->hl->nranges - 1)
2290 return;
2291 if (++(i->depth) > (i->hr->hi - i->hr->lo)) {
2292 i->depth = 0;
2293 i->hr = i->hl->hr[++i->idx];
2294 }
2295 }
2296
2297 /* advance iterator to end of current range (meaning within "[" "]")
2298 * i.e. advance iterator past all range objects that could be represented
2299 * in on bracketed hostlist.
2300 */
2301 static void _iterator_advance_range(hostlist_iterator_t i)
2302 {
2303 int nr, j;
2304 hostrange_t *hr;
2305 assert(i != NULL);
2306 assert(i->magic == HOSTLIST_MAGIC);
2307
2308 nr = i->hl->nranges;
2309 hr = i->hl->hr;
2310 j = i->idx;
2311 if (++i->depth > 0) {
2312 while (++j < nr && hostrange_within_range(i->hr, hr[j])) {;}
2313 i->idx = j;
2314 i->hr = i->hl->hr[i->idx];
2315 i->depth = 0;
2316 }
2317 }
2318
2319 char *hostlist_next(hostlist_iterator_t i)
2320 {
2321 char *buf = NULL;
2322 char suffix[16];
2323 int len = 0;
2324 assert(i != NULL);
2325 assert(i->magic == HOSTLIST_MAGIC);
2326 LOCK_HOSTLIST(i->hl);
2327 _iterator_advance(i);
2328
2329 if (i->idx > i->hl->nranges - 1) {
2330 UNLOCK_HOSTLIST(i->hl);
2331 return NULL;
2332 }
2333
2334 suffix[0] = '\0';
2335
2336 if (!i->hr->singlehost)
2337 snprintf (suffix, 15, "%0*lu", i->hr->width, i->hr->lo + i->depth);
2338
2339 len = strlen (i->hr->prefix) + strlen (suffix) + 1;
2340 if (!(buf = malloc (len)))
2341 out_of_memory("hostlist_next");
2342
2343 buf[0] = '\0';
2344 strcat (buf, i->hr->prefix);
2345 strcat (buf, suffix);
2346
2347 UNLOCK_HOSTLIST(i->hl);
2348 return (buf);
2349 }
2350
2351 char *hostlist_next_range(hostlist_iterator_t i)
2352 {
2353 char buf[MAXHOSTRANGELEN + 1];
2354 int j;
2355
2356 assert(i != NULL);
2357 assert(i->magic == HOSTLIST_MAGIC);
2358 LOCK_HOSTLIST(i->hl);
2359
2360 _iterator_advance_range(i);
2361
2362 if (i->idx > i->hl->nranges - 1) {
2363 UNLOCK_HOSTLIST(i->hl);
2364 return NULL;
2365 }
2366
2367 j = i->idx;
2368 _get_bracketed_list(i->hl, &j, MAXHOSTRANGELEN, buf);
2369
2370 UNLOCK_HOSTLIST(i->hl);
2371
2372 return strdup(buf);
2373 }
2374
2375 int hostlist_remove(hostlist_iterator_t i)
2376 {
2377 hostrange_t new;
2378 assert(i != NULL);
2379 assert(i->magic == HOSTLIST_MAGIC);
2380 LOCK_HOSTLIST(i->hl);
2381 new = hostrange_delete_host(i->hr, i->hr->lo + i->depth);
2382 if (new) {
2383 hostlist_insert_range(i->hl, new, i->idx + 1);
2384 hostrange_destroy(new);
2385 i->hr = i->hl->hr[++i->idx];
2386 i->depth = -1;
2387 } else if (hostrange_empty(i->hr)) {
2388 hostlist_delete_range(i->hl, i->idx);
2389 } else
2390 i->depth--;
2391
2392 i->hl->nhosts--;
2393 UNLOCK_HOSTLIST(i->hl);
2394
2395 return 1;
2396 }
2397
2398 /* ----[ hostset functions ]---- */
2399
2400 hostset_t hostset_create(const char *hostlist)
2401 {
2402 hostset_t new;
2403
2404 if (!(new = (hostset_t) malloc(sizeof(*new))))
2405 goto error1;
2406
2407 if (!(new->hl = hostlist_create(hostlist)))
2408 goto error2;
2409
2410 hostlist_uniq(new->hl);
2411 return new;
2412
2413 error2:
2414 free(new);
2415 error1:
2416 return NULL;
2417 }
2418
2419 hostset_t hostset_copy(const hostset_t set)
2420 {
2421 hostset_t new;
2422 if (!(new = (hostset_t) malloc(sizeof(*new))))
2423 goto error1;
2424
2425 if (!(new->hl = hostlist_copy(set->hl)))
2426 goto error2;
2427
2428 return new;
2429 error2:
2430 free(new);
2431 error1:
2432 return NULL;
2433 }
2434
2435 void hostset_destroy(hostset_t set)
2436 {
2437 if (set == NULL)
2438 return;
2439 hostlist_destroy(set->hl);
2440 free(set);
2441 }
2442
2443 /* inserts a single range object into a hostset
2444 * Assumes that the set->hl lock is already held
2445 * Updates hl->nhosts
2446 */
2447 static int hostset_insert_range(hostset_t set, hostrange_t hr)
2448 {
2449 int i = 0;
2450 int inserted = 0;
2451 int nhosts = 0;
2452 int ndups = 0;
2453 hostlist_t hl;
2454
2455 hl = set->hl;
2456
2457 if (hl->size == hl->nranges && !hostlist_expand(hl))
2458 return 0;
2459
2460 nhosts = hostrange_count(hr);
2461
2462 for (i = 0; i < hl->nranges; i++) {
2463 if (hostrange_cmp(hr, hl->hr[i]) <= 0) {
2464
2465 if ((ndups = hostrange_join(hr, hl->hr[i])) >= 0)
2466 hostlist_delete_range(hl, i);
2467 else if (ndups < 0)
2468 ndups = 0;
2469
2470 hostlist_insert_range(hl, hr, i);
2471
2472 /* now attempt to join hr[i] and hr[i-1] */
2473 if (i > 0) {
2474 int m;
2475 if ((m = _attempt_range_join(hl, i)) > 0)
2476 ndups += m;
2477 }
2478 hl->nhosts += nhosts - ndups;
2479 inserted = 1;
2480 break;
2481 }
2482 }
2483
2484 if (inserted == 0) {
2485 hl->hr[hl->nranges++] = hostrange_copy(hr);
2486 hl->nhosts += nhosts;
2487 if (hl->nranges > 1) {
2488 if ((ndups = _attempt_range_join(hl, hl->nranges - 1)) <= 0)
2489 ndups = 0;
2490 }
2491 }
2492
2493 /*
2494 * Return the number of unique hosts inserted
2495 */
2496 return nhosts - ndups;
2497 }
2498
2499 int hostset_insert(hostset_t set, const char *hosts)
2500 {
2501 int i, n = 0;
2502 hostlist_t hl = hostlist_create(hosts);
2503 if (!hl)
2504 return 0;
2505
2506 hostlist_uniq(hl);
2507 LOCK_HOSTLIST(set->hl);
2508 for (i = 0; i < hl->nranges; i++)
2509 n += hostset_insert_range(set, hl->hr[i]);
2510 UNLOCK_HOSTLIST(set->hl);
2511 hostlist_destroy(hl);
2512 return n;
2513 }
2514
2515
2516 /* linear search through N ranges for hostname "host"
2517 * */
2518 static int hostset_find_host(hostset_t set, const char *host)
2519 {
2520 int i;
2521 int retval = 0;
2522 hostname_t hn;
2523 LOCK_HOSTLIST(set->hl);
2524 hn = hostname_create(host);
2525 for (i = 0; i < set->hl->nranges; i++) {
2526 if (hostrange_hn_within(set->hl->hr[i], hn) >= 0) {
2527 retval = 1;
2528 goto done;
2529 }
2530 }
2531 done:
2532 UNLOCK_HOSTLIST(set->hl);
2533 hostname_destroy(hn);
2534 return retval;
2535 }
2536
2537 int hostset_within(hostset_t set, const char *hosts)
2538 {
2539 int nhosts, nfound;
2540 hostlist_t hl;
2541 char *hostname;
2542
2543 assert(set->hl->magic == HOSTLIST_MAGIC);
2544
2545 if (!(hl = hostlist_create(hosts)))
2546 return (0);
2547
2548 nhosts = hostlist_count(hl);
2549 nfound = 0;
2550
2551 while ((hostname = hostlist_pop(hl)) != NULL) {
2552 nfound += hostset_find_host(set, hostname);
2553 free(hostname);
2554 }
2555
2556 hostlist_destroy(hl);
2557
2558 return (nhosts == nfound);
2559 }
2560
2561 int hostset_delete(hostset_t set, const char *hosts)
2562 {
2563 return hostlist_delete(set->hl, hosts);
2564 }
2565
2566 int hostset_delete_host(hostset_t set, const char *hostname)
2567 {
2568 return hostlist_delete_host(set->hl, hostname);
2569 }
2570
2571 char *hostset_shift(hostset_t set)
2572 {
2573 return hostlist_shift(set->hl);
2574 }
2575
2576 char *hostset_pop(hostset_t set)
2577 {
2578 return hostlist_pop(set->hl);
2579 }
2580
2581 char *hostset_shift_range(hostset_t set)
2582 {
2583 return hostlist_shift_range(set->hl);
2584 }
2585
2586 char *hostset_pop_range(hostset_t set)
2587 {
2588 return hostlist_pop_range(set->hl);
2589 }
2590
2591 int hostset_count(hostset_t set)
2592 {
2593 return hostlist_count(set->hl);
2594 }
2595
2596 ssize_t hostset_ranged_string(hostset_t set, size_t n, char *buf)
2597 {
2598 return hostlist_ranged_string(set->hl, n, buf);
2599 }
2600
2601 ssize_t hostset_deranged_string(hostset_t set, size_t n, char *buf)
2602 {
2603 return hostlist_deranged_string(set->hl, n, buf);
2604 }
2605
2606 #if TEST_MAIN
2607
2608 int hostlist_nranges(hostlist_t hl)
2609 {
2610 return hl->nranges;
2611 }
2612
2613 int hostset_nranges(hostset_t set)
2614 {
2615 return set->hl->nranges;
2616 }
2617
2618 /* test iterator functionality on the list of hosts represented
2619 * by list
2620 */
2621 int iterator_test(char *list)
2622 {
2623 int j;
2624 char buf[1024];
2625 hostlist_t hl = hostlist_create(list);
2626 hostset_t set = hostset_create(list);
2627
2628 hostlist_iterator_t i = hostlist_iterator_create(hl);
2629 hostlist_iterator_t seti = hostset_iterator_create(set);
2630 hostlist_iterator_t i2 = hostlist_iterator_create(hl);
2631 char *host;
2632
2633
2634 hostlist_ranged_string(hl, 1024, buf);
2635 printf("iterator_test: hl = `%s' passed in `%s'\n", buf, list);
2636 host = hostlist_next(i);
2637 printf("first host in list hl = `%s'\n", host);
2638 free(host);
2639
2640 /* forge ahead three hosts with i2 */
2641 for (j = 0; j < 4; j++) {
2642 host = hostlist_next(i2);
2643 free(host);
2644 }
2645
2646 host = hostlist_shift(hl);
2647 printf("result of shift(hl) = `%s'\n", host);
2648 free(host);
2649 host = hostlist_next(i);
2650 printf("next host in list hl = `%s'\n", host);
2651 free(host);
2652 host = hostlist_next(i2);
2653 printf("next host for i2 = `%s'\n", host);
2654 free(host);
2655
2656 hostlist_iterator_destroy(i);
2657
2658 hostlist_destroy(hl);
2659 hostset_destroy(set);
2660 return 1;
2661 }
2662
2663 int main(int ac, char **av)
2664 {
2665 char buf[1024000];
2666 int i;
2667 char *str;
2668
2669 hostlist_t hl1, hl2, hl3;
2670 hostset_t set, set1;
2671 hostlist_iterator_t iter, iter2;
2672
2673 if (!(hl1 = hostlist_create(ac > 1 ? av[1] : NULL)))
2674 perror("hostlist_create");
2675 if (!(set = hostset_create(ac > 1 ? av[1] : NULL)))
2676 perror("hostlist_create");
2677
2678 hl3 = hostlist_create("f[0-5]");
2679 hostlist_delete(hl3, "f[1-3]");
2680 hostlist_ranged_string(hl3, 102400, buf);
2681 printf("after delete = `%s'\n", buf);
2682 hostlist_destroy(hl3);
2683
2684 for (i = 2; i < ac; i++) {
2685 hostlist_push(hl1, av[i]);
2686 hostset_insert(set, av[i]);
2687 }
2688
2689 hostlist_ranged_string(hl1, 102400, buf);
2690 printf("ranged = `%s'\n", buf);
2691
2692 iterator_test(buf);
2693
2694 hostlist_deranged_string(hl1, 10240, buf);
2695 printf("deranged = `%s'\n", buf);
2696
2697 hostset_ranged_string(set, 1024, buf);
2698 printf("hostset = `%s'\n", buf);
2699
2700 hostlist_sort(hl1);
2701 hostlist_ranged_string(hl1, 1024, buf);
2702 printf("sorted = `%s'\n", buf);
2703
2704 hostlist_uniq(hl1);
2705 hostlist_ranged_string(hl1, 1024, buf);
2706 printf("uniqed = `%s'\n", buf);
2707
2708 hl2 = hostlist_copy(hl1);
2709 printf("pop_range: ");
2710 while ((str = hostlist_pop_range(hl2))) {
2711 printf("`%s' ", str);
2712 free(str);
2713 }
2714 hostlist_destroy(hl2);
2715 printf("\n");
2716
2717 hl2 = hostlist_copy(hl1);
2718 printf("shift_range: ");
2719 while ((str = hostlist_shift_range(hl2))) {
2720 printf("`%s' ", str);
2721 free(str);
2722 }
2723 hostlist_destroy(hl2);
2724 printf("\n");
2725
2726 iter = hostset_iterator_create(set);
2727 iter2 = hostset_iterator_create(set);
2728 hostlist_iterator_destroy(iter2);
2729
2730 printf("next: ");
2731 while ((str = hostlist_next(iter))) {
2732 printf("`%s' ", str);
2733 free(str);
2734 }
2735 printf("\n");
2736
2737 hostlist_iterator_reset(iter);
2738 printf("next_range: ");
2739 while ((str = hostlist_next_range(iter))) {
2740 printf("`%s' ", str);
2741 free(str);
2742 }
2743 printf("\n");
2744
2745 printf("nranges = %d\n", hostset_nranges(set));
2746
2747 hostset_ranged_string(set, 1024, buf);
2748 printf("set = %s\n", buf);
2749
2750 hostset_destroy(set);
2751 hostlist_destroy(hl1);
2752 return 0;
2753 }
2754
2755 #endif /* TEST_MAIN */
2756
2757 /*
2758 * vi: tabstop=4 shiftwidth=4 expandtab
2759 */