File: sge_feature.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (626 lines) | stat: -rw-r--r-- 18,091 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*___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.
 *   Copyright (C) 2013 Dave Love University of Liverpool
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <string.h>
#include <errno.h>
#include <pthread.h>

#include "uti/sge_rmon.h"
#include "uti/config_file.h"
#include "uti/sge_prog.h"
#include "uti/sge_log.h"

#include "cull/cull.h"

#include "gdi/version.h"

#include "sgeobj/sge_feature.h"         
#include "sgeobj/sge_answer.h"
#include "sgeobj/sge_utility.h"
#include "sgeobj/msg_sgeobjlib.h"
#include "uti/sge_string.h"
#include "uti/sge_dstring.h"

#include "sge.h"
#include "basis_types.h"
#include "msg_common.h"

#define FEATURESET_DEFAULT FEATURESET_SGE


struct feature_state_t {
    int    already_read_from_file;
    lList* Master_FeatureSet_List;
};

/* Fixme:  Allow security methods to be dynamically loadable -- see
   comments in sge_security.c.  */

static const featureset_names_t featureset_list[] = {
   {FEATURE_NO_SECURITY,            "none"},
   {FEATURE_AFS_SECURITY,           "AFS"},
   /* The next two are both GSSAPI, and don't distinguish between
      modules built for one or the other.  */
   {FEATURE_DCE_SECURITY,           "DCE"},
   {FEATURE_KERBEROS_SECURITY,      "Kerberos"},
   {FEATURE_CSP_SECURITY,           "CSP"},
   {FEATURE_MUNGE_SECURITY,         "MUNGE"},
   {FEATURE_UNINITIALIZED,          NULL}
};
/* *INDENT-ON* */

static pthread_once_t feature_once = PTHREAD_ONCE_INIT;
static pthread_key_t feature_state_key;

static void feature_once_init(void);
static void feature_state_destroy(void* theState);
static void feature_state_init(struct feature_state_t* theState);
static feature_id_t feature_get_featureset_id(const char *name); 


/****** sgeobj/feature/feature_mt_init() **************************************
*  NAME
*     feature_mt_init() -- Initialize feature code for multi threading use.
*
*  SYNOPSIS
*     void feature_mt_init(void) 
*
*  FUNCTION
*     Set up feature code. This function must be called at least once before
*     any of the feature oriented functions can be used. This function is
*     idempotent, i.e. it is safe to call it multiple times.
*
*     Thread local storage for the feature code state information is reserved. 
*
*  INPUTS
*     void - NONE 
*
*  RESULT
*     void - NONE
*
*  NOTES
*     MT-NOTE: feature_mt_init() is MT safe 
*******************************************************************************/
void feature_mt_init(void)
{
   pthread_once(&feature_once, feature_once_init);
}

/****** sgeobj/feature/feature_set_already_read_from_file() *******************
*  NAME
*     feature_set_already_read_from_file()
*
*  SYNOPSIS
*     void feature_set_already_read_from_file(int i)
*
*  FUNCTION
*     Set per thread global variable already_read_from_file.
*******************************************************************************/
void feature_set_already_read_from_file(int i)
{
   GET_SPECIFIC(struct feature_state_t, feature_state, feature_state_init, feature_state_key, "feature_set_already_read_from_file");
   feature_state->already_read_from_file = i;
}

/****** sgeobj/feature/feature_get_already_read_from_file() *******************
*  NAME
*     feature_get_already_read_from_file()
*
*  SYNOPSIS
*     void feature_get_already_read_from_file(int i)
*
*  RESULT
*     Returns value of per thread global variable already_read_from_file.
*******************************************************************************/
int feature_get_already_read_from_file(void)
{
   GET_SPECIFIC(struct feature_state_t, feature_state, feature_state_init, feature_state_key, "feature_get_already_read_from_file");
   return feature_state->already_read_from_file;
}

/****** sgeobj/feature/feature_get_master_featureset_list() *******************
*  NAME
*     feature_get_master_featureset_list()
*
*  SYNOPSIS
*     lList **feature_get_master_featureset_list(void)
*
*  RESULT
*     Returns pointer to location where per thread global list 
*     Master_FeatureSet_List is stored.
*******************************************************************************/
lList **feature_get_master_featureset_list(void)
{
   GET_SPECIFIC(struct feature_state_t, feature_state, feature_state_init, feature_state_key, "feature_get_already_read_from_file");
   return &(feature_state->Master_FeatureSet_List);
}

/****** sgeobj/feature/feature_initialize_from_string() ***********************
*  NAME
*     feature_initialize_from_string() -- tag one featureset as active 
*
*  SYNOPSIS
*     int feature_initialize_from_string(char *mode) 
*
*  FUNCTION
*     This function interprets the mode string and tags the 
*     corresponding featureset entry within the Master_FeatureSet_List
*     as active.  
*
*  INPUTS
*     char *mode - product mode string (valid strings are defined in
*                  the array featureset_list[])
*
*  RESULT
*     0 OK
*    -3 unknown mode-string
*
*  NOTES
*     MT-NOTE: feature_initialize_from_string() is MT safe
******************************************************************************/
int feature_initialize_from_string(const char *mode) 
{
   feature_id_t id;
   int ret = 0;
   char *tok;
   struct saved_vars_s *context = NULL;

   DENTER(TOP_LAYER, "feature_initialize_from_string");

   /* Allow a list of security settings.  Don't make it
      space-separated, for the benefit of installer etc, which assume
      one field for security parameter.  */
   while ((tok = sge_strtok_r(mode, ",", &context))) {
      mode = NULL;
      id = feature_get_featureset_id(tok);
      if (id == FEATURE_UNINITIALIZED) {
         ERROR((SGE_EVENT, MSG_GDI_INVALIDPRODUCTMODESTRING_S, tok));
         ret = -3;
      }
#if !HAVE_MUNGE
      else if (FEATURE_MUNGE_SECURITY == id) {
         ERROR((SGE_EVENT, MSG_GDI_MODE_UNAVAILABLE_S, tok));
         ret = -3;
      }
#endif
      else {
         feature_activate(id);
         ret = 0;
      }
   }
   sge_free_saved_vars(context);
   DEXIT;
   return ret;
}

/****** sgeobj/feature/feature_initialize() ***********************************
*  NAME
*     feature_initialize() -- initialize this module
*
*  SYNOPSIS
*     static void feature_initialize(void)
*
*  FUNCTION
*     build up the CULL list "Master_FeatureSet_List" (FES_Type) with
*     information found in the array "enabled_features_mask"
*
*  INPUTS
*     static array enabled_features_mask[][]
*
*  RESULT
*     initialized Master_FeatureSet_List
*
*  NOTES
*     MT-NOTE: feature_initialize() is MT safe
******************************************************************************/
void feature_initialize(void)
{
   if (!*feature_get_master_featureset_list()) {
      lListElem *featureset;
      int featureset_id;

      for(featureset_id = 0;
          featureset_id < FEATURE_LAST_ENTRY;
          featureset_id++) {
         featureset = lAddElemUlong(feature_get_master_featureset_list(), FES_id,
                                  featureset_id, FES_Type);
         lSetUlong(featureset, FES_active, 0);
      }
   }
}

/* fixme:  allow stacking security features */
/****** sgeobj/feature/feature_activate() *************************************
*  NAME
*     feature_activate() -- set a feature active
*
*  SYNOPSIS
*     void feature_activate(featureset_id_t)
*
*  FUNCTION
*     Marks the feature with the given id active.
*      
*
*  INPUTS
*     id - feature set constant 
*
*  RESULT
*     modifies the Master_FeatureSet_List  
*
*  NOTES
*     MT-NOTE: feature_activate() is MT safe
******************************************************************************/
void feature_activate(feature_id_t id) 
{
   lList **feature_list_pp;
   lList *feature_list;
   lListElem *to_set;

   DENTER(TOP_LAYER, "feature_activate");

   /* Get feature list. If it hasn't been initialized yet, do so */
   feature_list_pp = feature_get_master_featureset_list();
   feature_list = *feature_list_pp;
   if (feature_list == NULL) {
      feature_initialize();
      feature_list_pp = feature_get_master_featureset_list();
      feature_list = *feature_list_pp;
   }

   if ((to_set = lGetElemUlong(feature_list, FES_id, id)))
      lSetUlong(to_set, FES_active, 1);

   DEXIT;
}

/****** sgeobj/feature/feature_get_active_featureset() *********************
*  NAME
*     feature_get_active_featureset() -- current active featureset
*
*  SYNOPSIS
*     u_long32 feature_get_active_featureset()
*
*  FUNCTION
*     return bitwise or of the current active features
*
*  RESULT
*     u_long32 - featureset
*
*  NOTES
*     MT-NOTE: feature_get_active_featureset() is MT safe
******************************************************************************/
u_long32 feature_get_active_featureset(void)
{
   lListElem *feature;
   u_long32 ret = FEATURE_UNINITIALIZED;
   lList **featurelist_pp = NULL;

   DENTER(TOP_LAYER, "feature_get_active_featureset");

   featurelist_pp = feature_get_master_featureset_list();
   if (featurelist_pp != NULL) {
      for_each(feature, *featurelist_pp) {
         if (lGetUlong(feature, FES_active)) {
            ret |= 1 << (lGetUlong(feature, FES_id) - 1);
            break;
         }
      }
   }
   DEXIT;
   return ret;  
}

/****** sgeobj/feature/feature_get_featureset_name() **************************
*  NAME
*     feature_get_featureset_name() -- return the product mode string
*
*  SYNOPSIS
*     char* feature_get_featureset_name(feature_id_t id) 
*
*  FUNCTION
*     returns the corresponding modestring for a featureset constant 
*
*  INPUTS
*     feature_id_t id - constant
*
*  RESULT
*     mode string 
*
*  NOTES
*     MT-NOTE: feature_get_featureset_name() is MT safe
******************************************************************************/
const char *feature_get_featureset_name(feature_id_t id) 
{
   int i = 0;
   char *ret = "<<unknown>>";

   DENTER(TOP_LAYER, "feature_get_featureset_name");
   while (featureset_list[i].name && featureset_list[i].id != id) {
      i++;
   }
   if (featureset_list[i].name) {
      ret = featureset_list[i].name;
   } 
   DEXIT;
   return ret; 
}

/****** sgeobj/feature/feature_get_featureset_id() ****************************
*  NAME
*     feature_get_featureset_id() -- Value for a featureset string 
*
*  SYNOPSIS
*     feature_id_t feature_get_featureset_id(char* name) 
*
*  FUNCTION
*     This function returns the corresponding enum value for
*     a given featureset string 
*
*  INPUTS
*     char* name - feature set name earlier known as product 
*                  mode string 
*
*  RESULT
*     feature_id_t 
*
*  NOTES
*     MT-NOTE: feature_get_featureset_id() is MT safe
******************************************************************************/
static feature_id_t feature_get_featureset_id(const char *name) 
{
   int i = 0;
   feature_id_t ret = FEATURE_UNINITIALIZED;

   DENTER(TOP_LAYER, "feature_get_featureset_id");
   if (!name) {
      DEXIT;
      return ret;
   }

   while (featureset_list[i].name && strcasecmp(featureset_list[i].name, name)) {
      i++;
   }
   if (featureset_list[i].name) {
      ret = featureset_list[i].id;
   } 
   DEXIT;
   return ret;
}

/****** sgeobj/feature/feature_is_enabled() ***********************************
*  NAME
*     feature_is_enabled() -- 0/1 whether the feature is enabled 
*
*  SYNOPSIS
*     bool feature_is_enabled(feature_id_t id) 
*
*  FUNCTION
*     return true or false whether the given feature is enabled or 
*     disabled in the current active featureset 
*
*  INPUTS
*     feature_id_t id 
*
*  RESULT
*     true or false
*
*  NOTES
*     MT-NOTE: feature_is_enabled() is MT safe
******************************************************************************/
bool feature_is_enabled(feature_id_t id) 
{
   lList **featurelist_pp = NULL;
   lListElem *feature;

   DENTER(BASIS_LAYER, "feature_is_enabled");
   featurelist_pp = feature_get_master_featureset_list();
   if (featurelist_pp != NULL) {
      for_each(feature, *featurelist_pp) {
         if ((lGetUlong(feature, FES_id) == id)
             && lGetUlong(feature, FES_active))
            return true;
      }
   }
   DEXIT;
   return false;
}  
 
/****** sgeobj/feature/feature_get_product_name() *****************************
*  NAME
*     feature_get_product_name() -- get product name string
*
*  SYNOPSIS
*     char* feature_get_product_name(featureset_product_name_id_t style)
*
*  FUNCTION
*     This function will return a text string containing the
*     the product name. The return value depends on the style
*     parameter. An invalid style value will automatically be
*     interpreted as FS_SHORT.
*
*  INPUTS
*     style     - FS_SHORT         = return short name
*                 FS_LONG          = return long name
*                 FS_VERSION       = return version
*                 FS_SHORT_VERSION = return short name and version
*                 FS_LONG_VERSION  = return long name and version
*     buffer    - buffer provided by caller 
*                 the string returned by this function points to 
*                 this buffer
*
*  RESULT
*     char* - string
*
*  NOTES
*     MT-NOTE: feature_get_product_name() is MT safe
******************************************************************************/
const char *feature_get_product_name(featureset_product_name_id_t style, dstring *buffer)
{
   const char *long_name  = "";
   const char *short_name = "";
   const char *version    = "";
   const char *ret = NULL;
   DENTER(TOP_LAYER, "feature_get_product_name");

   if (feature_get_active_featureset() != FEATURE_UNINITIALIZED ) {
      short_name = GE_SHORTNAME;
      long_name  = GE_LONGNAME;
   }
   version = GDI_VERSION; 

   switch (style) {
      case FS_SHORT:
         ret = short_name;
         break;

      case FS_LONG:
         ret = long_name;
         break;

      case FS_VERSION:
         ret = version;
         break;

      case FS_SHORT_VERSION:
         sge_dstring_sprintf(buffer, ""SFN" "SFN"", short_name, version);
         ret = sge_dstring_get_string(buffer);
         break;

      case FS_LONG_VERSION:
         sge_dstring_sprintf(buffer, ""SFN" "SFN"", long_name, version);
         ret = sge_dstring_get_string(buffer);
         break;

      default:
         ret = short_name;
         break;
   }
#ifdef DAILY_BUILD_NUMBER
   sge_dstring_sprintf_append(buffer, " (build %d)", DAILY_BUILD_NUMBER);
#endif

   DRETURN(ret);
}

/****** sgeobj/feature/feature_once_init() ************************************
*  NAME
*     feature_once_init() -- One-time feature code initialization.
*
*  SYNOPSIS
*     static feature_once_init(void) 
*
*  FUNCTION
*     Create access key for thread local storage. Register cleanup function.
*
*     This function must be called exactly once.
*
*  INPUTS
*     void - none
*
*  RESULT
*     void - none 
*
*  NOTES
*     MT-NOTE: feature_once_init() is MT safe. 
*******************************************************************************/
static void feature_once_init(void)
{
   pthread_key_create(&feature_state_key, feature_state_destroy);
}

/****** sgeobj/feature/feature_state_destroy() ********************************
*  NAME
*     feature_state_destroy() -- Free thread local storage
*
*  SYNOPSIS
*     static void feature_state_destroy(void* theState) 
*
*  FUNCTION
*     Free thread local storage.
*
*  INPUTS
*     void* theState - Pointer to memory which should be freed.
*
*  RESULT
*     static void - none
*
*  NOTES
*     MT-NOTE: feature_state_destroy() is MT safe.
*******************************************************************************/
static void feature_state_destroy(void* theState)
{
   struct feature_state_t* state = (struct feature_state_t *)theState;

   lFreeList(&(state->Master_FeatureSet_List));
   sge_free(&state);
}

/****** sgeobj/feature/feature_state_init() ***********************************
*  NAME
*     feature_state_init() -- Initialize feature code state.
*
*  SYNOPSIS
*     static void feature_state_init(struct feature_state_t* theState) 
*
*  FUNCTION
*     Initialize feature code state.
*
*  INPUTS
*     struct feature_state_t* theState - Pointer to feature state structure.
*
*  RESULT
*     static void - none
*
*  NOTES
*     MT-NOTE: feature_state_init() is MT safe. 
*******************************************************************************/
static void feature_state_init(struct feature_state_t* theState)
{
   memset(theState, 0, sizeof(struct feature_state_t));
}

const char *feature_get_featureset_names(dstring *buffer, u_long32 featureset)
{
   bool first = true;
   int i = 1;

   while (featureset_list[i].name) {
      if (featureset & ((1 << featureset_list[i].id) - 1)) {
         sge_dstring_sprintf_append(buffer, first ? "%s" : " %s",
                                    featureset_list[i].name);
         first = false;
      }
      i++;
   }
   return sge_dstring_get_string(buffer);
}