contrib: Remove gf_mkostemp copied from GLibC

gf_mkostemp is borrowed from GLibC a long time back,
we now have mkostemp or mkstemp alternatives with all
distributions and versions that we care for.

As a result removing this from the contrib directory
and modifying the one instance that is still using the
same.

This is part of code cleanup as we cleaned up coverity
SECUR_TEMP errors.

Updates: bz#1193929

Change-Id: I1ad7863043cdb0845c53748f5a0522e767079130
Signed-off-by: ShyamsundarR <srangana@redhat.com>
This commit is contained in:
ShyamsundarR 2018-07-30 20:16:21 -04:00 committed by Amar Tumballi
parent 82e5acd6b3
commit 9297e1950f
4 changed files with 13 additions and 114 deletions

View File

@ -1,107 +0,0 @@
/* Borrowed from glibc-2.16/sysdeps/posix/tempname.c */
/* Copyright (C) 1991-2001, 2006, 2007, 2009 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <time.h>
#include <inttypes.h>
static const char letters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/* Generate a temporary file name based on TMPL. TMPL must match the
rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
*/
#if !defined(TMP_MAX)
#define TMP_MAX 238328
#endif
int
gf_mkostemp (char *tmpl, int suffixlen, int flags)
{
int len;
char *XXXXXX;
static uint64_t value;
uint64_t random_time_bits;
unsigned int count;
int fd = -1;
/* A lower bound on the number of temporary files to attempt to
generate. The maximum total number of temporary file names that
can exist for a given template is 62**6. It should never be
necessary to try all these combinations. Instead if a reasonable
number of names is tried (we define reasonable as 62**3) fail to
give the system administrator the chance to remove the problems. */
unsigned int attempts = TMP_MAX; /* TMP_MAX == 62³ */
len = strlen (tmpl);
if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen],
"XXXXXX", 6))
return -1;
/* This is where the Xs start. */
XXXXXX = &tmpl[len - 6 - suffixlen];
/* Get some more or less random data. */
# if HAVE_GETTIMEOFDAY
struct timeval tv;
gettimeofday (&tv, NULL);
random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
# else
random_time_bits = time (NULL);
# endif
value += random_time_bits ^ getpid ();
for (count = 0; count < attempts; value += 7777, ++count) {
uint64_t v = value;
/* Fill in the random bits. */
XXXXXX[0] = letters[v % 62];
v /= 62;
XXXXXX[1] = letters[v % 62];
v /= 62;
XXXXXX[2] = letters[v % 62];
v /= 62;
XXXXXX[3] = letters[v % 62];
v /= 62;
XXXXXX[4] = letters[v % 62];
v /= 62;
XXXXXX[5] = letters[v % 62];
fd = open (tmpl, (flags & ~O_ACCMODE)
| O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (fd >= 0)
return fd;
else if (errno != EEXIST)
return -1;
}
/* We got out of the loop because we ran out of combinations to try. */
return -1;
}

View File

@ -31,7 +31,7 @@ libglusterfs_la_SOURCES = dict.c xlator.c logging.c \
circ-buff.c event-history.c gidcache.c ctx.c client_t.c event-poll.c \
event-epoll.c syncop-utils.c cluster-syncop.c refcount.c \
$(CONTRIBDIR)/libgen/basename_r.c \
$(CONTRIBDIR)/libgen/dirname_r.c $(CONTRIBDIR)/stdlib/gf_mkostemp.c \
$(CONTRIBDIR)/libgen/dirname_r.c \
strfd.c parse-utils.c $(CONTRIBDIR)/mount/mntent.c \
$(CONTRIBDIR)/libexecinfo/execinfo.c quota-common-utils.c rot-buffs.c \
$(CONTRIBDIR)/timer-wheel/timer-wheel.c \

View File

@ -4238,16 +4238,26 @@ gf_backtrace_fillframes (char *buf)
size_t idx = 0;
size_t pos = 0;
size_t inc = 0;
char tmpl[32] = "/tmp/btXXXXXX";
char tmpl[] = "/tmp/glfs-bt-XXXXXX";
frames = backtrace (array, GF_BACKTRACE_FRAME_COUNT);
if (!frames)
return -1;
fd = gf_mkostemp (tmpl, 0, O_RDWR);
/* coverity[secure_temp] mkstemp uses 0600 as the mode and is safe */
fd = mkstemp (tmpl);
if (fd == -1)
return -1;
/* Calling unlink so that when the file is closed or program
* terminates the temporary file is deleted.
*/
ret = sys_unlink (tmpl);
if (ret < 0) {
gf_msg (THIS->name, GF_LOG_INFO, 0, LG_MSG_FILE_OP_FAILED,
"Unable to delete temporary file: %s", tmpl);
}
/*The most recent two frames are the calling function and
* gf_backtrace_save, which we can infer.*/
@ -4280,8 +4290,6 @@ out:
if (fp)
fclose (fp);
sys_unlink (tmpl);
return (idx > 0)? 0: -1;
}

View File

@ -451,8 +451,6 @@ char *dirname_r(char *path);
#define dirname(path) dirname_r(path)
#endif /* THREAD_UNSAFE_DIRNAME */
int gf_mkostemp (char *tmpl, int suffixlen, int flags);
#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
/* Linux, Solaris, Cygwin */
#define ST_ATIM_SEC(stbuf) ((stbuf)->st_atim.tv_sec)