Improve process-already-running detection
Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
parent
b6fe3379b0
commit
62d20a97a7
|
@ -59,6 +59,7 @@ int main (int argc, char* argv[])
|
||||||
if(!init_shmem(true))
|
if(!init_shmem(true))
|
||||||
{
|
{
|
||||||
logg("Initialization of shared memory failed.");
|
logg("Initialization of shared memory failed.");
|
||||||
|
// Check if there is already a running FTL process
|
||||||
check_running_FTL();
|
check_running_FTL();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
46
src/procps.c
46
src/procps.c
|
@ -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)
|
static bool get_process_ppid(const pid_t pid, pid_t *ppid)
|
||||||
{
|
{
|
||||||
// Try to open status file
|
// 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);
|
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
|
||||||
FILE *f = fopen(filename, "r");
|
FILE *f = fopen(filename, "r");
|
||||||
if(f == NULL)
|
if(f == NULL)
|
||||||
|
@ -75,22 +75,25 @@ static bool get_process_creation_time(const pid_t pid, char timestr[84])
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void check_running_FTL(void)
|
bool check_running_FTL(void)
|
||||||
{
|
{
|
||||||
//pid_t pid;
|
|
||||||
DIR *dirPos;
|
DIR *dirPos;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
|
const pid_t ourselves = getpid();
|
||||||
|
bool process_running = false;
|
||||||
|
|
||||||
// Open /proc
|
// Open /proc
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if ((dirPos = opendir("/proc")) == NULL)
|
if ((dirPos = opendir("/proc")) == NULL)
|
||||||
{
|
{
|
||||||
logg("Dailed to access /proc: %s", strerror(errno));
|
logg("Failed to access /proc: %s", strerror(errno));
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop over entries in /proc
|
// Loop over entries in /proc
|
||||||
// This is much more efficient than iterating over all possible PIDs
|
// 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)
|
while ((entry = readdir(dirPos)) != NULL)
|
||||||
{
|
{
|
||||||
// We are only interested in subdirectories of /proc
|
// 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);
|
const pid_t pid = strtol(entry->d_name, NULL, 10);
|
||||||
|
|
||||||
// Skip our own process
|
// Skip our own process
|
||||||
if(pid == getpid())
|
if(pid == ourselves)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Get process name
|
// Get process name
|
||||||
|
@ -112,6 +115,10 @@ void check_running_FTL(void)
|
||||||
if(!get_process_name(pid, name))
|
if(!get_process_name(pid, name))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// Only process this is this is our own process
|
||||||
|
if(strcasecmp(name, PROCESS_NAME) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Get parent process ID (PPID)
|
// Get parent process ID (PPID)
|
||||||
pid_t ppid;
|
pid_t ppid;
|
||||||
if(!get_process_ppid(pid, &ppid))
|
if(!get_process_ppid(pid, &ppid))
|
||||||
|
@ -123,11 +130,30 @@ void check_running_FTL(void)
|
||||||
char timestr[84] = { 0 };
|
char timestr[84] = { 0 };
|
||||||
get_process_creation_time(pid, timestr);
|
get_process_creation_time(pid, timestr);
|
||||||
|
|
||||||
// Log this process if it is a duplicate of us
|
// If this is the first process we log, add a header
|
||||||
if(strcasecmp(name, PROCESS_NAME) == 0)
|
if(!process_running)
|
||||||
logg("---> %s is already running as PID %d (started %s, child of PID %i (%s))",
|
{
|
||||||
PROCESS_NAME, pid, timestr, ppid, ppid_name);
|
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);
|
closedir(dirPos);
|
||||||
|
return process_running;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,6 @@
|
||||||
|
|
||||||
#ifndef PROCPS_H
|
#ifndef PROCPS_H
|
||||||
#define PROCPS_H
|
#define PROCPS_H
|
||||||
void check_running_FTL(void);
|
bool check_running_FTL(void);
|
||||||
|
|
||||||
#endif // POCPS_H
|
#endif // POCPS_H
|
23
src/shmem.c
23
src/shmem.c
|
@ -211,7 +211,7 @@ bool strcmp_escaped(const char *a, const char *b)
|
||||||
free(aa);
|
free(aa);
|
||||||
if(Nb > 0)
|
if(Nb > 0)
|
||||||
free(bb);
|
free(bb);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,7 +426,6 @@ bool init_shmem(bool create_new)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/****************************** shared strings buffer ******************************/
|
/****************************** shared strings buffer ******************************/
|
||||||
// Try to create shared memory object
|
// Try to create shared memory object
|
||||||
|
@ -514,7 +513,8 @@ bool init_shmem(bool create_new)
|
||||||
|
|
||||||
void destroy_shmem(void)
|
void destroy_shmem(void)
|
||||||
{
|
{
|
||||||
pthread_mutex_destroy(&shmLock->lock);
|
if(shmLock != NULL)
|
||||||
|
pthread_mutex_destroy(&shmLock->lock);
|
||||||
shmLock = NULL;
|
shmLock = NULL;
|
||||||
|
|
||||||
delete_shm(&shm_lock);
|
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)
|
void delete_shm(SharedMemory *sharedMemory)
|
||||||
{
|
{
|
||||||
// Unmap shared memory
|
// Unmap shared memory (if mmapped)
|
||||||
int ret = munmap(sharedMemory->ptr, sharedMemory->size);
|
if(sharedMemory->ptr != NULL)
|
||||||
if(ret != 0)
|
{
|
||||||
logg("delete_shm(): munmap(%p, %zu) failed: %s", sharedMemory->ptr, sharedMemory->size, strerror(errno));
|
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,
|
// Now you can no longer `shm_open` the memory, and once all others
|
||||||
// and once all others unlink, it will be destroyed.
|
// unlink, it will be destroyed.
|
||||||
ret = shm_unlink(sharedMemory->name);
|
if(shm_unlink(sharedMemory->name) != 0)
|
||||||
if(ret != 0)
|
|
||||||
logg("delete_shm(): shm_unlink(%s) failed: %s", sharedMemory->name, strerror(errno));
|
logg("delete_shm(): shm_unlink(%s) failed: %s", sharedMemory->name, strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,6 @@
|
||||||
#undef vsnprintf
|
#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)
|
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
|
// Print into dynamically allocated memory
|
||||||
int _errno, length = 0;
|
int _errno, length = 0;
|
||||||
do
|
do
|
||||||
|
|
Loading…
Reference in New Issue