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

« back to all changes in this revision

Viewing changes to source/daemons/shepherd/sge_fileio.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 <sys/types.h>
 
34
#include <sys/time.h>
 
35
#include <sys/resource.h>
 
36
#include <sys/wait.h>
 
37
#include <stdio.h>
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <errno.h>
 
41
 
 
42
#include "basis_types.h"
 
43
#include "config_file.h"
 
44
#include "err_trace.h"
 
45
#include "execution_states.h"
 
46
#include "uti/sge_stdio.h"
 
47
#include "uti/sge_uidgid.h"
 
48
#include "msg_common.h"
 
49
 
 
50
bool shepherd_write_pid_file(pid_t pid, dstring *errmsg)
 
51
{
 
52
   bool ret = true;
 
53
   FILE *fp = NULL;
 
54
 
 
55
   fp = fopen("pid", "w");
 
56
   if (fp != NULL) {
 
57
      if (fprintf(fp, pid_t_fmt"\n", pid) < 0) {
 
58
         sge_dstring_sprintf(errmsg, MSG_FILE_CANNOT_WRITE_SS, "pid", strerror(errno));
 
59
         ret = false;
 
60
      } else {
 
61
         if (fflush(fp) < 0) {
 
62
            sge_dstring_sprintf(errmsg, MSG_FILE_CANNOT_FLUSH_SS, "pid", strerror(errno));
 
63
            ret = false;
 
64
         }
 
65
      }
 
66
      FCLOSE(fp);
 
67
   } else {
 
68
      sge_dstring_sprintf(errmsg, MSG_FILE_NOOPEN_SS, "pid", strerror(errno));
 
69
      ret = false;
 
70
   }
 
71
   return ret;
 
72
FCLOSE_ERROR:
 
73
   sge_dstring_sprintf(errmsg, MSG_FILE_NOCLOSE_SS, "pid", strerror(errno));
 
74
   return false;
 
75
}
 
76
 
 
77
bool
 
78
shepherd_read_qrsh_pid_file(const char *filename, pid_t *qrsh_pid,
 
79
                            int *replace_qrsh_pid)
 
80
{
 
81
   bool ret = true;
 
82
   FILE *fp = NULL;
 
83
 
 
84
   fp = fopen(filename, "r");
 
85
   if (fp != NULL) {
 
86
      int arguments = fscanf(fp, pid_t_fmt, qrsh_pid);
 
87
 
 
88
      if (arguments == 1) {
 
89
         char buffer[50];
 
90
 
 
91
         /* set pid from qrsh_starter as job_pid */
 
92
         sprintf(buffer, pid_t_fmt, *qrsh_pid);
 
93
         /* TODO: should better be add_or_replace */
 
94
         add_config_entry("job_pid", buffer);
 
95
         *replace_qrsh_pid = 0;
 
96
      } else {
 
97
         shepherd_trace("could not read qrsh_pid file");
 
98
         ret = false;
 
99
      }
 
100
      FCLOSE(fp);
 
101
   } else {
 
102
      /*
 
103
       * CR 6588743 - raising a shepherd_error here would set the queue in
 
104
       *              error state and rerun the job
 
105
       */
 
106
      shepherd_trace(MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
107
      ret = false;
 
108
   }
 
109
   return ret;
 
110
FCLOSE_ERROR:
 
111
   /*
 
112
    * CR 6588743 - raising a shepherd_error here would set the queue in
 
113
    *              error state and rerun the job
 
114
    */
 
115
   shepherd_trace(MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
116
   return false;
 
117
}
 
118
 
 
119
bool
 
120
shepherd_write_usage_file(u_long32 wait_status, int exit_status,
 
121
                          int child_signal, u_long32 start_time,
 
122
                          u_long32 end_time, struct rusage *rusage)
 
123
{
 
124
   bool ret = true;
 
125
   const char *const filename = "usage";
 
126
   FILE *fp = NULL;
 
127
 
 
128
   shepherd_trace("writing usage file to \"usage\"");
 
129
 
 
130
   fp = fopen(filename, "w");
 
131
   if (fp != NULL) {
 
132
      /*
 
133
       * the wait status is returned by japi_wait()
 
134
       * see sge_reportL.h for bitmask and makro definition
 
135
       */
 
136
      FPRINTF((fp, "wait_status="sge_u32"\n", wait_status));
 
137
      FPRINTF((fp, "exit_status=%d\n", exit_status));
 
138
      FPRINTF((fp, "signal=%d\n", child_signal));
 
139
 
 
140
      FPRINTF((fp, "start_time=%d\n", (int) start_time));
 
141
      FPRINTF((fp, "end_time=%d\n", (int) end_time));
 
142
      FPRINTF((fp, "ru_wallclock="sge_u32"\n", (u_long32) end_time-start_time));
 
143
#if defined(NEC_ACCOUNTING_ENTRIES)
 
144
      /* Additional accounting information for NEC SX-4 SX-5 */
 
145
#if defined(NECSX4) || defined(NECSX5)
 
146
#if defined(NECSX4)
 
147
      FPRINTF((fp, "necsx_necsx4="sge_u32"\n", 1));
 
148
#elif defined(NECSX5)
 
149
      FPRINTF((fp, "necsx_necsx5="sge_u32"\n", 1));
 
150
#endif
 
151
      FPRINTF((fp, "necsx_base_prty="sge_u32"\n", 0));
 
152
      FPRINTF((fp, "necsx_time_slice="sge_u32"\n", 0));
 
153
      FPRINTF((fp, "necsx_num_procs="sge_u32"\n", 0));
 
154
      FPRINTF((fp, "necsx_kcore_min="sge_u32"\n", 0));
 
155
      FPRINTF((fp, "necsx_mean_size="sge_u32"\n", 0));
 
156
      FPRINTF((fp, "necsx_maxmem_size="sge_u32"\n", 0));
 
157
      FPRINTF((fp, "necsx_chars_trnsfd="sge_u32"\n", 0));
 
158
      FPRINTF((fp, "necsx_blocks_rw="sge_u32"\n", 0));
 
159
      FPRINTF((fp, "necsx_inst="sge_u32"\n", 0));
 
160
      FPRINTF((fp, "necsx_vector_inst="sge_u32"\n", 0));
 
161
      FPRINTF((fp, "necsx_vector_elmt="sge_u32"\n", 0));
 
162
      FPRINTF((fp, "necsx_vec_exe="sge_u32"\n", 0));
 
163
      FPRINTF((fp, "necsx_flops="sge_u32"\n", 0));
 
164
      FPRINTF((fp, "necsx_conc_flops="sge_u32"\n", 0));
 
165
      FPRINTF((fp, "necsx_fpec="sge_u32"\n", 0));
 
166
      FPRINTF((fp, "necsx_cmcc="sge_u32"\n", 0));
 
167
      FPRINTF((fp, "necsx_bccc="sge_u32"\n", 0));
 
168
      FPRINTF((fp, "necsx_mt_open="sge_u32"\n", 0));
 
169
      FPRINTF((fp, "necsx_io_blocks="sge_u32"\n", 0));
 
170
      FPRINTF((fp, "necsx_multi_single="sge_u32"\n", 0));
 
171
      FPRINTF((fp, "necsx_max_nproc="sge_u32"\n", 0));
 
172
#endif
 
173
#endif
 
174
 
 
175
      FPRINTF((fp, "ru_utime=%f\n", (double)rusage->ru_utime.tv_sec + (double)rusage->ru_utime.tv_usec / 1000000.0));
 
176
      FPRINTF((fp, "ru_stime=%f\n", (double)rusage->ru_stime.tv_sec + (double)rusage->ru_stime.tv_usec / 1000000.0));
 
177
      FPRINTF((fp, "ru_maxrss=%ld\n", rusage->ru_maxrss));
 
178
      FPRINTF((fp, "ru_ixrss=%ld\n", rusage->ru_ixrss));
 
179
#if defined(ultrix)
 
180
      FPRINTF((fp, "ru_ismrss=%ld\n", rusage->ru_ismrss));
 
181
#endif
 
182
      FPRINTF((fp, "ru_idrss=%ld\n", rusage->ru_idrss));
 
183
      FPRINTF((fp, "ru_isrss=%ld\n", rusage->ru_isrss));
 
184
      FPRINTF((fp, "ru_minflt=%ld\n", rusage->ru_minflt));
 
185
      FPRINTF((fp, "ru_majflt=%ld\n", rusage->ru_majflt));
 
186
      FPRINTF((fp, "ru_nswap=%ld\n", rusage->ru_nswap));
 
187
      FPRINTF((fp, "ru_inblock=%ld\n", rusage->ru_inblock));
 
188
      FPRINTF((fp, "ru_oublock=%ld\n", rusage->ru_oublock));
 
189
      FPRINTF((fp, "ru_msgsnd=%ld\n", rusage->ru_msgsnd));
 
190
      FPRINTF((fp, "ru_msgrcv=%ld\n", rusage->ru_msgrcv));
 
191
      FPRINTF((fp, "ru_nsignals=%ld\n", rusage->ru_nsignals));
 
192
      FPRINTF((fp, "ru_nvcsw=%ld\n", rusage->ru_nvcsw));
 
193
      FPRINTF((fp, "ru_nivcsw=%ld\n", rusage->ru_nivcsw));
 
194
 
 
195
      FCLOSE(fp);
 
196
 
 
197
   } else {
 
198
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
199
      ret = false;
 
200
   }
 
201
   return ret;
 
202
FPRINTF_ERROR:
 
203
FCLOSE_ERROR:
 
204
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
205
   return false;
 
206
}
 
207
 
 
208
bool
 
209
shepherd_write_job_pid_file(const char *job_pid)
 
210
{
 
211
   bool ret = true;
 
212
   const char *const filename = "job_pid";
 
213
   FILE *fp = NULL;
 
214
 
 
215
   fp = fopen(filename, "w");
 
216
   if (fp != NULL) {
 
217
      FPRINTF((fp, "%s\n", job_pid));
 
218
      FCLOSE(fp);
 
219
   } else {
 
220
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
221
      ret = false;
 
222
   }
 
223
   return ret;
 
224
FPRINTF_ERROR:
 
225
FCLOSE_ERROR:
 
226
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
227
   return false;
 
228
}
 
229
 
 
230
bool
 
231
shepherd_write_sig_info_file(const char *filename, const char *task_id,
 
232
                             u_long32 exit_status)
 
233
{
 
234
   bool ret = true;
 
235
   FILE *fp = NULL;
 
236
 
 
237
   fp = fopen(filename, "a");
 
238
   if (fp != NULL) {
 
239
      FPRINTF((fp, "%s "sge_u32"\n", task_id, exit_status));
 
240
      FCLOSE(fp);
 
241
   } else {
 
242
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
243
      ret = false;
 
244
   }
 
245
   return ret;
 
246
FPRINTF_ERROR:
 
247
FCLOSE_ERROR:
 
248
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
249
   return false;
 
250
}
 
251
 
 
252
 
 
253
bool shepherd_write_osjobid_file(const char *osjobid)
 
254
{
 
255
   bool ret = true;
 
256
   const char *const filename = "osjobid";
 
257
   FILE *fp = NULL;
 
258
 
 
259
   fp = fopen(filename, "w");
 
260
   if (fp != NULL) {
 
261
      FPRINTF((fp, "%s\n", osjobid));
 
262
      FCLOSE(fp);
 
263
   } else {
 
264
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
265
      ret = false;
 
266
   }
 
267
   return ret;
 
268
FPRINTF_ERROR:
 
269
FCLOSE_ERROR:
 
270
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
271
   return false;
 
272
}
 
273
 
 
274
bool 
 
275
shepherd_write_processor_set_number_file(int proc_set)
 
276
{
 
277
   bool ret = true;
 
278
   const char *const filename = "processor_set_number";
 
279
   FILE *fp = NULL;
 
280
 
 
281
   fp = fopen(filename, "w");
 
282
   if (fp != NULL) {
 
283
      FPRINTF((fp, "%d\n", proc_set));
 
284
      FCLOSE(fp);
 
285
   } else {
 
286
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
287
      ret = false;
 
288
   }
 
289
   return ret;
 
290
FPRINTF_ERROR:
 
291
FCLOSE_ERROR:
 
292
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
293
   return false;
 
294
}
 
295
 
 
296
bool 
 
297
shepherd_write_shepherd_about_to_exit_file(void)
 
298
{
 
299
   bool ret = true;
 
300
   const char *const filename = "shepherd_about_to_exit";
 
301
   FILE *fd = NULL;
 
302
 
 
303
   fd = fopen(filename, "w");
 
304
   if (fd != NULL) {
 
305
      FCLOSE(fd);
 
306
   } else {
 
307
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
308
      ret = false;
 
309
   }
 
310
   return ret;
 
311
FCLOSE_ERROR:
 
312
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
313
   return false;
 
314
}
 
315
 
 
316
bool 
 
317
shepherd_read_exit_status_file(int *return_code)
 
318
{
 
319
   bool ret = true;
 
320
   FILE *fp = NULL;
 
321
   const char *const filename = "exit_status";
 
322
 
 
323
   fp = fopen(filename, "r");
 
324
   if (fp != NULL) {
 
325
      int arguments = fscanf(fp, "%d\n", return_code);
 
326
      /* retrieve first exit status from exit status file */
 
327
 
 
328
      if (arguments != 1) {
 
329
         shepherd_trace("could not read exit_status file");
 
330
         *return_code = ESSTATE_NO_EXITSTATUS;
 
331
         ret = false;
 
332
      }
 
333
   } else {
 
334
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
335
      ret = false;
 
336
   }
 
337
   FCLOSE(fp);
 
338
   return ret;
 
339
FCLOSE_ERROR:
 
340
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
341
   return false;
 
342
}
 
343
 
 
344
bool 
 
345
shepherd_read_qrsh_file(const char* pid_file_name, pid_t *qrsh_pid)
 
346
{
 
347
   bool ret = true;
 
348
   FILE *fp = NULL;
 
349
 
 
350
   fp = fopen(pid_file_name, "r");
 
351
   if (fp != NULL) {
 
352
      int arguments = fscanf(fp, pid_t_fmt, qrsh_pid);
 
353
 
 
354
      /* retrieve first exit status from exit status file */
 
355
      if (arguments != 1) {
 
356
         shepherd_trace("could not read qrsh_pid_file '%s'", pid_file_name);
 
357
         *qrsh_pid = 0;
 
358
         ret = false;
 
359
      } 
 
360
      FCLOSE(fp);
 
361
   } else {
 
362
      /*
 
363
       * CR 6588743 - raising a shepherd_error here would set the queue in
 
364
       *              error state and rerun the job
 
365
       */
 
366
      shepherd_trace(MSG_FILE_NOOPEN_SS, pid_file_name, strerror(errno));
 
367
      ret = false;
 
368
   }
 
369
   return ret;
 
370
FCLOSE_ERROR:
 
371
   /*
 
372
    * CR 6588743 - raising a shepherd_error here would set the queue in
 
373
    *              error state and rerun the job
 
374
    */
 
375
   shepherd_trace(MSG_FILE_NOCLOSE_SS, pid_file_name, strerror(errno));
 
376
   return false;
 
377
}
 
378
 
 
379
bool 
 
380
shepherd_read_processor_set_number_file(int *proc_set)
 
381
{
 
382
   bool ret = true;
 
383
   FILE *fp = NULL;
 
384
   const char *const filename = "processor_set_number";
 
385
 
 
386
   fp = fopen(filename, "r");
 
387
   if (fp != NULL) {
 
388
      int arguments = fscanf(fp, "%d", proc_set);
 
389
 
 
390
      if (arguments != 1) {
 
391
         shepherd_trace("could not read processor_set_number file");
 
392
         *proc_set = 0;
 
393
         ret = false;
 
394
      } 
 
395
   } else {
 
396
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
397
      ret = false;
 
398
   }
 
399
   FCLOSE(fp);
 
400
   return ret;
 
401
FCLOSE_ERROR:
 
402
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
403
   return false;
 
404
}
 
405
 
 
406
#if defined(IRIX) || defined(CRAY) || defined(NECSX4) || defined(NECSX5)
 
407
bool 
 
408
shepherd_read_osjobid_file(
 
409
#if (IRIX)
 
410
   ash_t *return_code,
 
411
#elif defined(NECSX4) || defined(NECSX5)
 
412
   id_t *return_code,
 
413
#elif defined(CRAY)
 
414
   int *return_code,
 
415
#endif
 
416
   bool is_error
 
417
)
 
418
{
 
419
   bool ret = true;
 
420
   FILE *fp = NULL;
 
421
   const char *const filename = "osjobid";
 
422
 
 
423
   fp = fopen(filename, "r");
 
424
   if (fp != NULL) {
 
425
      int arguments = 0;
 
426
 
 
427
#if defined(IRIX)
 
428
      arguments = fscanf(fp, "%lld\n", return_code);
 
429
#else
 
430
      arguments = fscanf(fp, "%d\n", return_code);
 
431
#endif
 
432
 
 
433
      if (arguments != 1) {
 
434
         shepherd_trace("could not read osjobid file");
 
435
         *return_code = 0;
 
436
         ret = false;
 
437
      }
 
438
      FCLOSE(fp);
 
439
   } else {
 
440
      if (is_error == true) {
 
441
         shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
442
      } else {
 
443
         shepherd_trace(MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
444
      }
 
445
      ret = false;
 
446
   }
 
447
   return ret;
 
448
FCLOSE_ERROR:
 
449
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
450
   return false;
 
451
}
 
452
#endif
 
453
 
 
454
void 
 
455
create_checkpointed_file(int ckpt_is_in_arena)
 
456
{
 
457
   const char *const filename = "checkpointed";
 
458
   FILE *fp = NULL;
 
459
 
 
460
   fp = fopen(filename, "w");
 
461
   if (fp != NULL) {
 
462
      if (ckpt_is_in_arena) {
 
463
         FPRINTF((fp, "1\n"));
 
464
      }
 
465
      FCLOSE(fp);
 
466
   } else {
 
467
      shepherd_error(1, MSG_FILE_NOOPEN_SS, filename, strerror(errno));
 
468
   }
 
469
   return;
 
470
FPRINTF_ERROR:
 
471
FCLOSE_ERROR:
 
472
   shepherd_error(1, MSG_FILE_NOCLOSE_SS, filename, strerror(errno));
 
473
   return;
 
474
}
 
475
 
 
476
int 
 
477
checkpointed_file_exists(void)
 
478
{
 
479
   SGE_STRUCT_STAT buf;
 
480
   return !SGE_STAT("checkpointed", &buf);
 
481
}
 
482
 
 
483
 
 
484