⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 监控linux指定目录
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ftw.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#include <time.h>
#include <getopt.h>
#include <errno.h>
#include "arg.h"
//#include "func.h"

int read_fp;

void sigusr2(int sig)
{
	//undone;
	printf("receive signal\n");

	flock(read_fp,LOCK_UN);

	close(read_fp);

	unsetenv("daemon");
	exit(0);
}

int check_running()
{
	read_fp = open(LOCK_FILE,O_CREAT);
	if ((flock(read_fp,LOCK_EX|LOCK_NB))<0)
	{
		printf("Another instance is already running\n");	
		exit(0);
	}
	
	if (geteuid())
	{
		printf("This program can only be executed by root. \n");
		return -1;
	}
	return 0;
}

int main(int argc, char **argv)
{
	char *p = NULL;
	runconf conf;

	memset(&conf, 0, sizeof(conf));
	get_param_from_file(&conf);
	if (argc < 2)
	{	
		//printf("please input file or directory\n");
		print_help();
		return -1;
	}
	setenv("daemon", "no", 1);
	if (parse_cmd_line(argc, argv, &conf) < 0) // parse command line.
	{ 
        return -1;
    }

	if (check_running() < 0)
	{
		return -1;
	}
	
	if (signal(SIGINT, sigusr2) == SIG_ERR)
	{
		printf("child can not reset the signal handler\n");
		return -1;
	}
	if (signal(SIGTERM, sigusr2) == SIG_ERR)
	{
		printf("child can not reset the signal handler\n");
		return -1;
	}
	p = getenv("daemon");
	if (0 == strcmp(p, "yes"))
	{
		daemon(0,0);
	}
	run(&conf);	
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -