2014-11-24 Roland McGrath <roland@hack.frob.com>
+ * nptl/sem_open.c (sem_open): Use __gen_tempname with
+ __gen_tempname_try_file and custom MODE value, rather than
+ using __mktemp and open in a loop.
+ * stdio-common/Versions (GLIBC_PRIVATE): Add __gen_tempname,
+ __gen_tempname_try_file.
+
* sysdeps/posix/tempname.c (__gen_tempname): Instead of FLAGS and KIND
arguments, take a function pointer TRY_NAME and void *TRY_NAME_ARG.
Call that *TRY_NAME to try a candidate name.
sizeof (sem_t) - sizeof (struct new_sem));
tmpfname = (char *) alloca (mountpoint.dirlen + 6 + 1);
- char *xxxxxx = __mempcpy (tmpfname, mountpoint.dir, mountpoint.dirlen);
-
- int retries = 0;
-#define NRETRIES 50
- while (1)
- {
- /* Add the suffix for mktemp. */
- strcpy (xxxxxx, "XXXXXX");
-
- /* We really want to use mktemp here. We cannot use mkstemp
- since the file must be opened with a specific mode. The
- mode cannot later be set since then we cannot apply the
- file create mask. */
- if (__mktemp (tmpfname) == NULL)
- return SEM_FAILED;
-
- /* Open the file. Make sure we do not overwrite anything. */
- fd = __libc_open (tmpfname, O_RDWR | O_CREAT | O_EXCL, mode);
- if (fd == -1)
- {
- if (errno == EEXIST)
- {
- if (++retries < NRETRIES)
- continue;
-
- __set_errno (EAGAIN);
- }
-
- return SEM_FAILED;
- }
-
- /* We got a file. */
- break;
- }
+ strcpy (__mempcpy (tmpfname, mountpoint.dir, mountpoint.dirlen),
+ "XXXXXX");
+
+ /* This is just like mkstemp, but with a specific mode. */
+ fd = __gen_tempname (tmpfname, 0,
+ &__gen_tempname_try_file, &((int[2]) { 0, mode }));
+ if (fd < 0)
+ {
+ if (errno == EEXIST)
+ __set_errno (EAGAIN);
+ return SEM_FAILED;
+ }
if (TEMP_FAILURE_RETRY (__libc_write (fd, &sem.initsem, sizeof (sem_t)))
== sizeof (sem_t)