File: sge_hgroup_qconf.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-13.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 57,140 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,445; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,704; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (480 lines) | stat: -rw-r--r-- 14,928 bytes parent folder | download | duplicates (6)
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
/*___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.
 *
 ************************************************************************/

#include "uti/sge_rmon.h"
#include "uti/sge_log.h"
#include "uti/sge_unistd.h"
#include "uti/sge_prog.h"
#include "uti/sge_edit.h"

#include "sgeobj/sge_conf.h"
#include "sgeobj/sge_answer.h"
#include "sgeobj/sge_object.h"
#include "sgeobj/sge_hgroup.h"
#include "sgeobj/sge_href.h"
#include "sgeobj/sge_hgroup.h"

#include "spool/flatfile/sge_flatfile.h"
#include "spool/flatfile/sge_flatfile_obj.h"

#include "gdi/sge_gdi.h"
#include "gdi/sge_gdi_ctx.h"

#include "sge.h"
#include "sge_hgroup_qconf.h"
#include "msg_common.h"
#include "msg_clients_common.h"


static void 
hgroup_list_show_elem(lList *hgroup_list, const char *name, int indent);
static bool
hgroup_provide_modify_context(sge_gdi_ctx_class_t *ctx, lListElem **this_elem, lList **answer_list,
                                   bool ignore_unchanged_message);


static void 
hgroup_list_show_elem(lList *hgroup_list, const char *name, int indent)
{
   const char *const indent_string = "   ";
   lListElem *hgroup = NULL;
   int i;

   DENTER(TOP_LAYER, "hgroup_list_show_elem");

   for (i = 0; i < indent; i++) {
      printf("%s", indent_string);
   }
   printf("%s\n", name);

   hgroup = lGetElemHost(hgroup_list, HGRP_name, name);   
   if (hgroup != NULL) {
      lList *sub_list = lGetList(hgroup, HGRP_host_list);
      lListElem *href = NULL;

      for_each(href, sub_list) {
         const char *href_name = lGetHost(href, HR_name);

         hgroup_list_show_elem(hgroup_list, href_name, indent + 1); 
      } 
   } 
   DRETURN_VOID;
}

bool 
hgroup_add_del_mod_via_gdi(sge_gdi_ctx_class_t *ctx,
                           lListElem *this_elem, lList **answer_list,
                           u_long32 gdi_command)
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_add_del_mod_via_gdi");
   if (this_elem != NULL) {
      lListElem *element = NULL;
      lList *hgroup_list = NULL;
      lList *gdi_answer_list = NULL;

      element = lCopyElem(this_elem);
      hgroup_list = lCreateList("", HGRP_Type);
      lAppendElem(hgroup_list, element);
      gdi_answer_list = ctx->gdi(ctx, SGE_HGRP_LIST, gdi_command,
                                &hgroup_list, NULL, NULL, false);
      answer_list_replace(answer_list, &gdi_answer_list);
      lFreeList(&hgroup_list);
   }
   DRETURN(ret);
}

lListElem *hgroup_get_via_gdi(sge_gdi_ctx_class_t *ctx,
                              lList **answer_list, const char *name) 
{
   lListElem *ret = NULL;

   DENTER(TOP_LAYER, "hgroup_get_via_gdi");
   if (name != NULL) {
      lList *gdi_answer_list = NULL;
      lEnumeration *what = NULL;
      lCondition *where = NULL;
      lList *hostgroup_list = NULL;

      what = lWhat("%T(ALL)", HGRP_Type);
      where = lWhere("%T(%I==%s)", HGRP_Type, HGRP_name, 
                     name);
      gdi_answer_list = ctx->gdi(ctx, SGE_HGRP_LIST, SGE_GDI_GET, 
                                &hostgroup_list, where, what, false);
      lFreeWhat(&what);
      lFreeWhere(&where);

      if (!answer_list_has_error(&gdi_answer_list)) {
         ret = lDechainElem(hostgroup_list, lFirst(hostgroup_list));
      } else {
         answer_list_replace(answer_list, &gdi_answer_list);
      }
      lFreeList(&hostgroup_list);
      lFreeList(&gdi_answer_list);
   } 
   DRETURN(ret);
}

static bool hgroup_provide_modify_context(sge_gdi_ctx_class_t *ctx, lListElem **this_elem, lList **answer_list,
                                   bool ignore_unchanged_message)
{
   bool ret = false;
   int status = 0;
   int fields_out[MAX_NUM_FIELDS];
   int missing_field = NoName;
   uid_t uid = ctx->get_uid(ctx);
   gid_t gid = ctx->get_gid(ctx);
   
   DENTER(TOP_LAYER, "hgroup_provide_modify_context");
   if (this_elem != NULL && *this_elem != NULL) {
      const char *filename = NULL;
      filename = spool_flatfile_write_object(answer_list, *this_elem,
                                                     false, HGRP_fields,
                                                     &qconf_sfi,
                                                     SP_DEST_TMP, SP_FORM_ASCII,
                                                     filename, false);
      if (answer_list_has_error(answer_list)) {
         if (filename != NULL) {
            unlink(filename);
            sge_free(&filename);
         }
         DRETURN(false);
      }
      
      status = sge_edit(filename, uid, gid);
      
      if (status >= 0) {
         lListElem *hgroup = NULL;

         fields_out[0] = NoName;
         hgroup = spool_flatfile_read_object(answer_list, HGRP_Type, NULL,
                                         HGRP_fields, fields_out, true, &qconf_sfi,
                                         SP_FORM_ASCII, NULL, filename);
            
         if (answer_list_output (answer_list)) {
            lFreeElem(&hgroup);
         }

         if (hgroup != NULL) {
            missing_field = spool_get_unprocessed_field(HGRP_fields, fields_out, answer_list);
         }

         if (missing_field != NoName) {
            lFreeElem(&hgroup);
            answer_list_output (answer_list);
         }      

         if (hgroup != NULL) {
            if (object_has_differences(*this_elem, answer_list,
                                       hgroup, false) || 
                ignore_unchanged_message) {
               lFreeElem(this_elem);
               *this_elem = hgroup;
               ret = true;
            } else {
               lFreeElem(&hgroup);
               answer_list_add(answer_list, MSG_FILE_NOTCHANGED,
                               STATUS_ERROR1, ANSWER_QUALITY_ERROR);
            }
         } else {
            answer_list_add(answer_list, MSG_FILE_ERRORREADINGINFILE,
                            STATUS_ERROR1, ANSWER_QUALITY_ERROR);
         }
      } else {
         answer_list_add(answer_list, MSG_PARSE_EDITFAILED,
                         STATUS_ERROR1, ANSWER_QUALITY_ERROR);
      }
      unlink(filename);
      sge_free(&filename);
   } 
   DRETURN(ret);
}

/****** sge_hgroup_qconf/hgroup_add() ******************************************
*  NAME
*     hgroup_add() -- creates a default hgroup object.
*
*  SYNOPSIS
*     bool hgroup_add(lList **answer_list, const char *name, bool 
*     is_name_validate) 
*
*  FUNCTION
*     To create a new hgrp, qconf needs a default object, that can be edited.
*
*  INPUTS
*     lList **answer_list   - any errors?
*     const char *name      - name of the hgrp
*     bool is_name_validate - should the name be validated? false, if one generates
*                             a template
*
*  RESULT
*     bool - true, if everything went fine
*
*  NOTES
*     MT-NOTE: hgroup_add() is MT safe 
*
*******************************************************************************/
bool hgroup_add(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *name, bool is_name_validate ) 
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_add");
   if (name != NULL) {
      lListElem *hgroup = hgroup_create(answer_list, name, NULL, is_name_validate);

      if (hgroup == NULL) {
         ret = false;
      }
      if (ret) {
         ret = hgroup_provide_modify_context(ctx, &hgroup, answer_list, true);
      }
      if (ret) {
         ret = hgroup_add_del_mod_via_gdi(ctx, hgroup, answer_list, SGE_GDI_ADD);
      }

      lFreeElem(&hgroup);
   }
  
   DRETURN(ret); 
}

bool hgroup_add_from_file(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *filename) 
{
   bool ret = true;
   int fields_out[MAX_NUM_FIELDS];
   int missing_field = NoName;

   DENTER(TOP_LAYER, "hgroup_add");

   if (filename != NULL) {
      lListElem *hgroup;

      fields_out[0] = NoName;
      hgroup = spool_flatfile_read_object(answer_list, HGRP_Type, NULL,
                                      HGRP_fields, fields_out, true, &qconf_sfi,
                                      SP_FORM_ASCII, NULL, filename);

      if (answer_list_output (answer_list)) {
         lFreeElem(&hgroup);
      }

      if (hgroup != NULL) {
         missing_field = spool_get_unprocessed_field (HGRP_fields, fields_out, answer_list);
      }

      if (missing_field != NoName) {
         lFreeElem(&hgroup);
         answer_list_output (answer_list);
      }

      if (hgroup == NULL) {
         ret = false;
      }
      if (ret) {
         ret = hgroup_add_del_mod_via_gdi(ctx, hgroup, answer_list, SGE_GDI_ADD); 
      }
      lFreeElem(&hgroup);
   }

   DRETURN(ret);
}

bool hgroup_modify(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *name)
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_modify");
   if (name != NULL) {
      lListElem *hgroup = hgroup_get_via_gdi(ctx, answer_list, name);

      if (hgroup == NULL) {
         answer_list_add_sprintf(answer_list, STATUS_ERROR1,
                                 ANSWER_QUALITY_ERROR, MSG_HGROUP_NOTEXIST_S, name);
         ret = false;
      }
      if (ret == true) {
         ret = hgroup_provide_modify_context(ctx, &hgroup, answer_list, false);
      }
      if (ret == true) {
         ret = hgroup_add_del_mod_via_gdi(ctx, hgroup, answer_list, SGE_GDI_MOD);
      }
      lFreeElem(&hgroup);
   }

   DRETURN(ret);
}

bool hgroup_modify_from_file(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *filename)
{
   bool ret = true;
   int fields_out[MAX_NUM_FIELDS];
   int missing_field = NoName;

   DENTER(TOP_LAYER, "hgroup_modify");
   if (filename != NULL) {
      lListElem *hgroup;

      fields_out[0] = NoName;
      hgroup = spool_flatfile_read_object(answer_list, HGRP_Type, NULL,
                                      HGRP_fields, fields_out, true, &qconf_sfi,
                                      SP_FORM_ASCII, NULL, filename);
            
      if (answer_list_output(answer_list)) {
         lFreeElem(&hgroup);
      }

      if (hgroup != NULL) {
         missing_field = spool_get_unprocessed_field(HGRP_fields, fields_out, answer_list);
      }

      if (missing_field != NoName) {
         lFreeElem(&hgroup);
         answer_list_output (answer_list);
      }      

      if (hgroup == NULL) {
         answer_list_add_sprintf(answer_list, STATUS_ERROR1,
                                 ANSWER_QUALITY_ERROR, MSG_HGROUP_FILEINCORRECT_S, filename);
         ret = false;
      }
      if (ret) {
         ret = hgroup_add_del_mod_via_gdi(ctx, hgroup, answer_list, SGE_GDI_MOD);
      }
      if (hgroup) {
         lFreeElem(&hgroup);
      }
   }

   DRETURN(ret);
}

bool hgroup_delete(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *name)
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_delete");
   if (name != NULL) {
      lListElem *hgroup = hgroup_create(answer_list, name, NULL, true); 
   
      if (hgroup != NULL) {
         ret = hgroup_add_del_mod_via_gdi(ctx, hgroup, answer_list, SGE_GDI_DEL); 
      }
      lFreeElem(&hgroup);
   }
   DRETURN(ret);
}

bool hgroup_show(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *name)
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_show");
   if (name != NULL) {
      lListElem *hgroup = hgroup_get_via_gdi(ctx, answer_list, name); 
   
      if (hgroup != NULL) {
         const char *filename;
         filename = spool_flatfile_write_object(answer_list, hgroup, false, HGRP_fields,
                                     &qconf_sfi, SP_DEST_STDOUT,
                                     SP_FORM_ASCII, NULL, false);
      
         sge_free(&filename);
         lFreeElem(&hgroup);

         if (answer_list_has_error(answer_list)) {
            DRETURN(false);
         }
      } else {
         answer_list_add_sprintf(answer_list, STATUS_ERROR1,
                                 ANSWER_QUALITY_ERROR, MSG_HGROUP_NOTEXIST_S, name); 
         ret = false;
      }
   }
   DRETURN(ret);
}

bool hgroup_show_structure(sge_gdi_ctx_class_t *ctx, lList **answer_list, const char *name, 
                           bool show_tree)
{
   bool ret = true;

   DENTER(TOP_LAYER, "hgroup_show_tree");
   if (name != NULL) {
      lList *hgroup_list = NULL;
      lListElem *hgroup = NULL;
      lEnumeration *what = NULL;
      lList *alp = NULL;
      lListElem *alep = NULL;

      what = lWhat("%T(ALL)", HGRP_Type);
      alp = ctx->gdi(ctx, SGE_HGRP_LIST, SGE_GDI_GET, &hgroup_list, NULL, what, false);
      lFreeWhat(&what);

      alep = lFirst(alp);
      answer_exit_if_not_recoverable(alep);
      if (answer_get_status(alep) != STATUS_OK) {
         fprintf(stderr, "%s\n", lGetString(alep, AN_text));
         lFreeList(&alp);
         DRETURN(false);
      }

      hgroup = lGetElemHost(hgroup_list, HGRP_name, name); 
      if (hgroup != NULL) {
         if (show_tree) {
            hgroup_list_show_elem(hgroup_list, name, 0);
         } else {
            dstring string = DSTRING_INIT;
            lList *sub_host_list = NULL;
            lList *sub_hgroup_list = NULL;

            hgroup_find_all_references(hgroup, answer_list, hgroup_list, 
                                       &sub_host_list, &sub_hgroup_list);
            href_list_make_uniq(sub_host_list, answer_list);
            href_list_append_to_dstring(sub_host_list, &string);
            if (sge_dstring_get_string(&string)) {
               printf("%s\n", sge_dstring_get_string(&string));
            }
            sge_dstring_free(&string);
            lFreeList(&sub_host_list);
            lFreeList(&sub_hgroup_list);
         }
      } else {
         answer_list_add_sprintf(answer_list, STATUS_ERROR1,
                                 ANSWER_QUALITY_ERROR, MSG_HGROUP_NOTEXIST_S, name); 
         ret = false;
      }

      lFreeList(&hgroup_list);
      lFreeList(&alp);
   }
   DRETURN(ret);
}