File: globus_scheduler_event_generator.c

package info (click to toggle)
globus-scheduler-event-generator 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,796 kB
  • sloc: sh: 11,855; ansic: 2,279; makefile: 219; perl: 91
file content (602 lines) | stat: -rw-r--r-- 15,735 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "globus_common.h"
#include "globus_scheduler_event_generator.h"
#include "globus_scheduler_event_generator_app.h"
#include "version.h"

static
int
globus_l_seg_activate(void);

static
int
globus_l_seg_deactivate(void);

globus_module_descriptor_t globus_i_scheduler_event_generator_module =
{
    "globus_scheduler_event_generator",
    globus_l_seg_activate,
    globus_l_seg_deactivate,
    NULL,
    NULL,
    &local_version,
    NULL
};

static char *                           globus_l_seg_extension;
static time_t                           globus_l_seg_timestamp;
static globus_mutex_t                   globus_l_seg_mutex;
static globus_scheduler_event_generator_fault_handler_t
                                        globus_l_seg_fault_handler;
static void *                           globus_l_seg_fault_arg;
static globus_scheduler_event_generator_event_handler_t
                                        globus_l_seg_event_handler;
static void *                           globus_l_seg_event_arg;
static
int
globus_l_seg_activate(void)
{
    int                                 rc;

    rc = globus_module_activate(GLOBUS_COMMON_MODULE);

    if (rc != GLOBUS_SUCCESS)
    {
        goto error;
    }

    globus_l_seg_extension = NULL;
    globus_l_seg_timestamp = 0;
    globus_l_seg_fault_handler = NULL;
    globus_l_seg_fault_arg = NULL;
    globus_l_seg_event_handler = NULL;
    globus_l_seg_event_arg = NULL;

    globus_mutex_init(&globus_l_seg_mutex, NULL);
error:
    return rc;
}
/* globus_l_seg_activate() */

static
int
globus_l_seg_deactivate(void)
{
    if (globus_l_seg_extension)
    {
        globus_extension_deactivate(globus_l_seg_extension);
        globus_l_seg_extension = NULL;
    }

    globus_mutex_destroy(&globus_l_seg_mutex);
    globus_module_deactivate(GLOBUS_COMMON_MODULE);

    return 0;
}
/* globus_l_seg_deactivate() */

/**
 * Send an arbitrary SEG notification.
 * @ingroup seg_api
 *
 * @param format
 *     Printf-style format of the SEG notification message
 * @param ...
 *     Varargs which will be interpreted as per format.
 *
 * @retval GLOBUS_SUCCESS
 *     Scheduler message sent or queued.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null format.
 * @retval GLOBUS_SEG_ERROR_INVALID_FORMAT
 *     Unable to determine length of formatted string.
 */
globus_result_t
globus_scheduler_event(
    const char * format,
    ...)
{
    globus_result_t                     result = GLOBUS_SUCCESS;
    char *                              buf;
    va_list                             ap;
    int                                 length;
    globus_scheduler_event_t            event;

    if (format == NULL)
    {
        result = GLOBUS_SEG_ERROR_NULL;

        goto error;
    }

    va_start(ap, format);
    length = globus_libc_vprintf_length(format, ap);
    va_end(ap);

    if (length <= 0)
    {
        result = GLOBUS_SEG_ERROR_INVALID_FORMAT(format);

        goto error;
    }

    buf = globus_libc_malloc(length+1);
    if (buf == NULL)
    {
        result = GLOBUS_SEG_ERROR_OUT_OF_MEMORY;

        goto error;
    }

    va_start(ap, format);
    vsprintf(buf, format, ap);
    va_end(ap);

    event.event_type = GLOBUS_SCHEDULER_EVENT_RAW;
    event.job_id = NULL;
    event.timestamp = 0;
    event.exit_code = 0;
    event.failure_code = 0;
    event.raw_event = (char *) buf;

    if (globus_l_seg_event_handler != NULL)
    {
        (*globus_l_seg_event_handler)(
                globus_l_seg_event_arg,
                &event);
    }

    free(buf);

error:
    return result;
}
/* globus_scheduler_event() */

/**
 * Send a job pending event to the JobSchedulerMonitor implementation.
 * @ingroup seg_api
 *
 * @param timestamp
 *        Timestamp to use for the event. If set to 0, the time which
 *        this function was called is used.
 * @param jobid
 *        String indicating the scheduler-specific name of the job.
 *
 * @retval GLOBUS_SUCCESS
 *     Scheduler message sent or queued.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null jobid.
 * @retval GLOBUS_SEG_ERROR_INVALID_FORMAT
 *     Unable to determine length of formatted string.
 */
globus_result_t
globus_scheduler_event_pending(
    time_t                              timestamp,
    const char *                        jobid)
{
    globus_scheduler_event_t            event;

    if (jobid == NULL)
    {
        return GLOBUS_SEG_ERROR_NULL;
    }

    event.event_type = GLOBUS_SCHEDULER_EVENT_PENDING;
    event.job_id = (char *) jobid;
    event.timestamp = timestamp;
    event.exit_code = 0;
    event.failure_code = 0;
    event.raw_event = NULL;
    
    if (globus_l_seg_event_handler != NULL)
    {
        (*globus_l_seg_event_handler)(
                globus_l_seg_event_arg,
                &event);
    }

    return GLOBUS_SUCCESS;
}
/* globus_scheduler_event_pending() */

/**
 * Send a job active event to the JobSchedulerMonitor implementation.
 * @ingroup seg_api
 *
 * @param timestamp
 *        Timestamp to use for the event. If set to 0, the time which
 *        this function was called is used.
 * @param jobid
 *        String indicating the scheduler-specific name of the job.
 *
 * @retval GLOBUS_SUCCESS
 *     Scheduler message sent or queued.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null jobid.
 * @retval GLOBUS_SEG_ERROR_INVALID_FORMAT
 *     Unable to determine length of formatted string.
 */
globus_result_t
globus_scheduler_event_active(
    time_t                              timestamp,
    const char *                        jobid)
{
    globus_scheduler_event_t            event;

    if (jobid == NULL)
    {
        return GLOBUS_SEG_ERROR_NULL;
    }

    event.event_type = GLOBUS_SCHEDULER_EVENT_ACTIVE;
    event.job_id = (char *) jobid;
    event.timestamp = timestamp;
    event.exit_code = 0;
    event.failure_code = 0;
    event.raw_event = NULL;

    if (globus_l_seg_event_handler != NULL)
    {
        (*globus_l_seg_event_handler)(
                globus_l_seg_event_arg,
                &event);
    }
    return GLOBUS_SUCCESS;
}

/**
 * Send a job failed event to the JobSchedulerMonitor implementation.
 * @ingroup seg_api
 *
 * @param timestamp
 *        Timestamp to use for the event. If set to 0, the time which
 *        this function was called is used.
 * @param jobid
 *        String indicating the scheduler-specific name of the job.
 * @param failure_code
 *        Failure code of the process if known.
 *
 * @retval GLOBUS_SUCCESS
 *     Scheduler message sent or queued.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null jobid.
 * @retval GLOBUS_SEG_ERROR_INVALID_FORMAT
 *     Unable to determine length of formatted string.
 */
globus_result_t
globus_scheduler_event_failed(
    time_t                              timestamp,
    const char *                        jobid,
    int                                 failure_code)
{
    globus_scheduler_event_t            event;

    if (jobid == NULL)
    {
        return GLOBUS_SEG_ERROR_NULL;
    }

    event.event_type = GLOBUS_SCHEDULER_EVENT_FAILED;
    event.job_id = (char *) jobid;
    event.timestamp = timestamp;
    event.exit_code = 0;
    event.failure_code = failure_code;
    event.raw_event = NULL;
    
    if (globus_l_seg_event_handler != NULL)
    {
        (*globus_l_seg_event_handler)(
                globus_l_seg_event_arg,
                &event);
    }
    return GLOBUS_SUCCESS;
}

/**
 * Send a job done event to the JobSchedulerMonitor implementation.
 * @ingroup seg_api
 *
 * @param timestamp
 *        Timestamp to use for the event. If set to 0, the time which
 *        this function was called is used.
 * @param jobid
 *        String indicating the scheduler-specific name of the job.
 * @param exit_code
 *        Exit code of the process if known.
 *
 * @retval GLOBUS_SUCCESS
 *     Scheduler message sent or queued.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null jobid.
 * @retval GLOBUS_SEG_ERROR_INVALID_FORMAT
 *     Unable to determine length of formatted string.
 */
globus_result_t
globus_scheduler_event_done(
    time_t                              timestamp,
    const char *                        jobid,
    int                                 exit_code)
{
    globus_scheduler_event_t            event;

    if (jobid == NULL)
    {
        return GLOBUS_SEG_ERROR_NULL;
    }

    event.event_type = GLOBUS_SCHEDULER_EVENT_DONE;
    event.job_id = (char *) jobid;
    event.timestamp = timestamp;
    event.exit_code = exit_code;
    event.failure_code = 0;
    event.raw_event = NULL;
    
    if (globus_l_seg_event_handler != NULL)
    {
        (*globus_l_seg_event_handler)(
                globus_l_seg_event_arg,
                &event);
    }
    return GLOBUS_SUCCESS;
}

/**
 * Get the timestamp for the earliest event an SEG module should send.
 * @ingroup seg_api
 *
 * @param timestamp
 *     Pointer to a time_t which will be set to the timestamp passed to the
 *     SEG executable. The module should not send any events which occur prior
 *     to this timestamp.
 * @retval GLOBUS_SEG_ERROR_NULL
 *     Null timestamp.
 * @retval GLOBUS_SUCCESS
 *     Timestamp value updated. If the timestamp was not set on the SEG
 *     command-line, then the value pointed to by @a timestamp will be set to
 *     0.
 */
globus_result_t
globus_scheduler_event_generator_get_timestamp(
    time_t *                            timestamp)
{
    if (timestamp == NULL)
    {
        return GLOBUS_SEG_ERROR_NULL;
    }
    globus_mutex_lock(&globus_l_seg_mutex);
    *timestamp = globus_l_seg_timestamp;
    globus_mutex_unlock(&globus_l_seg_mutex);

    return GLOBUS_SUCCESS;
}
/* globus_scheduler_event_generator_get_timestamp() */


globus_result_t
globus_scheduler_event_generator_set_timestamp(
    time_t                              timestamp)
{
    globus_result_t                     result = GLOBUS_SUCCESS;

    globus_mutex_lock(&globus_l_seg_mutex);
    if (globus_l_seg_timestamp > timestamp)
    {
        result = GLOBUS_SEG_ERROR_ALREADY_SET;
        goto error;
    }
    globus_l_seg_timestamp = timestamp;

    globus_mutex_unlock(&globus_l_seg_mutex);

    return GLOBUS_SUCCESS;

error:
    globus_mutex_unlock(&globus_l_seg_mutex);
    return result;
}
/* globus_scheduler_event_generator_set_timestamp() */

/**
 * Load a scheduler event generator module.
 *
 * If the module name begins with "/" then it is interpreted as a path name;
 * otherwise, the name is constructed using the convention:
 * @code $GLOBUS_LOCATION/lib/libglobus_seg_$MODULE_NAME_$FLAVOR.so@endcode,
 * where $GLOBUS_LOCATION is determined from the environment, $MODULE_NAME is
 * determined from the @a module_name parameter to the function, and $FLAVOR
 * is determined at compile time.
 *
 * @param module_name
 *     Name of the shared object to load.
 * @retval GLOBUS_SEG_ERROR_ALREADY_SET
 *     Already loaded a module.
 * @retval GLOBUS_SEG_ERROR_LOADING_MODULE
 *     Unable to load shared module.
 * @retval GLOBUS_SEG_ERROR_INVALID_MODULE
 *     Unable to find the loaded module's globus_module_descriptor_t
 */
globus_result_t
globus_scheduler_event_generator_load_module(
    const char *                        module_name)
{
    globus_result_t                     result;
    int                                 rc;

    globus_mutex_lock(&globus_l_seg_mutex);
    if (globus_l_seg_extension != NULL)
    {
        result = GLOBUS_SEG_ERROR_ALREADY_SET;

        goto unlock_error;
    }

    if (module_name[0] != '/')
    {
        globus_l_seg_extension = globus_common_create_string(
                "globus_seg_%s", module_name);
    }
    else
    {
        globus_l_seg_extension = globus_libc_strdup(module_name);
    }

    if (globus_l_seg_extension == NULL)
    {
        result = GLOBUS_SEG_ERROR_OUT_OF_MEMORY;

        goto unlock_error;
    }
    rc = globus_extension_activate(globus_l_seg_extension);

    if (rc != GLOBUS_SUCCESS)
    {
        result = GLOBUS_SEG_ERROR_INVALID_MODULE(
                module_name,
                "activation failed");

        goto free_extension_error;
    }
    globus_mutex_unlock(&globus_l_seg_mutex);

    return GLOBUS_SUCCESS;

free_extension_error:
    if (globus_l_seg_extension != NULL)
    {
        globus_libc_free(globus_l_seg_extension);
    }
unlock_error:
    globus_mutex_unlock(&globus_l_seg_mutex);
    return result;
}
/* globus_scheduler_event_generator_load_module() */

globus_result_t
globus_scheduler_event_generator_set_fault_handler(
    globus_scheduler_event_generator_fault_handler_t
                                        fault_handler,
    void *                              user_arg)
{
    globus_result_t                     result = GLOBUS_SUCCESS;

    globus_mutex_lock(&globus_l_seg_mutex);

    if (globus_l_seg_fault_handler != NULL)
    {
        result = GLOBUS_SEG_ERROR_ALREADY_SET;

        goto unlock_error;
    }

    globus_l_seg_fault_handler = fault_handler;
    globus_l_seg_fault_arg = user_arg;

    globus_mutex_unlock(&globus_l_seg_mutex);

    return result;

unlock_error:
    globus_mutex_unlock(&globus_l_seg_mutex);

    return result;
}
/* globus_scheduler_event_generator_set_fault_handler() */

globus_result_t
globus_scheduler_event_generator_set_event_handler(
    globus_scheduler_event_generator_event_handler_t
                                        event_handler,
    void *                              user_arg)
{
    globus_result_t                     result = GLOBUS_SUCCESS;

    globus_mutex_lock(&globus_l_seg_mutex);

    if (globus_l_seg_event_handler != NULL)
    {
        result = GLOBUS_SEG_ERROR_ALREADY_SET;

        goto unlock_error;
    }

    globus_l_seg_event_handler = event_handler;
    globus_l_seg_event_arg = user_arg;

    globus_mutex_unlock(&globus_l_seg_mutex);

    return result;

unlock_error:
    globus_mutex_unlock(&globus_l_seg_mutex);

    return result;
}
/* globus_scheduler_event_generator_set_event_handler() */

void
globus_scheduler_event_generator_fault(
    globus_result_t                     result)
{
    if (globus_l_seg_fault_handler != NULL)
    {
        (*globus_l_seg_fault_handler)(globus_l_seg_fault_arg, result);
    }
}
/* globus_scheduler_event_generator_fault() */

globus_result_t
globus_scheduler_event_copy(
    globus_scheduler_event_t **         copy,
    const globus_scheduler_event_t *    event)
{
    (*copy) = malloc(sizeof(globus_scheduler_event_t));

    (*copy)->event_type = event->event_type;
    (*copy)->job_id = event->job_id
        ? globus_libc_strdup(event->job_id)
        : NULL;
    (*copy)->timestamp = event->timestamp;
    (*copy)->exit_code = event->exit_code;
    (*copy)->failure_code = event->failure_code;
    (*copy)->raw_event = event->raw_event
        ? globus_libc_strdup(event->raw_event)
        : NULL;

    return GLOBUS_SUCCESS;
}
/* globus_scheduler_event_copy() */

void
globus_scheduler_event_destroy(
    globus_scheduler_event_t *          event)
{

    if (event->job_id)
    {
        free(event->job_id);
    }

    if (event->raw_event)
    {
        free(event->raw_event);
    }
    free(event);
}
/* globus_scheduler_event_copy() */