Improve instance detection
Fix -i option
This commit is contained in:
15
main.cpp
15
main.cpp
@ -116,7 +116,17 @@ bool createPidFile(const std::string& fileName)
|
||||
if(std::filesystem::exists(fileName))
|
||||
{
|
||||
std::cerr<<fileName<<" pid file exsists, only one instance may run at once\n";
|
||||
return false;
|
||||
std::string sigstopedName = Process(getpid()).getName();
|
||||
if(Process::byName(sigstopedName).size() > 1) return false;
|
||||
else
|
||||
{
|
||||
std::cerr<<"Only one "
|
||||
<<sigstopedName
|
||||
<<" process exists, either sigstoped died or you have severl diferently named binarys\n";
|
||||
|
||||
std::filesystem::remove(fileName);
|
||||
return createPidFile(fileName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -150,7 +160,7 @@ int main(int argc, char* argv[])
|
||||
std::string confDir = getConfdir();
|
||||
if(confDir.size() == 0) return 1;
|
||||
|
||||
if(!createPidFile(confDir+"pidfile")) return 1;
|
||||
if(!createPidFile(confDir+"pidfile"));
|
||||
|
||||
std::vector<std::string> applicationNames = getApplicationlist(confDir+"blacklist");
|
||||
|
||||
@ -185,6 +195,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
signal(SIGINT, sigTerm);
|
||||
signal(SIGTERM, sigTerm);
|
||||
signal(SIGHUP, sigTerm);
|
||||
signal(SIGUSR1, sigUser1);
|
||||
|
||||
while(!stop)
|
||||
|
12
process.cpp
12
process.cpp
@ -127,6 +127,18 @@ std::string Process::getName()
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::vector<Process> Process::byName(const std::string& name)
|
||||
{
|
||||
std::vector<pid_t> procs = getAllProcessPids();
|
||||
std::vector<Process> retProcs;
|
||||
for(const auto & pid : procs )
|
||||
{
|
||||
Process proc(pid);
|
||||
if(name == proc.getName()) retProcs.push_back(proc);
|
||||
}
|
||||
return retProcs;
|
||||
}
|
||||
|
||||
pid_t Process::convertPid(const std::string& pid)
|
||||
{
|
||||
pid_t ret;
|
||||
|
@ -32,7 +32,7 @@ private:
|
||||
|
||||
private:
|
||||
std::vector<std::string> openStatus();
|
||||
std::vector<pid_t> getAllProcessPids();
|
||||
static std::vector<pid_t> getAllProcessPids();
|
||||
static pid_t convertPid(const std::string& pid);
|
||||
|
||||
public:
|
||||
@ -47,6 +47,7 @@ public:
|
||||
pid_t getPPid();
|
||||
Process getParent(){return Process(getPPid());}
|
||||
std::vector<Process> getChildren();
|
||||
static std::vector<Process> byName(const std::string& name);
|
||||
Process(){}
|
||||
Process(pid_t pidIn);
|
||||
};
|
||||
|
@ -132,9 +132,9 @@ pid_t XInstance::getPid(Window wid)
|
||||
char errorString[1024];
|
||||
XGetErrorText(display, ret, errorString, 1024);
|
||||
debug("XGetWMClientMachine failed! " + std::string(errorString));
|
||||
return -1;
|
||||
if(!ignoreClientMachine) return -1;
|
||||
}
|
||||
char** xWidHostNameStringList;
|
||||
char** xWidHostNameStringList = nullptr;
|
||||
int nStrings;
|
||||
ret = XTextPropertyToStringList(&xWidHostNameTextProperty, &xWidHostNameStringList, &nStrings);
|
||||
if (!ret || nStrings == 0)
|
||||
@ -142,16 +142,16 @@ pid_t XInstance::getPid(Window wid)
|
||||
char errorString[1024];
|
||||
XGetErrorText(display, ret, errorString, 1024);
|
||||
debug("XTextPropertyToStringList failed! " + std::string(errorString));
|
||||
return -1;
|
||||
if(!ignoreClientMachine) return -1;
|
||||
}
|
||||
char hostName[HOST_NAME_MAX+1]={0};
|
||||
if(gethostname(hostName, HOST_NAME_MAX) != 0)
|
||||
{
|
||||
debug("Can't get host name");
|
||||
return -1;
|
||||
if(!ignoreClientMachine) return -1;
|
||||
}
|
||||
pid_t pid = -1;
|
||||
if(strcmp(hostName, xWidHostNameStringList[0]) == 0 || ignoreClientMachine)
|
||||
if(ignoreClientMachine || strcmp(hostName, xWidHostNameStringList[0]) == 0 )
|
||||
{
|
||||
unsigned char* data = nullptr;
|
||||
int format;
|
||||
@ -164,7 +164,7 @@ pid_t XInstance::getPid(Window wid)
|
||||
{
|
||||
debug("Window "+std::to_string(wid)+" is a remote window");
|
||||
}
|
||||
XFreeStringList(xWidHostNameStringList);
|
||||
if(xWidHostNameStringList) XFreeStringList(xWidHostNameStringList);
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user