1 /*****************************************************************************\
2 * $Id: hostlist.h,v 1.1.10.2 2008/12/18 18:02:14 johann Exp $
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 <mgrondona@llnl.gov>
9 * This file is part of SLURM, a resource management program.
10 * For details, see <http://www.llnl.gov/linux/slurm/>.
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)
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
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 \*****************************************************************************/
32 * If WITH_LSD_FATAL_ERROR_FUNC is defined, the linker will expect to
33 * find and external lsd_fatal_error(file,line,mesg) function. By default,
34 * lsd_fatal_error(file,line,mesg) is a macro definition that outputs an
35 * error message to stderr. This macro may be redefined to invoke another
36 * routine instead. e.g.:
38 * #define lsd_fatal_error(file,line,mesg) \
39 * error("%s:%s %s\n",file,line,mesg);
41 * If WITH_LSD_NOMEM_ERROR_FUNC is defined, the linker will expect to
42 * find an external lsd_nomem_error(file,line,mesg) function. By default,
43 * lsd_nomem_error(file,line,mesg) is a macro definition that returns NULL.
44 * This macro may be redefined to invoke another routine instead.
46 * If WITH_PTHREADS is defined, these routines will be thread-safe.
50 /* The hostlist opaque data type
52 * A hostlist is a list of hostnames optimized for a prefixXXXX style
53 * naming convention, where XXXX is a decimal, numeric suffix.
55 typedef struct hostlist * hostlist_t;
57 /* A hostset is a special case of a hostlist. It:
59 * 1. never contains duplicates
61 * (Note: sort occurs first on alphanumeric prefix -- where prefix
62 * matches, numeric suffixes will be sorted *by value*)
64 typedef struct hostset * hostset_t;
66 /* The hostlist iterator type (may be used with a hostset as well)
67 * used for non-destructive access to hostlist members.
70 typedef struct hostlist_iterator * hostlist_iterator_t;
72 /* ----[ hostlist_t functions: ]---- */
74 /* ----[ hostlist creation and destruction ]---- */
79 * Create a new hostlist from a string representation.
81 * The string representation (str) may contain one or more hostnames or
82 * bracketed hostlists separated by either `,' or whitespace. A bracketed
83 * hostlist is denoted by a common prefix followed by a list of numeric
84 * ranges contained within brackets: e.g. "tux[0-5,12,20-25]"
86 * Note: if this module is compiled with WANT_RECKLESS_HOSTRANGE_EXPANSION
87 * defined, a much more loose interpretation of host ranges is used.
88 * Reckless hostrange expansion allows all of the following (in addition to
89 * bracketed hostlists):
91 * o tux0-5,tux12,tux20-25
92 * o tux0-tux5,tux12,tux20-tux25
95 * If str is NULL, and empty hostlist is created and returned.
97 * If the create fails, hostlist_create() returns NULL.
99 * The returned hostlist must be freed with hostlist_destroy()
102 hostlist_t hostlist_create(const char *hostlist);
106 * Allocate a copy of a hostlist object. Returned hostlist must be freed
107 * with hostlist_destroy.
109 hostlist_t hostlist_copy(const hostlist_t hl);
111 /* hostlist_destroy():
113 * Destroy a hostlist object. Frees all memory allocated to the hostlist.
115 void hostlist_destroy(hostlist_t hl);
118 /* ----[ hostlist list operations ]---- */
122 * push a string representation of hostnames onto a hostlist.
124 * The hosts argument may take the same form as in hostlist_create()
126 * Returns the number of hostnames inserted into the list,
129 int hostlist_push(hostlist_t hl, const char *hosts);
132 /* hostlist_push_host():
134 * Push a single host onto the hostlist hl.
135 * This function is more efficient than hostlist_push() for a single
136 * hostname, since the argument does not need to be checked for ranges.
138 * return value is 1 for success, 0 for failure.
140 int hostlist_push_host(hostlist_t hl, const char *host);
143 /* hostlist_push_list():
145 * Push a hostlist (hl2) onto another list (hl1)
147 * Returns 1 for success, 0 for failure.
150 int hostlist_push_list(hostlist_t hl1, hostlist_t hl2);
155 * Returns the string representation of the last host pushed onto the list
156 * or NULL if hostlist is empty or there was an error allocating memory.
157 * The host is removed from the hostlist.
159 * Note: Caller is responsible for freeing the returned memory.
161 char * hostlist_pop(hostlist_t hl);
164 char * hostlist_nth(hostlist_t hl, int n);
168 * Returns the string representation of the first host in the hostlist
169 * or NULL if the hostlist is empty or there was an error allocating memory.
170 * The host is removed from the hostlist.
172 * Note: Caller is responsible for freeing the returned memory.
174 char * hostlist_shift(hostlist_t hl);
177 /* hostlist_pop_range():
179 * Pop the last bracketed list of hosts of the hostlist hl.
180 * Returns the string representation in bracketed list form.
181 * All hosts associated with the returned list are removed
184 * Caller is responsible for freeing returned memory
186 char * hostlist_pop_range(hostlist_t hl);
188 /* hostlist_shift_range():
190 * Shift the first bracketed hostlist (improperly: range) off the
191 * hostlist hl. Returns the string representation in bracketed list
192 * form. All hosts associated with the list are removed from the
195 * Caller is responsible for freeing returned memory.
197 char * hostlist_shift_range(hostlist_t hl);
202 * Searches hostlist hl for the first host matching hostname
203 * and returns position in list if found.
205 * Returns -1 if host is not found.
208 int hostlist_find(hostlist_t hl, const char *hostname);
210 /* hostlist_delete():
212 * Deletes all hosts in the list represented by `hosts'
214 * Returns the number of hosts successfully deleted
216 int hostlist_delete(hostlist_t hl, const char *hosts);
219 /* hostlist_delete_host():
221 * Deletes the first host that matches `hostname' from the hostlist hl.
222 * Note: "hostname" argument cannot contain a range of hosts
223 * (see hostlist_delete() for this functionality.)
225 * Returns 1 if successful, 0 if hostname is not found in list.
227 int hostlist_delete_host(hostlist_t hl, const char *hostname);
230 /* hostlist_delete_nth():
232 * Deletes the host from position n in the hostlist.
234 * Returns 1 if successful 0 on error.
237 int hostlist_delete_nth(hostlist_t hl, int n);
242 * Return the number of hosts in hostlist hl.
244 int hostlist_count(hostlist_t hl);
246 /* hostlist_is_empty(): return true if hostlist is empty. */
247 #define hostlist_is_empty(__hl) ( hostlist_count(__hl) == 0 )
249 /* ----[ Other hostlist operations ]---- */
253 * Sort the hostlist hl.
256 void hostlist_sort(hostlist_t hl);
260 * Sort the hostlist hl and remove duplicate entries.
263 void hostlist_uniq(hostlist_t hl);
266 /* ----[ hostlist print functions ]---- */
268 /* hostlist_ranged_string():
270 * Write the string representation of the hostlist hl into buf,
271 * writing at most n chars. Returns the number of bytes written,
272 * or -1 if truncation occurred.
274 * The result will be NULL terminated.
276 * hostlist_ranged_string() will write a bracketed hostlist representation
279 size_t hostlist_ranged_string(hostlist_t hl, size_t n, char *buf);
280 size_t hostset_ranged_string(hostset_t hs, size_t n, char *buf);
282 /* hostlist_deranged_string():
284 * Writes the string representation of the hostlist hl into buf,
285 * writing at most n chars. Returns the number of bytes written,
286 * or -1 if truncation occurred.
288 * hostlist_deranged_string() will not attempt to write a bracketed
289 * hostlist representation. Every hostname will be explicitly written.
291 size_t hostlist_deranged_string(hostlist_t hl, size_t n, char *buf);
292 size_t hostset_deranged_string(hostset_t hs, size_t n, char *buf);
295 /* ----[ hostlist utility functions ]---- */
298 /* hostlist_nranges():
300 * Return the number of ranges currently held in hostlist hl.
302 int hostlist_nranges(hostlist_t hl);
305 /* ----[ hostlist iterator functions ]---- */
307 /* hostlist_iterator_create():
309 * Creates and returns a hostlist iterator used for non destructive
310 * access to a hostlist or hostset. Returns NULL on failure.
312 hostlist_iterator_t hostlist_iterator_create(hostlist_t hl);
314 /* hostset_iterator_create():
316 * Same as hostlist_iterator_create(), but creates a hostlist_iterator
319 hostlist_iterator_t hostset_iterator_create(hostset_t set);
321 /* hostlist_iterator_destroy():
323 * Destroys a hostlist iterator.
325 void hostlist_iterator_destroy(hostlist_iterator_t i);
327 /* hostlist_iterator_reset():
329 * Reset an iterator to the beginning of the list.
331 void hostlist_iterator_reset(hostlist_iterator_t i);
335 * Returns a pointer to the next hostname on the hostlist
336 * or NULL at the end of the list
338 * The caller is responsible for freeing the returned memory.
340 char * hostlist_next(hostlist_iterator_t i);
343 /* hostlist_next_range():
345 * Returns the next bracketed hostlist or NULL if the iterator i is
346 * at the end of the list.
348 * The caller is responsible for freeing the returned memory.
351 char * hostlist_next_range(hostlist_iterator_t i);
354 /* hostlist_remove():
355 * Removes the last host returned by hostlist iterator i
357 * Returns 1 for success, 0 for failure.
359 int hostlist_remove(hostlist_iterator_t i);
362 /* ----[ hostset operations ]---- */
366 * Create a new hostset object from a string representation of a list of
367 * hosts. See hostlist_create() for valid hostlist forms.
369 hostset_t hostset_create(const char *hostlist);
373 * Copy a hostset object. Returned set must be freed with hostset_destroy().
375 hostset_t hostset_copy(hostset_t set);
377 /* hostset_destroy():
379 void hostset_destroy(hostset_t set);
382 * Add a host or list of hosts into hostset "set."
384 * Returns number of hosts successfully added to "set"
385 * (insertion of a duplicate is not considered successful)
387 int hostset_insert(hostset_t set, const char *hosts);
390 * Delete a host or list of hosts from hostset "set."
391 * Returns number of hosts deleted from set.
393 int hostset_delete(hostset_t set, const char *hosts);
396 * Return 1 if all hosts specified by "hosts" are within the hostset "set"
397 * Retrun 0 if every host in "hosts" is not in the hostset "set"
399 int hostset_within(hostset_t set, const char *hosts);
402 * hostset equivalent to hostlist_shift()
404 char * hostset_shift(hostset_t set);
406 /* hostset_shift_range():
407 * hostset eqivalent to hostlist_shift_range()
409 char * hostset_shift_range(hostset_t set);
412 * Count the number of hosts currently in hostset
414 int hostset_count(hostset_t set);
417 #endif /* !_HOSTLIST_H */