root/opal/mca/base/mca_base_cmd_line.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mca_base_cmd_line_setup
  2. mca_base_cmd_line_process_args
  3. process_arg
  4. add_to_env
  5. mca_base_cmd_line_wrap_args

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2014      Intel, Inc. All rights reserved.
  13  * Copyright (c) 2014-2015 Cisco Systems, Inc.  All rights reserved.
  14  * Copyright (c) 2018      Amazon.com, Inc. or its affiliates.  All Rights reserved.
  15  * $COPYRIGHT$
  16  *
  17  * Additional copyrights may follow
  18  *
  19  * $HEADER$
  20  */
  21 
  22 #include "opal_config.h"
  23 
  24 #include <stdio.h>
  25 #include <string.h>
  26 
  27 #include "opal/util/cmd_line.h"
  28 #include "opal/util/argv.h"
  29 #include "opal/util/opal_environ.h"
  30 #include "opal/util/show_help.h"
  31 #include "opal/util/printf.h"
  32 #include "opal/mca/base/base.h"
  33 #include "opal/constants.h"
  34 
  35 
  36 /*
  37  * Private variables
  38  */
  39 
  40 /*
  41  * Private functions
  42  */
  43 static int process_arg(const char *param, const char *value,
  44                        char ***params, char ***values);
  45 static void add_to_env(char **params, char **values, char ***env);
  46 
  47 
  48 /*
  49  * Add -mca to the possible command line options list
  50  */
  51 int mca_base_cmd_line_setup(opal_cmd_line_t *cmd)
  52 {
  53     int ret = OPAL_SUCCESS;
  54 
  55     ret = opal_cmd_line_make_opt3(cmd, '\0', OPAL_MCA_CMD_LINE_ID, OPAL_MCA_CMD_LINE_ID, 2,
  56                                   "Pass context-specific MCA parameters; they are considered global if --g"OPAL_MCA_CMD_LINE_ID" is not used and only one context is specified (arg0 is the parameter name; arg1 is the parameter value)");
  57     if (OPAL_SUCCESS != ret) {
  58         return ret;
  59     }
  60 
  61     ret = opal_cmd_line_make_opt3(cmd, '\0', "g"OPAL_MCA_CMD_LINE_ID, "g"OPAL_MCA_CMD_LINE_ID, 2,
  62                                   "Pass global MCA parameters that are applicable to all contexts (arg0 is the parameter name; arg1 is the parameter value)");
  63 
  64     if (OPAL_SUCCESS != ret) {
  65         return ret;
  66     }
  67 
  68     {
  69         opal_cmd_line_init_t entry =
  70             {"mca_base_param_file_prefix", '\0', "am", NULL, 1,
  71              NULL, OPAL_CMD_LINE_TYPE_STRING,
  72              "Aggregate MCA parameter set file list",
  73              OPAL_CMD_LINE_OTYPE_LAUNCH
  74             };
  75         ret = opal_cmd_line_make_opt_mca(cmd, entry);
  76         if (OPAL_SUCCESS != ret) {
  77             return ret;
  78         }
  79     }
  80 
  81     {
  82         opal_cmd_line_init_t entry =
  83             {"mca_base_envar_file_prefix", '\0', "tune", NULL, 1,
  84              NULL, OPAL_CMD_LINE_TYPE_STRING,
  85              "Application profile options file list",
  86              OPAL_CMD_LINE_OTYPE_DEBUG
  87             };
  88         ret = opal_cmd_line_make_opt_mca(cmd, entry);
  89         if (OPAL_SUCCESS != ret) {
  90             return ret;
  91         }
  92     }
  93 
  94     return ret;
  95 }
  96 
  97 
  98 /*
  99  * Look for and handle any -mca options on the command line
 100  */
 101 int mca_base_cmd_line_process_args(opal_cmd_line_t *cmd,
 102                                    char ***context_env, char ***global_env)
 103 {
 104     int i, num_insts, rc;
 105     char **params;
 106     char **values;
 107 
 108     /* If no relevant parameters were given, just return */
 109 
 110     if (!opal_cmd_line_is_taken(cmd, OPAL_MCA_CMD_LINE_ID) &&
 111         !opal_cmd_line_is_taken(cmd, "g"OPAL_MCA_CMD_LINE_ID)) {
 112         return OPAL_SUCCESS;
 113     }
 114 
 115     /* Handle app context-specific parameters */
 116 
 117     num_insts = opal_cmd_line_get_ninsts(cmd, OPAL_MCA_CMD_LINE_ID);
 118     params = values = NULL;
 119     for (i = 0; i < num_insts; ++i) {
 120         if (OPAL_SUCCESS != (rc = process_arg(opal_cmd_line_get_param(cmd, OPAL_MCA_CMD_LINE_ID, i, 0),
 121                                               opal_cmd_line_get_param(cmd, OPAL_MCA_CMD_LINE_ID, i, 1),
 122                                               &params, &values))) {
 123             return rc;
 124         }
 125     }
 126     if (NULL != params) {
 127         add_to_env(params, values, context_env);
 128         opal_argv_free(params);
 129         opal_argv_free(values);
 130     }
 131 
 132     /* Handle global parameters */
 133 
 134     num_insts = opal_cmd_line_get_ninsts(cmd, "g"OPAL_MCA_CMD_LINE_ID);
 135     params = values = NULL;
 136     for (i = 0; i < num_insts; ++i) {
 137         if (OPAL_SUCCESS != (rc = process_arg(opal_cmd_line_get_param(cmd, "g"OPAL_MCA_CMD_LINE_ID, i, 0),
 138                                               opal_cmd_line_get_param(cmd, "g"OPAL_MCA_CMD_LINE_ID, i, 1),
 139                                               &params, &values))) {
 140             return rc;
 141         }
 142     }
 143     if (NULL != params) {
 144         add_to_env(params, values, global_env);
 145         opal_argv_free(params);
 146         opal_argv_free(values);
 147     }
 148 
 149     /* All done */
 150 
 151     return OPAL_SUCCESS;
 152 }
 153 
 154 
 155 
 156 /*
 157  * Process a single MCA argument.
 158  */
 159 static int process_arg(const char *param, const char *value,
 160                        char ***params, char ***values)
 161 {
 162     int i;
 163     char *p1;
 164 
 165     /* check for quoted value */
 166     if ('\"' == value[0] && '\"' == value[strlen(value)-1]) {
 167         p1 = strdup(&value[1]);
 168         p1[strlen(p1)-1] = '\0';
 169     } else {
 170         p1 = strdup(value);
 171     }
 172 
 173     /* Look to see if we've already got an -mca argument for the same
 174        param.  Check against the list of MCA param's that we've
 175        already saved arguments for - if found, return an error. */
 176 
 177     for (i = 0; NULL != *params && NULL != (*params)[i]; ++i) {
 178         if (0 == strcmp(param, (*params)[i])) {
 179             /* cannot use show_help here as it may not get out prior
 180              * to the process exiting */
 181             fprintf(stderr,
 182                     "---------------------------------------------------------------------------\n"
 183                     "The following MCA parameter has been listed multiple times on the\n"
 184                     "command line:\n\n"
 185                     "  MCA param:   %s\n\n"
 186                     "MCA parameters can only be listed once on a command line to ensure there\n"
 187                     "is no ambiguity as to its value.  Please correct the situation and\n"
 188                     "try again.\n"
 189                     "---------------------------------------------------------------------------\n",
 190                     param);
 191             free(p1);
 192             return OPAL_ERROR;
 193         }
 194     }
 195 
 196     /* If we didn't already have an value for the same param, save
 197        this one away */
 198     opal_argv_append_nosize(params, param);
 199     opal_argv_append_nosize(values, p1);
 200     free(p1);
 201 
 202     return OPAL_SUCCESS;
 203 }
 204 
 205 
 206 static void add_to_env(char **params, char **values, char ***env)
 207 {
 208     int i;
 209     char *name;
 210 
 211     /* Loop through all the args that we've gotten and make env
 212        vars of the form OPAL_MCA_PREFIX*=value. */
 213 
 214     for (i = 0; NULL != params && NULL != params[i]; ++i) {
 215         (void) mca_base_var_env_name (params[i], &name);
 216         opal_setenv(name, values[i], true, env);
 217         free(name);
 218     }
 219 }
 220 
 221 void mca_base_cmd_line_wrap_args(char **args)
 222 {
 223     int i;
 224     char *tstr;
 225 
 226     for (i=0; NULL != args && NULL != args[i]; i++) {
 227         if (0 == strcmp(args[i], "-"OPAL_MCA_CMD_LINE_ID) ||
 228             0 == strcmp(args[i], "--"OPAL_MCA_CMD_LINE_ID)) {
 229             if (NULL == args[i+1] || NULL == args[i+2]) {
 230                 /* this should be impossible as the error would
 231                  * have been detected well before here, but just
 232                  * be safe */
 233                 return;
 234             }
 235             i += 2;
 236             opal_asprintf(&tstr, "\"%s\"", args[i]);
 237             free(args[i]);
 238             args[i] = tstr;
 239         }
 240     }
 241 }

/* [<][>][^][v][top][bottom][index][help] */