Skip to content

Commit 56b43d1

Browse files
authored
Merge pull request #9786 from edgargabriel/pr/sharedfp-sm-init-v4.0
v4.0.x: sharedfp_sm_file_component_query: add file open to ensure correct ope…
2 parents cf056cc + 6ce811e commit 56b43d1

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

ompi/mca/sharedfp/sm/sharedfp_sm.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
* University of Stuttgart. All rights reserved.
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
12-
* Copyright (c) 2008-2013 University of Houston. All rights reserved.
12+
* Copyright (c) 2008-2021 University of Houston. All rights reserved.
1313
* Copyright (c) 2018 Research Organization for Information Science
1414
* and Technology (RIST). All rights reserved.
15+
* Copyright (c) 2021 Cisco Systems, Inc. All rights reserved.
1516
* $COPYRIGHT$
1617
*
1718
* Additional copyrights may follow
@@ -31,6 +32,8 @@
3132
#include "ompi/mca/sharedfp/base/base.h"
3233
#include "ompi/mca/sharedfp/sm/sharedfp_sm.h"
3334

35+
#include "opal/util/basename.h"
36+
3437
/*
3538
* *******************************************************************
3639
* ************************ actions structure ************************
@@ -94,6 +97,30 @@ struct mca_sharedfp_base_module_1_0_0_t * mca_sharedfp_sm_component_file_query(o
9497
return NULL;
9598
}
9699
}
100+
101+
102+
/* Check that we can actually open the required file */
103+
char *filename_basename = opal_basename((char*)fh->f_filename);
104+
char *sm_filename;
105+
int comm_cid = -1;
106+
int pid = ompi_comm_rank (comm);
107+
108+
asprintf(&sm_filename, "%s/%s_cid-%d-%d.sm", ompi_process_info.job_session_dir,
109+
filename_basename, comm_cid, pid);
110+
free(filename_basename);
111+
112+
int sm_fd = open(sm_filename, O_RDWR | O_CREAT,
113+
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
114+
if ( sm_fd == -1){
115+
/*error opening file*/
116+
opal_output(0,"mca_sharedfp_sm_component_file_query: Error, unable to open file for mmap: %s\n",sm_filename);
117+
free(sm_filename);
118+
return NULL;
119+
}
120+
close (sm_fd);
121+
unlink(sm_filename);
122+
free (sm_filename);
123+
97124
/* This module can run */
98125
*priority = mca_sharedfp_sm_priority;
99126
return &sm;

ompi/mca/sharedfp/sm/sharedfp_sm_file_open.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* University of Stuttgart. All rights reserved.
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
12-
* Copyright (c) 2013-2018 University of Houston. All rights reserved.
12+
* Copyright (c) 2013-2021 University of Houston. All rights reserved.
1313
* Copyright (c) 2013 Intel, Inc. All rights reserved.
1414
* Copyright (c) 2015-2018 Research Organization for Information Science
1515
* and Technology (RIST). All rights reserved.
16-
* Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
16+
* Copyright (c) 2015-2021 Cisco Systems, Inc. All rights reserved.
1717
* Copyright (c) 2016-2017 IBM Corporation. All rights reserved.
1818
* $COPYRIGHT$
1919
*
@@ -41,6 +41,8 @@
4141
#include "ompi/mca/sharedfp/sharedfp.h"
4242
#include "ompi/mca/sharedfp/base/base.h"
4343

44+
#include "opal/util/basename.h"
45+
4446
#include <semaphore.h>
4547
#include <sys/mman.h>
4648
#include <libgen.h>
@@ -57,7 +59,6 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
5759
struct mca_sharedfp_sm_data * sm_data = NULL;
5860
char * filename_basename;
5961
char * sm_filename;
60-
int sm_filename_length;
6162
struct mca_sharedfp_sm_offset * sm_offset_ptr;
6263
struct mca_sharedfp_sm_offset sm_offset;
6364
int sm_fd;
@@ -101,16 +102,8 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
101102
** and then mapping it to memory
102103
** For sharedfp we also want to put the file backed shared memory into the tmp directory
103104
*/
104-
filename_basename = basename((char*)filename);
105+
filename_basename = opal_basename((char*)filename);
105106
/* format is "%s/%s_cid-%d-%d.sm", see below */
106-
sm_filename_length = strlen(ompi_process_info.job_session_dir) + 1 + strlen(filename_basename) + 5 + (3*sizeof(uint32_t)+1) + 4;
107-
sm_filename = (char*) malloc( sizeof(char) * sm_filename_length);
108-
if (NULL == sm_filename) {
109-
opal_output(0, "mca_sharedfp_sm_file_open: Error, unable to malloc sm_filename\n");
110-
free(sm_data);
111-
free(sh);
112-
return OMPI_ERR_OUT_OF_RESOURCE;
113-
}
114107

115108
comm_cid = ompi_comm_get_cid(comm);
116109
if ( 0 == fh->f_rank ) {
@@ -120,20 +113,21 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
120113
err = comm->c_coll->coll_bcast (&int_pid, 1, MPI_INT, 0, comm, comm->c_coll->coll_bcast_module );
121114
if ( OMPI_SUCCESS != err ) {
122115
opal_output(0,"mca_sharedfp_sm_file_open: Error in bcast operation \n");
123-
free(sm_filename);
116+
free(filename_basename);
124117
free(sm_data);
125118
free(sh);
126119
return err;
127120
}
128121

129-
snprintf(sm_filename, sm_filename_length, "%s/%s_cid-%d-%d.sm", ompi_process_info.job_session_dir,
122+
asprintf(&sm_filename, "%s/%s_cid-%d-%d.sm", ompi_process_info.job_session_dir,
130123
filename_basename, comm_cid, int_pid);
131124
/* open shared memory file, initialize to 0, map into memory */
132125
sm_fd = open(sm_filename, O_RDWR | O_CREAT,
133126
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
134127
if ( sm_fd == -1){
135128
/*error opening file*/
136129
opal_output(0,"mca_sharedfp_sm_file_open: Error, unable to open file for mmap: %s\n",sm_filename);
130+
free(filename_basename);
137131
free(sm_filename);
138132
free(sm_data);
139133
free(sh);
@@ -150,6 +144,7 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
150144
err = comm->c_coll->coll_barrier (comm, comm->c_coll->coll_barrier_module );
151145
if ( OMPI_SUCCESS != err ) {
152146
opal_output(0,"mca_sharedfp_sm_file_open: Error in barrier operation \n");
147+
free(filename_basename);
153148
free(sm_filename);
154149
free(sm_data);
155150
free(sh);
@@ -167,6 +162,7 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
167162
err = OMPI_ERROR;
168163
opal_output(0, "mca_sharedfp_sm_file_open: Error, unable to mmap file: %s\n",sm_filename);
169164
opal_output(0, "%s\n", strerror(errno));
165+
free(filename_basename);
170166
free(sm_filename);
171167
free(sm_data);
172168
free(sh);
@@ -185,6 +181,10 @@ int mca_sharedfp_sm_file_open (struct ompi_communicator_t *comm,
185181
sm_data->sem_name = (char*) malloc( sizeof(char) * 253);
186182
snprintf(sm_data->sem_name,252,"OMPIO_%s",filename_basename);
187183
#endif
184+
// We're now done with filename_basename. Free it here so that we
185+
// don't have to keep freeing it in the error/return cases.
186+
free(filename_basename);
187+
filename_basename = NULL;
188188

189189
if( (sm_data->mutex = sem_open(sm_data->sem_name, O_CREAT, 0644, 1)) != SEM_FAILED ) {
190190
#elif defined(HAVE_SEM_INIT)

0 commit comments

Comments
 (0)