34 static void GetTime(
struct timespec *tm);
35 static void CopyTime(
struct timespec *from,
struct timespec *to);
36 static int CompareTimes(
struct timespec *left,
struct timespec *right);
40 static int PcapDirectoryGetModifiedTime(
char const * file,
struct timespec * out);
44 struct timespec * older_than);
46 struct timespec *older_than);
50 void GetTime(
struct timespec *tm)
53 if(gettimeofday(&now, NULL) == 0) {
54 tm->tv_sec = now.tv_sec;
55 tm->tv_nsec = now.tv_usec * 1000L;
59 void CopyTime(
struct timespec *from,
struct timespec *to)
61 to->tv_sec = from->tv_sec;
62 to->tv_nsec = from->tv_nsec;
65 int CompareTimes(
struct timespec *left,
struct timespec *right)
67 if (left->tv_sec < right->tv_sec) {
69 }
else if (left->tv_sec > right->tv_sec) {
72 if (left->tv_nsec < right->tv_nsec) {
74 }
else if (left->tv_nsec > right->tv_nsec) {
101 if (pending != NULL) {
126 current_file =
TAILQ_FIRST(&ptv->directory_content);
176 DIR *temp_dir = NULL;
179 temp_dir = opendir(filename);
181 if (temp_dir == NULL) {
184 SCLogError(
"%s: Permission denied", filename);
188 SCLogError(
"%s: Not a valid file descriptor opened for reading", filename);
192 SCLogError(
"%s: Per process open file descriptor limit reached", filename);
196 SCLogError(
"%s: System wide open file descriptor limit reached", filename);
200 SCLogError(
"%s: Does not exist, or name is an empty string", filename);
203 SCLogError(
"%s: Insufficient memory to complete the operation", filename);
207 SCLogDebug(
"%s: plain file, not a directory", filename);
216 *directory = temp_dir;
223 int PcapDirectoryGetModifiedTime(
char const *file,
struct timespec *out)
231 if ((ret =
SCStatFn(file, &buf)) != 0)
235 out->tv_sec = buf.st_mtimespec.tv_sec;
236 out->tv_nsec = buf.st_mtimespec.tv_nsec;
238 out->tv_sec = buf.st_mtime;
240 out->tv_sec = buf.st_mtim.tv_sec;
241 out->tv_nsec = buf.st_mtim.tv_nsec;
258 if (
unlikely(file_to_add == NULL)) {
264 SCLogError(
"File was passed with null filename");
273 file_to_compare =
TAILQ_FIRST(&pv->directory_content);
274 while(file_to_compare != NULL) {
277 file_to_compare = NULL;
280 if (next_file_to_compare == NULL) {
284 file_to_compare = next_file_to_compare;
293 struct timespec *older_than
300 SCLogError(
"No directory filename was passed");
303 struct dirent * dir = NULL;
306 while ((dir = readdir(pv->
directory)) != NULL) {
308 if (dir->d_type != DT_REG) {
312 if (strcmp(dir->d_name,
".") == 0 ||
313 strcmp(dir->d_name,
"..") == 0) {
317 char pathbuff[PATH_MAX] = {0};
321 written = snprintf(pathbuff, PATH_MAX,
"%s/%s", pv->
filename, dir->d_name);
323 if (written <= 0 || written >= PATH_MAX) {
328 struct timespec temp_time;
329 memset(&temp_time, 0,
sizeof(
struct timespec));
331 if (PcapDirectoryGetModifiedTime(pathbuff, &temp_time) == 0) {
332 SCLogDebug(
"%" PRIuMAX
" < %" PRIuMAX
"(%s) < %" PRIuMAX
")",
343 else if (CompareTimes(&temp_time, older_than) >= 0) {
348 SCLogDebug(
"Unable to get modified time on %s, skipping", pathbuff);
353 if (
unlikely(file_to_add == NULL)) {
354 SCLogError(
"Failed to allocate pending file");
367 memset(&file_to_add->
modified_time, 0,
sizeof(
struct timespec));
387 struct timespec *older_than)
390 SCLogError(
"Failed to populate directory buffer");
399 older_than->tv_sec = older_than->tv_sec - pv->
delay;
405 struct timespec last_time_seen;
406 memset(&last_time_seen, 0,
sizeof(
struct timespec));
409 current_file =
TAILQ_FIRST(&pv->directory_content);
412 if (
unlikely(current_file == NULL)) {
422 SCLogError(
"Failed to allocate PcapFileFileVars");
427 if (
unlikely(pftv->filename == NULL)) {
432 pftv->shared = pv->
shared;
451 SCLogInfo(
"Processed file %s, processed up to %" PRIuMAX,
455 if(CompareTimes(¤t_file->
modified_time, &last_time_seen) > 0) {
462 status = PcapRunStatus(pv);
468 SCLogInfo(
"Updating processed to %" PRIuMAX,
471 status = PcapRunStatus(pv);
475 older_than->tv_sec = older_than->tv_sec - pv->
delay;
484 DIR *directory_check = NULL;
486 struct timespec older_than;
487 memset(&older_than, 0,
sizeof(
struct timespec));
488 older_than.tv_sec = LONG_MAX;
489 uint32_t poll_seconds;
492 memset(&safe_tm, 0,
sizeof(safe_tm));
493 poll_seconds = (uint32_t)localtime_r(&ptv->
poll_interval, &safe_tm)->tm_sec;
496 poll_seconds = (uint32_t)localtime(&ptv->
poll_interval)->tm_sec;
500 GetTime(&older_than);
501 older_than.tv_sec = older_than.tv_sec - ptv->
delay;
507 SCLogInfo(
"Processing pcaps directory %s, files must be newer than %" PRIuMAX
" and older than %" PRIuMAX,
510 status = PcapDirectoryDispatchForTimeRange(ptv, &older_than);
514 status = PcapRunStatus(ptv);
520 SCLogInfo(
"Directory %s no longer exists, stopping",
523 }
else if(directory_check != NULL) {
524 closedir(directory_check);
525 directory_check = NULL;
529 SCLogDebug(
"Not looping, stopping directory mode");
538 status = PcapDirectoryFailure(ptv);
540 SCLogInfo(
"Directory run mode complete");
541 status = PcapDirectoryDone(ptv);