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