2023-12-01 09:17:09 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include "async_scanner.h"
|
|
|
|
|
|
|
|
#define BUF_LEN_FOR_PID 64
|
|
|
|
|
|
|
|
|
2023-12-29 08:58:41 +00:00
|
|
|
static async_scanner *scanner = nullptr;
|
2023-12-01 09:17:09 +00:00
|
|
|
static void sigHandler(int sig)
|
|
|
|
{
|
|
|
|
// if (sig == SIGINT || sig == SIGTERM)
|
|
|
|
// remove(MY_PID_FILE);
|
|
|
|
printf("exit now for signal: %d\n", sig);
|
2023-12-29 08:58:41 +00:00
|
|
|
if(scanner)
|
|
|
|
{
|
|
|
|
scanner->stop();
|
|
|
|
scanner->release();
|
|
|
|
}
|
|
|
|
|
2023-12-01 09:17:09 +00:00
|
|
|
_exit(0);
|
|
|
|
}
|
2024-01-23 07:07:17 +00:00
|
|
|
static std::string get_command(void)
|
|
|
|
{
|
|
|
|
std::string cmd("");
|
|
|
|
char in[2] = {0};
|
|
|
|
|
|
|
|
while((in[0] = getchar()) != 0x0a)
|
|
|
|
cmd += in;
|
2023-12-01 09:17:09 +00:00
|
|
|
|
2024-01-23 07:07:17 +00:00
|
|
|
return std::move(cmd);
|
|
|
|
}
|
2023-12-01 09:17:09 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
/* Ctrl + C */
|
|
|
|
if (signal(SIGINT, sigHandler) == SIG_ERR)
|
|
|
|
{
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* kill pid / killall name */
|
|
|
|
if (signal(SIGTERM, sigHandler) == SIG_ERR)
|
|
|
|
{
|
|
|
|
exit(-1);
|
|
|
|
}
|
2024-01-23 07:07:17 +00:00
|
|
|
|
|
|
|
/* kill pid / killall name */
|
|
|
|
signal(SIGKILL, sigHandler);
|
|
|
|
|
2023-12-29 08:58:41 +00:00
|
|
|
scanner = new async_scanner();
|
2023-12-01 10:03:10 +00:00
|
|
|
int err = scanner->last_error();
|
|
|
|
|
2024-01-23 07:07:17 +00:00
|
|
|
while(err == 0)
|
2023-12-02 02:15:44 +00:00
|
|
|
{
|
2024-01-23 07:07:17 +00:00
|
|
|
std::string cmd(get_command());
|
|
|
|
if(cmd == "exit")
|
|
|
|
break;
|
|
|
|
if(cmd == "mem")
|
|
|
|
utils::print_memory_usage(" Memory usage", false);
|
2023-12-02 02:15:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-29 08:58:41 +00:00
|
|
|
async_scanner* tmp = scanner;
|
|
|
|
|
|
|
|
scanner = nullptr;
|
|
|
|
tmp->stop();
|
|
|
|
tmp->release();
|
2023-12-01 10:03:10 +00:00
|
|
|
|
2023-12-01 09:17:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|