0

i wrote some code that monitors a directory DIR with inotify() and when a file gets moved in DIR i get a .txt output of that file(its an nfcapd file with flows of my network interface). This happens every 5 minutes.

After that, i used Snort's DPX starter kit, with which you can extend Snort by writing your own preprocessor. This preprocessor,like all the others, is just a function that's executed every time a new packet is available. My problem is that i want, when a new file gets exported from my previous code(so every 5 minutes), to read that file inside the preprocessor's function.

So, is there any way of getting the time and executing only if it's the desired time?

if (time is 15:36){
    func(output.txt);}

i'm writing in c.

Thanks

4

2 回答 2

1

您可以执行以下操作:

#include <time.h>
...

time_t t = time(NULL); //obtain current time in seconds
struct tm broken_time;
localtime_r(&t, &broken_time); // split time into fields

if(broken_time.tm_hour == 15 && broken_time.tm_min == 36) { //perform the check
    func(output.txt);
}
于 2013-02-10T15:05:35.147 回答
1

由于您使用的是inotify,我假设您的环境支持 POSIX 信号。

您可以使用alarm()在经过预定时间后发出信号,并让适当的信号处理程序执行您需要执行的任何工作。它将避免我认为最终会在您的代码中成为一个非常丑陋的无限循环。

所以,在你的情况下,函数处理SIGALRM不需要担心它是什么时间,它会知道预定的时间已经过去了,因为它被输入了。但是,您需要提供一些函数可以访问的上下文以知道要做什么,在没有看到您的代码的情况下很难建议如何操作。

我不完全确定您是否走在正确的道路上,但是alarm()鉴于您所描述的,使用可能是最明智的方法。

于 2013-02-10T15:17:27.913 回答