Skip to content

Commit

Permalink
Merge pull request #5918 from raffenet/mkstemp-cleanup
Browse files Browse the repository at this point in the history
Add MPL_mkstemp and cleanup leftovers

Approved-by: Hui Zhou <[email protected]>
  • Loading branch information
raffenet authored Mar 31, 2022
2 parents 616b759 + 7864e1d commit 625c71d
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 114 deletions.
5 changes: 0 additions & 5 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3804,11 +3804,6 @@ fi
# ----------------------------------------------------------------------------
# Look for some non-posix, but commonly provided functions
# ----------------------------------------------------------------------------
# mkstemp() is a better replacement for mktemp()
AC_CHECK_FUNCS(mkstemp)
if test "$ac_cv_func_mkstemp" = "yes" ; then
PAC_FUNC_NEEDS_DECL([#include <stdlib.h>],mkstemp)
fi
# putenv() sets environment variable
AC_CHECK_FUNCS(putenv)
if test "$ac_cv_func_putenv" = "yes" ; then
Expand Down
1 change: 0 additions & 1 deletion src/mpid/ch3/channels/nemesis/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ noinst_HEADERS += \
include $(top_srcdir)/src/mpid/ch3/channels/nemesis/src/Makefile.mk
include $(top_srcdir)/src/mpid/ch3/channels/nemesis/netmod/Makefile.mk
include $(top_srcdir)/src/mpid/ch3/channels/nemesis/utils/monitor/Makefile.mk
include $(top_srcdir)/src/mpid/ch3/channels/nemesis/utils/replacements/Makefile.mk

endif BUILD_CH3_NEMESIS
5 changes: 0 additions & 5 deletions src/mpid/ch3/channels/nemesis/subconfigure.m4
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ if test "${with_papi}" != "no" ; then
# AC_CHECK_LIB(perfctr, perfctr_info, , [AC_MSG_ERROR(['perfctr library not found. Did you specify --with-papi=?'])])
fi
# handle missing mkstemp, or missing mkstemp declaration
AC_CHECK_FUNCS(mkstemp)
AC_CHECK_FUNCS(rand)
AC_CHECK_FUNCS(srand)
# Check for available shared memory functions
#PAC_ARG_SHARED_MEMORY
#if test "$with_shared_memory" != "mmap" -a "$with_shared_memory" != "sysv"; then
Expand Down
7 changes: 0 additions & 7 deletions src/mpid/ch3/channels/nemesis/utils/Makefile.mk

This file was deleted.

7 changes: 0 additions & 7 deletions src/mpid/ch3/channels/nemesis/utils/replacements/Makefile.mk

This file was deleted.

4 changes: 0 additions & 4 deletions src/mpid/common/shm/mpidu_init_shm_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
#include <sys/shm.h>
#endif

#if defined(HAVE_MKSTEMP) && defined(NEEDS_MKSTEMP_DECL)
extern int mkstemp(char *t);
#endif

typedef struct memory_list {
void *ptr;
MPIDU_shm_seg_t *memory;
Expand Down
2 changes: 2 additions & 0 deletions src/mpl/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,8 @@ fi
AC_CHECK_FUNCS(mkstemp)
if test "$ac_cv_func_mkstemp" = "yes" ; then
PAC_FUNC_NEEDS_DECL([#include <stdlib.h>],mkstemp)
else
AC_MSG_WARN([The mkstemp function is not provided in the standard library. Using a possibly insecure replacement function.])
fi
# fdopen() converts from an fd to a FILE*
AC_CHECK_FUNCS(fdopen)
Expand Down
10 changes: 10 additions & 0 deletions src/mpl/include/mpl_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@
/* Returns the number of processors currently available in the system */
int MPL_get_nprocs(void);

#if defined (MPL_HAVE_MKSTEMP) && defined (MPL_NEEDS_MKSTEMP_DECL)
int mkstemp(char *template);
#endif

#if defined MPL_HAVE_MKSTEMP
#define MPL_mkstemp mkstemp
#else
int MPL_mkstemp(char *template);
#endif

#endif /* MPL_MISC_H_INCLUDED */
50 changes: 2 additions & 48 deletions src/mpl/src/dbg/mpl_dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#include <errno.h>
#endif

#if defined(MPL_HAVE_MKSTEMP) && defined(MPL_NEEDS_MKSTEMP_DECL)
extern int mkstemp(char *t);
#endif

#if defined(MPL_HAVE_FDOPEN) && defined(MPL_NEEDS_FDOPEN_DECL)
extern FILE *fdopen(int fd, const char *mode);
#endif
Expand Down Expand Up @@ -574,7 +570,7 @@ Environment variables\n\
return 0;
}

#if defined (MPL_HAVE_MKSTEMP) && defined (MPL_HAVE_FDOPEN)
#ifdef MPL_HAVE_FDOPEN

/* creates a temporary file in the same directory the user specified
* for the log file */
Expand All @@ -598,7 +594,7 @@ static int dbg_open_tmpfile(FILE ** dbg_fp)

MPL_strncpy(basename, temp_pattern, sizeof(temp_pattern));

fd = mkstemp(temp_filename);
fd = MPL_mkstemp(temp_filename);
if (fd == -1)
goto fn_fail;

Expand All @@ -615,48 +611,6 @@ static int dbg_open_tmpfile(FILE ** dbg_fp)
goto fn_exit;
}

#elif defined(MPL_HAVE__MKTEMP_S) && defined(MPL_HAVE_FOPEN_S)

/* creates a temporary file in the same directory the user specified
* for the log file */
static int dbg_open_tmpfile(FILE ** dbg_fp)
{
int mpl_errno = MPL_SUCCESS;
const char temp_pattern[] = "templogXXXXXX";
int fd;
char *basename;
int ret;
errno_t ret_errno;

ret = MPL_strncpy(temp_filename, file_pattern, MAXPATHLEN);
if (ret)
goto fn_fail;

find_basename(temp_filename, &basename);

/* make sure there's enough room in temp_filename to store temp_pattern */
if (basename - temp_filename > MAXPATHLEN - sizeof(temp_pattern))
goto fn_fail;

MPL_strncpy(basename, temp_pattern, sizeof(temp_pattern));

ret_errno = _mktemp_s(temp_filename, MAXPATHLEN);
if (ret_errno != 0)
goto fn_fail;

ret_errno = fopen_s(dbg_fp, temp_filename, "a+");
if (ret_errno != 0)
goto fn_fail;

fn_exit:
return mpl_errno;
fn_fail:
MPL_error_printf("Could not open log file %s\n", temp_filename);
dbg_initialized = DBG_ERROR;
mpl_errno = MPL_ERR_DBG_INTERN;
goto fn_exit;
}

#else

/* creates a temporary file in some directory, which may not be where
Expand Down
3 changes: 2 additions & 1 deletion src/mpl/src/misc/Makefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# See COPYRIGHT in top-level directory.
#

lib@MPLLIBNAME@_la_SOURCES += src/misc/mpl_misc.c
lib@MPLLIBNAME@_la_SOURCES += src/misc/mpl_misc.c \
src/misc/mpl_mkstemp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* See COPYRIGHT in top-level directory
*/

/* for ATTRIBUTE */
#include "mpichconf.h"
#include "mpl.h"

#include <errno.h>
Expand All @@ -16,61 +14,49 @@
/* here to prevent "has no symbols" warnings from ranlib on OS X */
static int dummy ATTRIBUTE((unused)) MPL_USED = 0;

#if !defined (HAVE_MKSTEMP) || !HAVE_MKSTEMP
#ifndef MPL_HAVE_MKSTEMP

#warning The mkstemp function is not provided in the standard library.
#warning Using a possibly insecure replacement function.

#if !defined (HAVE_RAND) || !HAVE_RAND
static notrand = 0;
#define srand(x) notrand = x
#define rand() (++notrand)
#endif

#define MAX_TRIES 100 /* max number of filenames we try before giving up */
#define NUM_XS 6 /* number of X's in the template */
#define MAX_TRIES 100 /* max number of filenames we try before giving up */
#define NUM_XS 6 /* number of X's in the template */

/* replaces NUM_XS characters in xs with random characters
xs must be at least NUM_XS chars long */
static void randchar (char xs[])
static void randchar(char xs[])
{
char chararray[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
int i;

for (i = 0; i < NUM_XS; ++i)
xs[i] = chararray[rand() % (sizeof(chararray) - 1)];
}


int mkstemp (char *template)
int MPL_mkstemp(char *template)
{
int fd;
unsigned int seed = (unsigned int) getpid(); /* can probably use something better */
char *X; /* points to the XXXXXX substring in template */
unsigned int seed = (unsigned int) getpid(); /* can probably use something better */
char *X; /* points to the XXXXXX substring in template */
int i;
srand (seed);

srand(seed);

/* find the end of the template string */
X = template;
while (*X)
++X;

/* back up NUM_XS characters and check to make sure they're all 'X' */
for (i = 0 ; i < NUM_XS; ++i)
{
for (i = 0; i < NUM_XS; ++i) {
--X;
if (X < template || *X != 'X')
{
if (X < template || *X != 'X') {
errno = EINVAL;
return -1;
}
}

for (i = 0; i < MAX_TRIES; ++i)
{
randchar (X);
fd = open (template, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);

for (i = 0; i < MAX_TRIES; ++i) {
randchar(X);
fd = open(template, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd != -1)
return fd;
}
Expand Down
8 changes: 2 additions & 6 deletions src/mpl/src/shm/mpl_shm_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ MPL_SUPPRESS_OSX_HAS_NO_SYMBOLS_WARNING;
#include <sys/mman.h>
#endif

#if defined (MPL_HAVE_MKSTEMP) && defined (MPL_NEEDS_MKSTEMP_DECL)
extern int mkstemp(char *template);
#endif

inline int MPLI_shm_lhnd_close(MPL_shm_hnd_t hnd)
{
MPLI_shm_lhnd_t lhnd = MPLI_SHM_LHND_INVALID;
Expand Down Expand Up @@ -74,10 +70,10 @@ static inline int MPL_shm_seg_create_attach_templ(MPL_shm_hnd_t hnd, intptr_t se
char *chosen_fname = NULL;

chosen_fname = dev_shm_fname;
lhnd = mkstemp(chosen_fname);
lhnd = MPL_mkstemp(chosen_fname);
if (lhnd == -1) {
chosen_fname = tmp_fname;
lhnd = mkstemp(chosen_fname);
lhnd = MPL_mkstemp(chosen_fname);
if (lhnd == -1) {
rc = MPL_ERR_SHM_INTERN;
goto fn_fail;
Expand Down

0 comments on commit 625c71d

Please sign in to comment.