diff options
Diffstat (limited to 'dpid')
-rw-r--r-- | dpid/dpid.c | 2 | ||||
-rw-r--r-- | dpid/misc_new.c | 29 | ||||
-rw-r--r-- | dpid/misc_new.h | 1 |
3 files changed, 24 insertions, 8 deletions
diff --git a/dpid/dpid.c b/dpid/dpid.c index f62aab5f..4f2acc2f 100644 --- a/dpid/dpid.c +++ b/dpid/dpid.c @@ -608,7 +608,7 @@ int init_dpi_socket(struct dp *dpi_attr, char *sockdir) dpi_nm = get_basename(dpi_attr->path); dpi_attr->sockpath = dStrconcat(sockdir, "/", dpi_nm, "-XXXXXX", NULL); - mktemp(dpi_attr->sockpath); + a_Misc_mkfname(dpi_attr->sockpath); if (strlen(dpi_attr->sockpath) > sp_len) { ERRMSG("init_all_dpi_sockets", "socket path is too long", 0); MSG_ERR("\n - it should be <= %lu chars", (ulong_t)sp_len); diff --git a/dpid/misc_new.c b/dpid/misc_new.c index 038e1fa6..17c242d0 100644 --- a/dpid/misc_new.c +++ b/dpid/misc_new.c @@ -138,13 +138,29 @@ int a_Misc_nohang_rdtag(int socket, int timeout, Dstr **tag) /* * Alternative to mkdtemp(). * Not as strong as mkdtemp, but enough for creating a directory. - * (adapted from dietlibc) */ char *a_Misc_mkdtemp(char *template) { + for (;;) { + if (a_Misc_mkfname(template) && mkdir(template, 0700) == 0) + break; + if (errno == EEXIST) + continue; + return 0; + } + return template; +} + +/* + * Return a new, non-existent file name from a template + * (adapted from dietlibc; alternative to mkdtemp()) + */ +char *a_Misc_mkfname(char *template) +{ char *tmp = template + strlen(template) - 6; int i; unsigned int random; + struct stat stat_buf; if (tmp < template) goto error; @@ -155,6 +171,7 @@ char *a_Misc_mkdtemp(char *template) return 0; } srand((uint_t)(time(0) ^ getpid())); + for (;;) { random = (unsigned) rand(); for (i = 0; i < 6; ++i) { @@ -162,11 +179,9 @@ char *a_Misc_mkdtemp(char *template) tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0'; } - if (mkdir(template, 0700) == 0) - break; - if (errno == EEXIST) - continue; - return 0; + if (stat(template, &stat_buf) == -1 && errno == ENOENT) + return template; + + MSG_ERR("a_Misc_mkfname: another round for %s \n", template); } - return template; } diff --git a/dpid/misc_new.h b/dpid/misc_new.h index 94e10676..e2bf6e9a 100644 --- a/dpid/misc_new.h +++ b/dpid/misc_new.h @@ -8,5 +8,6 @@ int a_Misc_close_fd(int fd); Dstr *a_Misc_rdtag(int socket); char *a_Misc_readtag(int sock); char *a_Misc_mkdtemp(char *template); +char *a_Misc_mkfname(char *template); #endif |