| 1 |
/*****************************************************************************\ |
| 2 |
* $Id: hostlist.h 7428 2008-05-23 16:08:31Z 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 |
#ifndef _HOSTLIST_H |
| 28 |
#define _HOSTLIST_H |
| 29 |
|
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
/* Notes: |
| 33 |
* |
| 34 |
* If WITH_LSD_FATAL_ERROR_FUNC is defined, the linker will expect to |
| 35 |
* find and external lsd_fatal_error(file,line,mesg) function. By default, |
| 36 |
* lsd_fatal_error(file,line,mesg) is a macro definition that outputs an |
| 37 |
* error message to stderr. This macro may be redefined to invoke another |
| 38 |
* routine instead. e.g.: |
| 39 |
* |
| 40 |
* #define lsd_fatal_error(file,line,mesg) \ |
| 41 |
* error("%s:%s %s\n",file,line,mesg); |
| 42 |
* |
| 43 |
* If WITH_LSD_NOMEM_ERROR_FUNC is defined, the linker will expect to |
| 44 |
* find an external lsd_nomem_error(file,line,mesg) function. By default, |
| 45 |
* lsd_nomem_error(file,line,mesg) is a macro definition that returns NULL. |
| 46 |
* This macro may be redefined to invoke another routine instead. |
| 47 |
* |
| 48 |
* If WITH_PTHREADS is defined, these routines will be thread-safe. |
| 49 |
* |
| 50 |
*/ |
| 51 |
|
| 52 |
/* The hostlist opaque data type |
| 53 |
* |
| 54 |
* A hostlist is a list of hostnames optimized for a prefixXXXX style |
| 55 |
* naming convention, where XXXX is a decimal, numeric suffix. |
| 56 |
*/ |
| 57 |
typedef struct hostlist * hostlist_t; |
| 58 |
|
| 59 |
/* A hostset is a special case of a hostlist. It: |
| 60 |
* |
| 61 |
* 1. never contains duplicates |
| 62 |
* 2. is always sorted |
| 63 |
* (Note: sort occurs first on alphanumeric prefix -- where prefix |
| 64 |
* matches, numeric suffixes will be sorted *by value*) |
| 65 |
*/ |
| 66 |
typedef struct hostset * hostset_t; |
| 67 |
|
| 68 |
/* The hostlist iterator type (may be used with a hostset as well) |
| 69 |
* used for non-destructive access to hostlist members. |
| 70 |
* |
| 71 |
*/ |
| 72 |
typedef struct hostlist_iterator * hostlist_iterator_t; |
| 73 |
|
| 74 |
/* ----[ hostlist_t functions: ]---- */ |
| 75 |
|
| 76 |
/* ----[ hostlist creation and destruction ]---- */ |
| 77 |
|
| 78 |
/* |
| 79 |
* hostlist_create(): |
| 80 |
* |
| 81 |
* Create a new hostlist from a string representation. |
| 82 |
* |
| 83 |
* The string representation (str) may contain one or more hostnames or |
| 84 |
* bracketed hostlists separated by either `,' or whitespace. A bracketed |
| 85 |
* hostlist is denoted by a common prefix followed by a list of numeric |
| 86 |
* ranges contained within brackets: e.g. "tux[0-5,12,20-25]" |
| 87 |
* |
| 88 |
* Note: if this module is compiled with WANT_RECKLESS_HOSTRANGE_EXPANSION |
| 89 |
* defined, a much more loose interpretation of host ranges is used. |
| 90 |
* Reckless hostrange expansion allows all of the following (in addition to |
| 91 |
* bracketed hostlists): |
| 92 |
* |
| 93 |
* o tux0-5,tux12,tux20-25 |
| 94 |
* o tux0-tux5,tux12,tux20-tux25 |
| 95 |
* o tux0-5,12,20-25 |
| 96 |
* |
| 97 |
* If str is NULL, and empty hostlist is created and returned. |
| 98 |
* |
| 99 |
* If the create fails, hostlist_create() returns NULL. |
| 100 |
* |
| 101 |
* The returned hostlist must be freed with hostlist_destroy() |
| 102 |
* |
| 103 |
*/ |
| 104 |
hostlist_t hostlist_create(const char *hostlist); |
| 105 |
|
| 106 |
/* hostlist_copy(): |
| 107 |
* |
| 108 |
* Allocate a copy of a hostlist object. Returned hostlist must be freed |
| 109 |
* with hostlist_destroy. |
| 110 |
*/ |
| 111 |
hostlist_t hostlist_copy(const hostlist_t hl); |
| 112 |
|
| 113 |
/* hostlist_destroy(): |
| 114 |
* |
| 115 |
* Destroy a hostlist object. Frees all memory allocated to the hostlist. |
| 116 |
*/ |
| 117 |
void hostlist_destroy(hostlist_t hl); |
| 118 |
|
| 119 |
|
| 120 |
/* ----[ hostlist list operations ]---- */ |
| 121 |
|
| 122 |
/* hostlist_push(): |
| 123 |
* |
| 124 |
* push a string representation of hostnames onto a hostlist. |
| 125 |
* |
| 126 |
* The hosts argument may take the same form as in hostlist_create() |
| 127 |
* |
| 128 |
* Returns the number of hostnames inserted into the list, |
| 129 |
* or 0 on failure. |
| 130 |
*/ |
| 131 |
int hostlist_push(hostlist_t hl, const char *hosts); |
| 132 |
|
| 133 |
|
| 134 |
/* hostlist_push_host(): |
| 135 |
* |
| 136 |
* Push a single host onto the hostlist hl. |
| 137 |
* This function is more efficient than hostlist_push() for a single |
| 138 |
* hostname, since the argument does not need to be checked for ranges. |
| 139 |
* |
| 140 |
* return value is 1 for success, 0 for failure. |
| 141 |
*/ |
| 142 |
int hostlist_push_host(hostlist_t hl, const char *host); |
| 143 |
|
| 144 |
|
| 145 |
/* hostlist_push_list(): |
| 146 |
* |
| 147 |
* Push a hostlist (hl2) onto another list (hl1) |
| 148 |
* |
| 149 |
* Returns 1 for success, 0 for failure. |
| 150 |
* |
| 151 |
*/ |
| 152 |
int hostlist_push_list(hostlist_t hl1, hostlist_t hl2); |
| 153 |
|
| 154 |
|
| 155 |
/* hostlist_pop(): |
| 156 |
* |
| 157 |
* Returns the string representation of the last host pushed onto the list |
| 158 |
* or NULL if hostlist is empty or there was an error allocating memory. |
| 159 |
* The host is removed from the hostlist. |
| 160 |
* |
| 161 |
* Note: Caller is responsible for freeing the returned memory. |
| 162 |
*/ |
| 163 |
char * hostlist_pop(hostlist_t hl); |
| 164 |
|
| 165 |
|
| 166 |
char * hostlist_nth(hostlist_t hl, int n); |
| 167 |
|
| 168 |
/* hostlist_shift(): |
| 169 |
* |
| 170 |
* Returns the string representation of the first host in the hostlist |
| 171 |
* or NULL if the hostlist is empty or there was an error allocating memory. |
| 172 |
* The host is removed from the hostlist. |
| 173 |
* |
| 174 |
* Note: Caller is responsible for freeing the returned memory. |
| 175 |
*/ |
| 176 |
char * hostlist_shift(hostlist_t hl); |
| 177 |
|
| 178 |
|
| 179 |
/* hostlist_pop_range(): |
| 180 |
* |
| 181 |
* Pop the last bracketed list of hosts of the hostlist hl. |
| 182 |
* Returns the string representation in bracketed list form. |
| 183 |
* All hosts associated with the returned list are removed |
| 184 |
* from hl. |
| 185 |
* |
| 186 |
* Caller is responsible for freeing returned memory |
| 187 |
*/ |
| 188 |
char * hostlist_pop_range(hostlist_t hl); |
| 189 |
|
| 190 |
/* hostlist_shift_range(): |
| 191 |
* |
| 192 |
* Shift the first bracketed hostlist (improperly: range) off the |
| 193 |
* hostlist hl. Returns the string representation in bracketed list |
| 194 |
* form. All hosts associated with the list are removed from the |
| 195 |
* hostlist. |
| 196 |
* |
| 197 |
* Caller is responsible for freeing returned memory. |
| 198 |
*/ |
| 199 |
char * hostlist_shift_range(hostlist_t hl); |
| 200 |
|
| 201 |
|
| 202 |
/* hostlist_find(): |
| 203 |
* |
| 204 |
* Searches hostlist hl for the first host matching hostname |
| 205 |
* and returns position in list if found. |
| 206 |
* |
| 207 |
* Returns -1 if host is not found. |
| 208 |
* |
| 209 |
*/ |
| 210 |
int hostlist_find(hostlist_t hl, const char *hostname); |
| 211 |
|
| 212 |
/* hostlist_delete(): |
| 213 |
* |
| 214 |
* Deletes all hosts in the list represented by `hosts' |
| 215 |
* |
| 216 |
* Returns the number of hosts successfully deleted |
| 217 |
*/ |
| 218 |
int hostlist_delete(hostlist_t hl, const char *hosts); |
| 219 |
|
| 220 |
|
| 221 |
/* hostlist_delete_host(): |
| 222 |
* |
| 223 |
* Deletes the first host that matches `hostname' from the hostlist hl. |
| 224 |
* Note: "hostname" argument cannot contain a range of hosts |
| 225 |
* (see hostlist_delete() for this functionality.) |
| 226 |
* |
| 227 |
* Returns 1 if successful, 0 if hostname is not found in list. |
| 228 |
*/ |
| 229 |
int hostlist_delete_host(hostlist_t hl, const char *hostname); |
| 230 |
|
| 231 |
|
| 232 |
/* hostlist_delete_nth(): |
| 233 |
* |
| 234 |
* Deletes the host from position n in the hostlist. |
| 235 |
* |
| 236 |
* Returns 1 if successful 0 on error. |
| 237 |
* |
| 238 |
*/ |
| 239 |
int hostlist_delete_nth(hostlist_t hl, int n); |
| 240 |
|
| 241 |
|
| 242 |
/* hostlist_count(): |
| 243 |
* |
| 244 |
* Return the number of hosts in hostlist hl. |
| 245 |
*/ |
| 246 |
int hostlist_count(hostlist_t hl); |
| 247 |
|
| 248 |
/* hostlist_is_empty(): return true if hostlist is empty. */ |
| 249 |
#define hostlist_is_empty(__hl) ( hostlist_count(__hl) == 0 ) |
| 250 |
|
| 251 |
/* ----[ Other hostlist operations ]---- */ |
| 252 |
|
| 253 |
/* hostlist_sort(): |
| 254 |
* |
| 255 |
* Sort the hostlist hl. |
| 256 |
* |
| 257 |
*/ |
| 258 |
void hostlist_sort(hostlist_t hl); |
| 259 |
|
| 260 |
/* hostlist_uniq(): |
| 261 |
* |
| 262 |
* Sort the hostlist hl and remove duplicate entries. |
| 263 |
* |
| 264 |
*/ |
| 265 |
void hostlist_uniq(hostlist_t hl); |
| 266 |
|
| 267 |
|
| 268 |
/* ----[ hostlist print functions ]---- */ |
| 269 |
|
| 270 |
/* hostlist_ranged_string(): |
| 271 |
* |
| 272 |
* Write the string representation of the hostlist hl into buf, |
| 273 |
* writing at most n chars. Returns the number of bytes written, |
| 274 |
* or -1 if truncation occurred. |
| 275 |
* |
| 276 |
* The result will be NULL terminated. |
| 277 |
* |
| 278 |
* hostlist_ranged_string() will write a bracketed hostlist representation |
| 279 |
* where possible. |
| 280 |
*/ |
| 281 |
ssize_t hostlist_ranged_string(hostlist_t hl, size_t n, char *buf); |
| 282 |
ssize_t hostset_ranged_string(hostset_t hs, size_t n, char *buf); |
| 283 |
|
| 284 |
/* hostlist_deranged_string(): |
| 285 |
* |
| 286 |
* Writes the string representation of the hostlist hl into buf, |
| 287 |
* writing at most n chars. Returns the number of bytes written, |
| 288 |
* or -1 if truncation occurred. |
| 289 |
* |
| 290 |
* hostlist_deranged_string() will not attempt to write a bracketed |
| 291 |
* hostlist representation. Every hostname will be explicitly written. |
| 292 |
*/ |
| 293 |
ssize_t hostlist_deranged_string(hostlist_t hl, size_t n, char *buf); |
| 294 |
ssize_t hostset_deranged_string(hostset_t hs, size_t n, char *buf); |
| 295 |
|
| 296 |
|
| 297 |
/* ----[ hostlist utility functions ]---- */ |
| 298 |
|
| 299 |
|
| 300 |
/* hostlist_nranges(): |
| 301 |
* |
| 302 |
* Return the number of ranges currently held in hostlist hl. |
| 303 |
*/ |
| 304 |
int hostlist_nranges(hostlist_t hl); |
| 305 |
|
| 306 |
|
| 307 |
/* ----[ hostlist iterator functions ]---- */ |
| 308 |
|
| 309 |
/* hostlist_iterator_create(): |
| 310 |
* |
| 311 |
* Creates and returns a hostlist iterator used for non destructive |
| 312 |
* access to a hostlist or hostset. Returns NULL on failure. |
| 313 |
*/ |
| 314 |
hostlist_iterator_t hostlist_iterator_create(hostlist_t hl); |
| 315 |
|
| 316 |
/* hostset_iterator_create(): |
| 317 |
* |
| 318 |
* Same as hostlist_iterator_create(), but creates a hostlist_iterator |
| 319 |
* from a hostset. |
| 320 |
*/ |
| 321 |
hostlist_iterator_t hostset_iterator_create(hostset_t set); |
| 322 |
|
| 323 |
/* hostlist_iterator_destroy(): |
| 324 |
* |
| 325 |
* Destroys a hostlist iterator. |
| 326 |
*/ |
| 327 |
void hostlist_iterator_destroy(hostlist_iterator_t i); |
| 328 |
|
| 329 |
/* hostlist_iterator_reset(): |
| 330 |
* |
| 331 |
* Reset an iterator to the beginning of the list. |
| 332 |
*/ |
| 333 |
void hostlist_iterator_reset(hostlist_iterator_t i); |
| 334 |
|
| 335 |
/* hostlist_next(): |
| 336 |
* |
| 337 |
* Returns a pointer to the next hostname on the hostlist |
| 338 |
* or NULL at the end of the list |
| 339 |
* |
| 340 |
* The caller is responsible for freeing the returned memory. |
| 341 |
*/ |
| 342 |
char * hostlist_next(hostlist_iterator_t i); |
| 343 |
|
| 344 |
|
| 345 |
/* hostlist_next_range(): |
| 346 |
* |
| 347 |
* Returns the next bracketed hostlist or NULL if the iterator i is |
| 348 |
* at the end of the list. |
| 349 |
* |
| 350 |
* The caller is responsible for freeing the returned memory. |
| 351 |
* |
| 352 |
*/ |
| 353 |
char * hostlist_next_range(hostlist_iterator_t i); |
| 354 |
|
| 355 |
|
| 356 |
/* hostlist_remove(): |
| 357 |
* Removes the last host returned by hostlist iterator i |
| 358 |
* |
| 359 |
* Returns 1 for success, 0 for failure. |
| 360 |
*/ |
| 361 |
int hostlist_remove(hostlist_iterator_t i); |
| 362 |
|
| 363 |
|
| 364 |
/* ----[ hostset operations ]---- */ |
| 365 |
|
| 366 |
/* hostset_create(): |
| 367 |
* |
| 368 |
* Create a new hostset object from a string representation of a list of |
| 369 |
* hosts. See hostlist_create() for valid hostlist forms. |
| 370 |
*/ |
| 371 |
hostset_t hostset_create(const char *hostlist); |
| 372 |
|
| 373 |
/* hostset_copy(): |
| 374 |
* |
| 375 |
* Copy a hostset object. Returned set must be freed with hostset_destroy(). |
| 376 |
*/ |
| 377 |
hostset_t hostset_copy(hostset_t set); |
| 378 |
|
| 379 |
/* hostset_destroy(): |
| 380 |
*/ |
| 381 |
void hostset_destroy(hostset_t set); |
| 382 |
|
| 383 |
/* hostset_insert(): |
| 384 |
* Add a host or list of hosts into hostset "set." |
| 385 |
* |
| 386 |
* Returns number of hosts successfully added to "set" |
| 387 |
* (insertion of a duplicate is not considered successful) |
| 388 |
*/ |
| 389 |
int hostset_insert(hostset_t set, const char *hosts); |
| 390 |
|
| 391 |
/* hostset_delete(): |
| 392 |
* Delete a host or list of hosts from hostset "set." |
| 393 |
* Returns number of hosts deleted from set. |
| 394 |
*/ |
| 395 |
int hostset_delete(hostset_t set, const char *hosts); |
| 396 |
|
| 397 |
/* hostset_within(): |
| 398 |
* Return 1 if all hosts specified by "hosts" are within the hostset "set" |
| 399 |
* Retrun 0 if every host in "hosts" is not in the hostset "set" |
| 400 |
*/ |
| 401 |
int hostset_within(hostset_t set, const char *hosts); |
| 402 |
|
| 403 |
/* hostset_shift(): |
| 404 |
* hostset equivalent to hostlist_shift() |
| 405 |
*/ |
| 406 |
char * hostset_shift(hostset_t set); |
| 407 |
|
| 408 |
/* hostset_shift_range(): |
| 409 |
* hostset eqivalent to hostlist_shift_range() |
| 410 |
*/ |
| 411 |
char * hostset_shift_range(hostset_t set); |
| 412 |
|
| 413 |
/* hostset_count(): |
| 414 |
* Count the number of hosts currently in hostset |
| 415 |
*/ |
| 416 |
int hostset_count(hostset_t set); |
| 417 |
|
| 418 |
|
| 419 |
#endif /* !_HOSTLIST_H */ |