Can a socket be used if its file is read-only?

For example, if a program needs to be use a socket to communicate with a daemon, but it cannot be allowed to delete the socket.

Because, say, it wouldn't be able to recreate it, would making the file read-only work? This is for both Linux and BSD variants.


would making the file read-only work?

What makes you think you can't delete a file that is read-only? The ability to delete files is governed by write access to the directory that the file is in[*], and not at all by the file permissions. To see this, do:

touch a
chmod 0000 a
rm a

While rm might prompt about removing a write-protected file, the operation is allowed.

So, what you need to do is not give the program accessing the daemon write permissions on the directory containing the socket file, and Bob's your uncle (as far as deleting the socket file goes).

[*] - note that things are complicated a bit by the sticky bit, which can lead to examination of the file's owner, but not the file's permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.