Improve process-already-running detection

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2021-02-17 12:45:44 +01:00
parent b6fe3379b0
commit 62d20a97a7
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
5 changed files with 50 additions and 29 deletions

View File

@ -59,6 +59,7 @@ int main (int argc, char* argv[])
if(!init_shmem(true))
{
logg("Initialization of shared memory failed.");
// Check if there is already a running FTL process
check_running_FTL();
return EXIT_FAILURE;
}

View File

@ -44,7 +44,7 @@ static bool get_process_name(const pid_t pid, char name[128])
static bool get_process_ppid(const pid_t pid, pid_t *ppid)
{
// Try to open status file
char filename[sizeof("/proc/%u/task/%u/comm") + sizeof(int)*3 * 2];
char filename[sizeof("/proc/%u/task/%u/status") + sizeof(int)*3 * 2];
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
FILE *f = fopen(filename, "r");
if(f == NULL)
@ -75,22 +75,25 @@ static bool get_process_creation_time(const pid_t pid, char timestr[84])
return true;
}
void check_running_FTL(void)
bool check_running_FTL(void)
{
//pid_t pid;
DIR *dirPos;
struct dirent *entry;
const pid_t ourselves = getpid();
bool process_running = false;
// Open /proc
errno = 0;
if ((dirPos = opendir("/proc")) == NULL)
{
logg("Dailed to access /proc: %s", strerror(errno));
return;
logg("Failed to access /proc: %s", strerror(errno));
return false;
}
// Loop over entries in /proc
// This is much more efficient than iterating over all possible PIDs
pid_t last_pid = 0;
size_t last_len = 0u;
while ((entry = readdir(dirPos)) != NULL)
{
// We are only interested in subdirectories of /proc
@ -104,7 +107,7 @@ void check_running_FTL(void)
const pid_t pid = strtol(entry->d_name, NULL, 10);
// Skip our own process
if(pid == getpid())
if(pid == ourselves)
continue;
// Get process name
@ -112,6 +115,10 @@ void check_running_FTL(void)
if(!get_process_name(pid, name))
continue;
// Only process this is this is our own process
if(strcasecmp(name, PROCESS_NAME) != 0)
continue;
// Get parent process ID (PPID)
pid_t ppid;
if(!get_process_ppid(pid, &ppid))
@ -123,11 +130,30 @@ void check_running_FTL(void)
char timestr[84] = { 0 };
get_process_creation_time(pid, timestr);
// Log this process if it is a duplicate of us
if(strcasecmp(name, PROCESS_NAME) == 0)
logg("---> %s is already running as PID %d (started %s, child of PID %i (%s))",
PROCESS_NAME, pid, timestr, ppid, ppid_name);
// If this is the first process we log, add a header
if(!process_running)
{
process_running = true;
logg("HINT: %s is already running!", PROCESS_NAME);
}
if(last_pid != ppid)
{
// Independent process, may be child of init/systemd
logg("%s (%d) ──> %s (PID %d, started %s)",
ppid_name, ppid, name, pid, timestr);
last_pid = pid;
last_len = snprintf(NULL, 0, "%s (%d) ──> ", ppid_name, ppid);
}
else
{
// Process parented by the one we analyzed before,
// highlight their relationship
logg("%*s └─> %s (PID %d, started %s)",
(int)last_len, "", name, pid, timestr);
}
}
closedir(dirPos);
return process_running;
}

View File

@ -10,6 +10,6 @@
#ifndef PROCPS_H
#define PROCPS_H
void check_running_FTL(void);
bool check_running_FTL(void);
#endif // POCPS_H

View File

@ -211,7 +211,7 @@ bool strcmp_escaped(const char *a, const char *b)
free(aa);
if(Nb > 0)
free(bb);
return result;
}
@ -426,7 +426,6 @@ bool init_shmem(bool create_new)
return false;
}
}
/****************************** shared strings buffer ******************************/
// Try to create shared memory object
@ -514,7 +513,8 @@ bool init_shmem(bool create_new)
void destroy_shmem(void)
{
pthread_mutex_destroy(&shmLock->lock);
if(shmLock != NULL)
pthread_mutex_destroy(&shmLock->lock);
shmLock = NULL;
delete_shm(&shm_lock);
@ -742,15 +742,16 @@ bool realloc_shm(SharedMemory *sharedMemory, const size_t size1, const size_t si
void delete_shm(SharedMemory *sharedMemory)
{
// Unmap shared memory
int ret = munmap(sharedMemory->ptr, sharedMemory->size);
if(ret != 0)
logg("delete_shm(): munmap(%p, %zu) failed: %s", sharedMemory->ptr, sharedMemory->size, strerror(errno));
// Unmap shared memory (if mmapped)
if(sharedMemory->ptr != NULL)
{
if(munmap(sharedMemory->ptr, sharedMemory->size) != 0)
logg("delete_shm(): munmap(%p, %zu) failed: %s", sharedMemory->ptr, sharedMemory->size, strerror(errno));
}
// Now you can no longer `shm_open` the memory,
// and once all others unlink, it will be destroyed.
ret = shm_unlink(sharedMemory->name);
if(ret != 0)
// Now you can no longer `shm_open` the memory, and once all others
// unlink, it will be destroyed.
if(shm_unlink(sharedMemory->name) != 0)
logg("delete_shm(): shm_unlink(%s) failed: %s", sharedMemory->name, strerror(errno));
}

View File

@ -15,13 +15,6 @@
#undef vsnprintf
int FTLvsnprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const size_t maxlen, const char *format, va_list args)
{
// Sanity check
if(buffer == NULL)
{
syscalls_report_error("vsnprintf() called with NULL buffer",
stdout, 0, format, func, file, line);
return 0;
}
// Print into dynamically allocated memory
int _errno, length = 0;
do