mirror of
https://github.com/fluencelabs/musl
synced 2025-06-24 20:21:59 +00:00
fix tempnam name generation, and a small bug in tmpnam on retry limit
This commit is contained in:
@ -1,19 +1,22 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <limits.h>
|
#include <time.h>
|
||||||
#include <errno.h>
|
|
||||||
#include "libc.h"
|
#include "libc.h"
|
||||||
|
#include "atomic.h"
|
||||||
|
|
||||||
|
#define MAXTRIES 100
|
||||||
|
|
||||||
char *tempnam(const char *dir, const char *pfx)
|
char *tempnam(const char *dir, const char *pfx)
|
||||||
{
|
{
|
||||||
static int lock;
|
|
||||||
static int index;
|
static int index;
|
||||||
char *s;
|
char *s;
|
||||||
|
struct timespec ts;
|
||||||
int pid = getpid();
|
int pid = getpid();
|
||||||
int l;
|
size_t l;
|
||||||
|
int n;
|
||||||
|
int try=0;
|
||||||
|
|
||||||
if (!dir) dir = P_tmpdir;
|
if (!dir) dir = P_tmpdir;
|
||||||
if (!pfx) pfx = "temp";
|
if (!pfx) pfx = "temp";
|
||||||
@ -21,22 +24,18 @@ char *tempnam(const char *dir, const char *pfx)
|
|||||||
if (access(dir, R_OK|W_OK|X_OK) != 0)
|
if (access(dir, R_OK|W_OK|X_OK) != 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
l = strlen(dir) + 1 + strlen(pfx) + 2 + sizeof(int)*3*2 + 1;
|
l = strlen(dir) + 1 + strlen(pfx) + 3*(sizeof(int)*3+2) + 1;
|
||||||
s = malloc(l);
|
s = malloc(l);
|
||||||
if (!s) {
|
if (!s) return s;
|
||||||
errno = ENOMEM;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCK(&lock);
|
do {
|
||||||
for (; index < TMP_MAX; index++) {
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
snprintf(s, l, "%s/%s-%d-%d", dir, pfx, pid, index);
|
n = ts.tv_nsec ^ (unsigned)&s ^ (unsigned)s;
|
||||||
if (access(s, F_OK) != 0) {
|
snprintf(s, l, "%s/%s-%d-%d-%x", dir, pfx, pid, a_fetch_add(&index, 1), n);
|
||||||
UNLOCK(&lock);
|
} while (!access(s, F_OK) && try++<MAXTRIES);
|
||||||
return s;
|
if (try>=MAXTRIES) {
|
||||||
}
|
|
||||||
}
|
|
||||||
UNLOCK(&lock);
|
|
||||||
free(s);
|
free(s);
|
||||||
return NULL;
|
return 0;
|
||||||
|
}
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
@ -26,5 +26,5 @@ char *tmpnam(char *s)
|
|||||||
n = ts.tv_nsec ^ (unsigned)&s ^ (unsigned)s;
|
n = ts.tv_nsec ^ (unsigned)&s ^ (unsigned)s;
|
||||||
snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n);
|
snprintf(s, L_tmpnam, "/tmp/t%x-%x", a_fetch_add(&index, 1), n);
|
||||||
} while (!__syscall(SYS_access, s, F_OK) && try++<MAXTRIES);
|
} while (!__syscall(SYS_access, s, F_OK) && try++<MAXTRIES);
|
||||||
return try==MAXTRIES ? 0 : s;
|
return try>=MAXTRIES ? 0 : s;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user