Skip to content

Commit 693e26b

Browse files
committed
btl/sm: fix race condition
write to file and then rename, so when the file is open for read, its content is known to have been written. Fixes open-mpi/ompi#1230 (back-ported from commit open-mpi/ompi@db4f483)
1 parent 134bddb commit 693e26b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ompi/mca/btl/sm/btl_sm_component.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
591591
int rc = OMPI_SUCCESS;
592592
int fd = -1;
593593
char *fname = NULL;
594+
char *tmpfname = NULL;
594595
/* used as a temporary store so we can extract shmem_ds info */
595596
mca_common_sm_module_t *tmp_modp = NULL;
596597

@@ -648,7 +649,12 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
648649

649650
/* now just write the contents of tmp_modp->shmem_ds to the full
650651
* sizeof(opal_shmem_ds_t), so we know where the mpool_res_size starts. */
651-
if (-1 == (fd = open(fname, O_CREAT | O_RDWR, 0600))) {
652+
asprintf(&tmpfname, "%s.tmp", fname);
653+
if (NULL == tmpfname) {
654+
rc = OMPI_ERR_OUT_OF_RESOURCE;
655+
goto out;
656+
}
657+
if (-1 == (fd = open(tmpfname, O_CREAT | O_RDWR, 0600))) {
652658
int err = errno;
653659
opal_show_help("help-mpi-btl-sm.txt", "sys call fail", true,
654660
"open(2)", strerror(err), err);
@@ -674,11 +680,18 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
674680
/* only do this for the mpool case */
675681
OBJ_RELEASE(tmp_modp);
676682
}
683+
if (0 != rename(tmpfname, fname)) {
684+
rc = OMPI_ERR_IN_ERRNO;
685+
goto out;
686+
}
677687

678688
out:
679689
if (-1 != fd) {
680690
(void)close(fd);
681691
}
692+
if (NULL != tmpfname) {
693+
free(tmpfname);
694+
}
682695
return rc;
683696
}
684697

0 commit comments

Comments
 (0)