/** * Script to be executed as root via cron to rotate user-session * logs which are generated by ttyrec in a defined cycle. * */ #define RETENTION_IN_DAYS 7 #define USER_SESSION_DIR "/etc" #include "stdio.h" #include "dirent.h" #include "sys/stat.h" #include "unistd.h" #include "fcntl.h" #include "sys/param.h" #include "stdlib.h" #include "time.h" void rotate_session_dir(); int main() { printf("Retention set to %d days for user session folder %s\n", RETENTION_IN_DAYS, USER_SESSION_DIR); rotate_session_dir(); return 0; } void rotate_session_dir() { int secs_in_week = 60 * 60 * 24 * RETENTION_IN_DAYS; struct dirent *entry; struct stat file_status; DIR *dir = opendir(USER_SESSION_DIR); time_t system_time; time_t time_diff; system_time = time(NULL); //printf("System time is %d", system_time); if (!dir) { perror("Could not open defined User Session directory"); exit(1); } chdir(USER_SESSION_DIR); while ((entry = readdir(dir)) != NULL) { if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) continue; if (stat(entry->d_name, &file_status) == 0) { if (!S_ISDIR(file_status.st_mode)) { /** * Remove all files from folder which are older * than 7 days -- append-only flag will need * to be removed from each file before the remove * operation. * * Note: need to remote append only attr here as well * on the file... how!? */ time_diff = difftime(time(NULL), file_status.st_atime); if (secs_in_week > time_diff) { printf("We need to delete the file: %s\n", entry->d_name); } } } } if (closedir(dir) == -1) { perror("closedir failure"); exit(1); } }