newtx/scanner/main.cpp

80 lines
1.3 KiB
C++

#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
static async_scanner *scanner = nullptr;
static void sigHandler(int sig)
{
// if (sig == SIGINT || sig == SIGTERM)
// remove(MY_PID_FILE);
printf("exit now for signal: %d\n", sig);
if(scanner)
{
scanner->stop();
scanner->release();
}
_exit(0);
}
static std::string get_command(void)
{
std::string cmd("");
char in[2] = {0};
while((in[0] = getchar()) != 0x0a)
cmd += in;
return std::move(cmd);
}
int main()
{
/* Ctrl + C */
if (signal(SIGINT, sigHandler) == SIG_ERR)
{
exit(-1);
}
/* kill pid / killall name */
if (signal(SIGTERM, sigHandler) == SIG_ERR)
{
exit(-1);
}
/* kill pid / killall name */
signal(SIGKILL, sigHandler);
scanner = new async_scanner();
int err = scanner->last_error();
while(err == 0)
{
std::string cmd(get_command());
if(cmd == "exit")
break;
if(cmd == "mem")
utils::print_memory_usage(" Memory usage", false);
}
async_scanner* tmp = scanner;
scanner = nullptr;
tmp->stop();
tmp->release();
return 0;
}