26

I have a server application that creates a UNIX Domain Socket in a specific path with a name and bind()s to it.

I need to delete the socket only when I close/stop the application intentionally, from within the the application code; otherwise it needs to be open. How do I do this?

Thanks!

Edit: Consider that I start and run my application from inside a terminal.

6 Answers 6

35

You're making this harder than it needs to be. Put the unlink() right before the bind(). That's how everybody else does it. (Example: BSD syslogd, one of the classic unix-domain-socket-based services)

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. i thought i had to go after the bind. thanks for specifying it had to go before.
You could still do an unlink on exit to prevent the stale socket from showing up in directory listings when the server isn't running. But that's purely cosmetic.
7

If you have multiple exit points from your application and you don't want to modify each of them to call cleanup routine, then you may use "dirty" approach.

When socket is just created, register cleanup routine with atexit(3). Routine (which is simply a call to unlink(2)) will be called when application is terminated normally. But it won't be called if application is terminated with signal. So, if you want to cleanup after receiving SIGINT and similar signals, you also need to setup signal handlers properly.

3 Comments

ok, but the file still remais there. I also need to delete the file from the folder
@Pheonix7, have you checked exit code of unlink for errors?
when i exit the application with ctr+c the terminal doesnt say anything. When i restart the application i get a bind error: Address in use
4

You can check if socket if active by looking at /proc/net/unix. Then delete, ether in a cron, or before you start/restart your application.

echo "active sockets in /path/"
cat /proc/net/unix | grep -Eo '/path/.*'

echo "all sockets in path including stale sockets"
ls -1F /path/ | egrep '=$' | sed 's/=$//'

echo "example command to remove stale sockets"
comm -1 -3 <(cat /proc/net/unix | grep -Eo '/path/.*' | sort)  <(ls -1F /path/ | egrep '=$' | sed 's/=$//' | sort) | xargs -n1 echo rm

Comments

2

If you are using Linux, you can use an abstract namespace socket: Set the first byte in the sockaddr_un.sun_path to \0, and with bytes still in this array. An abstract socket is not placed on the filesystem, which has mild side effects.

References

  1. https://gavv.net/articles/unix-socket-reuse/
  2. man 7 unix

Abstract sockets

Socket permissions have no meaning for abstract sockets: the process umask(2) has no effect when binding an abstract socket, and changing the ownership and permissions of the object (via fchown(2) and fchmod(2)) has no effect on the accessibility of the socket.

Abstract sockets automatically disappear when all open references to the socket are closed.

The abstract socket namespace is a nonportable Linux extension.

Comments

1

Just execute

unlink("Path/to/Socket");

before you exit your program.

1 Comment

but i exit my application from the terminal, so where would i put the unlink in the code?
1

You can remove the socket file with a simple:

unlink(path);

Wherever you program exit your program, check if it exists, and remove it.

2 Comments

but i exit my application from the terminal, so where would i put the unlink in the code?
It doesn't matter where you exit your application from, unless you're saying that you're hitting Control-C or something. In that case, you need to create a signal handler that takes care of the socket before exiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.