2009-02-19 23:46:36 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
2011-06-24 00:38:02 +04:00
|
|
|
* Copyright (c) 2004-2011 The University of Tennessee and The University
|
2009-02-19 23:46:36 +03:00
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2015-06-24 06:59:57 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
2009-02-19 23:46:36 +03:00
|
|
|
* University of Stuttgart. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2013-05-18 01:45:49 +04:00
|
|
|
* Copyright (c) 2013 Cisco Systems, Inc. All rights reserved.
|
2017-01-23 20:14:20 +03:00
|
|
|
* Copyright (c) 2017 Intel, Inc. All rights reserved.
|
2009-02-19 23:46:36 +03:00
|
|
|
* $COPYRIGHT$
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2009-02-19 23:46:36 +03:00
|
|
|
* Additional copyrights may follow
|
2015-06-24 06:59:57 +03:00
|
|
|
*
|
2009-02-19 23:46:36 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
#include "orte_config.h"
|
|
|
|
#include "orte/types.h"
|
|
|
|
#include "orte/constants.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2009-06-24 00:25:38 +04:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
2009-02-19 23:46:36 +03:00
|
|
|
|
|
|
|
#include "opal/util/argv.h"
|
|
|
|
|
|
|
|
#include "orte/mca/errmgr/errmgr.h"
|
2009-06-24 00:25:38 +04:00
|
|
|
#include "orte/mca/odls/odls_types.h"
|
|
|
|
#include "orte/mca/rml/base/rml_contact.h"
|
2009-08-11 06:51:27 +04:00
|
|
|
#include "orte/mca/rmaps/rmaps_types.h"
|
2009-02-19 23:46:36 +03:00
|
|
|
#include "orte/util/show_help.h"
|
|
|
|
#include "orte/util/name_fns.h"
|
2009-06-24 00:25:38 +04:00
|
|
|
#include "orte/util/nidmap.h"
|
2009-02-19 23:46:36 +03:00
|
|
|
#include "orte/runtime/orte_globals.h"
|
2011-06-24 00:38:02 +04:00
|
|
|
#include "orte/mca/ess/ess.h"
|
2009-02-19 23:46:36 +03:00
|
|
|
|
|
|
|
#include "orte/util/regex.h"
|
|
|
|
|
2009-06-17 06:54:20 +04:00
|
|
|
#define ORTE_MAX_NODE_PREFIX 50
|
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
static int regex_parse_node_ranges(char *base, char *ranges, int num_digits, char *suffix, char ***names);
|
|
|
|
static int regex_parse_node_range(char *base, char *range, int num_digits, char *suffix, char ***names);
|
2009-02-19 23:46:36 +03:00
|
|
|
|
|
|
|
int orte_regex_extract_node_names(char *regexp, char ***names)
|
|
|
|
{
|
2011-07-07 22:54:30 +04:00
|
|
|
int i, j, k, len, ret;
|
2009-02-19 23:46:36 +03:00
|
|
|
char *base;
|
2011-07-07 22:54:30 +04:00
|
|
|
char *orig, *suffix;
|
2009-02-19 23:46:36 +03:00
|
|
|
bool found_range = false;
|
|
|
|
bool more_to_come = false;
|
2011-07-07 22:54:30 +04:00
|
|
|
int num_digits;
|
|
|
|
|
2009-06-17 06:54:20 +04:00
|
|
|
if (NULL == regexp) {
|
|
|
|
*names = NULL;
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
orig = base = strdup(regexp);
|
|
|
|
if (NULL == base) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
OPAL_OUTPUT_VERBOSE((1, orte_debug_output,
|
|
|
|
"%s regex:extract:nodenames: checking nodelist: %s",
|
2009-03-06 00:50:47 +03:00
|
|
|
ORTE_NAME_PRINT(ORTE_PROC_MY_NAME),
|
2009-02-19 23:46:36 +03:00
|
|
|
regexp));
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
do {
|
|
|
|
/* Find the base */
|
|
|
|
len = strlen(base);
|
|
|
|
for (i = 0; i <= len; ++i) {
|
|
|
|
if (base[i] == '[') {
|
|
|
|
/* we found a range. this gets dealt with below */
|
|
|
|
base[i] = '\0';
|
|
|
|
found_range = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (base[i] == ',') {
|
|
|
|
/* we found a singleton node, and there are more to come */
|
|
|
|
base[i] = '\0';
|
|
|
|
found_range = false;
|
|
|
|
more_to_come = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (base[i] == '\0') {
|
|
|
|
/* we found a singleton node */
|
|
|
|
found_range = false;
|
|
|
|
more_to_come = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-05 07:36:30 +04:00
|
|
|
if (i == 0 && !found_range) {
|
2009-02-19 23:46:36 +03:00
|
|
|
/* we found a special character at the beginning of the string */
|
|
|
|
orte_show_help("help-regex.txt", "regex:special-char", true, regexp);
|
|
|
|
free(orig);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
if (found_range) {
|
2011-07-07 22:54:30 +04:00
|
|
|
/* If we found a range, get the number of digits in the numbers */
|
|
|
|
i++; /* step over the [ */
|
|
|
|
for (j=i; j < len; j++) {
|
|
|
|
if (base[j] == ':') {
|
|
|
|
base[j] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j >= len) {
|
|
|
|
/* we didn't find the number of digits */
|
|
|
|
orte_show_help("help-regex.txt", "regex:num-digits-missing", true, regexp);
|
|
|
|
free(orig);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
|
|
|
num_digits = strtol(&base[i], NULL, 10);
|
|
|
|
i = j + 1; /* step over the : */
|
|
|
|
/* now find the end of the range */
|
2009-02-19 23:46:36 +03:00
|
|
|
for (j = i; j < len; ++j) {
|
|
|
|
if (base[j] == ']') {
|
|
|
|
base[j] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (j >= len) {
|
|
|
|
/* we didn't find the end of the range */
|
|
|
|
orte_show_help("help-regex.txt", "regex:end-range-missing", true, regexp);
|
|
|
|
free(orig);
|
|
|
|
return ORTE_ERR_BAD_PARAM;
|
|
|
|
}
|
2011-07-07 22:54:30 +04:00
|
|
|
/* check for a suffix */
|
|
|
|
if (j+1 < len && base[j+1] != ',') {
|
|
|
|
/* find the next comma, if present */
|
|
|
|
for (k=j+1; k < len && base[k] != ','; k++);
|
|
|
|
if (k < len) {
|
|
|
|
base[k] = '\0';
|
|
|
|
}
|
|
|
|
suffix = strdup(&base[j+1]);
|
|
|
|
if (k < len) {
|
|
|
|
base[k] = ',';
|
|
|
|
}
|
|
|
|
j = k-1;
|
|
|
|
} else {
|
|
|
|
suffix = NULL;
|
|
|
|
}
|
2011-07-14 02:49:56 +04:00
|
|
|
OPAL_OUTPUT_VERBOSE((1, orte_debug_output,
|
|
|
|
"%s regex:extract:nodenames: parsing range %s %s %s",
|
|
|
|
ORTE_NAME_PRINT(ORTE_PROC_MY_NAME),
|
|
|
|
base, base + i, suffix));
|
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
ret = regex_parse_node_ranges(base, base + i, num_digits, suffix, names);
|
|
|
|
if (NULL != suffix) {
|
|
|
|
free(suffix);
|
|
|
|
}
|
|
|
|
if (ORTE_SUCCESS != ret) {
|
2009-02-19 23:46:36 +03:00
|
|
|
orte_show_help("help-regex.txt", "regex:bad-value", true, regexp);
|
|
|
|
free(orig);
|
|
|
|
return ret;
|
2011-07-07 22:54:30 +04:00
|
|
|
}
|
|
|
|
if (j+1 < len && base[j + 1] == ',') {
|
2009-02-19 23:46:36 +03:00
|
|
|
more_to_come = true;
|
|
|
|
base = &base[j + 2];
|
|
|
|
} else {
|
|
|
|
more_to_come = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* If we didn't find a range, just add the node */
|
|
|
|
if(ORTE_SUCCESS != (ret = opal_argv_append_nosize(names, base))) {
|
|
|
|
ORTE_ERROR_LOG(ret);
|
|
|
|
free(orig);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-08-05 18:37:36 +04:00
|
|
|
/* step over the comma */
|
|
|
|
i++;
|
2009-02-19 23:46:36 +03:00
|
|
|
/* set base equal to the (possible) next base to look at */
|
2011-07-07 22:54:30 +04:00
|
|
|
base = &base[i];
|
2009-02-19 23:46:36 +03:00
|
|
|
}
|
|
|
|
} while(more_to_come);
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
free(orig);
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* All done */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse one or more ranges in a set
|
|
|
|
*
|
|
|
|
* @param base The base text of the node name
|
|
|
|
* @param *ranges A pointer to a range. This can contain multiple ranges
|
2015-06-24 06:59:57 +03:00
|
|
|
* (i.e. "1-3,10" or "5" or "9,0100-0130,250")
|
2009-02-19 23:46:36 +03:00
|
|
|
* @param ***names An argv array to add the newly discovered nodes to
|
|
|
|
*/
|
2011-07-07 22:54:30 +04:00
|
|
|
static int regex_parse_node_ranges(char *base, char *ranges, int num_digits, char *suffix, char ***names)
|
2009-02-19 23:46:36 +03:00
|
|
|
{
|
|
|
|
int i, len, ret;
|
|
|
|
char *start, *orig;
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Look for commas, the separator between ranges */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
len = strlen(ranges);
|
|
|
|
for (orig = start = ranges, i = 0; i < len; ++i) {
|
|
|
|
if (',' == ranges[i]) {
|
|
|
|
ranges[i] = '\0';
|
2011-07-07 22:54:30 +04:00
|
|
|
ret = regex_parse_node_range(base, start, num_digits, suffix, names);
|
2009-02-19 23:46:36 +03:00
|
|
|
if (ORTE_SUCCESS != ret) {
|
|
|
|
ORTE_ERROR_LOG(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
start = ranges + i + 1;
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Pick up the last range, if it exists */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
if (start < orig + len) {
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
OPAL_OUTPUT_VERBOSE((1, orte_debug_output,
|
|
|
|
"%s regex:parse:ranges: parse range %s (2)",
|
2009-03-06 00:50:47 +03:00
|
|
|
ORTE_NAME_PRINT(ORTE_PROC_MY_NAME), start));
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
ret = regex_parse_node_range(base, start, num_digits, suffix, names);
|
2009-02-19 23:46:36 +03:00
|
|
|
if (ORTE_SUCCESS != ret) {
|
|
|
|
ORTE_ERROR_LOG(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* All done */
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a single range in a set and add the full names of the nodes
|
|
|
|
* found to the names argv
|
|
|
|
*
|
|
|
|
* @param base The base text of the node name
|
2015-06-24 06:59:57 +03:00
|
|
|
* @param *ranges A pointer to a single range. (i.e. "1-3" or "5")
|
2009-02-19 23:46:36 +03:00
|
|
|
* @param ***names An argv array to add the newly discovered nodes to
|
|
|
|
*/
|
2011-07-07 22:54:30 +04:00
|
|
|
static int regex_parse_node_range(char *base, char *range, int num_digits, char *suffix, char ***names)
|
2009-02-19 23:46:36 +03:00
|
|
|
{
|
2011-07-07 22:54:30 +04:00
|
|
|
char *str, tmp[132];
|
|
|
|
size_t i, k, start, end;
|
|
|
|
size_t base_len, len;
|
2009-02-19 23:46:36 +03:00
|
|
|
bool found;
|
|
|
|
int ret;
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2014-01-19 20:31:26 +04:00
|
|
|
if (NULL == base || NULL == range) {
|
|
|
|
return ORTE_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
len = strlen(range);
|
|
|
|
base_len = strlen(base);
|
|
|
|
/* Silence compiler warnings; start and end are always assigned
|
|
|
|
properly, below */
|
|
|
|
start = end = 0;
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Look for the beginning of the first number */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
for (found = false, i = 0; i < len; ++i) {
|
|
|
|
if (isdigit((int) range[i])) {
|
|
|
|
if (!found) {
|
|
|
|
start = atoi(range + i);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_NOT_FOUND);
|
|
|
|
return ORTE_ERR_NOT_FOUND;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Look for the end of the first number */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
for (found = false; i < len; ++i) {
|
|
|
|
if (!isdigit(range[i])) {
|
2009-02-19 23:46:36 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Was there no range, just a single number? */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
if (i >= len) {
|
|
|
|
end = start;
|
|
|
|
found = true;
|
2011-07-07 22:54:30 +04:00
|
|
|
} else {
|
|
|
|
/* Nope, there was a range. Look for the beginning of the second
|
|
|
|
* number
|
|
|
|
*/
|
2009-02-19 23:46:36 +03:00
|
|
|
for (; i < len; ++i) {
|
2011-07-07 22:54:30 +04:00
|
|
|
if (isdigit(range[i])) {
|
|
|
|
end = strtol(range + i, NULL, 10);
|
2009-02-19 23:46:36 +03:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_NOT_FOUND);
|
|
|
|
return ORTE_ERR_NOT_FOUND;
|
|
|
|
}
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* Make strings for all values in the range */
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
len = base_len + num_digits + 32;
|
|
|
|
if (NULL != suffix) {
|
|
|
|
len += strlen(suffix);
|
|
|
|
}
|
2009-02-23 17:09:07 +03:00
|
|
|
str = (char *) malloc(len);
|
2009-02-19 23:46:36 +03:00
|
|
|
if (NULL == str) {
|
|
|
|
ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
|
|
|
|
return ORTE_ERR_OUT_OF_RESOURCE;
|
|
|
|
}
|
|
|
|
for (i = start; i <= end; ++i) {
|
2011-07-07 22:54:30 +04:00
|
|
|
memset(str, 0, len);
|
|
|
|
strcpy(str, base);
|
|
|
|
/* we need to zero-pad the digits */
|
|
|
|
for (k=0; k < (size_t)num_digits; k++) {
|
|
|
|
str[k+base_len] = '0';
|
|
|
|
}
|
|
|
|
memset(tmp, 0, 132);
|
|
|
|
snprintf(tmp, 132, "%lu", (unsigned long)i);
|
|
|
|
for (k=0; k < strlen(tmp); k++) {
|
|
|
|
str[base_len + num_digits - k - 1] = tmp[strlen(tmp)-k-1];
|
2009-02-19 23:46:36 +03:00
|
|
|
}
|
2009-06-18 08:36:00 +04:00
|
|
|
/* if there is a suffix, add it */
|
2011-07-07 22:54:30 +04:00
|
|
|
if (NULL != suffix) {
|
|
|
|
strcat(str, suffix);
|
2009-06-18 08:36:00 +04:00
|
|
|
}
|
2009-02-19 23:46:36 +03:00
|
|
|
ret = opal_argv_append_nosize(names, str);
|
|
|
|
if(ORTE_SUCCESS != ret) {
|
|
|
|
ORTE_ERROR_LOG(ret);
|
|
|
|
free(str);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(str);
|
2015-06-24 06:59:57 +03:00
|
|
|
|
2009-02-19 23:46:36 +03:00
|
|
|
/* All done */
|
|
|
|
return ORTE_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:33:14 +03:00
|
|
|
/***** CLASS INSTANTIATIONS ****/
|
2014-03-18 01:25:05 +04:00
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
static void range_construct(orte_regex_range_t *ptr)
|
|
|
|
{
|
2017-03-30 04:28:25 +03:00
|
|
|
ptr->vpid = 0;
|
2011-07-07 22:54:30 +04:00
|
|
|
ptr->cnt = 0;
|
|
|
|
}
|
|
|
|
OBJ_CLASS_INSTANCE(orte_regex_range_t,
|
|
|
|
opal_list_item_t,
|
|
|
|
range_construct, NULL);
|
|
|
|
|
|
|
|
static void orte_regex_node_construct(orte_regex_node_t *ptr)
|
|
|
|
{
|
|
|
|
ptr->prefix = NULL;
|
|
|
|
ptr->suffix = NULL;
|
|
|
|
ptr->num_digits = 0;
|
|
|
|
OBJ_CONSTRUCT(&ptr->ranges, opal_list_t);
|
|
|
|
}
|
|
|
|
static void orte_regex_node_destruct(orte_regex_node_t *ptr)
|
2009-06-18 08:36:00 +04:00
|
|
|
{
|
2009-06-24 00:25:38 +04:00
|
|
|
opal_list_item_t *item;
|
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
if (NULL != ptr->prefix) {
|
|
|
|
free(ptr->prefix);
|
2009-06-18 08:36:00 +04:00
|
|
|
}
|
2011-07-07 22:54:30 +04:00
|
|
|
if (NULL != ptr->suffix) {
|
|
|
|
free(ptr->suffix);
|
2009-06-18 08:36:00 +04:00
|
|
|
}
|
2009-06-24 00:25:38 +04:00
|
|
|
|
2011-07-07 22:54:30 +04:00
|
|
|
while (NULL != (item = opal_list_remove_first(&ptr->ranges))) {
|
|
|
|
OBJ_RELEASE(item);
|
2009-06-24 00:25:38 +04:00
|
|
|
}
|
2011-07-07 22:54:30 +04:00
|
|
|
OBJ_DESTRUCT(&ptr->ranges);
|
2009-06-18 08:36:00 +04:00
|
|
|
}
|
2011-07-07 22:54:30 +04:00
|
|
|
OBJ_CLASS_INSTANCE(orte_regex_node_t,
|
|
|
|
opal_list_item_t,
|
|
|
|
orte_regex_node_construct,
|
|
|
|
orte_regex_node_destruct);
|