Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma clang diagnostic push
- #pragma ide diagnostic ignored "hicpp-signed-bitwise"
- #include <stdbool.h>
- #include <string.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <signal.h>
- #include <stdarg.h>
- #include <unistd.h>
- #include <limits.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <sys/mman.h>
- #include <stdio.h>
- #include <sys/stat.h>
- #include <syslog.h>
- #include <signal.h>
- #include <utime.h>
- #include <time.h>
- #include <sys/time.h>
- #include <errno.h>
- int sleep_time = 15;
- int fsiz = 26214400; // 25 MB
- int sigusr_flag = 0;
- void delete(char *source, char *target);
- void signal_handling(int sigusr_flag);
- int check_path(struct stat type);
- int check_arguments(char *source, char *target, int count, char *arg[]);
- bool copy_small(char *inf, char *outf);
- bool copy_big(char *inf, char *outf);
- void copy(char *source, char *target);
- int check_path(struct stat type) {
- if (S_ISREG(type.st_mode))
- {
- return 1;
- }
- if (S_ISDIR(type.st_mode))
- {
- return 0;
- }
- }
- int check_arguments(char *source, char *target, int count, char *arg[]) {
- int i, pom, pom_1, pom_2;
- if (count >= 3) {
- strcpy(source, arg[1]);
- strcpy(target, arg[2]);
- for (i = 3; i < count; i++) {
- if (strcmp(arg[i], "-S") == 0 && pom_2 != 1) {
- pom_1 = 1;
- } else if (strcmp(arg[i], "-P") == 0 && pom_1 != 1) {
- pom_2 = 1;
- } else if ((pom = atoi(arg[i])) != 0 && pom_1 == 1 && pom_2 != 1) {
- sleep_time = pom;
- pom_1 = 2;
- } else if ((pom = atoi(arg[i])) != 0 && pom_2 == 1 && pom_1 != 1) {
- fsiz = pom;
- pom_2 = 2;
- }
- }
- if (pom_1 == 1 || pom_2 == 1) {
- return -1;
- }
- }
- }
- void signal_handling(int sigusr_flag) {
- syslog(LOG_INFO, "Demon odebral sygnal SIGURS1\n");
- sigusr_flag = 1;
- }
- bool copy_small(char *inf, char *outf) {
- ssize_t output, input;
- struct stat stat_s;
- const int buff = 4096;
- int buffer[buff];
- int f_source, f_target;
- if ((f_source = open(inf, O_RDONLY)) < -1) {
- syslog(LOG_ERR, "BLAD: copy_small: Nie mozna otworzyc: '%s'", inf);
- return false;
- }
- if (fstat(f_source, &stat_s) < 0) {
- syslog(LOG_ERR, "BLAD: copy_small: stat(): '%s'", inf);
- return false;
- }
- if ((f_target = open(outf, O_WRONLY | O_CREAT | O_TRUNC, stat_s.st_mode)) < 0) {
- syslog(LOG_ERR, "BLAD: copy_small: Nie mozna otworzyc: '%s'", inf);
- return false;
- }
- while ((input = read(f_source, &buffer, buff)) != 0) {
- output = write(f_target, &buffer, input);
- }
- close(f_source);
- close(f_target);
- return true;
- }
- bool copy_big(char *inf, char *outf) {
- int f_source, f_target;
- char *src, *dst;
- struct stat statbuf;
- // otworz plik zrodlowy
- if ((f_source = open(inf, O_RDONLY)) < 0) {
- syslog(LOG_ERR, "BLAD: copy_big: Nie mozna otworzyc: '%s'", inf);
- return false;
- }
- if (fstat(f_source, &statbuf) < 0) {
- syslog(LOG_ERR, "BLAD: copy_big: stat(): '%s'", inf);
- return false;
- }
- // utworz plik wysjciowy
- if ((f_target = open(outf, O_WRONLY | O_CREAT | O_TRUNC, statbuf.st_mode)) < 0) {
- syslog(LOG_ERR, "BLAD: copy_big: Nie mozna otworzyc: '%s'", outf);
- return false;
- }
- if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, f_source, 0)) == (caddr_t) - 1) {
- syslog(LOG_ERR, "BLAD: copy_big: mmap: '%s'", inf);
- return false;
- }
- write(f_target, src, statbuf.st_size);
- munmap(src, statbuf.st_size);
- close(f_source);
- close(f_target);
- return true;
- }
- void copy(char *source, char *target) {
- struct dirent *file_s;
- struct stat stat_t;
- struct stat stat_s;
- DIR *dir_s;
- struct utimbuf new_time;
- char tmp_s[PATH_MAX];
- char tmp_t[PATH_MAX];
- if ((dir_s = opendir(source))) {
- while (file_s = readdir(dir_s)) {
- snprintf(tmp_s, PATH_MAX, "%s/%s", source, file_s->d_name);
- snprintf(tmp_t, PATH_MAX, "%s/%s", target, file_s->d_name);
- stat(tmp_s, &stat_s);
- if (file_s->d_type == DT_REG) {
- if (stat(tmp_t, &stat_t) != 0) {
- if (fsiz > stat_s.st_size) {
- if (copy_small(tmp_s, tmp_t)) {
- syslog(LOG_INFO, "Skopiowano: '%s' do '%s'", tmp_s, tmp_t);
- } else {
- syslog(LOG_INFO, "Nie skopiowano: '%s' do '%s'", tmp_s, tmp_t);
- }
- } else {
- if (copy_big(tmp_s, tmp_t)) {
- syslog(LOG_INFO, "(mmap) Skopiowano plik: '%s' do '%s'", tmp_s, tmp_t);
- } else {
- syslog(LOG_INFO, "(mmap) Nie skopiowano pliku : '%s' do '%s'", tmp_s, tmp_t);
- }
- }
- new_time.modtime = stat_s.st_mtime;
- utime(tmp_t, &new_time);
- }
- }
- }
- } else {
- syslog(LOG_INFO, "Niepowodzenie w otworzeniu sciezki: '%s', '%s'", source, target);
- }
- closedir(dir_s);
- }
- void delete(char *source, char *target) {
- struct dirent *dest;
- struct dirent *file_s;
- struct stat stat_s;
- struct stat stat_t;
- DIR *path_t;
- char tmp_s[PATH_MAX];
- char tmp_t[PATH_MAX];
- if ((path_t = opendir(target)) != 0) {
- while (dest = readdir(path_t)) {
- if (strcmp(dest->d_name, ".") || strcmp(dest->d_name, "..")) {
- snprintf(tmp_s, PATH_MAX, "%s/%s", source, dest->d_name);
- snprintf(tmp_t, PATH_MAX, "%s/%s", target, dest->d_name);
- if (stat(tmp_t, &stat_t) == 0) {
- if (check_path(stat_t) == 1) {
- if (stat(tmp_s, &stat_s) == 0) {
- } else {
- if (unlink(tmp_t) == 0) {
- syslog(LOG_INFO, "Usunieto plik: '%s'", tmp_t);
- } else {
- syslog(LOG_ERR, "Nie udalo sie usunac pliku: '%s'", tmp_t);
- }
- }
- }
- } else {
- syslog(LOG_ERR, "Podana sciezka nie istnieje '%s'", tmp_t);
- }
- }
- }
- } else {
- syslog(LOG_ERR, "Blad sciezki: '%s' ", target);
- }
- closedir(path_t);
- }
- int main(int argc, char **argv) {
- char source[PATH_MAX];
- char target[PATH_MAX];
- struct stat source_path;
- struct stat dest_path;
- struct sigaction new_action, old_action;
- pid_t pid, sid;
- int proces;
- if (check_arguments(source, target, argc, argv) == -1) {
- exit(-1);
- }
- if (lstat(source, &source_path) != 0) {
- exit(-1);
- }
- if (lstat(target, &dest_path) != 0) {
- exit(-1);
- }
- if (check_path(source_path) != 0)
- {
- exit(-1);
- }
- if (check_path(dest_path) != 0)
- {
- exit(-1);
- }
- //Proces potomny
- pid = fork(); // Tworzenie procesu potomnego
- if (pid < 0) {
- printf("Blad utworzenia procesu potomnego\n");
- exit(-1);
- }
- // Proces macierzysty
- if (pid > 0) {
- return 0;
- }
- // Proces potomny,
- if (pid == 0) {
- proces = getpid(); // Zmiennej 'proces' przypisujemy numer procesu potomnego, aby zakończyć pracę demona poleceniem 'kill'.
- printf("Demon zostal uruchomiony\n");
- printf("\nArgumenty:\n -S:\t%d Sekund\n -P:\t%d Bajtów\n \n", sleep_time, fsiz);
- printf("Numer procesu: %d\nAby zakonczyc prace demona uzyj polecenia: kill <nr_procesu>\nAby wykorzystac SIGUSR1 uzyj polecenia: kill -s 10 <nr_procesu>\n",
- proces);
- openlog("demon", LOG_PID, LOG_DAEMON); //wpis do logu systemowego
- syslog(LOG_INFO, "Demon synchronizujacy zostal uruchomiony.\n"); //wysyla informacje do logu systemowego,
- sid = setsid();
- if (sid < 0) //setsid() zwraca PID aktualnego procesu
- {
- syslog(LOG_ERR, "Wystąpił błąd przy wysylaniu informacji do logu systemowego\n");
- exit(-1);
- }
- umask(0);
- new_action.sa_handler = signal_handling;
- sigfillset(&new_action.sa_mask);
- new_action.sa_flags = 0;
- while (1) {
- switch (sigusr_flag) {
- case 0:
- syslog(LOG_INFO, "Demon wybudził się po %d sekundach.\n", sleep_time);
- break;
- case 1:
- syslog(LOG_INFO, "Wykryto SIGUSR1- Demon zostaje wybudzony.");
- break;
- }
- delete(source, target);
- copy(source, target);
- syslog(LOG_INFO, "Demon wykonał synchronizacje, przechodzi w stan uśpienia\n");
- sleep(sleep_time);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement