Browse Source

watchDogDaemon: line buffer, end on client EOF, handler cleanup; la_mojo sends getpid()

watchDogDaemon (kernel.cpp)
- Client lines are assembled in a buffer. readLine() on the non-blocking
  socket dropped the bytes of a line that had only partly arrived.
- EOF (client closed or crashed without "-1") ends the monitoring of that
  client. Before, EOF read as "no data", and after the deadline kill(9) went
  to a PID that may belong to another process by then.
- The handler record is zeroed. "finished" was uninitialized (malloc), so
  the accept loop could pthread_join() a live handler and block.
- Finished handlers are joined, freed and erased correctly. erase()
  inside the for loop skipped the next one, and the records were never
  freed. A record whose thread could not be created is freed.
  pthread_create() returns an error number, not -1.
- The bind() result is checked.
- The log says which PID is watched or stopped, and for a stall how long
  ago the last update was ("WD update STALLED! (PID n, last update N ms
  ago)").

kernelPrototype
- Sends getpid() as the PID to kill (was gettid(): the same value only in
  the main thread).

Tested with the rebuilt daemon: the kernel scenarios as before (normal,
6 s step: not killed; hang: killed). A client that closes without "-1":
its process isn't killed later. An update split into 3 pieces, 6 times
with a 4 s deadline: understood, not killed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/26/head
Claude 8 hours ago
parent
commit
6cdca3c075
Failed to extract signature
  1. 164
      Projects/watchDogDaemon/kernel.cpp
  2. 2
      Projects/watchDogDaemon/kernel.h
  3. 2
      lib/App/common/kernelPrototype.cpp

164
Projects/watchDogDaemon/kernel.cpp

@ -219,10 +219,9 @@ bool SP_KERNEL::create(void)
int x = fcntl(m_iServerSocket, F_GETFL, 0);
fcntl(m_iServerSocket, F_SETFL, x | O_NONBLOCK);
bind(m_iServerSocket, (struct sockaddr *) &server_addr, slen);
if (m_iServerSocket == INVALID_SOCKET)
if (m_iServerSocket == INVALID_SOCKET || bind(m_iServerSocket, (struct sockaddr *) &server_addr, slen) != 0)
{
LOG_ERROR("Could not bind to the UX socket!");
LOG_ERROR("Could not bind to the UX socket!"); // the bind() result was not checked
return false;
};
listen(m_iServerSocket, 5);
@ -234,69 +233,102 @@ void SP_KERNEL::WaitFinish(bool force)
waitThreadEnd(force);
}
/*
One client: first line = PID to kill, then "N" = next update within N ms, "-1" = stop monitoring.
- Lines are assembled in a buffer: readLine() on the non-blocking socket dropped the bytes of a line
that had only partly arrived.
- EOF (the client closed / crashed without "-1") ends the monitoring: before, EOF read as "no data"
and after the deadline kill(9) went to a PID that may belong to another process by then.
*/
void * SP_KERNEL::sw_proc(void * ptr)
{
char tB[64];
uint64_t cMS;
uint64_t tMS;
WATCHDOG_PROC_REC * mR = (WATCHDOG_PROC_REC *) ptr;
WATCHDOG_PROC_REC * mR = (WATCHDOG_PROC_REC *) ptr;
pid_t CPID = 0;
char tLine[128];
size_t tHave = 0;
char jB[192];
if (mR)
{
cMS = getMilliseconds();
tMS = cMS + SW_DEFAULT_TIMEOUT;
while (1)
uint64_t tLastUpdate = getMilliseconds();
uint64_t tMS = tLastUpdate + SW_DEFAULT_TIMEOUT;
bool tRun = true;
while (tRun)
{
cMS = getMilliseconds();
int retVal = readLine(mR->client_socket, tB, 64);
if (retVal < 0)
{
if (errno != EAGAIN && errno != EINTR)
{
mR->master->m_pLog->Log("K_T", mR->master, "Peer Closes Connection!");
shutdown(mR->client_socket, 2);
close(mR->client_socket);
mR->client_socket = -1;
break; //-----> exit thread
};
}
else
uint64_t cMS = getMilliseconds();
// read what is there (non-blocking)
for (;;)
{
if (retVal)
char tB[64];
ssize_t tN = read(mR->client_socket, tB, sizeof(tB));
if (tN > 0)
{
// char jB[128];
// sprintf( jB, "Server: I recieved [%d] %s from client!", retVal, tB );
// mR->master->m_pLog->Log( "K_T", mR->master, jB );
if (CPID)
for (ssize_t k = 0; k < tN; k++)
{
int64_t nMS = atoll(tB);
// Stop WD and exit
if (nMS < 0)
if (tB[k] == '\n' || tB[k] == '\r')
{
if (tHave == 0)
{
continue;
};
tLine[tHave] = 0;
tHave = 0;
if (CPID == 0)
{
CPID = (pid_t) atoll(tLine);
snprintf(jB, sizeof(jB), "Watching PID %d", (int) CPID);
mR->master->m_pLog->Log("K_T", mR->master, jB);
}
else
{
int64_t nMS = atoll(tLine);
if (nMS < 0)
{
snprintf(jB, sizeof(jB), "PID %d: monitoring stopped by the client", (int) CPID);
mR->master->m_pLog->Log("K_T", mR->master, jB);
tRun = false; // Stop WD and exit
break;
};
tMS = cMS + nMS;
tLastUpdate = cMS;
};
}
else if (tHave < sizeof(tLine) - 1)
{
shutdown(mR->client_socket, 2);
close(mR->client_socket);
mR->client_socket = -1;
break;
tLine[tHave++] = tB[k];
};
// sprintf( jB, "Will Wait next [%d] milliseconds", nMS );
// mR->master->m_pLog->Log( "K_T", mR->master, jB );
tMS = cMS + nMS;
}
else
};
if (! tRun)
{
CPID = (pid_t) atoll(tB);
break;
};
continue;
};
if (tN == 0)
{
snprintf(jB, sizeof(jB), "PID %d: peer closed the connection - monitoring ended", (int) CPID);
mR->master->m_pLog->Log("K_T", mR->master, jB);
tRun = false;
}
else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
{
mR->master->m_pLog->Log("K_T", mR->master, "Peer Closes Connection!");
tRun = false;
};
break;
};
if (! tRun)
{
break;
};
if (cMS > tMS)
{
// WD TO EXIT!
mR->master->m_pLog->Log("K_T", mR->master, "WD update STALLED!");
snprintf(jB, sizeof(jB), "WD update STALLED! (PID %d, last update %llu ms ago)", (int) CPID, (unsigned long long) (cMS - tLastUpdate));
mR->master->m_pLog->Log("K_T", mR->master, jB);
if (CPID)
{
// Try Kill Client Process;
char jB[128];
sprintf(jB, "Try Killing Process[%lld] By Signal 9!", CPID);
snprintf(jB, sizeof(jB), "Try Killing Process[%d] By Signal 9!", (int) CPID);
mR->master->m_pLog->Log("K_T", mR->master, jB);
kill(CPID, 9);
}
@ -308,17 +340,13 @@ void * SP_KERNEL::sw_proc(void * ptr)
};
SLEEP(SW_TIMEOUT_PERCISION);
};
shutdown(mR->client_socket, 2);
close(mR->client_socket);
mR->client_socket = -1;
mR->finished = true;
};
mR->finished = true;
pthread_exit(NULL);
return NULL;
/*
read(client_socket, &ch, 1);
printf("\nServer: I recieved %c from client!\n", ch);
ch++;
write(client_socket, &ch, 1);
close(client_socket);
*/
}
void SP_KERNEL::m_fThreadProc(void)
@ -338,20 +366,20 @@ void SP_KERNEL::m_fThreadProc(void)
{
case EAGAIN:
{
// Checkup client disconnection
if (m_vWPV.size())
// Checkup client disconnection: join and free finished handlers
// (erase() inside the for loop skipped the next handler; the records were never freed)
for (WATCHDOG_PROCS_VECTOR::iterator index = m_vWPV.begin(); index != m_vWPV.end();)
{
for (WATCHDOG_PROCS_VECTOR::iterator index = m_vWPV.begin(); index < m_vWPV.end(); index++)
if ((*index)->finished)
{
if ((*index)->finished)
{
LOG("Detach WD Procedure");
pthread_join((*index)->handler, NULL);
LOG("WD Procedure Detached");
// Remove Client Handler
LOG("Remove WD Procedure");
m_vWPV.erase(index);
};
pthread_join((*index)->handler, NULL);
LOG("WD Procedure Removed");
free(*index);
index = m_vWPV.erase(index);
}
else
{
++index;
};
};
SLEEP(10);
@ -391,14 +419,18 @@ void SP_KERNEL::m_fThreadProc(void)
int x = fcntl(ClientSocket, F_GETFL, 0);
fcntl(ClientSocket, F_SETFL, x | O_NONBLOCK);
memset(tPRC, 0, sizeof(WATCHDOG_PROC_REC)); // finished was uninitialized (malloc): a live handler
// could be joined - the accept loop blocked
tPRC->client_socket = ClientSocket;
tPRC->master = this;
tPRC->finished = false;
int RetVal = pthread_create(&tPRC->handler, NULL, sw_proc, (void *) tPRC);
if (RetVal == -1)
if (RetVal != 0) // pthread_create returns an error number, not -1
{
LOG_ERROR("No Client Thread Created!");
shutdown(ClientSocket, 2);
close(ClientSocket);
free(tPRC);
}
else
{

2
Projects/watchDogDaemon/kernel.h

@ -16,7 +16,7 @@ typedef struct S_WATCHDOG_PROC_REC
SP_KERNEL * master;
int client_socket;
uint64_t timeout;
bool finished;
volatile bool finished;
pthread_t handler;
} WATCHDOG_PROC_REC;

2
lib/App/common/kernelPrototype.cpp

@ -80,7 +80,7 @@ kernelPrototype::kernelPrototype(config_setting_t * ptr) :
// LOG( (char *) ident.identity );
// LOG( ident.firmware_version );
char tB[64];
sprintf(tB, "%d\n", gettid());
sprintf(tB, "%d\n", (int) getpid()); // the process to kill (was gettid(): same value only in the main thread)
LOG("PID SENT TO WD!");
// MSG_NOSIGNAL: a daemon that restarted must not kill us with SIGPIPE
if (send(m_iWatchdogFd, tB, strlen(tB), MSG_NOSIGNAL) < 0)

Loading…
Cancel
Save