1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
/* $Id: start_activity.c,v 1.23 2004/10/21 05:49:26 graziano Exp $ */
#include "config_nws.h"
#include <stdio.h> /* printf() sprintf() */
#include <stdlib.h> /* atoi() free() REALLOC() */
#include <string.h> /* strcat() strcpy() strchr() */
#include <unistd.h> /* getopt() */
#define NWSAPI_SHORTNAMES
#include "nws_sensor.h"
#include "diagnostic.h" /* NWS diagnostic messages. */
#include "cliques.h"
#include "osutil.h"
#include "register.h"
#include "nws_api.h" /* Interface functions */
/*
** This program allows the user to launch and terminate NWS activities from the
** command line. See the USERSGUIDE for a description of the command-line
** options.
*/
/*
** Parses #option# as a "name:value" pair and appends it to the #optionsCount#-
** long string array #optionsList#, updating #optionsCount# as appropriate.
** Allows for the pair to be split between sequential calls (i.e. "name:" one
** call, "value" the next). Returns 1 if successful, else 0.
*/
static int
AddOption(const char *option,
char ***optionList,
size_t *optionsCount) {
static int isValue = 0;
char **lastOption;
const char *valueStart;
if(isValue) {
valueStart = option;
}
else {
valueStart = strchr(option, ':');
if(valueStart == NULL) {
fprintf(stderr, "AddOption: colon missing from option %s\n", option);
return 0;
}
valueStart++;
}
if(isValue) {
lastOption = (*optionList) + (*optionsCount - 1);
*lastOption =
REALLOC(*lastOption, strlen(*lastOption) + strlen(option) + 1);
strcat(*lastOption, option);
}
else {
(*optionsCount)++;
*optionList = (char **)
REALLOC(*optionList, *optionsCount * sizeof(char *));
(*optionList)[*optionsCount - 1] = strdup(option);
}
isValue = *valueStart == '\0';
return 1;
}
/*
** Frees all elements of the #length#-long allocated element array #array#.
*/
static void
FreeAllElements(void **array,
size_t length) {
int i;
for(i = 0; i < length; i++)
free(array[i]);
}
/*
** Returns the value of the #attributeName# attribute from #object#, or an
** empty string if no such attribute exists. The returned memory needs to be freed
*/
static char *
GetNwsAttributeValue(const Object object,
const char *attributeName) {
NwsAttribute attribute;
char *tmp;
int ret;
ForEachNwsAttribute(object, attribute) {
tmp = NwsAttributeName_r(attribute);
if (tmp == NULL) {
continue;
}
ret = strcmp(tmp, attributeName);
free(tmp);
if (ret == 0) {
return NwsAttributeValue_r(attribute);
}
}
return strdup("");
}
/*
** Retrieves control and skill names matching the partial names in #control#
** and #skill# that have been registered by #sensor#, then attempts to narrow
** the choices to one control/skill pair using the options listed in the
** #optionsCount#-long list #options#. If successful, returns 1 and sets
** #control# and #skill# to the retrieved names; otherwise, returns 0.
*/
static int
GetControlAndSkill(const HostSpec *sensor,
char *control,
char *skill,
const char **options,
int optionsCount) {
Object controlObject;
ObjectSet controlObjects;
char filter[255 + 1];
int i;
char optionName[255 + 1];
char possiblePairs[255 + 1];
char recognizedOptions[255 + 1];
Object skillObject;
ObjectSet skillObjects;
char supportedSkills[255 + 1];
char *tmp;
/* Grab lists of matching controls and objects from the name server. */
sprintf(filter, "(&(%s=%s:%d)%s(%s=*%s*))",
HOST_ATTR, sensor->machineName, sensor->machinePort,
CONTROLS, CONTROL_ATTR, control);
if(!GetObjects(filter, &controlObjects)) {
fprintf(stderr, "GetControlAndSkill: unable to contact name server\n");
return 0;
}
sprintf(filter, "(&(%s=%s:%d)%s(%s=*%s*))",
HOST_ATTR, sensor->machineName, sensor->machinePort,
SKILLS, SKILL_ATTR, skill);
if(!GetObjects(filter, &skillObjects)) {
FreeObjectSet(&controlObjects);
fprintf(stderr, "GetControlAndSkill: unable to contact name server\n");
return 0;
}
/*
** For each control/supported skill pair, match the option list against the
** control and skill options to see if the pair recognizes all of them.
** Throw out pairs where this isn't true and hope that we have exactly one
** pair at the end.
*/
possiblePairs[0] = '\0';
ForEachObject(controlObjects, controlObject) {
tmp = GetNwsAttributeValue(controlObject, SKILL_ATTR);
strcpy(supportedSkills, tmp);
free(tmp);
ForEachObject(skillObjects, skillObject) {
tmp = GetNwsAttributeValue(skillObject, SKILL_ATTR);
if(strstr(supportedSkills, tmp) == NULL) {
free(tmp);
continue;
}
free(tmp);
strcpy(recognizedOptions, ",");
tmp = GetNwsAttributeValue(controlObject, OPTION_ATTR);
strcat(recognizedOptions, tmp);
free(tmp);
strcat(recognizedOptions, ",");
tmp = GetNwsAttributeValue(skillObject, OPTION_ATTR);
strcat(recognizedOptions, tmp);
free(tmp);
strcat(recognizedOptions, ",");
for(i = 0; i < optionsCount; i++) {
strcpy(optionName, ",");
strcat(optionName, options[i]);
*strchr(optionName, ':') = '\0';
strcat(optionName, ",");
if(strstr(recognizedOptions, optionName) == NULL)
break; /* Unknown option. */
}
if(i == optionsCount) {
tmp = GetNwsAttributeValue(controlObject, CONTROL_ATTR);
strcpy(control, tmp);
free(tmp);
tmp = GetNwsAttributeValue(skillObject, SKILL_ATTR);
strcpy(skill, tmp);
free(tmp);
if(possiblePairs[0] != '\0')
strcat(possiblePairs, "\n");
strcat(possiblePairs, CONTROL_ATTR);
strcat(possiblePairs, ":");
strcat(possiblePairs, control);
strcat(possiblePairs, " ");
strcat(possiblePairs, SKILL_ATTR);
strcat(possiblePairs, ":");
strcat(possiblePairs, skill);
}
}
}
FreeObjectSet(&controlObjects);
FreeObjectSet(&skillObjects);
if(possiblePairs[0] == '\0') {
fprintf(stderr, "%s\n%s\n%s\n",
"No registered control and skill support all listed options",
"Please check your spelling, make sure the requested skill is",
" available on this host and try again");
return 0;
}
else if(strchr(possiblePairs, '\n') != NULL) {
fprintf(stderr, "Ambiguous control/skill; please specify one of:\n");
fprintf(stderr, "%s\n", possiblePairs);
return 0;
}
return 1;
}
/*
** Returns 1 or 0 depending or not an object named #name# is registered with
** the name server.
*/
static int
LookupName(const char *name) {
char filter[127 + 1];
ObjectSet objects;
int returnValue;
sprintf(filter, "(%s=%s)", NAME_ATTR, name);
if(!GetObjects(filter, &objects)) {
return 0;
}
returnValue = FirstObject(objects) != NULL;
FreeObjectSet(&objects);
return returnValue;
}
/*
** Reads #fileName# and appends to #optionsList# any "name:value" pairs
** contained within. Returns 1 if successful, else 0.
*/
static int
ParseOptionsFile(const char *fileName,
char ***optionsList,
size_t *optionsCount) {
char line[255 + 1];
FILE *optionsFile;
char *word;
optionsFile = fopen(fileName, "r");
if(optionsFile == NULL) {
fprintf(stderr, "ParseOptionsFile: unable to open file %s\n", fileName);
return 0;
}
while(fgets(line, sizeof(line), optionsFile) != NULL) {
for(word = strtok(line, " \n\t");
(word != NULL) && (*word != '#');
word = strtok(NULL, " \n\t")) {
if(!AddOption(word, optionsList, optionsCount)) {
fclose(optionsFile);
return 0; /* Message will come from AddOption(). */
}
}
}
(void)fclose(optionsFile);
return 1;
}
static void
RemoveOption(const char *optionName,
char ***options,
size_t *optionsCount,
char *value) {
int i;
int j;
int nameLen = strlen(optionName);
*value = '\0';
for(i = 0; i < *optionsCount; ) {
if((strncmp((*options)[i], optionName, nameLen) == 0) &&
((*options)[i][nameLen] == ':')) {
strcpy(value, &(*options)[i][nameLen + 1]);
free((*options)[i]);
(*optionsCount)--;
for(j = i; j < *optionsCount; j++)
(*options)[j] = (*options)[j + 1];
}
else {
i++;
}
}
}
#define START_COMMAND "start"
#define SWITCHES "f:Fa"
int
main(int argc,
char *argv[]) {
extern char *optarg;
extern int optind;
char activityName[127+1];
char controlName[127 + 1];
char *tmp;
HostSpec nsSpec;
int opt;
char **options;
size_t optionsCount;
char optionsFile[127 + 1];
HostSpec sensorSpec;
char skillName[127 + 1];
const char *USAGE = "start_activity [-f file] [-F] [-a] host [option ...]";
int force = 0;
int all = 0;
optionsFile[0] = '\0';
DirectDiagnostics(DIAGERROR, stderr);
DirectDiagnostics(DIAGFATAL, stderr);
while((opt = getopt(argc, argv, SWITCHES)) != EOF) {
switch(opt) {
case 'f':
strcpy(optionsFile, optarg);
break;
case 'F':
force = 1;
break;
case 'a':
all=1;
break;
default:
fprintf(stderr, "Unrecognized switch\n%s\n", USAGE);
exit(1);
break;
}
}
if(optind >= argc) {
fprintf(stderr, "%s\n", USAGE);
exit(1);
}
tmp = EnvironmentValue_r("SENSOR_PORT", NULL);
if (tmp == NULL) {
opt = GetDefaultPort(NWSAPI_SENSOR_HOST);
} else {
opt = atoi(tmp);
free(tmp);
}
sensorSpec = *MakeHostSpec (argv[optind++], opt);
if(!GetNameServer(&sensorSpec, &nsSpec)) {
fprintf(stderr, "Unable to contact sensor %s:%d\n",
sensorSpec.machineName, sensorSpec.machinePort);
exit(1);
}
else if(!UseNameServer(&nsSpec)) {
fprintf(stderr, "Unable to contact name server %s:%d\n",
nsSpec.machineName, nsSpec.machinePort);
exit(1);
}
if (all) {
printf("Restart all activities on %s:%d.\n",
sensorSpec.machineName, sensorSpec.machinePort);
exit (!RestartActivities(&sensorSpec));
}
options = NULL;
optionsCount = 0;
if((optionsFile[0] != '\0') &&
!ParseOptionsFile(optionsFile, &options, &optionsCount)) {
FreeAllElements((void **)options, optionsCount);
free(options);
exit(1); /* Message will come from ParseOptionsFile() */
}
for(; optind < argc; optind++) {
if(!AddOption(argv[optind], &options, &optionsCount)) {
FreeAllElements((void **)options, optionsCount);
free(options);
exit(1); /* Message will come from AddOption() */
}
}
activityName[0] = '\0';
controlName[0] = '\0';
skillName[0] = '\0';
RemoveOption("name", &options, &optionsCount, activityName);
RemoveOption(CONTROL_ATTR, &options, &optionsCount, controlName);
RemoveOption(SKILL_ATTR, &options, &optionsCount, skillName);
if(!GetControlAndSkill(&sensorSpec,
controlName,
skillName,
(const char **)options,
optionsCount)) {
FreeAllElements((void **)options, optionsCount);
free(options);
exit(1); /* Message will come from GetControlAndSkill() */
}
if(activityName[0] == '\0') {
char ciccio[63 + 1];
/* there is no name: let's get the default */
snprintf(ciccio, 64, "%s:%d", sensorSpec.machineName, sensorSpec.machinePort);
tmp = NameOfActivity_r(ciccio, skillName, "");
if (tmp != NULL) {
strncpy(activityName, tmp, sizeof(activityName));
free(tmp);
} else {
ERROR("couldn't generate activity name\n");
exit(1);
}
}
if (strcmp(controlName, "clique") == 0) {
if (strlen(activityName) >= MAX_CLIQUE_NAME_SIZE) {
WARN("Clique name too long: chopping it\n");
activityName[MAX_CLIQUE_NAME_SIZE-1] = '\0';
}
}
if(LookupName(activityName)) {
if (force == 0) {
fprintf(stderr, "Activity '%s' already registered, you can \n\thalt the activity (with halt_activity)\n\tuse -F to force restart\n\tuse a different name (with the 'name:' option)\n", activityName);
exit(1);
}
else {
fprintf(stderr, "Activity already registered; restarting anyway.\n");
}
}
if(StartActivity(&sensorSpec,
activityName,
controlName,
skillName,
(const char **)options,
optionsCount)) {
printf("Started activity %s on %s:%d\n",
activityName, sensorSpec.machineName, sensorSpec.machinePort);
}
FreeAllElements((void **)options, optionsCount);
free(options);
return 0;
}
|