diff options
author | Misael Lopez Cruz <misael.lopez@ti.com> | 2015-12-02 18:29:11 -0600 |
---|---|---|
committer | Angela Stegmaier <angelabaker@ti.com> | 2016-01-05 12:59:26 -0600 |
commit | 15b57d952f72092c83e00dbf232afaf44ae8387b (patch) | |
tree | 8b75511805d650b638435a6052c69f417ed87100 | |
parent | 9e48f0b4e0c41b7f2916d3ca9e930f919c2c5762 (diff) | |
download | ipc-15b57d952f72092c83e00dbf232afaf44ae8387b.tar.gz |
lad: Allow only one instance of the daemon
Make sure only one instance of the LAD daemon is running
at a time. This implementation is based on syslink's
daemon.
Signed-off-by: Misael Lopez Cruz <misael.lopez@ti.com>
-rw-r--r-- | linux/src/daemon/lad.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/linux/src/daemon/lad.c b/linux/src/daemon/lad.c index 1c7c707..c18e3dc 100644 --- a/linux/src/daemon/lad.c +++ b/linux/src/daemon/lad.c @@ -35,6 +35,7 @@ #include <ti/ipc/Std.h> +#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -62,6 +63,8 @@ #define DAEMON 1 /* 1 = run as a daemon; 0 = run as app */ +#define READ_BUF_SIZE 50 + Bool logFile = FALSE; FILE *logPtr = NULL; struct timeval start_tv; @@ -80,6 +83,7 @@ static Char clientFIFOName[LAD_MAXNUMCLIENTS][LAD_MAXLENGTHFIFONAME]; static FILE * responseFIFOFilePtr[LAD_MAXNUMCLIENTS]; /* local internal routines */ +static Bool isDaemonRunning(Char *pidName); static LAD_ClientHandle assignClientId(Void); static Void cleanupFifos(Void); static Void cleanupDepartedClients(Void); @@ -147,6 +151,11 @@ int main(int argc, char * argv[]) } } + if (isDaemonRunning(argv[0])) { + printf("Multiple instances of LAD are not supported!\n"); + exit(EXIT_FAILURE); + } + /* change to LAD's working directory */ if ((chdir(LAD_WORKINGDIR)) < 0) { @@ -867,6 +876,74 @@ exitNow: /* + * ======== isDaemonRunning ======== + */ +static Bool isDaemonRunning(Char *pidName) +{ + DIR *dir; + pid_t pid; + Int dirNum; + FILE *fp; + struct dirent * next; + Bool isRunning = FALSE; + Char filename [READ_BUF_SIZE]; + Char buffer [READ_BUF_SIZE]; + Char *bptr = buffer; + Char *name; + + pid = getpid(); + dir = opendir("/proc"); + if (!dir) { + printf("Warning: Cannot open /proc filesystem\n"); + return isRunning; + } + + name = strrchr(pidName, '/'); + if (name) { + pidName = (name + 1); + } + + while ((next = readdir(dir)) != NULL) { + /* If it isn't a number, we don't want it */ + if (!isdigit(*next->d_name)) { + continue; + } + + dirNum = strtol(next->d_name, NULL, 10); + if (dirNum == pid) { + continue; + } + + snprintf(filename, READ_BUF_SIZE, "/proc/%s/cmdline", next->d_name); + if (!(fp = fopen(filename, "r"))) { + continue; + } + if (fgets(buffer, READ_BUF_SIZE, fp) == NULL) { + fclose(fp); + continue; + } + fclose (fp); + + name = strrchr(buffer, '/'); + if (name && (name + 1)) { + bptr = name + 1; + } + else { + bptr = buffer; + } + + /* Buffer should contain the entire command line */ + if (strcmp(bptr, pidName) == 0) { + isRunning = TRUE; + break; + } + } + closedir (dir); + + return isRunning; +} + +/* * ======== assignClientId ======== */ static LAD_ClientHandle assignClientId(Void) |