Determine if only one instance is running: Difference between revisions
Content added Content deleted
(Remove needless logging) |
m (Remove needless import) |
||
Line 372: | Line 372: | ||
All we need is to generate a unique name for our program and pass it as the address when calling bind(). The trick is that instead of specifying a file path as the address, we pass a null byte followed by the name of our choosing (e.g. "\0my-unique-name"). The initial null byte is what distinguishes abstract socket names from conventional Unix domain socket path names, which consist of a string of one or more non-null bytes terminated by a null byte. |
All we need is to generate a unique name for our program and pass it as the address when calling bind(). The trick is that instead of specifying a file path as the address, we pass a null byte followed by the name of our choosing (e.g. "\0my-unique-name"). The initial null byte is what distinguishes abstract socket names from conventional Unix domain socket path names, which consist of a string of one or more non-null bytes terminated by a null byte. |
||
Read more here: [https://blog.petrzemek.net/2017/07/24/ensuring-that-a-linux-program-is-running-at-most-once-by-using-abstract-sockets/] |
Read more here: [https://blog.petrzemek.net/2017/07/24/ensuring-that-a-linux-program-is-running-at-most-once-by-using-abstract-sockets/] |
||
<lang d> |
<lang d> |
||
⚫ | |||
bool is_unique_instance() |
bool is_unique_instance() |
||
{ |
{ |
||
⚫ | |||
auto socket = new Socket(AddressFamily.UNIX, SocketType.STREAM); |
auto socket = new Socket(AddressFamily.UNIX, SocketType.STREAM); |
||
auto addr = new UnixAddress("\0/tmp/myapp.uniqueness.sock"); |
auto addr = new UnixAddress("\0/tmp/myapp.uniqueness.sock"); |