Detect unix domain socket deletion?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















Assume that you have an application that upon startup creates a unix domain socket and starts listening to it.
Then any subsequent instance of the application connects to that socket, passes data to the 1st instance and exits (or just exits if it detects another instance running).



As far as I understand it, this kind of socket is represented as a file on the filesystem. Let's assume that this file gets deleted by a random 3rd party program or by the user.
Now subsequent processes won't be able to detect/communicate with the 1st instance.

Can the 1st somehow detect the deletion of the socket-file and then recreate it?










share|improve this question




























    1















    Assume that you have an application that upon startup creates a unix domain socket and starts listening to it.
    Then any subsequent instance of the application connects to that socket, passes data to the 1st instance and exits (or just exits if it detects another instance running).



    As far as I understand it, this kind of socket is represented as a file on the filesystem. Let's assume that this file gets deleted by a random 3rd party program or by the user.
    Now subsequent processes won't be able to detect/communicate with the 1st instance.

    Can the 1st somehow detect the deletion of the socket-file and then recreate it?










    share|improve this question


























      1












      1








      1








      Assume that you have an application that upon startup creates a unix domain socket and starts listening to it.
      Then any subsequent instance of the application connects to that socket, passes data to the 1st instance and exits (or just exits if it detects another instance running).



      As far as I understand it, this kind of socket is represented as a file on the filesystem. Let's assume that this file gets deleted by a random 3rd party program or by the user.
      Now subsequent processes won't be able to detect/communicate with the 1st instance.

      Can the 1st somehow detect the deletion of the socket-file and then recreate it?










      share|improve this question
















      Assume that you have an application that upon startup creates a unix domain socket and starts listening to it.
      Then any subsequent instance of the application connects to that socket, passes data to the 1st instance and exits (or just exits if it detects another instance running).



      As far as I understand it, this kind of socket is represented as a file on the filesystem. Let's assume that this file gets deleted by a random 3rd party program or by the user.
      Now subsequent processes won't be able to detect/communicate with the 1st instance.

      Can the 1st somehow detect the deletion of the socket-file and then recreate it?







      unix-sockets






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 3 at 23:26







      user10260481

















      asked Feb 3 at 8:10









      user10260481user10260481

      162




      162




















          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer

























          • Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

            – user10260481
            Feb 3 at 20:58











          • The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

            – Uncle Billy
            Feb 4 at 1:21











          • If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

            – Uncle Billy
            Feb 4 at 1:30











          • I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

            – user10260481
            Feb 6 at 23:27











          • That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

            – Uncle Billy
            Feb 7 at 3:34










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498395%2fdetect-unix-domain-socket-deletion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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.






          share|improve this answer

























          • Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

            – user10260481
            Feb 3 at 20:58











          • The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

            – Uncle Billy
            Feb 4 at 1:21











          • If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

            – Uncle Billy
            Feb 4 at 1:30











          • I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

            – user10260481
            Feb 6 at 23:27











          • That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

            – Uncle Billy
            Feb 7 at 3:34















          1














          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.






          share|improve this answer

























          • Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

            – user10260481
            Feb 3 at 20:58











          • The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

            – Uncle Billy
            Feb 4 at 1:21











          • If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

            – Uncle Billy
            Feb 4 at 1:30











          • I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

            – user10260481
            Feb 6 at 23:27











          • That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

            – Uncle Billy
            Feb 7 at 3:34













          1












          1








          1







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 7 at 7:56

























          answered Feb 3 at 9:35









          Uncle BillyUncle Billy

          5915




          5915












          • Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

            – user10260481
            Feb 3 at 20:58











          • The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

            – Uncle Billy
            Feb 4 at 1:21











          • If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

            – Uncle Billy
            Feb 4 at 1:30











          • I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

            – user10260481
            Feb 6 at 23:27











          • That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

            – Uncle Billy
            Feb 7 at 3:34

















          • Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

            – user10260481
            Feb 3 at 20:58











          • The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

            – Uncle Billy
            Feb 4 at 1:21











          • If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

            – Uncle Billy
            Feb 4 at 1:30











          • I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

            – user10260481
            Feb 6 at 23:27











          • That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

            – Uncle Billy
            Feb 7 at 3:34
















          Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

          – user10260481
          Feb 3 at 20:58





          Ofc, I meant recreate the socket with the same path. Clients will see it after it is recreated since it has the same path, right? (assuming the clients are run after the re-creation of the socket)

          – user10260481
          Feb 3 at 20:58













          The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

          – Uncle Billy
          Feb 4 at 1:21





          The only way to create a socket file is by bind()ing an AF_UNIX socket file descriptor to it, and you can only do that a single time. If someone deletes your socket file, you'll have to close the old socket and redo the whole socket/bind/listen/accept loop again. If someone removes the /tmp/.X11-unix/X0 file, you'll have to kill and restart the X11 server, you cannot simply re-create it with a different inode and expect the X11 server to listen on it. Please re-read my whole answer, I've explained why.

          – Uncle Billy
          Feb 4 at 1:21













          If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

          – Uncle Billy
          Feb 4 at 1:30





          If you're going to test that on Linux, notice that new X11 servers and apps are using abstract sockets in preference, which cannot be removed or moved to a different address, as file-based sockets can.

          – Uncle Billy
          Feb 4 at 1:30













          I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

          – user10260481
          Feb 6 at 23:27





          I only meant that, once deletion has be detected, the listener will destroy the current socket and create a new one using the same name. This will re-create the file on disk and new instances will be able to find it and communicate back to the listener.

          – user10260481
          Feb 6 at 23:27













          That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

          – Uncle Billy
          Feb 7 at 3:34





          That's no different from any other file, and frankly, in that case I don't see the point of your question: that 3rd party could just kill your server instead of removing its socket (if it can do the latter it can also do the former). If you want an example on how to use inotify/kqueue/fstat from my 4th paragraph and cannot find any example, please ask a question on stackoverflow.

          – Uncle Billy
          Feb 7 at 3:34

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498395%2fdetect-unix-domain-socket-deletion%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown






          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?