diff --git a/linux/src/daemon/lad.c b/linux/src/daemon/lad.c
index ca0b1ca12123d7756ebaab094dc9fadc9215c6cf..c18e3dc357502e2a96ddbfc1ac9d23eeb0b8511f 100644 (file)
--- a/linux/src/daemon/lad.c
+++ b/linux/src/daemon/lad.c
#include <ti/ipc/Std.h>
+#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
+#include <dirent.h>
#include <ti/ipc/MessageQ.h>
#include <_MessageQ.h>
#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;
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);
static Int connectToLAD(String clientName, Int pid, String clientProto, Int *clientIdPtr);
static Void disconnectFromLAD(Int clientId);
h : print this help message\n\
g : enable GateMP support \n\
l <logfile> : name of logfile for LAD\n\
+ n <nprocs> : total number of processors in the system\n\
p <oct value>: set LAD's directory permissions\n\
+ r <nreserve> : number of reserved queues\n\
+ s <synctype> : type of Ipc_ProcSync (ALL|PAIR|NONE)\n\
b <value> : Processor's base cluster id \n\
\n\
Examples:\n\
int main(int argc, char * argv[])
{
MessageQ_Handle handle;
+ Ipc_Config ipcCfg;
UInt16 *procIdPtr;
Int statusIO;
Int clientId;
Int c;
#if defined(GATEMP_SUPPORT)
Int status;
+ UInt16 procId;
#endif
String tmpString;
#if DAEMON
if ((chdir(LAD_ROOTDIR)) < 0) {
/* if can't change directory assume it needs to be created, do it */
if ((mkdir(LAD_ROOTDIR, 0666)) < 0) {
- printf("\nERROR: Failed to create LAD's root directory!\n");
+ fprintf(stderr,
+ "\nERROR: Failed to create LAD's root directory!\n");
exit(EXIT_FAILURE);
}
}
+ 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) {
/* if can't change directory assume it needs to be created, do it */
if ((mkdir(LAD_WORKINGDIR, 0666)) < 0) {
- printf("\nERROR: Failed to create LAD's working directory!\n");
+ fprintf(stderr,
+ "\nERROR: Failed to create LAD's working directory!\n");
exit(EXIT_FAILURE);
}
/* now change to the new directory */
if ((chdir(LAD_WORKINGDIR)) < 0) {
- printf("\nERROR: Failed to change to LAD's working directory!\n");
+ fprintf(stderr,
+ "\nERROR: Failed to change to LAD's working directory!\n");
exit(EXIT_FAILURE);
}
+ } else {
+ cleanupFifos();
}
/* process command line args */
while (1) {
- c = getopt(argc, argv, "b:ghl:p:");
+ c = getopt(argc, argv, "b:ghl:n:p:r:s:");
if (c == -1) {
break;
}
#endif
break;
case 'b':
- if (_MultiProc_cfg.id == 0xFFFF &&
- _MultiProc_cfg.baseIdOfCluster == 0xFFFF) {
- printf("\nSetting base cluster id to %s\n", optarg);
- _MultiProc_cfg.id = atoi(optarg);
- _MultiProc_cfg.baseIdOfCluster = atoi(optarg);
- }
- else {
- printf("\nBase cluster id in the MultiProcCfg file must be\n"
- "set to MultiProc_INVALIDID(0xFFFF) when using -b option\n");
- exit(EXIT_FAILURE);
- }
+ printf("\nSet LAD's base cluster id to %s\n", optarg);
+ _MultiProc_cfg.id = atoi(optarg);
+ _MultiProc_cfg.baseIdOfCluster = atoi(optarg);
break;
case 'h':
printf("%s", LAD_USAGE);
case 'l':
logPtr = fopen(optarg, "w");
if (logPtr == NULL) {
- printf("\nERROR: unable to open log file %s\n", optarg);
+ fprintf(stderr, "\nERROR: unable to open log file %s\n",
+ optarg);
exit(EXIT_FAILURE);
}
else {
}
}
break;
+ case 'n':
+ printf("\nSet LAD's number of processors to %s\n", optarg);
+ _MultiProc_cfg.numProcessors = atoi(optarg);
+ break;
case 'p':
printf("\nSet LAD's directory permissions to '%s'\n", optarg);
chmod(LAD_ROOTDIR, strtol(optarg, NULL, 8));
chmod(LAD_WORKINGDIR, strtol(optarg, NULL, 8));
break;
+ case 'r':
+ printf("\nSet LAD's number of reserved queues to %s\n", optarg);
+ _MessageQ_setNumReservedEntries(atoi(optarg));
+ break;
+ case 's':
+ printf("\nSet LAD's synchronization scheme to ProcSync_%s\n",
+ optarg);
+
+ Ipc_getConfig(&ipcCfg);
+
+ if (!strcmp(optarg, "ALL")) {
+ ipcCfg.procSync = Ipc_ProcSync_ALL;
+ }
+ else if (!strcmp(optarg, "PAIR")) {
+ ipcCfg.procSync = Ipc_ProcSync_PAIR;
+ }
+ else if (!strcmp(optarg, "NONE")) {
+ ipcCfg.procSync = Ipc_ProcSync_NONE;
+ }
+ else {
+ fprintf(stderr,
+ "Error: bad synchronization specified, must be "
+ "ALL|PAIR|NONE\n");
+ exit(EXIT_FAILURE);
+ }
+
+ Ipc_setConfig(&ipcCfg);
+ break;
default:
fprintf (stderr, "\nUnrecognized argument\n");
exit(EXIT_FAILURE);
}
/* Check to ensure id and baseId are not invalid */
- printf ("id = %d baseId= %d\n", _MultiProc_cfg.id, _MultiProc_cfg.baseIdOfCluster);
- if (_MultiProc_cfg.id == 0xFFFF || _MultiProc_cfg.baseIdOfCluster == 0xFFFF){
- printf("\nBase cluster id is set to an INVALID value\n");
- printf("Use -b option to set value or modify the MultiProcCfg file\n");
+ printf ("\nnumProcessors = %d id = %d baseId = %d\n",
+ _MultiProc_cfg.numProcessors, _MultiProc_cfg.id,
+ _MultiProc_cfg.baseIdOfCluster);
+
+ if (_MultiProc_cfg.id == 0xFFFF ||
+ _MultiProc_cfg.baseIdOfCluster == 0xFFFF) {
+ fprintf(stderr, "\nBase cluster id is set to an INVALID value\n");
+ fprintf(stderr,
+ "Use -b option to set value or modify the MultiProcCfg file\n"
+ );
exit(EXIT_FAILURE);
}
+ if ((_MultiProc_cfg.baseIdOfCluster + _MultiProc_cfg.numProcsInCluster) >
+ _MultiProc_cfg.numProcessors) {
+ fprintf(stderr,
+ "\nNumber of processors (%d) must be >= base cluster id + "
+ "number of processors in cluster (%d + %d)\n",
+ _MultiProc_cfg.numProcessors, _MultiProc_cfg.baseIdOfCluster,
+ _MultiProc_cfg.numProcsInCluster);
+ exit(EXIT_FAILURE);
+ }
#if DAEMON
/* if fork of child failed then exit immediately; no child created */
if (pid < 0) {
- printf("\nERROR: Failed to fork child process!");
+ fprintf(stderr, "\nERROR: Failed to fork child process!");
exit(EXIT_FAILURE);
}
/* exit with failure code if failed to get session ID... */
if (sid < 0) {
- printf("\nERROR: Failed to acquire new session ID!");
+ fprintf(stderr, "\nERROR: Failed to acquire new session ID!");
exit(EXIT_FAILURE);
}
break;
case LAD_NAMESERVER_DELETE:
- LOG1("LAD_NAMESERVER_DELETE: calling NameServer_delete(%p)...\n", cmd.args.delete.handle)
+ LOG1("LAD_NAMESERVER_DELETE: calling NameServer_delete(%p)...\n",
+ cmd.args.nsdelete.handle)
- rsp.delete.handle = cmd.args.delete.handle;
- rsp.delete.status = NameServer_delete(&rsp.delete.handle);
+ rsp.nsdelete.handle = cmd.args.nsdelete.handle;
+ rsp.nsdelete.status = NameServer_delete(&rsp.nsdelete.handle);
- LOG1(" status = %d\n", rsp.status)
+ LOG1(" status = %d\n", rsp.nsdelete.status)
LOG0("DONE\n")
break;
case LAD_NAMESERVER_GET:
LOG2("LAD_NAMESERVER_GET: calling NameServer_get(%p, '%s'",
- cmd.args.get.handle, cmd.args.get.name)
+ cmd.args.get.handle, cmd.args.get.name)
LOG0(")...\n")
if (cmd.args.get.procId[0] == (UInt16)-1) {
else {
procIdPtr = cmd.args.get.procId;
}
- rsp.status = NameServer_get(
+ rsp.get.status = NameServer_get(
cmd.args.get.handle,
cmd.args.get.name,
rsp.get.buf,
rsp.get.len = cmd.args.get.len;
LOG1(" value = 0x%x\n", rsp.get.len)
- LOG1(" status = %d\n", rsp.status)
+ LOG1(" status = %d\n", rsp.get.status)
LOG0("DONE\n")
break;
case LAD_NAMESERVER_ADDUINT32:
- LOG1("LAD_NAMESERVER_ADDUINT32: calling NameServer_addUInt32(%p, ", cmd.args.addUInt32.handle)
- LOG2("'%s', 0x%x)...\n", cmd.args.addUInt32.name, cmd.args.addUInt32.val)
+ LOG3("LAD_NAMESERVER_ADDUINT32: calling NameServer_addUInt32"
+ "(%p, '%s', 0x%x)...\n", cmd.args.addUInt32.handle,
+ cmd.args.addUInt32.name, cmd.args.addUInt32.val)
rsp.entryPtr = NameServer_addUInt32(
cmd.args.addUInt32.handle,
break;
case LAD_NAMESERVER_GETUINT32:
- LOG2("LAD_NAMESERVER_GETUINT32: calling NameServer_getUInt32(%p, '%s')...\n", cmd.args.getUInt32.handle, cmd.args.getUInt32.name)
+ LOG2("LAD_NAMESERVER_GETUINT32: calling NameServer_getUInt32"
+ "(%p, '%s')...\n", cmd.args.getUInt32.handle,
+ cmd.args.getUInt32.name)
if (cmd.args.getUInt32.procId[0] == (UInt16)-1) {
procIdPtr = NULL;
else {
procIdPtr = cmd.args.getUInt32.procId;
}
- rsp.status = NameServer_getUInt32(
+ rsp.getUInt32.status = NameServer_getUInt32(
cmd.args.getUInt32.handle,
cmd.args.getUInt32.name,
&rsp.getUInt32.val,
procIdPtr);
LOG1(" value = 0x%x\n", rsp.getUInt32.val)
- LOG1(" status = %d\n", rsp.status)
+ LOG1(" status = %d\n", rsp.getUInt32.status)
LOG0("DONE\n")
break;
+ case LAD_NAMESERVER_GETLOCAL:
+ rsp.get.status = NameServer_getLocal(cmd.args.getLocal.handle,
+ cmd.args.getLocal.name, &rsp.get.buf, &cmd.args.getLocal.len);
+ rsp.get.len = cmd.args.getLocal.len;
+ break;
+
+ case LAD_NAMESERVER_GETLOCALUINT32:
+ rsp.getUInt32.status = NameServer_getLocalUInt32(
+ cmd.args.getLocalUInt32.handle,
+ cmd.args.getLocalUInt32.name,
+ &rsp.getUInt32.val);
+ break;
+
case LAD_NAMESERVER_REMOVE:
LOG2("LAD_NAMESERVER_REMOVE: calling NameServer_remove(%p, '%s')...\n", cmd.args.remove.handle, cmd.args.remove.name)
break;
#if defined(GATEMP_SUPPORT)
+ case LAD_GATEMP_ATTACH:
+ procId = cmd.args.attach.procId;
+ LOG1("LAD_GATEMP_ATTACH: calling GateMP_attach(%d)...\n", procId)
+
+ rsp.status = GateMP_attach(procId);
+
+ LOG1(" status = %d\n", rsp.status)
+ LOG0("DONE\n")
+
+ break;
+
+ case LAD_GATEMP_DETACH:
+ procId = cmd.args.detach.procId;
+ LOG1("LAD_GATEMP_DETACH: calling GateMP_detach(%d)...\n", procId)
+
+ rsp.status = GateMP_detach(procId);
+
+ LOG1(" status = %d\n", rsp.status)
+ LOG0("DONE\n")
+
+ break;
+
case LAD_GATEMP_START:
LOG0("LAD_GATEMP_START: calling GateMP_start()...\n")
rsp.gateMPStart.nameServerHandle = GateMP_getNameServer();
- rsp.gateMPStart.status = GateMP_S_SUCCESS;;
+ rsp.gateMPStart.status = GateMP_S_SUCCESS;
LOG1(" status = %d\n", rsp.gateMPStart.status)
LOG0("DONE\n")
case LAD_NAMESERVER_GET:
case LAD_NAMESERVER_ADDUINT32:
case LAD_NAMESERVER_GETUINT32:
+ case LAD_NAMESERVER_GETLOCAL:
+ case LAD_NAMESERVER_GETLOCALUINT32:
case LAD_NAMESERVER_REMOVE:
case LAD_NAMESERVER_REMOVEENTRY:
case LAD_NAMESERVER_ATTACH:
case LAD_MESSAGEQ_MSGINIT:
case LAD_MULTIPROC_GETCONFIG:
#if defined(GATEMP_SUPPORT)
+ case LAD_GATEMP_ATTACH:
+ case LAD_GATEMP_DETACH:
case LAD_GATEMP_START:
case LAD_GATEMP_GETNUMRESOURCES:
case LAD_GATEMP_GETFREERESOURCE:
}
+/*
+ * ======== 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 ========
*/
return(clientId);
}
+/*
+ * ======== cleanupFifos ========
+ */
+static void cleanupFifos(Void)
+{
+ DIR *dir;
+ struct dirent entry;
+ struct dirent *result;
+ size_t dirnamelen;
+ size_t maxcopy;
+ Char pathname[PATH_MAX];
+ Char *namep;
+
+ if ((dir = opendir(LAD_WORKINGDIR)) == NULL)
+ return;
+
+ dirnamelen = snprintf(pathname, sizeof(pathname), "%s/", LAD_WORKINGDIR);
+ if (dirnamelen >= sizeof(pathname)) {
+ closedir(dir);
+ return;
+ }
+ namep = pathname + dirnamelen;
+ maxcopy = PATH_MAX - dirnamelen;
+ while (readdir_r(dir, &entry, &result) == 0 && result != NULL) {
+ /* Delete old FIFOs left over */
+ if ((entry.d_type == DT_FIFO) && (strlen(entry.d_name) < maxcopy)) {
+ strncpy(namep, entry.d_name, maxcopy);
+ unlink(pathname);
+ }
+ }
+ closedir(dir);
+}
/*
* ======== cleanupDepartedClients ========
@@ -892,9 +1096,6 @@ static Int connectToLAD(String clientName, Int pid, String clientProto, Int *cli
openResponseFIFO:
- /* if response FIFO exists from previous LAD session delete it now */
- unlink(clientName);
-
/* create the dedicated response FIFO to the client */
statusIO = mkfifo(clientName, 0777);
if (statusIO != 0) {