Determine if only one instance is running: Difference between revisions

Content added Content deleted
(Check SocketOSException type)
(Fix formatting)
Line 374: Line 374:
<lang d>import std.socket;
<lang d>import std.socket;


import std.socket;
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");
try {
try
{
socket.bind(addr);
socket.bind(addr);
return true;
return true;
}
} catch (SocketOSException e) {
catch (SocketOSException e)
{
import core.stdc.errno : EADDRINUSE;
import core.stdc.errno : EADDRINUSE;


if (e.errorCode == EADDRINUSE) {
if (e.errorCode == EADDRINUSE)
{
log("Duplicate instance detected.");
log("Duplicate instance detected.");
return false;
return false;
}
}
else {
else
throw e;
{
}
throw e;
}
}
}
}
}