~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/comm/cl_host_list.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <errno.h>
 
3
#include <string.h>
 
4
#include <sys/time.h>
 
5
#include <stdlib.h>
 
6
#include "cl_util.h"
 
7
 
 
8
 
 
9
/*___INFO__MARK_BEGIN__*/
 
10
/*************************************************************************
 
11
 * 
 
12
 *  The Contents of this file are made available subject to the terms of
 
13
 *  the Sun Industry Standards Source License Version 1.2
 
14
 * 
 
15
 *  Sun Microsystems Inc., March, 2001
 
16
 * 
 
17
 * 
 
18
 *  Sun Industry Standards Source License Version 1.2
 
19
 *  =================================================
 
20
 *  The contents of this file are subject to the Sun Industry Standards
 
21
 *  Source License Version 1.2 (the "License"); You may not use this file
 
22
 *  except in compliance with the License. You may obtain a copy of the
 
23
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
24
 * 
 
25
 *  Software provided under this License is provided on an "AS IS" basis,
 
26
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
27
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
28
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
29
 *  See the License for the specific provisions governing your rights and
 
30
 *  obligations concerning the Software.
 
31
 * 
 
32
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
33
 * 
 
34
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
35
 * 
 
36
 *   All Rights Reserved.
 
37
 * 
 
38
 ************************************************************************/
 
39
/*___INFO__MARK_END__*/
 
40
 
 
41
#include "cl_host_list.h"
 
42
#include "cl_host_alias_list.h"
 
43
#include "cl_commlib.h"
 
44
#include "uti/sge_hostname.h"
 
45
 
 
46
static struct in_addr*   cl_com_copy_in_addr(struct in_addr *in_addr);
 
47
static cl_com_hostent_t* cl_com_copy_hostent(cl_com_hostent_t* hostent);
 
48
 
 
49
#ifdef __CL_FUNCTION__
 
50
#undef __CL_FUNCTION__
 
51
#endif
 
52
#define __CL_FUNCTION__ "cl_host_list_setup()"
 
53
int cl_host_list_setup(cl_raw_list_t** list_p, 
 
54
                       char* list_name,
 
55
                       cl_host_resolve_method_t method, 
 
56
                       char* host_alias_file, 
 
57
                       char* local_domain_name,
 
58
                       unsigned long entry_life_time,
 
59
                       unsigned long entry_update_time,
 
60
                       unsigned long entry_reresolve_time,
 
61
                       cl_bool_t create_hash) {
 
62
   int ret_val = CL_RETVAL_OK;
 
63
   cl_host_list_data_t* ldata = NULL;
 
64
 
 
65
   ldata = (cl_host_list_data_t*) malloc(sizeof(cl_host_list_data_t));
 
66
   if (ldata == NULL ) {
 
67
      return CL_RETVAL_MALLOC;
 
68
   }
 
69
   ldata->host_alias_file      = NULL;
 
70
   ldata->alias_file_changed   = 0;
 
71
   ldata->host_alias_list      = NULL;
 
72
   ldata->resolve_method       = method;
 
73
   ldata->entry_life_time      = entry_life_time;
 
74
   ldata->entry_update_time    = entry_update_time;
 
75
   ldata->entry_reresolve_time = entry_reresolve_time;
 
76
   ldata->last_refresh_time    = 0;
 
77
 
 
78
   if (local_domain_name == NULL && method == CL_LONG) {
 
79
      CL_LOG(CL_LOG_WARNING,"can't compare short host names without default domain when method is CL_LONG");
 
80
   }
 
81
 
 
82
 
 
83
   if (entry_life_time == 0) {
 
84
      unsigned long help_value = 0;
 
85
 
 
86
      help_value = cl_util_get_ulong_value(getenv("SGE_COMMLIB_HOST_LIST_LIFE_TIME"));
 
87
      if (help_value > 0) {
 
88
         CL_LOG(CL_LOG_INFO,"environment variable SGE_COMMLIB_HOST_LIST_LIFE_TIME is set");
 
89
         ldata->entry_life_time = help_value;
 
90
      } else {
 
91
         CL_LOG(CL_LOG_INFO,"using default value for entry_life_time");
 
92
         ldata->entry_life_time = CL_HOST_LIST_DEFAULT_LIFE_TIME;
 
93
      }
 
94
   }
 
95
 
 
96
   if (entry_update_time == 0) {
 
97
      unsigned long help_value = 0;
 
98
 
 
99
      help_value = cl_util_get_ulong_value(getenv("SGE_COMMLIB_HOST_LIST_UPDATE_TIME"));
 
100
      if (help_value > 0) {
 
101
         CL_LOG(CL_LOG_INFO,"environment variable SGE_COMMLIB_HOST_LIST_UPDATE_TIME is set");
 
102
         ldata->entry_update_time = help_value;
 
103
      } else {
 
104
         CL_LOG(CL_LOG_INFO,"using default value for entry_update_time");
 
105
         ldata->entry_update_time = CL_HOST_LIST_DEFAULT_UPDATE_TIME;
 
106
      }
 
107
   }
 
108
 
 
109
   if (entry_reresolve_time == 0) {
 
110
      unsigned long help_value = 0;
 
111
 
 
112
      help_value = cl_util_get_ulong_value(getenv("SGE_COMMLIB_HOST_LIST_RERESOLVE_TIME"));
 
113
      if (help_value > 0) {
 
114
         CL_LOG(CL_LOG_INFO,"environment variable SGE_COMMLIB_HOST_LIST_RERESOLVE_TIME is set");
 
115
         ldata->entry_reresolve_time = help_value;
 
116
      } else {
 
117
         CL_LOG(CL_LOG_INFO,"using default value for entry_reresolve_time");
 
118
         ldata->entry_reresolve_time = CL_HOST_LIST_DEFAULT_RERESOLVE_TIME;
 
119
      }
 
120
   }
 
121
 
 
122
   if ( ldata->entry_life_time > CL_HOST_LIST_MAX_LIFE_TIME) {
 
123
      CL_LOG_INT(CL_LOG_WARNING,"entry_life_time exceeds maximum of",CL_HOST_LIST_MAX_LIFE_TIME);
 
124
      CL_LOG(CL_LOG_WARNING,"using default value for entry_life_time");
 
125
      ldata->entry_life_time = CL_HOST_LIST_DEFAULT_LIFE_TIME;
 
126
   }
 
127
 
 
128
   if ( ldata->entry_update_time > CL_HOST_LIST_MAX_UPDATE_TIME) {
 
129
      CL_LOG_INT(CL_LOG_WARNING,"entry_update_time exceeds maximum of",CL_HOST_LIST_MAX_UPDATE_TIME);
 
130
      CL_LOG(CL_LOG_WARNING,"using default value for entry_update_time");
 
131
      ldata->entry_update_time = CL_HOST_LIST_DEFAULT_UPDATE_TIME;
 
132
   }
 
133
 
 
134
   if ( ldata->entry_reresolve_time > CL_HOST_LIST_MAX_RERESOLVE_TIME) {
 
135
      CL_LOG_INT(CL_LOG_WARNING,"entry_reresolve_time exceeds maximum of",CL_HOST_LIST_MAX_RERESOLVE_TIME);
 
136
      CL_LOG(CL_LOG_WARNING,"using default value for entry_reresolve_time");
 
137
      ldata->entry_reresolve_time = CL_HOST_LIST_DEFAULT_RERESOLVE_TIME;
 
138
   }
 
139
 
 
140
   if (ldata->entry_life_time <= ldata->entry_update_time || ldata->entry_life_time <= ldata->entry_reresolve_time) {
 
141
      free(ldata); 
 
142
      CL_LOG(CL_LOG_ERROR,"entry_life_time must be >= entry_update_time and >= entry_reresolve_time");
 
143
      cl_commlib_push_application_error(CL_LOG_ERROR, CL_RETVAL_PARAMS, "SGE_COMMLIB_HOST_LIST_LIFE_TIME must be >= SGE_COMMLIB_HOST_LIST_UPDATE_TIME and >= SGE_COMMLIB_HOST_LIST_RERESOLVE_TIME");
 
144
      return CL_RETVAL_PARAMS;
 
145
   }
 
146
   if (ldata->entry_update_time <= ldata->entry_reresolve_time) {
 
147
      free(ldata); 
 
148
      CL_LOG(CL_LOG_ERROR,"entry_update_time must be >= entry_reresolve_time");
 
149
      cl_commlib_push_application_error(CL_LOG_ERROR, CL_RETVAL_PARAMS, "SGE_COMMLIB_HOST_LIST_UPDATE_TIME must be >= SGE_COMMLIB_HOST_LIST_RERESOLVE_TIME");
 
150
      return CL_RETVAL_PARAMS;
 
151
   }
 
152
 
 
153
   ret_val = cl_host_alias_list_setup(&(ldata->host_alias_list), "host alias list");
 
154
   if (ret_val != CL_RETVAL_OK) {
 
155
      free(ldata);
 
156
      CL_LOG(CL_LOG_ERROR,"error setting up host alias list");
 
157
      return ret_val;
 
158
   }
 
159
 
 
160
   if (host_alias_file != NULL) {
 
161
      ldata->host_alias_file = strdup(host_alias_file);
 
162
      ldata->alias_file_changed = 1;
 
163
      if (ldata->host_alias_file == NULL) {
 
164
         free(ldata);
 
165
         return CL_RETVAL_MALLOC;
 
166
      }
 
167
   } else {
 
168
      ldata->host_alias_file = NULL;
 
169
   }
 
170
 
 
171
   if (local_domain_name != NULL) {
 
172
      ldata->local_domain_name = strdup(local_domain_name);
 
173
      if (ldata->local_domain_name == NULL) {
 
174
         if (ldata->host_alias_file != NULL) {
 
175
            free(ldata->host_alias_file);
 
176
         }
 
177
         free(ldata);
 
178
         return CL_RETVAL_MALLOC;
 
179
      }
 
180
   } else {
 
181
      ldata->local_domain_name = NULL;
 
182
   }
 
183
 
 
184
   
 
185
 
 
186
   ret_val = cl_raw_list_setup(list_p,list_name, 1);
 
187
   if (ret_val != CL_RETVAL_OK) {
 
188
      if (ldata->host_alias_file != NULL) {
 
189
         free(ldata->host_alias_file);
 
190
      }
 
191
      if (ldata->local_domain_name != NULL) {
 
192
         free(ldata->local_domain_name);
 
193
      }
 
194
      free(ldata);
 
195
      return ret_val;
 
196
   }
 
197
 
 
198
   switch(ldata->resolve_method) {
 
199
      case CL_SHORT:
 
200
         CL_LOG(CL_LOG_INFO,"using short hostname for host compare operations");
 
201
         break;
 
202
 
 
203
      case CL_LONG:
 
204
         CL_LOG(CL_LOG_INFO,"using long hostname for host compare operations");
 
205
         break;
 
206
 
 
207
      default:
 
208
         CL_LOG(CL_LOG_WARNING,"undefined resolving method");
 
209
         break;
 
210
   }
 
211
 
 
212
   if (ldata->host_alias_file != NULL) {
 
213
      CL_LOG_STR(CL_LOG_INFO,"using host alias file:", ldata->host_alias_file);
 
214
   } else {
 
215
      CL_LOG(CL_LOG_INFO,"no host alias file specified");
 
216
   }
 
217
   if (ldata->local_domain_name != NULL) {
 
218
      CL_LOG_STR(CL_LOG_INFO,"using local domain name:", ldata->local_domain_name);
 
219
   } else {
 
220
      CL_LOG(CL_LOG_INFO,"no local domain specified");
 
221
   }
 
222
 
 
223
   /* create hashtable */
 
224
   if (create_hash == CL_TRUE) {
 
225
      ldata->ht = sge_htable_create(4, dup_func_string, hash_func_string, hash_compare_string);
 
226
      if (ldata->ht == NULL) {
 
227
         cl_raw_list_cleanup(list_p);
 
228
         if (ldata->host_alias_file != NULL) {
 
229
            free(ldata->host_alias_file);
 
230
         }
 
231
         if (ldata->local_domain_name != NULL) {
 
232
            free(ldata->local_domain_name);
 
233
         }
 
234
         free(ldata);
 
235
         return CL_RETVAL_MALLOC;
 
236
      }
 
237
      CL_LOG_INT(CL_LOG_INFO,"created hash table with size =", 4);
 
238
   } else {
 
239
      CL_LOG(CL_LOG_INFO,"created NO hash table!");
 
240
      ldata->ht = NULL;
 
241
   }
 
242
 
 
243
   /* set private list data */
 
244
   (*list_p)->list_data = ldata;
 
245
 
 
246
   CL_LOG_INT(CL_LOG_INFO,"entry_life_time is", (int)ldata->entry_life_time);
 
247
   CL_LOG_INT(CL_LOG_INFO,"entry_update_time is", (int)ldata->entry_update_time);
 
248
   CL_LOG_INT(CL_LOG_INFO,"entry_reresolve_time is", (int)ldata->entry_reresolve_time);
 
249
 
 
250
   return ret_val;
 
251
}
 
252
 
 
253
#ifdef __CL_FUNCTION__
 
254
#undef __CL_FUNCTION__
 
255
#endif
 
256
#define __CL_FUNCTION__ "cl_host_list_copy()"
 
257
int cl_host_list_copy(cl_raw_list_t** destination, cl_raw_list_t* source, cl_bool_t create_hash) {
 
258
   int ret_val = CL_RETVAL_OK;
 
259
   cl_host_list_data_t* ldata_source = NULL;
 
260
   cl_host_list_data_t* ldata_dest = NULL;
 
261
   cl_host_alias_list_elem_t* alias_elem = NULL;
 
262
   cl_host_list_elem_t* host_elem = NULL;
 
263
 
 
264
   if (source == NULL) {
 
265
      return CL_RETVAL_PARAMS;
 
266
   }  
 
267
 
 
268
   ret_val = cl_raw_list_lock(source);
 
269
   if (ret_val != CL_RETVAL_OK) {
 
270
      return ret_val;
 
271
   }
 
272
 
 
273
   /* create a new host list */
 
274
   ldata_source = (cl_host_list_data_t*) source->list_data;
 
275
   if (ldata_source != NULL) {
 
276
      ret_val = cl_host_list_setup(destination, 
 
277
                                   source->list_name,
 
278
                                   ldata_source->resolve_method,
 
279
                                   ldata_source->host_alias_file,
 
280
                                   ldata_source->local_domain_name,
 
281
                                   ldata_source->entry_life_time,
 
282
                                   ldata_source->entry_update_time,
 
283
                                   ldata_source->entry_reresolve_time,
 
284
                                   create_hash);
 
285
   } else {
 
286
      CL_LOG(CL_LOG_ERROR,"not list data specified");
 
287
      ret_val = CL_RETVAL_UNKNOWN;
 
288
   }
 
289
 
 
290
   if (ret_val != CL_RETVAL_OK) {
 
291
      cl_raw_list_unlock(source);
 
292
      cl_host_list_cleanup(destination);
 
293
      return ret_val;
 
294
   }
 
295
 
 
296
   /* list created, now get private data structures */ 
 
297
   ldata_dest = (cl_host_list_data_t*) (*destination)->list_data;
 
298
 
 
299
   ldata_dest->alias_file_changed = ldata_source->alias_file_changed;
 
300
   ldata_dest->last_refresh_time  = ldata_source->last_refresh_time;
 
301
 
 
302
   /* now copy alias list */
 
303
   cl_raw_list_lock(ldata_source->host_alias_list);
 
304
 
 
305
   alias_elem = cl_host_alias_list_get_first_elem(ldata_source->host_alias_list);
 
306
   while(alias_elem) {
 
307
      ret_val = cl_host_alias_list_append_host(ldata_dest->host_alias_list, 
 
308
                                               alias_elem->local_resolved_hostname,
 
309
                                               alias_elem->alias_name, 0);
 
310
      if (ret_val != CL_RETVAL_OK) {
 
311
         cl_raw_list_unlock(ldata_source->host_alias_list);
 
312
         cl_raw_list_unlock(source);
 
313
         cl_host_list_cleanup(destination);
 
314
         return ret_val;
 
315
      }
 
316
      alias_elem = cl_host_alias_list_get_next_elem(alias_elem);
 
317
   }
 
318
   cl_raw_list_unlock(ldata_source->host_alias_list);
 
319
 
 
320
   /* ok, now copy the entries */
 
321
   host_elem = cl_host_list_get_first_elem(source);
 
322
   while(host_elem) {
 
323
      cl_com_host_spec_t* new_host_spec = NULL;
 
324
      
 
325
      new_host_spec = ( cl_com_host_spec_t*) malloc( sizeof(cl_com_host_spec_t) );
 
326
      if (new_host_spec == NULL) {
 
327
         cl_raw_list_unlock(source);
 
328
         cl_host_list_cleanup(destination);
 
329
         return CL_RETVAL_MALLOC;
 
330
      }
 
331
 
 
332
      /* copy host_spec_ type */
 
333
      new_host_spec->resolve_error     = host_elem->host_spec->resolve_error;
 
334
      new_host_spec->last_resolve_time = host_elem->host_spec->last_resolve_time;
 
335
      new_host_spec->creation_time     = host_elem->host_spec->creation_time;
 
336
       
 
337
      if ( host_elem->host_spec->resolved_name ) {
 
338
         new_host_spec->resolved_name = strdup(host_elem->host_spec->resolved_name);
 
339
         if ( new_host_spec->resolved_name == NULL) {
 
340
            cl_com_free_hostspec(&new_host_spec);
 
341
            cl_raw_list_unlock(source);
 
342
            cl_host_list_cleanup(destination);
 
343
            return CL_RETVAL_MALLOC;
 
344
         }
 
345
      } else {
 
346
         new_host_spec->resolved_name = NULL;
 
347
      }
 
348
 
 
349
      if ( host_elem->host_spec->unresolved_name ) {
 
350
         new_host_spec->unresolved_name = strdup(host_elem->host_spec->unresolved_name);
 
351
         if ( new_host_spec->unresolved_name == NULL) {
 
352
            cl_com_free_hostspec(&new_host_spec);
 
353
            cl_raw_list_unlock(source);
 
354
            cl_host_list_cleanup(destination);
 
355
            return CL_RETVAL_MALLOC;
 
356
         }
 
357
      } else {
 
358
         new_host_spec->unresolved_name = NULL;
 
359
      }
 
360
 
 
361
      if ( host_elem->host_spec->in_addr) {
 
362
         new_host_spec->in_addr = cl_com_copy_in_addr(host_elem->host_spec->in_addr);
 
363
         if ( new_host_spec->in_addr == NULL) {
 
364
            cl_com_free_hostspec(&new_host_spec);
 
365
            cl_raw_list_unlock(source);
 
366
            cl_host_list_cleanup(destination);
 
367
            return CL_RETVAL_MALLOC;
 
368
         }
 
369
      } else {
 
370
         new_host_spec->in_addr = NULL;
 
371
      }
 
372
 
 
373
      if ( host_elem->host_spec->hostent) {
 
374
         new_host_spec->hostent = cl_com_copy_hostent(host_elem->host_spec->hostent);
 
375
         if ( new_host_spec->hostent == NULL) {
 
376
            cl_com_free_hostspec(&new_host_spec);
 
377
            cl_raw_list_unlock(source);
 
378
            cl_host_list_cleanup(destination);
 
379
            return CL_RETVAL_MALLOC;
 
380
         }
 
381
      } else {
 
382
         new_host_spec->hostent = NULL;
 
383
      }
 
384
 
 
385
      cl_host_list_append_host((*destination), new_host_spec, 0);
 
386
      host_elem = cl_host_list_get_next_elem(host_elem);
 
387
   }
 
388
   
 
389
   ret_val = cl_raw_list_unlock( source );
 
390
   return ret_val;
 
391
}
 
392
 
 
393
#ifdef __CL_FUNCTION__
 
394
#undef __CL_FUNCTION__
 
395
#endif
 
396
#define __CL_FUNCTION__ "cl_host_list_get_data()"
 
397
cl_host_list_data_t* cl_host_list_get_data(cl_raw_list_t* list_p) {
 
398
 
 
399
   cl_host_list_data_t* ldata = NULL;
 
400
   cl_raw_list_t* hostlist = NULL;
 
401
 
 
402
   if (list_p == NULL) {
 
403
      hostlist = cl_com_get_host_list();
 
404
   } else {
 
405
      hostlist = list_p;
 
406
   }
 
407
   if (hostlist == NULL) {
 
408
      CL_LOG(CL_LOG_WARNING,"no global hostlist");
 
409
      return NULL;
 
410
   }
 
411
 
 
412
   ldata = (cl_host_list_data_t*) hostlist->list_data;
 
413
   return ldata;
 
414
}
 
415
 
 
416
 
 
417
#ifdef __CL_FUNCTION__
 
418
#undef __CL_FUNCTION__
 
419
#endif
 
420
#define __CL_FUNCTION__ "cl_host_list_set_alias_file_dirty()"
 
421
int cl_host_list_set_alias_file_dirty(cl_raw_list_t* list_p) {
 
422
   int ret_val;
 
423
   cl_host_list_data_t* ldata = NULL;
 
424
 
 
425
   if (list_p == NULL ) {
 
426
      return CL_RETVAL_PARAMS;
 
427
   }
 
428
   
 
429
   /* lock host list */
 
430
   ret_val = cl_raw_list_lock(list_p);
 
431
   if (ret_val != CL_RETVAL_OK) {
 
432
      return ret_val;
 
433
   }
 
434
 
 
435
   /* list_p should be a hostlist */
 
436
   ldata = (cl_host_list_data_t*) list_p->list_data;
 
437
   if (ldata != NULL) {
 
438
      ldata->alias_file_changed = 1;
 
439
   } else {
 
440
      cl_raw_list_unlock(list_p);
 
441
      return CL_RETVAL_NO_FRAMEWORK_INIT;
 
442
   }
 
443
 
 
444
   /* unlock host list */
 
445
   ret_val = cl_raw_list_unlock(list_p);
 
446
   if (ret_val != CL_RETVAL_OK) {
 
447
      return ret_val;
 
448
   }
 
449
   return CL_RETVAL_OK;
 
450
}
 
451
 
 
452
 
 
453
 
 
454
 
 
455
#ifdef __CL_FUNCTION__
 
456
#undef __CL_FUNCTION__
 
457
#endif
 
458
#define __CL_FUNCTION__ "cl_host_list_set_alias_file()"
 
459
int cl_host_list_set_alias_file(cl_raw_list_t* list_p, const char *host_alias_file) {
 
460
   int ret_val;
 
461
   cl_host_list_data_t* ldata = NULL;
 
462
 
 
463
   if (list_p == NULL || host_alias_file == NULL) {
 
464
      return CL_RETVAL_PARAMS;
 
465
   }
 
466
   
 
467
   /* lock host list */
 
468
   ret_val = cl_raw_list_lock(list_p);
 
469
   if (ret_val != CL_RETVAL_OK) {
 
470
      return ret_val;
 
471
   }
 
472
 
 
473
   /* list_p should be a hostlist */
 
474
   ldata = (cl_host_list_data_t*) list_p->list_data;
 
475
   if (ldata != NULL) {
 
476
      if (ldata->host_alias_file != NULL) {
 
477
         free(ldata->host_alias_file);
 
478
         ldata->host_alias_file = NULL;
 
479
      }
 
480
      ldata->host_alias_file = strdup(host_alias_file);
 
481
      CL_LOG_STR(CL_LOG_INFO,"using host alias file:",ldata->host_alias_file);
 
482
      ldata->alias_file_changed = 1;
 
483
      if (ldata->host_alias_file == NULL) {
 
484
         cl_raw_list_unlock(list_p);
 
485
         return CL_RETVAL_MALLOC;
 
486
      }
 
487
   } else {
 
488
      cl_raw_list_unlock(list_p);
 
489
      return CL_RETVAL_NO_FRAMEWORK_INIT;
 
490
   }
 
491
 
 
492
   /* unlock host list */
 
493
   ret_val = cl_raw_list_unlock(list_p);
 
494
   if (ret_val != CL_RETVAL_OK) {
 
495
      return ret_val;
 
496
   }
 
497
   return CL_RETVAL_OK;
 
498
}
 
499
 
 
500
#ifdef __CL_FUNCTION__
 
501
#undef __CL_FUNCTION__
 
502
#endif
 
503
#define __CL_FUNCTION__ "cl_host_list_cleanup()"
 
504
int cl_host_list_cleanup(cl_raw_list_t** list_p) {
 
505
   cl_host_list_data_t* ldata = NULL;
 
506
   cl_host_list_elem_t* elem = NULL;
 
507
   
 
508
   if (list_p == NULL) {
 
509
      /* we expect an address of an pointer */
 
510
      return CL_RETVAL_PARAMS;
 
511
   }
 
512
   if (*list_p == NULL) {
 
513
      /* we expect an initalized pointer */
 
514
      return CL_RETVAL_PARAMS;
 
515
   }
 
516
 
 
517
   /* delete all entries in list */
 
518
   cl_raw_list_lock(*list_p);
 
519
   while ( (elem = cl_host_list_get_first_elem(*list_p)) != NULL) {
 
520
      cl_raw_list_remove_elem(*list_p, elem->raw_elem);
 
521
      cl_com_free_hostspec(&( elem->host_spec ));
 
522
      free(elem);
 
523
   }
 
524
   cl_raw_list_unlock(*list_p);
 
525
 
 
526
   /* clean list private data */
 
527
   ldata = (*list_p)->list_data;
 
528
   if (ldata != NULL) {
 
529
      if (ldata->ht != NULL) {
 
530
         sge_htable_destroy(ldata->ht);
 
531
      }
 
532
      cl_host_alias_list_cleanup(&(ldata->host_alias_list));
 
533
      if (ldata->local_domain_name != NULL) {
 
534
         free(ldata->local_domain_name);
 
535
      }
 
536
      if (ldata->host_alias_file != NULL) {
 
537
         free(ldata->host_alias_file);
 
538
      }
 
539
      free(ldata);
 
540
   }
 
541
   (*list_p)->list_data = NULL;
 
542
 
 
543
   return cl_raw_list_cleanup(list_p);
 
544
}
 
545
 
 
546
 
 
547
#ifdef __CL_FUNCTION__
 
548
#undef __CL_FUNCTION__
 
549
#endif
 
550
#define __CL_FUNCTION__ "cl_host_list_append_host()"
 
551
int cl_host_list_append_host(cl_raw_list_t* list_p,cl_com_host_spec_t* host, int lock_list ) {
 
552
 
 
553
   int ret_val;
 
554
   cl_host_list_elem_t* new_elem = NULL;
 
555
 
 
556
   if (host == NULL || list_p == NULL) {
 
557
      return CL_RETVAL_PARAMS;
 
558
   }
 
559
 
 
560
   /* lock the list */
 
561
   if (lock_list == 1) {
 
562
      if (  ( ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
 
563
         return ret_val;
 
564
      }
 
565
   }
 
566
 
 
567
   /* add new element list */
 
568
   new_elem = (cl_host_list_elem_t*) malloc(sizeof(cl_host_list_elem_t));
 
569
   if (new_elem == NULL) {
 
570
      if (lock_list == 1) {
 
571
         cl_raw_list_unlock(list_p);
 
572
      }
 
573
      return CL_RETVAL_MALLOC;
 
574
   }
 
575
 
 
576
   new_elem->host_spec = host;
 
577
   new_elem->raw_elem = cl_raw_list_append_elem(list_p, (void*) new_elem);
 
578
   if ( new_elem->raw_elem == NULL) {
 
579
      free(new_elem);
 
580
      if (lock_list == 1) { 
 
581
         cl_raw_list_unlock(list_p);
 
582
      }
 
583
      return CL_RETVAL_MALLOC;
 
584
   }
 
585
 
 
586
   /* add element to hash table */
 
587
   if (host->unresolved_name != NULL) {
 
588
      cl_host_list_data_t* ldata = list_p->list_data;
 
589
      if (ldata->ht != NULL) {
 
590
         sge_htable_store(ldata->ht, host->unresolved_name, new_elem);
 
591
      }
 
592
   }
 
593
   
 
594
   /* unlock the thread list */
 
595
   if (lock_list == 1) {
 
596
      if (  ( ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
 
597
         return ret_val;
 
598
      }
 
599
   }
 
600
   return CL_RETVAL_OK;
 
601
}
 
602
 
 
603
 
 
604
 
 
605
 
 
606
 
 
607
#ifdef __CL_FUNCTION__
 
608
#undef __CL_FUNCTION__
 
609
#endif
 
610
#define __CL_FUNCTION__ "cl_host_list_remove_host()"
 
611
int cl_host_list_remove_host(cl_raw_list_t* list_p, cl_com_host_spec_t* host, int lock_list) {
 
612
   int ret_val = CL_RETVAL_OK;
 
613
   int function_return = CL_RETVAL_UNKOWN_HOST_ERROR;
 
614
   cl_host_list_elem_t* elem = NULL;
 
615
   
 
616
   if (list_p == NULL || host == NULL) {
 
617
      return CL_RETVAL_PARAMS;
 
618
   }
 
619
 
 
620
   if (lock_list != 0) {   
 
621
      /* lock list */
 
622
      if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
 
623
         return ret_val;
 
624
      }
 
625
   }
 
626
 
 
627
    elem = cl_host_list_get_elem_host(list_p, host->unresolved_name);
 
628
    if (elem != NULL) {
 
629
 
 
630
         /* remove element from hash table */
 
631
         if (host->unresolved_name != NULL) {
 
632
            cl_host_list_data_t* ldata = list_p->list_data;
 
633
            if (ldata->ht != NULL) {
 
634
               sge_htable_delete(ldata->ht, host->unresolved_name);
 
635
            }
 
636
         }
 
637
   
 
638
         cl_raw_list_remove_elem(list_p, elem->raw_elem);
 
639
         function_return = CL_RETVAL_OK;
 
640
         cl_com_free_hostspec(&(elem->host_spec));
 
641
         free(elem);
 
642
    }
 
643
 
 
644
   if (lock_list != 0) {
 
645
      /* unlock list */
 
646
      if ((ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
 
647
         return ret_val;
 
648
      }
 
649
   }
 
650
   return function_return;
 
651
}
 
652
 
 
653
#ifdef __CL_FUNCTION__
 
654
#undef __CL_FUNCTION__
 
655
#endif
 
656
#define __CL_FUNCTION__ "cl_host_list_get_elem_host()"
 
657
cl_host_list_elem_t* cl_host_list_get_elem_host(cl_raw_list_t* list_p, const char *unresolved_hostname) {
 
658
   cl_host_list_elem_t *elem = NULL;
 
659
 
 
660
   if (list_p != NULL && unresolved_hostname != NULL) {
 
661
      cl_host_list_data_t* ldata = list_p->list_data;
 
662
      if (ldata->ht != NULL) {
 
663
         if (sge_htable_lookup(ldata->ht, unresolved_hostname, (const void **)&elem) == True) {
 
664
            return elem;
 
665
         }
 
666
      } else {
 
667
         /* Search without having hash table */
 
668
         CL_LOG(CL_LOG_INFO,"no hash table available, searching sequential");
 
669
         elem = cl_host_list_get_first_elem(list_p);
 
670
         while ( elem != NULL) {
 
671
            if (elem->host_spec != NULL && elem->host_spec->unresolved_name != NULL ) {
 
672
               if (strcmp(elem->host_spec->unresolved_name,unresolved_hostname) == 0) {
 
673
                  /* found matching element */
 
674
                  return elem;
 
675
               }
 
676
            }
 
677
            elem = cl_host_list_get_next_elem(elem);
 
678
         }
 
679
      }
 
680
   }
 
681
   return NULL;
 
682
}
 
683
 
 
684
#ifdef __CL_FUNCTION__
 
685
#undef __CL_FUNCTION__
 
686
#endif
 
687
#define __CL_FUNCTION__ "cl_host_list_get_first_elem()"
 
688
cl_host_list_elem_t* cl_host_list_get_first_elem(cl_raw_list_t* list_p) {
 
689
   cl_raw_list_elem_t* raw_elem = cl_raw_list_get_first_elem(list_p);
 
690
   if (raw_elem) {
 
691
      return (cl_host_list_elem_t*) raw_elem->data;
 
692
   }
 
693
   return NULL;
 
694
}
 
695
 
 
696
#ifdef __CL_FUNCTION__
 
697
#undef __CL_FUNCTION__
 
698
#endif
 
699
#define __CL_FUNCTION__ "cl_host_list_get_least_elem()"
 
700
cl_host_list_elem_t* cl_host_list_get_least_elem(cl_raw_list_t* list_p) {
 
701
   cl_raw_list_elem_t* raw_elem = cl_raw_list_get_least_elem(list_p);
 
702
   if (raw_elem) {
 
703
      return (cl_host_list_elem_t*) raw_elem->data;
 
704
   }
 
705
   return NULL;
 
706
}
 
707
 
 
708
#ifdef __CL_FUNCTION__
 
709
#undef __CL_FUNCTION__
 
710
#endif
 
711
#define __CL_FUNCTION__ "cl_host_list_get_next_elem()"
 
712
cl_host_list_elem_t* cl_host_list_get_next_elem(cl_host_list_elem_t* elem) {
 
713
   cl_raw_list_elem_t* next_raw_elem = NULL;
 
714
   
 
715
   if (elem != NULL) {
 
716
      cl_raw_list_elem_t* raw_elem = elem->raw_elem;
 
717
      next_raw_elem = cl_raw_list_get_next_elem(raw_elem);
 
718
      if (next_raw_elem) {
 
719
         return (cl_host_list_elem_t*) next_raw_elem->data;
 
720
      }
 
721
   }
 
722
   return NULL;
 
723
}
 
724
 
 
725
 
 
726
#ifdef __CL_FUNCTION__
 
727
#undef __CL_FUNCTION__
 
728
#endif
 
729
#define __CL_FUNCTION__ "cl_host_list_get_last_elem()"
 
730
cl_host_list_elem_t* cl_host_list_get_last_elem(cl_host_list_elem_t* elem) {
 
731
   cl_raw_list_elem_t* last_raw_elem = NULL;
 
732
   
 
733
 
 
734
   if (elem != NULL) {
 
735
      cl_raw_list_elem_t* raw_elem = elem->raw_elem;
 
736
      last_raw_elem = cl_raw_list_get_last_elem(raw_elem);
 
737
      if (last_raw_elem) {
 
738
         return (cl_host_list_elem_t*) last_raw_elem->data;
 
739
      }
 
740
   }
 
741
   return NULL;
 
742
}
 
743
 
 
744
#ifdef __CL_FUNCTION__
 
745
#undef __CL_FUNCTION__
 
746
#endif
 
747
#define __CL_FUNCTION__ "cl_com_copy_in_addr()"
 
748
static struct in_addr* cl_com_copy_in_addr(struct in_addr *addr) {
 
749
   struct in_addr* copy = NULL;    
 
750
   
 
751
   if (addr == NULL) {
 
752
      return NULL;
 
753
   }
 
754
 
 
755
   copy = (struct in_addr*) malloc(sizeof(struct in_addr));
 
756
   if (copy != NULL) {
 
757
      memcpy((char*) copy , addr, sizeof(struct in_addr));
 
758
   }
 
759
   return copy;
 
760
}
 
761
 
 
762
#ifdef __CL_FUNCTION__
 
763
#undef __CL_FUNCTION__
 
764
#endif
 
765
#define __CL_FUNCTION__ "cl_com_copy_hostent()"
 
766
static cl_com_hostent_t* cl_com_copy_hostent(cl_com_hostent_t* hostent) {
 
767
   cl_com_hostent_t* copy = NULL;
 
768
 
 
769
   if (hostent == NULL) {
 
770
      return NULL;
 
771
   }
 
772
 
 
773
   copy = (cl_com_hostent_t*)malloc(sizeof(cl_com_hostent_t));
 
774
   if (copy != NULL) {
 
775
      copy->he = NULL;
 
776
 
 
777
      if (hostent->he != NULL) {
 
778
         copy->he = sge_copy_hostent(hostent->he);
 
779
         if (copy->he == NULL ) {
 
780
            CL_LOG(CL_LOG_ERROR,"could not copy hostent structure");
 
781
            free(copy);
 
782
            return NULL;
 
783
         }
 
784
      } 
 
785
   }
 
786
   return copy;
 
787
}
 
788