On Thu, Aug 24, 2023 at 01:00:02PM +0200, Reinhard Max wrote: > On Thu, 24 Aug 2023 at 10:09, Miroslav Lichvar wrote: > > > Is there a portable way to detect when a Unix socket is no longer > > referenced by anything? > > how about just trying to connect to the socket when the respective file > exists? If the connection succeeds the socket is in use and if it fails with > ECONNREFUSED it is unused and can be deleted and reused.
Ok, thanks. On Linux it seems to be failing with EPERM when the socket is already connected (as would be expected for libchrony applications). If the process doesn't have write permissions to the socket, it seems to be failing with EACCES no matter the socket is unused, only bound, or also connected. Normally I think there will be only a limited number of users with write access to /var/run/chrony, so I guess that's not a problem. On Solaris/illumos the socket permissions are ignored, so that might work better there. The race condition due to the clients connecting after binding doesn't seem to be a problem here. Is it reasonable to expect this split between ECONNREFUSED and success/EPERM/EACCESS will work on all POSIX systems? -- Miroslav Lichvar -- To unsubscribe email chrony-dev-requ...@chrony.tuxfamily.org with "unsubscribe" in the subject. For help email chrony-dev-requ...@chrony.tuxfamily.org with "help" in the subject. Trouble? Email listmas...@chrony.tuxfamily.org.