This is the mail archive of the
glibc-cvs@sourceware.org
mailing list for the glibc project.
GNU C Library master sources branch roland/namedsem created. glibc-2.20-215-g61a9806
- From: roland at sourceware dot org
- To: glibc-cvs at sourceware dot org
- Date: 21 Nov 2014 22:28:43 -0000
- Subject: GNU C Library master sources branch roland/namedsem created. glibc-2.20-215-g61a9806
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU C Library master sources".
The branch, roland/namedsem has been created
at 61a98065247cf99cd841c0eec463dd8b3d965c09 (commit)
- Log -----------------------------------------------------------------
http://sourceware.org/git/gitweb.cgi?p=glibc.git;a=commitdiff;h=61a98065247cf99cd841c0eec463dd8b3d965c09
commit 61a98065247cf99cd841c0eec463dd8b3d965c09
Author: Roland McGrath <roland@hack.frob.com>
Date: Fri Nov 21 13:50:48 2014 -0800
unfinished
diff --git a/ChangeLog b/ChangeLog
index 3f0cbbc..49ea03c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,23 @@
-2014-11-20 Roland McGrath <roland@hack.frob.com>
+2014-11-21 Roland McGrath <roland@hack.frob.com>
+
+ * stdio-common/tmpnam.c: Update caller.
+ * stdio-common/tmpnam_r.c: Likewise.
+ * stdio-common/tmpfile.c: Likewise.
+ * misc/mktemp.c (__mktemp): Prototypify. Update caller.
+ * libio/oldtmpfile.c (__old_tmpfile): Update caller.
+ * misc/mkostemp.c: Prototypify. Update caller.
+ * misc/mkstemp.c: Likewise.
+ * misc/mkostemps.c: Likewise.
+ * misc/mkstemp64.c: Likewise.
+ * misc/mkdtemp.c (try_mkdir): New function.
+ (mkdtemp): Use it with new __gen_tempname signature. Prototypify.
+ * misc/mkostemp.c: Prototypify. Just call mkostemps.
+ * misc/mkstemps.c: Likewise.
+ * misc/mkstemps64.c: Likewise.
+ * misc/mkostemp64.c: Prototypify.
+ Just call mkostemp with modified flags.
+ * misc/mkostemps64.c: Prototypify.
+ Just call mkostemps with modified flags.
* nptl/unwind.c (unwind_cleanup): Use __libc_fatal.
diff --git a/include/stdio.h b/include/stdio.h
index 1c54fc4..812bd16 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -82,12 +82,22 @@ extern int __path_search (char *__tmpl, size_t __tmpl_len,
const char *__dir, const char *__pfx,
int __try_tempdir);
-extern int __gen_tempname (char *__tmpl, int __suffixlen, int __flags,
- int __kind);
-/* The __kind argument to __gen_tempname may be one of: */
-# define __GT_FILE 0 /* create a file */
-# define __GT_DIR 1 /* create a directory */
-# define __GT_NOCREATE 2 /* just find a name not currently in use */
+/* The *TRY_NAME function is called repeatedly on candidate names until
+ it returns >= 0. If it returns -2, the next candidate name is tried.
+ If it returns -1 (with errno set), __gen_tempname fails immediately. */
+extern int __gen_tempname (char *__tmpl, int __suffixlen,
+ int (*__try_name) (const char *__name, void *__arg),
+ void *__try_name_arg);
+
+/* These are the common TRY_NAME functions. */
+
+/* ARG is ignored. */
+extern int __gen_tempname_try_nocreate (const char *__name, void *__arg);
+
+/* ARG is int[2] of {flags, mode}. */
+extern int __gen_tempname_try_file (const char *__name, void *__arg);
+#define __GT_FILE_DEFAULTS (&((int[2]) { 0, 0600 }))
+
/* Print out MESSAGE on the error output and abort. */
extern void __libc_fatal (const char *__message)
diff --git a/libio/oldtmpfile.c b/libio/oldtmpfile.c
index a6a5e71..3c3af9c 100644
--- a/libio/oldtmpfile.c
+++ b/libio/oldtmpfile.c
@@ -37,7 +37,9 @@ __old_tmpfile (void)
if (__path_search (buf, FILENAME_MAX, NULL, "tmpf", 0))
return NULL;
- fd = __gen_tempname (buf, 0, 0, __GT_FILE);
+
+ int flags_and_mode[2] = { 0, 0 };
+ fd = __gen_tempname (buf, 0, &__gen_tempname_try_file, __GT_FILE_DEFAULTS);
if (fd < 0)
return NULL;
diff --git a/misc/mkdtemp.c b/misc/mkdtemp.c
index 6bd72ca..520bd72 100644
--- a/misc/mkdtemp.c
+++ b/misc/mkdtemp.c
@@ -17,6 +17,14 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/stat.h>
+
+
+static int
+try_mkdir (const char *name, void *arg __attribute__ ((unused)))
+{
+ return __mkdir (name, S_IRUSR | S_IWUSR | S_IXUSR);
+}
/* Generate a unique temporary directory from TEMPLATE.
The last six characters of TEMPLATE must be "XXXXXX";
@@ -24,10 +32,9 @@
The directory is created, mode 700, and its name is returned.
(This function comes from OpenBSD.) */
char *
-mkdtemp (template)
- char *template;
+mkdtemp (char *template)
{
- if (__gen_tempname (template, 0, 0, __GT_DIR))
+ if (__gen_tempname (template, 0, &try_mkdir, NULL))
return NULL;
else
return template;
diff --git a/misc/mkostemp.c b/misc/mkostemp.c
index f0dc3c1..695e87a 100644
--- a/misc/mkostemp.c
+++ b/misc/mkostemp.c
@@ -17,21 +17,16 @@
#include <stdio.h>
#include <stdlib.h>
-
-#ifndef __GT_FILE
-# define __GT_FILE 0
-#endif
+#include <sys/stat.h>
/* Generate a unique temporary file name from TEMPLATE.
The last six characters of TEMPLATE must be "XXXXXX";
they are replaced with a string that makes the filename unique.
Then open the file and return a fd. */
int
-mkostemp (template, flags)
- char *template;
- int flags;
+mkostemp (char *template, int flags)
{
- return __gen_tempname (template, 0, flags, __GT_FILE);
+ return mkostemps (template, 0, flags);
}
#if !defined O_LARGEFILE || O_LARGEFILE == 0
diff --git a/misc/mkostemp64.c b/misc/mkostemp64.c
index c68e03d..a7bf00d 100644
--- a/misc/mkostemp64.c
+++ b/misc/mkostemp64.c
@@ -27,11 +27,9 @@
they are replaced with a string that makes the filename unique.
Then open the file and return a fd. */
int
-mkostemp64 (template, flags)
- char *template;
- int flags;
+mkostemp64 (char *template, int flags)
{
- return __gen_tempname (template, 0, flags | O_LARGEFILE, __GT_FILE);
+ return mkostemp (template, flags | O_LARGEFILE);
}
#endif
diff --git a/misc/mkostemps.c b/misc/mkostemps.c
index 2f9745c..330ff6c 100644
--- a/misc/mkostemps.c
+++ b/misc/mkostemps.c
@@ -19,19 +19,12 @@
#include <stdio.h>
#include <stdlib.h>
-#ifndef __GT_FILE
-# define __GT_FILE 0
-#endif
-
/* Generate a unique temporary file name from TEMPLATE. The last six
characters before a suffix of length SUFFIXLEN of TEMPLATE must be
"XXXXXX"; they are replaced with a string that makes the filename
unique. Then open the file and return a fd. */
int
-mkostemps (template, suffixlen, flags)
- char *template;
- int suffixlen;
- int flags;
+mkostemps (char *template, int suffixlen, int flags)
{
if (suffixlen < 0)
{
@@ -39,7 +32,8 @@ mkostemps (template, suffixlen, flags)
return -1;
}
- return __gen_tempname (template, suffixlen, flags, __GT_FILE);
+ return __gen_tempname (template, suffixlen, &__gen_tempname_try_file,
+ &((int[2]) { flags, S_IRUSR | S_IWUSR }));
}
#if !defined O_LARGEFILE || O_LARGEFILE == 0
diff --git a/misc/mkostemps64.c b/misc/mkostemps64.c
index 1d1d3fa..971afc2 100644
--- a/misc/mkostemps64.c
+++ b/misc/mkostemps64.c
@@ -28,18 +28,9 @@
"XXXXXX"; they are replaced with a string that makes the filename
unique. Then open the file and return a fd. */
int
-mkostemps64 (template, suffixlen, flags)
- char *template;
- int suffixlen;
- int flags;
+mkostemps64 (char *template, int suffixlen, int flags)
{
- if (suffixlen < 0)
- {
- __set_errno (EINVAL);
- return -1;
- }
-
- return __gen_tempname (template, suffixlen, flags | O_LARGEFILE, __GT_FILE);
+ return mkostemps (template, sufflixen, flags | O_LARGEFILE);
}
#endif
diff --git a/misc/mkstemp.c b/misc/mkstemp.c
index a01095b..8657924 100644
--- a/misc/mkstemp.c
+++ b/misc/mkstemp.c
@@ -18,19 +18,15 @@
#include <stdio.h>
#include <stdlib.h>
-#ifndef __GT_FILE
-# define __GT_FILE 0
-#endif
-
/* Generate a unique temporary file name from TEMPLATE.
The last six characters of TEMPLATE must be "XXXXXX";
they are replaced with a string that makes the filename unique.
Then open the file and return a fd. */
int
-mkstemp (template)
- char *template;
+mkstemp (char *template)
{
- return __gen_tempname (template, 0, 0, __GT_FILE);
+ return __gen_tempname (template, 0,
+ &__gen_tempname_try_file, __GT_FILE_DEFAULTS);
}
#if !defined O_LARGEFILE || O_LARGEFILE == 0
diff --git a/misc/mkstemp64.c b/misc/mkstemp64.c
index 3ca4657..e15c7d6 100644
--- a/misc/mkstemp64.c
+++ b/misc/mkstemp64.c
@@ -27,10 +27,10 @@
they are replaced with a string that makes the filename unique.
Then open the file and return a fd. */
int
-mkstemp64 (template)
- char *template;
+mkstemp64 (char *template)
{
- return __gen_tempname (template, 0, O_LARGEFILE, __GT_FILE);
+ return __gen_tempname (template, 0, &__gen_tempname_try_file,
+ (&((int[2]) { O_LARGEFILE, 0600 })));
}
#endif
diff --git a/misc/mkstemps.c b/misc/mkstemps.c
index d58fce3..3f2c01e 100644
--- a/misc/mkstemps.c
+++ b/misc/mkstemps.c
@@ -19,26 +19,14 @@
#include <stdio.h>
#include <stdlib.h>
-#ifndef __GT_FILE
-# define __GT_FILE 0
-#endif
-
/* Generate a unique temporary file name from TEMPLATE. The last six
characters before a suffix of length SUFFIXLEN of TEMPLATE must be
"XXXXXX"; they are replaced with a string that makes the filename
unique. Then open the file and return a fd. */
int
-mkstemps (template, suffixlen)
- char *template;
- int suffixlen;
+mkstemps (char *template, int suffixlen)
{
- if (suffixlen < 0)
- {
- __set_errno (EINVAL);
- return -1;
- }
-
- return __gen_tempname (template, suffixlen, 0, __GT_FILE);
+ return mkostemps (template, suffixlen, 0);
}
#if !defined O_LARGEFILE || O_LARGEFILE == 0
diff --git a/misc/mkstemps64.c b/misc/mkstemps64.c
index 1b0f024..d3d7013 100644
--- a/misc/mkstemps64.c
+++ b/misc/mkstemps64.c
@@ -28,17 +28,9 @@
"XXXXXX"; they are replaced with a string that makes the filename
unique. Then open the file and return a fd. */
int
-mkstemps64 (template, suffixlen)
- char *template;
- int suffixlen;
+mkstemps64 (char *template, int suffixlen)
{
- if (suffixlen < 0)
- {
- __set_errno (EINVAL);
- return -1;
- }
-
- return __gen_tempname (template, suffixlen, O_LARGEFILE, __GT_FILE);
+ return mkostemps (template, suffixlen, O_LARGEFILE);
}
#endif
diff --git a/misc/mktemp.c b/misc/mktemp.c
index a60f95c..737e329 100644
--- a/misc/mktemp.c
+++ b/misc/mktemp.c
@@ -22,10 +22,9 @@
The last six characters of TEMPLATE must be "XXXXXX";
they are replaced with a string that makes the filename unique. */
char *
-__mktemp (template)
- char *template;
+__mktemp (char *template)
{
- if (__gen_tempname (template, 0, 0, __GT_NOCREATE) < 0)
+ if (__gen_tempname (template, 0, &__gen_tempname_try_nocreate, NULL) < 0)
/* We return the null string if we can't find a unique file name. */
template[0] = '\0';
diff --git a/stdio-common/tmpfile.c b/stdio-common/tmpfile.c
index 022c2f6..89689a6 100644
--- a/stdio-common/tmpfile.c
+++ b/stdio-common/tmpfile.c
@@ -44,7 +44,8 @@ tmpfile (void)
#ifdef FLAGS
flags = FLAGS;
#endif
- fd = __gen_tempname (buf, 0, flags, __GT_FILE);
+ fd = __gen_tempname (buf, 0, &__gen_tempname_try_file,
+ &((int[2]) { flags, S_IRUSR | S_IWUSR }));
if (fd < 0)
return NULL;
diff --git a/stdio-common/tmpnam.c b/stdio-common/tmpnam.c
index d8cd509..810d0ff 100644
--- a/stdio-common/tmpnam.c
+++ b/stdio-common/tmpnam.c
@@ -34,11 +34,11 @@ tmpnam (char *s)
/* In the following call we use the buffer pointed to by S if
non-NULL although we don't know the size. But we limit the size
to L_tmpnam characters in any case. */
- if (__builtin_expect (__path_search (tmpbuf, L_tmpnam, NULL, NULL, 0),
- 0))
+ if (__glibc_unlikely (__path_search (tmpbuf, L_tmpnam, NULL, NULL, 0)))
return NULL;
- if (__glibc_unlikely (__gen_tempname (tmpbuf, 0, 0, __GT_NOCREATE)))
+ if (__glibc_unlikely (__gen_tempname (tmpbuf, 0,
+ __gen_tempname_try_nocreate, NULL)))
return NULL;
if (s == NULL)
diff --git a/stdio-common/tmpnam_r.c b/stdio-common/tmpnam_r.c
index aa2e24e..ad7cc54 100644
--- a/stdio-common/tmpnam_r.c
+++ b/stdio-common/tmpnam_r.c
@@ -25,9 +25,10 @@ tmpnam_r (char *s)
if (s == NULL)
return NULL;
- if (__path_search (s, L_tmpnam, NULL, NULL, 0))
+ if (__glibc_unlikely (__path_search (s, L_tmpnam, NULL, NULL, 0)))
return NULL;
- if (__gen_tempname (s, 0, 0, __GT_NOCREATE))
+ if (__glibc_unlikely (__gen_tempname (s, 0,
+ __gen_tempname_try_nocreate, NULL)))
return NULL;
return s;
diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c
index 3aa11b0..d7ff01f 100644
--- a/sysdeps/posix/tempname.c
+++ b/sysdeps/posix/tempname.c
@@ -180,23 +180,21 @@ static const char letters[] =
The name constructed does not exist at the time of the call to
__gen_tempname. TMPL is overwritten with the result.
- KIND may be one of:
- __GT_NOCREATE: simply verify that the name does not exist
- at the time of the call.
- __GT_FILE: create the file using open(O_CREAT|O_EXCL)
- and return a read-write fd. The file is mode 0600.
- __GT_DIR: create a directory, which will be mode 0700.
+ The *TRY_NAME function is called repeatedly on candidate names until
+ it returns >= 0. If it returns -2, the next candidate name is tried.
+ If it returns -1 (with errno set), __gen_tempname fails immediately.
We use a clever algorithm to get hard-to-predict names. */
int
-__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
+__gen_tempname (char *tmpl, int suffixlen,
+ int (*try_name) (const char *name, void *arg),
+ void *try_name_arg)
{
int len;
char *XXXXXX;
static uint64_t value;
uint64_t random_time_bits;
unsigned int count;
- int fd = -1;
int save_errno = errno;
struct_stat64 st;
@@ -255,6 +253,14 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
v /= 62;
XXXXXX[5] = letters[v % 62];
+ int fd = (*try_name) (tmpl, try_name_arg);
+ if (fd != -2)
+ {
+ if (fd >= 0)
+ __set_errno (save_errno);
+ return fd;
+ }
+
switch (kind)
{
case __GT_FILE:
@@ -289,17 +295,38 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
assert (! "invalid KIND in __gen_tempname");
abort ();
}
-
- if (fd >= 0)
- {
- __set_errno (save_errno);
- return fd;
- }
+ if (fd == -1)
else if (errno != EEXIST)
return -1;
+
}
/* We got out of the loop because we ran out of combinations to try. */
__set_errno (EEXIST);
return -1;
}
+
+
+int
+__gen_tempname_try_nocreate (const char *name, void *arg)
+{
+ struct stat64 st;
+ if (__lxstat64 (_STAT_VER, name, &st) == 0)
+ /* This name exists. Try another. */
+ return -2;
+
+ {
+ if (errno == ENOENT)
+ {
+ __set_errno (save_errno);
+ return 0;
+ }
+ else
+ /* Give up now. */
+ return -1;
+ }
+
+}
+
+/* ARG is int[2] of {flags, mode}. */
+extern int __gen_tempname_try_file (const char *__name, void *__arg);
-----------------------------------------------------------------------
hooks/post-receive
--
GNU C Library master sources