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

Commit 49611b4

Browse files
committed
Merge pull request #861 from ggouaillardet/topic/v2.x/sm_write_and_rename
btl/sm: fix race condition
2 parents b43e6b0 + 70896ec commit 49611b4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

opal/mca/btl/sm/btl_sm_component.c

Lines changed: 16 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,20 @@ 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+
(void)close(fd);
686+
fd = -1;
687+
if (0 != rename(tmpfname, fname)) {
688+
rc = OPAL_ERR_IN_ERRNO;
689+
goto out;
690+
}
679691

680692
out:
681693
if (-1 != fd) {
682694
(void)close(fd);
683695
}
696+
if (NULL != tmpfname) {
697+
free(tmpfname);
698+
}
684699
return rc;
685700
}
686701

0 commit comments

Comments
 (0)