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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include "cl_host_alias_list.h"
#include "cl_commlib.h"
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_setup()"
int cl_host_alias_list_setup(cl_raw_list_t** list_p, char* list_name) {
int ret_val = CL_RETVAL_OK;
ret_val = cl_raw_list_setup(list_p, list_name, 1 );
if (list_name != NULL) {
CL_LOG_STR(CL_LOG_INFO,"host alias list setup ok for list:", list_name);
}
return ret_val;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_cleanup()"
int cl_host_alias_list_cleanup(cl_raw_list_t** list_p) {
cl_host_alias_list_elem_t* elem = NULL;
int ret_val = CL_RETVAL_OK;
if (list_p == NULL) {
/* we expect an address of an pointer */
return CL_RETVAL_PARAMS;
}
if (*list_p == NULL) {
/* we expect an initalized pointer */
return CL_RETVAL_PARAMS;
}
/* delete all entries in list */
cl_raw_list_lock(*list_p);
while ( (elem = cl_host_alias_list_get_first_elem(*list_p)) != NULL) {
cl_raw_list_remove_elem(*list_p, elem->raw_elem);
free(elem->local_resolved_hostname);
free(elem->alias_name);
free(elem);
}
cl_raw_list_unlock(*list_p);
ret_val = cl_raw_list_cleanup(list_p);
CL_LOG(CL_LOG_INFO,"host alias cleanup done");
return ret_val;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_append_host()"
int cl_host_alias_list_append_host(cl_raw_list_t* list_p, char* local_resolved_name, char* alias_name, int lock_list) {
cl_host_alias_list_elem_t* new_elem = NULL;
int ret_val;
char* help = NULL;
if (list_p == NULL || local_resolved_name == NULL || alias_name == NULL ) {
return CL_RETVAL_PARAMS;
}
if ( cl_host_alias_list_get_alias_name(list_p, local_resolved_name, &help ) == CL_RETVAL_OK) {
CL_LOG_STR(CL_LOG_ERROR,"alias for host exists already:", help);
free(help);
return CL_RETVAL_ALIAS_EXISTS;
}
#if 0
/* CR:
*
* enable this code for 1:1 mapping or for virtual host mapping
* (e.g. my_virtual_hostname real_host_name in alias file )
*
* DO NOT FORGET TO ALSO ENABLE CODE IN cl_com_cached_gethostbyname()
*/
if ( cl_host_alias_list_get_local_resolved_name(list_p, alias_name, &help ) == CL_RETVAL_OK) {
CL_LOG_STR(CL_LOG_ERROR,"hostname for alias exists already:", help);
free(help);
return CL_RETVAL_ALIAS_EXISTS;
}
#endif
/* lock the list */
if (lock_list == 1) {
if ( ( ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
}
/* add new element list */
new_elem = (cl_host_alias_list_elem_t*) malloc(sizeof(cl_host_alias_list_elem_t));
if (new_elem == NULL) {
if (lock_list == 1) {
cl_raw_list_unlock(list_p);
}
return CL_RETVAL_MALLOC;
}
new_elem->local_resolved_hostname = strdup(local_resolved_name);
if (new_elem->local_resolved_hostname == NULL) {
free(new_elem);
if (lock_list == 1) {
cl_raw_list_unlock(list_p);
}
return CL_RETVAL_MALLOC;
}
new_elem->alias_name = strdup(alias_name);
if (new_elem->alias_name == NULL) {
free(new_elem->local_resolved_hostname);
free(new_elem);
if (lock_list == 1) {
cl_raw_list_unlock(list_p);
}
return CL_RETVAL_MALLOC;
}
new_elem->raw_elem = cl_raw_list_append_elem(list_p, (void*) new_elem);
if ( new_elem->raw_elem == NULL) {
free(new_elem->local_resolved_hostname);
free(new_elem->alias_name);
free(new_elem);
if (lock_list == 1) {
cl_raw_list_unlock(list_p);
}
return CL_RETVAL_MALLOC;
}
/* unlock the thread list */
if (lock_list == 1) {
if ( ( ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
}
return CL_RETVAL_OK;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_remove_host()"
int cl_host_alias_list_remove_host(cl_raw_list_t* list_p, cl_host_alias_list_elem_t* element, int lock_list) {
cl_host_alias_list_elem_t* elem = NULL;
int ret_val = CL_RETVAL_OK;
int function_return = CL_RETVAL_UNKNOWN;
if ( list_p == NULL || element == NULL) {
return CL_RETVAL_PARAMS;
}
if (lock_list != 0) {
/* lock list */
if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
}
elem = cl_host_alias_list_get_first_elem(list_p);
while ( elem != NULL) {
if (elem == element) {
/* found matching element */
cl_raw_list_remove_elem(list_p, elem->raw_elem);
function_return = CL_RETVAL_OK;
free(elem->local_resolved_hostname);
free(elem->alias_name);
free(elem);
elem = NULL;
break;
}
elem = cl_host_alias_list_get_next_elem(elem);
}
if (lock_list != 0) {
/* unlock list */
if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
}
return function_return;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_local_resolved_name()"
int cl_host_alias_list_get_local_resolved_name(cl_raw_list_t* list_p, char* alias_name,char** local_resolved_name ) {
cl_host_alias_list_elem_t* elem = NULL;
int ret_val;
if (list_p == NULL || alias_name == NULL || local_resolved_name == NULL) {
return CL_RETVAL_PARAMS;
}
if (*local_resolved_name != NULL) {
CL_LOG(CL_LOG_ERROR,"need empty pointer pointer");
return CL_RETVAL_PARAMS;
}
if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
elem = cl_host_alias_list_get_first_elem(list_p);
while ( elem != NULL) {
if ( strcasecmp(alias_name,elem->alias_name) == 0) {
*local_resolved_name = strdup(elem->local_resolved_hostname);
if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
free(*local_resolved_name);
*local_resolved_name = NULL;
return ret_val;
}
if (*local_resolved_name == NULL) {
return CL_RETVAL_MALLOC;
}
return CL_RETVAL_OK;
}
elem = cl_host_alias_list_get_next_elem(elem);
}
if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
return CL_RETVAL_UNKNOWN;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_alias_name()"
int cl_host_alias_list_get_alias_name(cl_raw_list_t* list_p, char* local_resolved_name, char** alias_name) {
cl_host_alias_list_elem_t* elem = NULL;
int ret_val;
if (list_p == NULL || local_resolved_name == NULL || alias_name == NULL) {
return CL_RETVAL_PARAMS;
}
if (*alias_name != NULL) {
CL_LOG(CL_LOG_ERROR,"need empty pointer pointer");
return CL_RETVAL_PARAMS;
}
if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
elem = cl_host_alias_list_get_first_elem(list_p);
while ( elem != NULL) {
if ( strcasecmp(local_resolved_name,elem->local_resolved_hostname) == 0) {
*alias_name = strdup(elem->alias_name);
if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
free(*alias_name);
*alias_name = NULL;
return ret_val;
}
if (*alias_name == NULL) {
return CL_RETVAL_MALLOC;
}
return CL_RETVAL_OK;
}
elem = cl_host_alias_list_get_next_elem(elem);
}
if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
return ret_val;
}
return CL_RETVAL_UNKNOWN;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_first_elem()"
cl_host_alias_list_elem_t* cl_host_alias_list_get_first_elem(cl_raw_list_t* list_p) {
cl_raw_list_elem_t* raw_elem = cl_raw_list_get_first_elem(list_p);
if (raw_elem) {
return (cl_host_alias_list_elem_t*) raw_elem->data;
}
return NULL;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_least_elem()"
cl_host_alias_list_elem_t* cl_host_alias_list_get_least_elem(cl_raw_list_t* list_p) {
cl_raw_list_elem_t* raw_elem = cl_raw_list_get_least_elem(list_p);
if (raw_elem) {
return (cl_host_alias_list_elem_t*) raw_elem->data;
}
return NULL;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_next_elem()"
cl_host_alias_list_elem_t* cl_host_alias_list_get_next_elem(cl_host_alias_list_elem_t* elem) {
cl_raw_list_elem_t* next_raw_elem = NULL;
if (elem != NULL) {
cl_raw_list_elem_t* raw_elem = elem->raw_elem;
next_raw_elem = cl_raw_list_get_next_elem(raw_elem);
if (next_raw_elem) {
return (cl_host_alias_list_elem_t*) next_raw_elem->data;
}
}
return NULL;
}
#ifdef __CL_FUNCTION__
#undef __CL_FUNCTION__
#endif
#define __CL_FUNCTION__ "cl_host_alias_list_get_last_elem()"
cl_host_alias_list_elem_t* cl_host_alias_list_get_last_elem(cl_host_alias_list_elem_t* elem) {
cl_raw_list_elem_t* last_raw_elem = NULL;
if (elem != NULL) {
cl_raw_list_elem_t* raw_elem = elem->raw_elem;
last_raw_elem = cl_raw_list_get_last_elem(raw_elem);
if (last_raw_elem) {
return (cl_host_alias_list_elem_t*) last_raw_elem->data;
}
}
return NULL;
}
|