File: session_dir.c

package info (click to toggle)
openmpi 4.1.0-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 126,560 kB
  • sloc: ansic: 685,465; makefile: 42,952; f90: 19,220; sh: 7,002; java: 6,360; perl: 3,524; cpp: 2,227; python: 1,350; lex: 989; fortran: 61; tcl: 12
file content (609 lines) | stat: -rw-r--r-- 20,088 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2006 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2014      Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2015      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * Copyright (c) 2015-2018 Intel, Inc.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 *
 */

#include "orte_config.h"
#include "orte/constants.h"

#include <stdio.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif  /* HAVE_SYS_PARAM_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif  /* HAVE_SYS_TYPES_H */
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif  /* HAVE_UNISTD_H */
#include <errno.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif  /* HAVE_DIRENT_H */
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif  /* HAVE_PWD_H */

#include "opal/util/argv.h"
#include "opal/util/output.h"
#include "opal/util/os_path.h"
#include "opal/util/os_dirpath.h"
#include "opal/util/basename.h"
#include "opal/util/opal_environ.h"

#include "orte/util/proc_info.h"
#include "orte/util/name_fns.h"
#include "orte/util/show_help.h"

#include "orte/mca/errmgr/errmgr.h"
#include "orte/mca/ras/base/base.h"
#include "orte/runtime/runtime.h"
#include "orte/runtime/orte_globals.h"

#include "orte/util/session_dir.h"

/*******************************
 * Local function Declarations
 *******************************/
static int orte_create_dir(char *directory);

static bool orte_dir_check_file(const char *root, const char *path);

#define OMPI_PRINTF_FIX_STRING(a) ((NULL == a) ? "(null)" : a)

/****************************
 * Funcationality
 ****************************/
/*
 * Check and create the directory requested
 */
static int orte_create_dir(char *directory)
{
    mode_t my_mode = S_IRWXU;  /* I'm looking for full rights */
    int ret;

    /* Sanity check before creating the directory with the proper mode,
     * Make sure it doesn't exist already */
    if( ORTE_ERR_NOT_FOUND !=
        (ret = opal_os_dirpath_access(directory, my_mode)) ) {
        /* Failure because opal_os_dirpath_access() indicated that either:
         * - The directory exists and we can access it (no need to create it again),
         *    return OPAL_SUCCESS, or
         * - don't have access rights, return OPAL_ERROR
         */
        if (ORTE_SUCCESS != ret) {
            ORTE_ERROR_LOG(ret);
        }
        return(ret);
    }

    /* Get here if the directory doesn't exist, so create it */
    if (ORTE_SUCCESS != (ret = opal_os_dirpath_create(directory, my_mode))) {
        ORTE_ERROR_LOG(ret);
    }
    return ret;
}


static int _setup_tmpdir_base(void)
{
    int rc = ORTE_SUCCESS;

    /* make sure that we have tmpdir_base set
     * if we need it
     */
    if (NULL == orte_process_info.tmpdir_base) {
        orte_process_info.tmpdir_base =
                strdup(opal_tmp_directory());
        if (NULL == orte_process_info.tmpdir_base) {
            rc = ORTE_ERR_OUT_OF_RESOURCE;
            goto exit;
        }
    }
exit:
    if( ORTE_SUCCESS != rc ){
        ORTE_ERROR_LOG(rc);
    }
    return rc;
}

int orte_setup_top_session_dir(void)
{
    int rc = ORTE_SUCCESS;
    /* get the effective uid */
    uid_t uid = geteuid();

    /* construct the top_session_dir if we need */
    if (NULL == orte_process_info.top_session_dir) {
        if (ORTE_SUCCESS != (rc = _setup_tmpdir_base())) {
            return rc;
        }
        if( NULL == orte_process_info.nodename ||
                NULL == orte_process_info.tmpdir_base ){
            /* we can't setup top session dir */
            rc = ORTE_ERR_BAD_PARAM;
            goto exit;
        }

        if (0 > asprintf(&orte_process_info.top_session_dir,
                         "%s/ompi.%s.%lu", orte_process_info.tmpdir_base,
                         orte_process_info.nodename, (unsigned long)uid)) {
            orte_process_info.top_session_dir = NULL;
            rc = ORTE_ERR_OUT_OF_RESOURCE;
            goto exit;
        }
    }
exit:
    if( ORTE_SUCCESS != rc ){
        ORTE_ERROR_LOG(rc);
    }
    return rc;
}

static int _setup_jobfam_session_dir(orte_process_name_t *proc)
{
    int rc = ORTE_SUCCESS;

    /* construct the top_session_dir if we need */
    if (NULL == orte_process_info.jobfam_session_dir) {
        if (ORTE_SUCCESS != (rc = orte_setup_top_session_dir())) {
            return rc;
        }

        if (ORTE_PROC_IS_MASTER) {
            if (0 > asprintf(&orte_process_info.jobfam_session_dir,
                             "%s/dvm", orte_process_info.top_session_dir)) {
                rc = ORTE_ERR_OUT_OF_RESOURCE;
                goto exit;
            }
        } else if (ORTE_PROC_IS_HNP) {
            if (0 > asprintf(&orte_process_info.jobfam_session_dir,
                             "%s/pid.%lu", orte_process_info.top_session_dir,
                             (unsigned long)orte_process_info.pid)) {
                rc = ORTE_ERR_OUT_OF_RESOURCE;
                goto exit;
            }
        } else {
            /* we were not given one, so define it */
            if (NULL == proc || (ORTE_JOBID_INVALID == proc->jobid)) {
                if (0 > asprintf(&orte_process_info.jobfam_session_dir,
                                 "%s/jobfam", orte_process_info.top_session_dir) ) {
                    rc = ORTE_ERR_OUT_OF_RESOURCE;
                    goto exit;
                }
            } else {
                if (0 > asprintf(&orte_process_info.jobfam_session_dir,
                                 "%s/jf.%d", orte_process_info.top_session_dir,
                                 ORTE_JOB_FAMILY(proc->jobid))) {
                    orte_process_info.jobfam_session_dir = NULL;
                    rc = ORTE_ERR_OUT_OF_RESOURCE;
                    goto exit;
                }
            }
        }
    }
exit:
    if( ORTE_SUCCESS != rc ){
        ORTE_ERROR_LOG(rc);
    }
    return rc;
}

static int
_setup_job_session_dir(orte_process_name_t *proc)
{
    int rc = ORTE_SUCCESS;

    /* construct the top_session_dir if we need */
    if( NULL == orte_process_info.job_session_dir ){
        if( ORTE_SUCCESS != (rc = _setup_jobfam_session_dir(proc)) ){
            return rc;
        }
        if (ORTE_JOBID_INVALID != proc->jobid) {
            if (0 > asprintf(&orte_process_info.job_session_dir,
                             "%s/%d", orte_process_info.jobfam_session_dir,
                             ORTE_LOCAL_JOBID(proc->jobid))) {
                orte_process_info.job_session_dir = NULL;
                rc = ORTE_ERR_OUT_OF_RESOURCE;
                goto exit;
            }
        } else {
            orte_process_info.job_session_dir = NULL;
        }
    }

exit:
    if( ORTE_SUCCESS != rc ){
        ORTE_ERROR_LOG(rc);
    }
    return rc;
}

static int
_setup_proc_session_dir(orte_process_name_t *proc)
{
    int rc = ORTE_SUCCESS;

    /* construct the top_session_dir if we need */
    if( NULL == orte_process_info.proc_session_dir ){
        if( ORTE_SUCCESS != (rc = _setup_job_session_dir(proc)) ){
            return rc;
        }
        if (ORTE_VPID_INVALID != proc->vpid) {
            if (0 > asprintf(&orte_process_info.proc_session_dir,
                             "%s/%d", orte_process_info.job_session_dir,
                             proc->vpid)) {
                orte_process_info.proc_session_dir = NULL;
                rc = ORTE_ERR_OUT_OF_RESOURCE;
                goto exit;
            }
        } else {
            orte_process_info.proc_session_dir = NULL;
        }
    }

exit:
    if( ORTE_SUCCESS != rc ){
        ORTE_ERROR_LOG(rc);
    }
    return rc;
}

int orte_session_setup_base(orte_process_name_t *proc)
{
    int rc;

    /* Ensure that system info is set */
    orte_proc_info();

    /* setup job and proc session directories */
    if( ORTE_SUCCESS != (rc = _setup_job_session_dir(proc)) ){
        return rc;
    }

    if( ORTE_SUCCESS != (rc = _setup_proc_session_dir(proc)) ){
        return rc;
    }

    /* BEFORE doing anything else, check to see if this prefix is
     * allowed by the system
     */
    if (NULL != orte_prohibited_session_dirs ||
            NULL != orte_process_info.tmpdir_base ) {
        char **list;
        int i, len;
        /* break the string into tokens - it should be
         * separated by ','
         */
        list = opal_argv_split(orte_prohibited_session_dirs, ',');
        len = opal_argv_count(list);
        /* cycle through the list */
        for (i=0; i < len; i++) {
            /* check if prefix matches */
            if (0 == strncmp(orte_process_info.tmpdir_base, list[i], strlen(list[i]))) {
                /* this is a prohibited location */
                orte_show_help("help-orte-runtime.txt",
                               "orte:session:dir:prohibited",
                               true, orte_process_info.tmpdir_base,
                               orte_prohibited_session_dirs);
                opal_argv_free(list);
                return ORTE_ERR_FATAL;
            }
        }
        opal_argv_free(list);  /* done with this */
    }
    return ORTE_SUCCESS;
}

/*
 * Construct the session directory and create it if necessary
 */
int orte_session_dir(bool create, orte_process_name_t *proc)
{
    int rc = ORTE_SUCCESS;

    /*
     * Get the session directory full name
     */
    if (ORTE_SUCCESS != (rc = orte_session_setup_base(proc))) {
        if (ORTE_ERR_FATAL == rc) {
            /* this indicates we should abort quietly */
            rc = ORTE_ERR_SILENT;
        }
        goto cleanup;
    }

    /*
     * Now that we have the full path, go ahead and create it if necessary
     */
    if( create ) {
        if( ORTE_SUCCESS != (rc = orte_create_dir(orte_process_info.proc_session_dir)) ) {
            ORTE_ERROR_LOG(rc);
            goto cleanup;
        }
    }

    if (orte_debug_flag) {
        opal_output(0, "procdir: %s",
                    OMPI_PRINTF_FIX_STRING(orte_process_info.proc_session_dir));
        opal_output(0, "jobdir: %s",
                    OMPI_PRINTF_FIX_STRING(orte_process_info.job_session_dir));
        opal_output(0, "top: %s",
                    OMPI_PRINTF_FIX_STRING(orte_process_info.jobfam_session_dir));
        opal_output(0, "top: %s",
                    OMPI_PRINTF_FIX_STRING(orte_process_info.top_session_dir));
        opal_output(0, "tmp: %s",
                    OMPI_PRINTF_FIX_STRING(orte_process_info.tmpdir_base));
    }

cleanup:
    return rc;
}

/*
 * A job has aborted - so force cleanup of the session directory
 */
int
orte_session_dir_cleanup(orte_jobid_t jobid)
{
    /* special case - if a daemon is colocated with mpirun,
     * then we let mpirun do the rest to avoid a race
     * condition. this scenario always results in the rank=1
     * daemon colocated with mpirun */
    if (orte_ras_base.launch_orted_on_hn &&
        ORTE_PROC_IS_DAEMON &&
        1 == ORTE_PROC_MY_NAME->vpid) {
        return ORTE_SUCCESS;
    }

    if (!orte_create_session_dirs || orte_process_info.rm_session_dirs ) {
        /* we haven't created them or RM will clean them up for us*/
        return ORTE_SUCCESS;
    }

    if (NULL == orte_process_info.jobfam_session_dir ||
        NULL == orte_process_info.proc_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        return ORTE_ERR_NOT_INITIALIZED;
    }


    /* recursively blow the whole session away for our job family,
     * saving only output files
     */
    opal_os_dirpath_destroy(orte_process_info.jobfam_session_dir,
                            true, orte_dir_check_file);

    if (opal_os_dirpath_is_empty(orte_process_info.jobfam_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_cleanup: found jobfam session dir empty - deleting");
        }
        rmdir(orte_process_info.jobfam_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.job_session_dir, 0)) {
                opal_output(0, "sess_dir_cleanup: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_cleanup: job session dir not empty - leaving");
            }
        }
    }

    if (NULL != orte_process_info.top_session_dir) {
        if (opal_os_dirpath_is_empty(orte_process_info.top_session_dir)) {
            if (orte_debug_flag) {
                opal_output(0, "sess_dir_cleanup: found top session dir empty - deleting");
            }
            rmdir(orte_process_info.top_session_dir);
        } else {
            if (orte_debug_flag) {
                if (OPAL_ERR_NOT_FOUND ==
                        opal_os_dirpath_access(orte_process_info.top_session_dir, 0)) {
                    opal_output(0, "sess_dir_cleanup: top session dir does not exist");
                } else {
                    opal_output(0, "sess_dir_cleanup: top session dir not empty - leaving");
                }
            }
        }
    }

    /* now attempt to eliminate the top level directory itself - this
     * will fail if anything is present, but ensures we cleanup if
     * we are the last one out
     */
    if( NULL != orte_process_info.top_session_dir ){
        opal_os_dirpath_destroy(orte_process_info.top_session_dir,
                                false, orte_dir_check_file);
    }


    return ORTE_SUCCESS;
}


int
orte_session_dir_finalize(orte_process_name_t *proc)
{
    if (!orte_create_session_dirs || orte_process_info.rm_session_dirs ) {
        /* we haven't created them or RM will clean them up for us*/
        return ORTE_SUCCESS;
    }

    if (NULL == orte_process_info.job_session_dir ||
        NULL == orte_process_info.proc_session_dir) {
        /* this should never happen - it means we are calling
         * cleanup *before* properly setting up the session
         * dir system. This leaves open the possibility of
         * accidentally removing directories we shouldn't
         * touch
         */
        return ORTE_ERR_NOT_INITIALIZED;
    }

    opal_os_dirpath_destroy(orte_process_info.proc_session_dir,
                            false, orte_dir_check_file);

    if (opal_os_dirpath_is_empty(orte_process_info.proc_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found proc session dir empty - deleting");
        }
        rmdir(orte_process_info.proc_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.proc_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: proc session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: proc session dir not empty - leaving");
            }
        }
    }

    /* special case - if a daemon is colocated with mpirun,
     * then we let mpirun do the rest to avoid a race
     * condition. this scenario always results in the rank=1
     * daemon colocated with mpirun */
    if (orte_ras_base.launch_orted_on_hn &&
        ORTE_PROC_IS_DAEMON &&
        1 == ORTE_PROC_MY_NAME->vpid) {
        return ORTE_SUCCESS;
    }

    opal_os_dirpath_destroy(orte_process_info.job_session_dir,
                            false, orte_dir_check_file);

    /* only remove the jobfam session dir if we are the
     * local daemon and we are finalizing our own session dir */
    if ((ORTE_PROC_IS_HNP || ORTE_PROC_IS_DAEMON) &&
        (ORTE_PROC_MY_NAME == proc)) {
        opal_os_dirpath_destroy(orte_process_info.jobfam_session_dir,
                                false, orte_dir_check_file);
    }

    if( NULL != orte_process_info.top_session_dir ){
        opal_os_dirpath_destroy(orte_process_info.top_session_dir,
                                false, orte_dir_check_file);
    }

    if (opal_os_dirpath_is_empty(orte_process_info.job_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found job session dir empty - deleting");
        }
        rmdir(orte_process_info.job_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.job_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: job session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: job session dir not empty - leaving");
            }
        }
    }

    if (opal_os_dirpath_is_empty(orte_process_info.jobfam_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found jobfam session dir empty - deleting");
        }
        rmdir(orte_process_info.jobfam_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.jobfam_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: jobfam session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: jobfam session dir not empty - leaving");
            }
        }
    }

    if (opal_os_dirpath_is_empty(orte_process_info.jobfam_session_dir)) {
        if (orte_debug_flag) {
            opal_output(0, "sess_dir_finalize: found jobfam session dir empty - deleting");
        }
        rmdir(orte_process_info.jobfam_session_dir);
    } else {
        if (orte_debug_flag) {
            if (OPAL_ERR_NOT_FOUND ==
                    opal_os_dirpath_access(orte_process_info.jobfam_session_dir, 0)) {
                opal_output(0, "sess_dir_finalize: jobfam session dir does not exist");
            } else {
                opal_output(0, "sess_dir_finalize: jobfam session dir not empty - leaving");
            }
        }
    }

    if (NULL != orte_process_info.top_session_dir) {
        if (opal_os_dirpath_is_empty(orte_process_info.top_session_dir)) {
            if (orte_debug_flag) {
                opal_output(0, "sess_dir_finalize: found top session dir empty - deleting");
            }
            rmdir(orte_process_info.top_session_dir);
        } else {
            if (orte_debug_flag) {
                if (OPAL_ERR_NOT_FOUND ==
                        opal_os_dirpath_access(orte_process_info.top_session_dir, 0)) {
                    opal_output(0, "sess_dir_finalize: top session dir does not exist");
                } else {
                    opal_output(0, "sess_dir_finalize: top session dir not empty - leaving");
                }
            }
        }
    }

    return ORTE_SUCCESS;
}

static bool
orte_dir_check_file(const char *root, const char *path)
{
    struct stat st;
    char *fullpath;

    /*
     * Keep:
     *  - non-zero files starting with "output-"
     */
    if (0 == strncmp(path, "output-", strlen("output-"))) {
        fullpath = opal_os_path(false, &fullpath, root, path, NULL);
        stat(fullpath, &st);
        free(fullpath);
        if (0 == st.st_size) {
            return true;
        }
        return false;
    }

    return true;
}