Skip to content
This repository was archived by the owner on Sep 30, 2022. It is now read-only.

Commit 10fef2b

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 (cherry picked from commit open-mpi/ompi@db4f483)
1 parent a333db5 commit 10fef2b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

opal/mca/btl/sm/btl_sm_component.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
593593
int rc = OPAL_SUCCESS;
594594
int fd = -1;
595595
char *fname = NULL;
596+
char *tmpfname = NULL;
596597
/* used as a temporary store so we can extract shmem_ds info */
597598
mca_common_sm_module_t *tmp_modp = NULL;
598599

@@ -650,7 +651,12 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
650651

651652
/* now just write the contents of tmp_modp->shmem_ds to the full
652653
* sizeof(opal_shmem_ds_t), so we know where the mpool_res_size starts. */
653-
if (-1 == (fd = open(fname, O_CREAT | O_RDWR, 0600))) {
654+
asprintf(&tmpfname, "%s.tmp", fname);
655+
if (NULL == tmpfname) {
656+
rc = OPAL_ERR_OUT_OF_RESOURCE;
657+
goto out;
658+
}
659+
if (-1 == (fd = open(tmpfname, O_CREAT | O_RDWR, 0600))) {
654660
int err = errno;
655661
opal_show_help("help-mpi-btl-sm.txt", "sys call fail", true,
656662
"open(2)", strerror(err), err);
@@ -676,11 +682,18 @@ create_rndv_file(mca_btl_sm_component_t *comp_ptr,
676682
/* only do this for the mpool case */
677683
OBJ_RELEASE(tmp_modp);
678684
}
685+
if (0 != rename(tmpfname, fname)) {
686+
rc = OPAL_ERR_IN_ERRNO;
687+
goto out;
688+
}
679689

680690
out:
681691
if (-1 != fd) {
682692
(void)close(fd);
683693
}
694+
if (NULL != tmpfname) {
695+
free(tmpfname);
696+
}
684697
return rc;
685698
}
686699

0 commit comments

Comments
 (0)