Pheonix7
Pheonix7

Reputation: 2191

How can I delete a UNIX Domain Socket file when I exit my application?

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.

Upvotes: 25

Views: 35441

Answers (6)

bgStack15
bgStack15

Reputation: 200

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.

Upvotes: 1

Jeff Peters
Jeff Peters

Reputation: 126

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

Upvotes: 4

user2404501
user2404501

Reputation:

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)

Upvotes: 33

gudok
gudok

Reputation: 4179

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.

Upvotes: 6

Will
Will

Reputation: 24699

You can remove the socket file with a simple:

unlink(path);

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

Upvotes: 1

Dominic Gibson
Dominic Gibson

Reputation: 67

Just execute

unlink("Path/to/Socket");

before you exit your program.

Upvotes: 1

Related Questions