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

« back to all changes in this revision

Viewing changes to source/utilbin/spooledit.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
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
#include <errno.h>
 
37
 
 
38
#include "rmon/sgermon.h"
 
39
#include "uti/sge_string.h"
 
40
#include "uti/sge_stdio.h"
 
41
#include "sge_all_listsL.h"
 
42
#include "sge_bootstrap.h"
 
43
#include "sgermon.h"
 
44
#include "sge_log.h"
 
45
#include "sge_unistd.h"
 
46
#include "sge_dstring.h"
 
47
#include "sge_spool.h"
 
48
#include "sge_uidgid.h"
 
49
#include "setup_path.h"
 
50
#include "sge_prog.h"
 
51
#include "sge_feature.h"
 
52
#include "sge_answer.h"
 
53
#include "sge_mt_init.h"
 
54
#include "spool/sge_spooling.h"
 
55
#include "spool/loader/sge_spooling_loader.h"
 
56
#include "spool/berkeleydb/sge_bdb.h"
 
57
#include "gdi/sge_gdi_ctx.h"
 
58
 
 
59
#include "msg_common.h"
 
60
#include "msg_utilbin.h"
 
61
 
 
62
 
 
63
 
 
64
static void 
 
65
usage(const char *argv0)
 
66
{
 
67
   fprintf(stderr, "%s\n %s command\n\n", MSG_UTILBIN_USAGE, argv0);
 
68
   fprintf(stderr, "%s\n", MSG_DBSTAT_COMMANDINTRO1);
 
69
   fprintf(stderr, "%s\n", MSG_DBSTAT_COMMANDINTRO2);
 
70
   fprintf(stderr, "%s\n", MSG_DBSTAT_LIST);
 
71
   fprintf(stderr, "%s\n", MSG_DBSTAT_DUMP);
 
72
   fprintf(stderr, "%s\n", MSG_DBSTAT_LOAD);
 
73
   fprintf(stderr, "%s\n", MSG_DBSTAT_DELETE);
 
74
}
 
75
 
 
76
static int 
 
77
init_framework(sge_gdi_ctx_class_t *ctx, bdb_info *info)
 
78
{
 
79
   int ret = EXIT_FAILURE;
 
80
 
 
81
   lList *answer_list = NULL;
 
82
   lListElem *spooling_context = NULL;
 
83
   const char *spooling_method = ctx->get_spooling_method(ctx);
 
84
   const char *spooling_lib = ctx->get_spooling_lib(ctx);
 
85
   const char *spooling_params = ctx->get_spooling_params(ctx);
 
86
 
 
87
   DENTER(TOP_LAYER, "init_framework");
 
88
 
 
89
#ifdef HP1164   
 
90
   sge_set_admin_username("none", NULL);
 
91
#endif
 
92
 
 
93
   /* create spooling context */
 
94
   spooling_context = spool_create_dynamic_context(&answer_list, 
 
95
                                                   spooling_method,
 
96
                                                   spooling_lib, 
 
97
                                                   spooling_params);
 
98
   answer_list_output(&answer_list);
 
99
   if (!strcmp(bootstrap_get_spooling_method(),"classic")) {
 
100
      CRITICAL((SGE_EVENT, MSG_SPOOLDEFAULTS_CANTHANDLECLASSICSPOOLING));
 
101
   } else if (spooling_context == NULL) {
 
102
      CRITICAL((SGE_EVENT, MSG_SPOOLDEFAULTS_CANNOTCREATECONTEXT));
 
103
   } else {
 
104
      spool_set_default_context(spooling_context);
 
105
      spool_set_option(&answer_list, spooling_context, "recover=false");
 
106
      answer_list_output(&answer_list);
 
107
 
 
108
      /* initialize spooling context */
 
109
      if (!spool_startup_context(&answer_list, spooling_context, true)) {
 
110
         CRITICAL((SGE_EVENT, MSG_SPOOLDEFAULTS_CANNOTSTARTUPCONTEXT));
 
111
      } else {
 
112
         /* search the berkeley db info - take it from any object type, 
 
113
          * berkeleydb spools all objects using the same rule.
 
114
          */
 
115
         lListElem *type = spool_context_search_type(spooling_context, 
 
116
                                                     SGE_TYPE_JOB);
 
117
         lListElem *rule = spool_type_search_default_rule(type);
 
118
         *info = (bdb_info)lGetRef(rule, SPR_clientdata);
 
119
         ret = EXIT_SUCCESS;
 
120
      }
 
121
      answer_list_output(&answer_list);
 
122
   }  
 
123
 
 
124
   DRETURN(ret);
 
125
}
 
126
 
 
127
static bdb_database
 
128
get_database_from_key(const char *key)
 
129
{
 
130
   bdb_database database = BDB_CONFIG_DB;
 
131
 
 
132
   if (key != NULL) {
 
133
      if (strncmp(key, "J", 1) == 0 ||
 
134
          strncmp(key, "PET", 3) == 0) {
 
135
         database = BDB_JOB_DB;
 
136
      }
 
137
   }
 
138
   
 
139
   return database;
 
140
}
 
141
 
 
142
static const lDescr *
 
143
get_descr_from_key(const char *key) 
 
144
{
 
145
   const lDescr *descr = NULL;
 
146
 
 
147
   DENTER(TOP_LAYER, "get_descr_from_key");
 
148
 
 
149
   if (key != NULL) {
 
150
      struct saved_vars_s *context = NULL;
 
151
      const char *type_name;
 
152
 
 
153
      type_name = sge_strtok_r(key, ":", &context);
 
154
      if (type_name != NULL) {
 
155
         sge_object_type type = object_name_get_type(type_name);
 
156
         if (type != SGE_TYPE_ALL) {
 
157
            descr = object_type_get_descr(type);
 
158
         }
 
159
      }
 
160
 
 
161
      sge_free_saved_vars(context);
 
162
 
 
163
      if (descr == NULL) {
 
164
         ERROR((SGE_EVENT, MSG_DBSTAT_INVALIDKEY_S, key));
 
165
      }
 
166
   }
 
167
 
 
168
   DRETURN(descr);
 
169
}
 
170
 
 
171
static int 
 
172
list_objects(bdb_info info, const char *key) 
 
173
{
 
174
   int ret = EXIT_SUCCESS;
 
175
   bool dbret;
 
176
   lList *answer_list = NULL;
 
177
   bdb_database database;
 
178
 
 
179
   DENTER(TOP_LAYER, "list_objects");
 
180
 
 
181
   database = get_database_from_key(key);
 
182
 
 
183
   {
 
184
      /* read the list */
 
185
      lList *list = NULL;
 
186
      dbret = spool_berkeleydb_read_keys(&answer_list, info, database,
 
187
                                         &list, key);
 
188
      if (dbret) {
 
189
         /* if no key was given, read the job database as well */
 
190
         if (strlen(key) == 0) {
 
191
            dbret = spool_berkeleydb_read_keys(&answer_list, info, BDB_JOB_DB,
 
192
                                               &list, key);
 
193
         }
 
194
      }
 
195
 
 
196
      if (dbret) {
 
197
         const lListElem *elem;
 
198
         for_each(elem, list) {
 
199
            fprintf(stdout, "%s\n", lGetString(elem, STU_name));
 
200
         }
 
201
      } else {
 
202
         answer_list_output(&answer_list);
 
203
         ret = EXIT_FAILURE;
 
204
      }
 
205
 
 
206
      lFreeList(&list);
 
207
   }
 
208
 
 
209
   DRETURN(ret);
 
210
}
 
211
 
 
212
static int 
 
213
dump_object(bdb_info info, const char *key) 
 
214
{
 
215
   int ret = EXIT_SUCCESS;
 
216
   bool dbret;
 
217
   lList *answer_list = NULL;
 
218
   bdb_database database;
 
219
 
 
220
   DENTER(TOP_LAYER, "dump_object");
 
221
 
 
222
   database = get_database_from_key(key);
 
223
 
 
224
   /* start a transaction */
 
225
   dbret = spool_berkeleydb_start_transaction(&answer_list, info);
 
226
   if (!dbret) {
 
227
      answer_list_output(&answer_list);
 
228
      ret = EXIT_FAILURE;
 
229
   } else {
 
230
      /* job script is spooled as string, not as cull object */
 
231
      if (strncmp(key, "JOBSCRIPT:", 10) == 0) {
 
232
         const char *job_script;
 
233
         job_script = spool_berkeleydb_read_string(&answer_list, info, database, key);
 
234
         if (job_script == NULL) {
 
235
            answer_list_output(&answer_list);
 
236
            ret = EXIT_FAILURE;
 
237
         } else {
 
238
            printf(job_script);
 
239
            FREE(job_script);
 
240
         }
 
241
      } else {
 
242
         /* read object */
 
243
         lListElem *object;
 
244
         object = spool_berkeleydb_read_object(&answer_list, info, database, key);
 
245
         if (object == NULL) {
 
246
            answer_list_output(&answer_list);
 
247
            ret = EXIT_FAILURE;
 
248
         } else {
 
249
            lDumpElemFp(stdout, object, 0);
 
250
            lFreeElem(&object);
 
251
         }
 
252
      }
 
253
   }
 
254
 
 
255
   /* close the transaction */
 
256
   dbret = spool_berkeleydb_end_transaction(&answer_list, info, 
 
257
                                            (ret == EXIT_SUCCESS) ? true : false);
 
258
   if (!dbret) {
 
259
      answer_list_output(&answer_list);
 
260
      ret = EXIT_FAILURE;
 
261
   }
 
262
 
 
263
   DRETURN(ret);
 
264
}
 
265
 
 
266
static int 
 
267
load_object(bdb_info info, const char *key, const char *fname) 
 
268
{
 
269
   int ret = EXIT_SUCCESS;
 
270
   bool dbret;
 
271
   lList *answer_list = NULL;
 
272
   bdb_database database;
 
273
   const lDescr *descr;
 
274
   lListElem *object = NULL;
 
275
 
 
276
   DENTER(TOP_LAYER, "load_object");
 
277
 
 
278
   database = get_database_from_key(key);
 
279
   descr    = get_descr_from_key(key);
 
280
 
 
281
   if (descr == NULL) {
 
282
      ret = EXIT_FAILURE;
 
283
   }
 
284
 
 
285
   if (ret == EXIT_SUCCESS) {
 
286
      FILE *fd;
 
287
 
 
288
      if((fd = fopen(fname, "r")) == NULL) {
 
289
         ERROR((SGE_EVENT, MSG_ERROROPENINGFILEFORREADING_SS, fname, strerror(errno)));
 
290
         ret = EXIT_FAILURE;
 
291
      } else {
 
292
         object = lUndumpElemFp(fd, descr);
 
293
         FCLOSE(fd);
 
294
         if (object == NULL) {
 
295
            ERROR((SGE_EVENT, MSG_DBSTAT_ERRORUNDUMPING_S, fname));
 
296
            ret = EXIT_FAILURE;
 
297
         }
 
298
      }
 
299
   }
 
300
 
 
301
   if (object != NULL) {
 
302
      /* start a transaction */
 
303
      dbret = spool_berkeleydb_start_transaction(&answer_list, info);
 
304
      if (!dbret) {
 
305
         answer_list_output(&answer_list);
 
306
         ret = EXIT_FAILURE;
 
307
      } else {
 
308
         /* read object */
 
309
         dbret = spool_berkeleydb_write_object(&answer_list, info, database, object, key);
 
310
         if (!dbret) {
 
311
            answer_list_output(&answer_list);
 
312
            ret = EXIT_FAILURE;
 
313
         }
 
314
 
 
315
         /* close the transaction */
 
316
         dbret = spool_berkeleydb_end_transaction(&answer_list, info, 
 
317
                                                  (ret == EXIT_SUCCESS) ? true : false);
 
318
         if (!dbret) {
 
319
            answer_list_output(&answer_list);
 
320
            ret = EXIT_FAILURE;
 
321
         }
 
322
      }
 
323
   }
 
324
 
 
325
   lFreeElem(&object);
 
326
 
 
327
   DRETURN(ret);
 
328
 
 
329
FCLOSE_ERROR:
 
330
   ERROR((SGE_EVENT, MSG_FILE_ERRORCLOSEINGXY_SS, fname, strerror(errno)));
 
331
   DRETURN(EXIT_FAILURE);
 
332
}
 
333
 
 
334
static int 
 
335
delete_object( bdb_info info, const char *key) 
 
336
{
 
337
   int ret = EXIT_SUCCESS;
 
338
   bool dbret;
 
339
   lList *answer_list = NULL;
 
340
   bdb_database database;
 
341
 
 
342
   DENTER(TOP_LAYER, "delete_object");
 
343
 
 
344
   database = get_database_from_key(key);
 
345
 
 
346
   /* start a transaction */
 
347
   dbret = spool_berkeleydb_start_transaction(&answer_list, info);
 
348
   if (!dbret) {
 
349
      answer_list_output(&answer_list);
 
350
      ret = EXIT_FAILURE;
 
351
   } else {
 
352
      /* delete object with given key */
 
353
      dbret = spool_berkeleydb_delete_object(&answer_list, info, database, 
 
354
                                             key, false);
 
355
      if (!dbret) {
 
356
         answer_list_output(&answer_list);
 
357
         ret = EXIT_FAILURE;
 
358
      } else {
 
359
         fprintf(stdout, "deleted object with key "SFQ"\n", key);
 
360
      }
 
361
   }
 
362
 
 
363
   /* close the transaction */
 
364
   dbret = spool_berkeleydb_end_transaction(&answer_list, info, 
 
365
                                            (ret == EXIT_SUCCESS) ? true : false);
 
366
   if (!dbret) {
 
367
      answer_list_output(&answer_list);
 
368
      ret = EXIT_FAILURE;
 
369
   }
 
370
 
 
371
   DRETURN(ret);
 
372
}
 
373
 
 
374
 
 
375
int 
 
376
main(int argc, char *argv[])
 
377
{
 
378
   int ret = EXIT_SUCCESS;
 
379
   lList *answer_list = NULL;
 
380
   sge_gdi_ctx_class_t *ctx = NULL;
 
381
 
 
382
   DENTER_MAIN(TOP_LAYER, "spooledit");
 
383
 
 
384
   if (sge_setup2(&ctx, SPOOLDEFAULTS, MAIN_THREAD, &answer_list, false) != AE_OK) {
 
385
      answer_list_output(&answer_list);
 
386
      SGE_EXIT((void**)&ctx, 1);
 
387
   }
 
388
 
 
389
   if (ret == EXIT_SUCCESS) {
 
390
      /* parse commandline */
 
391
      if (argc < 2) {
 
392
         usage(argv[0]);
 
393
         ret = EXIT_FAILURE;
 
394
      } else {
 
395
         bdb_info info = NULL;
 
396
         ret = init_framework(ctx, &info);
 
397
 
 
398
         if (ret == EXIT_SUCCESS) {
 
399
            if (strcmp(argv[1], "list") == 0) {
 
400
               ret = list_objects(info, argc > 2 ? argv[2] : "");
 
401
            } else if (strcmp(argv[1], "dump") == 0) {
 
402
               if (argc < 3) {
 
403
                  usage(argv[0]);
 
404
                  ret = EXIT_FAILURE;
 
405
               } else {
 
406
                  ret = dump_object(info, argv[2]);
 
407
               }
 
408
            } else if (strcmp(argv[1], "load") == 0) {
 
409
               if (argc < 4) {
 
410
                  usage(argv[0]);
 
411
                  ret = EXIT_FAILURE;
 
412
               } else {
 
413
                  ret = load_object(info, argv[2], argv[3]);
 
414
               }
 
415
            } else if (strcmp(argv[1], "delete") == 0) {
 
416
               if (argc < 3) {
 
417
                  usage(argv[0]);
 
418
                  ret = EXIT_FAILURE;
 
419
               } else {
 
420
                  ret = delete_object(info, argv[2]);
 
421
               }
 
422
            } else {
 
423
               usage(argv[0]);
 
424
               ret = EXIT_FAILURE;
 
425
            }
 
426
         }
 
427
      }
 
428
   }
 
429
 
 
430
   if (spool_get_default_context() != NULL) {
 
431
#if 0
 
432
      time_t next_trigger = 0;
 
433
      if (!spool_trigger_context(&answer_list, spool_get_default_context(), 
 
434
                                 0, &next_trigger)) {
 
435
         ret = EXIT_FAILURE;
 
436
      }
 
437
#endif
 
438
      if (!spool_shutdown_context(&answer_list, spool_get_default_context())) {
 
439
         ret = EXIT_FAILURE;
 
440
      }
 
441
   }
 
442
 
 
443
   answer_list_output(&answer_list);
 
444
 
 
445
   SGE_EXIT((void**)&ctx, ret);
 
446
 
 
447
   DRETURN(ret);
 
448
}