File: shmem_posix_common_utils.c

package info (click to toggle)
openmpi 1.6.5-9.1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 91,628 kB
  • ctags: 44,305
  • sloc: ansic: 408,966; cpp: 44,454; sh: 27,828; makefile: 10,486; asm: 3,882; python: 1,239; lex: 805; perl: 549; csh: 253; fortran: 232; f90: 126; tcl: 12
file content (109 lines) | stat: -rw-r--r-- 3,879 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
/*
 * Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 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) 2007-2010 Cisco Systems, Inc.  All rights reserved.
 * Copyright (c) 2008      Sun Microsystems, Inc.  All rights reserved.
 * Copyright (c) 2010-2011 Los Alamos National Security, LLC.
 *                         All rights reserved.
 * Copyright (c) 2011      NVIDIA Corporation.  All rights reserved.
 *
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"

#include <errno.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif  /* HAVE_FCNTL_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif /* HAVE_SYS_MMAN_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif /* HAVE_NETDB_H */

#include "opal/util/output.h"
#include "opal/util/show_help.h"
#include "opal/mca/shmem/base/base.h"
#include "opal/mca/shmem/shmem.h"

#include "shmem_posix.h"
#include "shmem_posix_common_utils.h"

/* ////////////////////////////////////////////////////////////////////////// */
int
shmem_posix_shm_open(char *posix_file_name_buff, size_t size)
{
    int attempt = 0, fd = -1;

    /* workaround for simultaneous posix shm_opens on the same node (e.g.
     * multiple Open MPI jobs sharing a node).  name collision during component
     * runtime will happen, so protect against it by trying a few times.
     */
    do {
        /* format: /open_mpi.nnnn
         * see comment in shmem_posix.h that explains why we chose to do things
         * this way.
         */
        snprintf(posix_file_name_buff, size, "%s%04d",
                 OPAL_SHMEM_POSIX_FILE_NAME_PREFIX, attempt++);
        /* the check for the existence of the object and its creation if it
         * does not exist are performed atomically.
         */
        if (-1 == (fd = shm_open(posix_file_name_buff,
                                 O_CREAT | O_EXCL | O_RDWR, 0600))) {
            int err = errno;
            /* the object already exists, so try again with a new name */
            if (EEXIST == err) {
                continue;
            }
            /* a "real" error occurred. fd is already set to -1, so get out
             * of here. we can't be selected :-(.
             */
            else {
                char hn[MAXHOSTNAMELEN];
                gethostname(hn, MAXHOSTNAMELEN - 1);
                hn[MAXHOSTNAMELEN - 1] = '\0';
                opal_output_verbose(10, opal_shmem_base_output,
                     "shmem_posix_shm_open: disqualifying posix because "
                     "shm_open(2) failed with error: %s (errno %d)\n",
                     strerror(err), err);
                break;
            }
        }
        /* we found an available file name */
        else {
            break;
        }
    } while (attempt < OPAL_SHMEM_POSIX_MAX_ATTEMPTS);

    /* if we didn't find a name, let the user know that we tried and failed */
    if (attempt >= OPAL_SHMEM_POSIX_MAX_ATTEMPTS) {
        opal_output(0, "shmem: posix: file name search - max attempts exceeded."
                    "cannot continue with posix.\n");
    }
    return fd;
}