If you do this in C, use mkdtempto create a directory and put your socket in that directory.
Other functions, such as tmpnamor mktemp, are unsafe; since they do not create or open a temporary file for you, it is easy to be vulnerable to executing a pre-existing symbolic link (placed by an attacker who guessed about your temp file name) in some important file (for example, /etc/passwd), overwriting it.
Note that there is no way to “block” the path - all you can do is create something there. If you need to put the socket in the end, it is best to use the directory as a placeholder.
source
share