From: Kim Nguyễn Date: Sat, 30 Nov 2013 08:27:36 +0000 (+0100) Subject: Replace "unsafe" function tempnam by custom code. X-Git-Tag: v0.1~27 X-Git-Url: http://git.nguyen.vg/gitweb/?p=tatoo.git;a=commitdiff_plain;h=30675098fce9aaa56027adc42042ea70c986fe01 Replace "unsafe" function tempnam by custom code. --- diff --git a/remake.cpp b/remake.cpp index b6c3c6b..30440a8 100644 --- a/remake.cpp +++ b/remake.cpp @@ -2560,7 +2560,28 @@ static void create_server() if (sigaction(SIGINT, &sa, NULL) == -1) goto error; // Prepare a named unix socket in temporary directory. - socket_name = tempnam(NULL, "rmk-"); + struct stat tmpstat; + char * tmpdir; + if ((0 == stat(tmpdir = getenv("TMPDIR"), &tmpstat) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)P_tmpdir, &tmpstat)) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)"/tmp", &tmpstat)) + && (S_ISDIR(tmpstat.st_mode)))); + else goto error; + std::stringstream tmpname; + long int rnd = now ^ getpid(); + do + { + srandom(rnd); + rnd = random(); + tmpname << tmpdir << "/rmk-"; + tmpname.fill('0'); + tmpname.width(8); + tmpname << rnd; + } + while (access (tmpname.str().c_str(), F_OK) == 0); + socket_name = strdup(tmpname.str().c_str()); if (!socket_name) goto error2; struct sockaddr_un socket_addr; size_t len = strlen(socket_name);