Replace "unsafe" function tempnam by custom code.
[tatoo.git] / remake.cpp
index b6c3c6b..30440a8 100644 (file)
@@ -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);