Skip to main content
change confusing statement -- someone may assume that a fstat on socket fd will return info about the inode it's bound to
Source Link
user313992
user313992

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/old_sock &
[1] 19241
ln -f /tmp/old_sock /tmp/new_sock; rm /tmp/old_sock
     # or mv /tmp/old_sock /tmp/new_sock 
echo yup | nc -U /tmp/new_sock
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux, kqueue(2) on BSD, or simply do a fstatstat(2) periodically and checkon the st_nlink valuepath (just like with any other file).

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights and cannot be "removed" or moved to a different address.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/old_sock &
[1] 19241
ln -f /tmp/old_sock /tmp/new_sock; rm /tmp/old_sock
     # or mv /tmp/old_sock /tmp/new_sock 
echo yup | nc -U /tmp/new_sock
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux, kqueue(2) on BSD, or simply do a fstat(2) periodically and check the st_nlink value (just like with any other file).

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights and cannot be "removed" or moved to a different address.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/old_sock &
[1] 19241
ln -f /tmp/old_sock /tmp/new_sock; rm /tmp/old_sock
     # or mv /tmp/old_sock /tmp/new_sock 
echo yup | nc -U /tmp/new_sock
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux, kqueue(2) on BSD, or simply do a stat(2) periodically on the path (just like with any other file).

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights and cannot be "removed" or moved to a different address.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

kqueue and fstat info
Source Link
user313992
user313992

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/sock1old_sock &
[1] 19241
ln -f /tmp/sock1old_sock /tmp/sock2;new_sock; rm /tmp/sock1old_sock
     # or mv /tmp/old_sock /tmp/new_sock 
echo yup | nc -U /tmp/sock2new_sock
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux, kqueue(2) on BSD, or simply do a fstat(2) periodically and check the st_nlink value (just like with any other file).

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights and cannot be "removed" or moved to a different address.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/sock1 &
[1] 19241
ln /tmp/sock1 /tmp/sock2; rm /tmp/sock1
echo yup | nc -U /tmp/sock2
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux.

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/old_sock &
[1] 19241
ln -f /tmp/old_sock /tmp/new_sock; rm /tmp/old_sock
     # or mv /tmp/old_sock /tmp/new_sock 
echo yup | nc -U /tmp/new_sock
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux, kqueue(2) on BSD, or simply do a fstat(2) periodically and check the st_nlink value (just like with any other file).

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights and cannot be "removed" or moved to a different address.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

clarify + note for the trivial interpretation of the question
Source Link
user313992
user313992

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the processsocket that createdwas bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/sock1 &
[1] 19241
ln /tmp/sock1 /tmp/sock2; rm /tmp/sock1
echo yup | nc -U /tmp/sock2
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux.

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the process that created it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/sock1 &
[1] 19241
ln /tmp/sock1 /tmp/sock2; rm /tmp/sock1
echo yup | nc -U /tmp/sock2
yup

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

You cannot re-create a Unix socket file that was deleted.

In fact, you can create another socket file with the same path (by trying to bind() the socket file descriptor again to the same address), but any client trying to connect to it will get a "Connection refused" instead of connecting to the socket that was bound to it in the first place.

That's because Unix sockets are basically bound to inodes, not to paths. You can easily check that if your netcat supports Unix sockets:

nc -lU /tmp/sock1 &
[1] 19241
ln /tmp/sock1 /tmp/sock2; rm /tmp/sock1
echo yup | nc -U /tmp/sock2
yup

All your program can do is to close the old socket, create another and bind it to the same address. In order to be notified of the removal of a file, you can use inotify(7) on Linux.

Notice that Linux also has "abstract" Unix sockets -- sockets which are bound to a sequence of bytes[1] instead of any file-system object, and which are not subject to any file-style access rights.

[1] you can call it a "string", but beware that it can also contain NUL bytes, unlike a file-system path.

clarify note about abstract sockets
Source Link
user313992
user313992
Loading
added 212 characters in body
Source Link
user313992
user313992
Loading
Source Link
user313992
user313992
Loading