Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. #pragma clang diagnostic push
  2. #pragma ide diagnostic ignored "hicpp-signed-bitwise"
  3.  
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <dirent.h>
  8. #include <signal.h>
  9. #include <stdarg.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <sys/types.h>
  13. #include <fcntl.h>
  14. #include <sys/mman.h>
  15. #include <stdio.h>
  16. #include <sys/stat.h>
  17. #include <syslog.h>
  18. #include <signal.h>
  19. #include <utime.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <errno.h>
  23.  
  24. int sleep_time = 15;
  25. int fsiz = 26214400; // 25 MB
  26. int sigusr_flag = 0;
  27.  
  28. void delete(char *source, char *target);
  29.  
  30. void signal_handling(int sigusr_flag);
  31.  
  32. int check_path(struct stat type);
  33.  
  34. int check_arguments(char *source, char *target, int count, char *arg[]);
  35.  
  36. bool copy_small(char *inf, char *outf);
  37.  
  38. bool copy_big(char *inf, char *outf);
  39.  
  40. void copy(char *source, char *target);
  41.  
  42.  
  43. int check_path(struct stat type) {
  44. if (S_ISREG(type.st_mode))
  45. {
  46. return 1;
  47. }
  48. if (S_ISDIR(type.st_mode))
  49. {
  50. return 0;
  51. }
  52. }
  53.  
  54. int check_arguments(char *source, char *target, int count, char *arg[]) {
  55. int i, pom, pom_1, pom_2;
  56.  
  57. if (count >= 3) {
  58.  
  59. strcpy(source, arg[1]);
  60. strcpy(target, arg[2]);
  61. for (i = 3; i < count; i++) {
  62. if (strcmp(arg[i], "-S") == 0 && pom_2 != 1) {
  63. pom_1 = 1;
  64. } else if (strcmp(arg[i], "-P") == 0 && pom_1 != 1) {
  65. pom_2 = 1;
  66. } else if ((pom = atoi(arg[i])) != 0 && pom_1 == 1 && pom_2 != 1) {
  67. sleep_time = pom;
  68. pom_1 = 2;
  69. } else if ((pom = atoi(arg[i])) != 0 && pom_2 == 1 && pom_1 != 1) {
  70.  
  71. fsiz = pom;
  72. pom_2 = 2;
  73. }
  74.  
  75. }
  76.  
  77. if (pom_1 == 1 || pom_2 == 1) {
  78.  
  79. return -1;
  80. }
  81.  
  82. }
  83. }
  84.  
  85.  
  86. void signal_handling(int sigusr_flag) {
  87. syslog(LOG_INFO, "Demon odebral sygnal SIGURS1\n");
  88. sigusr_flag = 1;
  89. }
  90.  
  91. bool copy_small(char *inf, char *outf) {
  92. ssize_t output, input;
  93. struct stat stat_s;
  94. const int buff = 4096;
  95. int buffer[buff];
  96. int f_source, f_target;
  97.  
  98. if ((f_source = open(inf, O_RDONLY)) < -1) {
  99. syslog(LOG_ERR, "BLAD: copy_small: Nie mozna otworzyc: '%s'", inf);
  100. return false;
  101. }
  102.  
  103. if (fstat(f_source, &stat_s) < 0) {
  104. syslog(LOG_ERR, "BLAD: copy_small: stat(): '%s'", inf);
  105. return false;
  106. }
  107.  
  108. if ((f_target = open(outf, O_WRONLY | O_CREAT | O_TRUNC, stat_s.st_mode)) < 0) {
  109. syslog(LOG_ERR, "BLAD: copy_small: Nie mozna otworzyc: '%s'", inf);
  110. return false;
  111. }
  112.  
  113. while ((input = read(f_source, &buffer, buff)) != 0) {
  114. output = write(f_target, &buffer, input);
  115. }
  116.  
  117. close(f_source);
  118. close(f_target);
  119. return true;
  120. }
  121.  
  122. bool copy_big(char *inf, char *outf) {
  123.  
  124. int f_source, f_target;
  125. char *src, *dst;
  126. struct stat statbuf;
  127.  
  128. // otworz plik zrodlowy
  129. if ((f_source = open(inf, O_RDONLY)) < 0) {
  130. syslog(LOG_ERR, "BLAD: copy_big: Nie mozna otworzyc: '%s'", inf);
  131. return false;
  132. }
  133. if (fstat(f_source, &statbuf) < 0) {
  134. syslog(LOG_ERR, "BLAD: copy_big: stat(): '%s'", inf);
  135. return false;
  136. }
  137. // utworz plik wysjciowy
  138. if ((f_target = open(outf, O_WRONLY | O_CREAT | O_TRUNC, statbuf.st_mode)) < 0) {
  139. syslog(LOG_ERR, "BLAD: copy_big: Nie mozna otworzyc: '%s'", outf);
  140. return false;
  141. }
  142.  
  143. if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, f_source, 0)) == (caddr_t) - 1) {
  144. syslog(LOG_ERR, "BLAD: copy_big: mmap: '%s'", inf);
  145. return false;
  146. }
  147.  
  148. write(f_target, src, statbuf.st_size);
  149. munmap(src, statbuf.st_size);
  150.  
  151. close(f_source);
  152. close(f_target);
  153. return true;
  154. }
  155.  
  156. void copy(char *source, char *target) {
  157. struct dirent *file_s;
  158. struct stat stat_t;
  159. struct stat stat_s;
  160. DIR *dir_s;
  161. struct utimbuf new_time;
  162.  
  163. char tmp_s[PATH_MAX];
  164. char tmp_t[PATH_MAX];
  165.  
  166.  
  167. if ((dir_s = opendir(source))) {
  168.  
  169. while (file_s = readdir(dir_s)) {
  170. snprintf(tmp_s, PATH_MAX, "%s/%s", source, file_s->d_name);
  171. snprintf(tmp_t, PATH_MAX, "%s/%s", target, file_s->d_name);
  172.  
  173. stat(tmp_s, &stat_s);
  174. if (file_s->d_type == DT_REG) {
  175. if (stat(tmp_t, &stat_t) != 0) {
  176.  
  177. if (fsiz > stat_s.st_size) {
  178. if (copy_small(tmp_s, tmp_t)) {
  179. syslog(LOG_INFO, "Skopiowano: '%s' do '%s'", tmp_s, tmp_t);
  180. } else {
  181. syslog(LOG_INFO, "Nie skopiowano: '%s' do '%s'", tmp_s, tmp_t);
  182. }
  183. } else {
  184. if (copy_big(tmp_s, tmp_t)) {
  185. syslog(LOG_INFO, "(mmap) Skopiowano plik: '%s' do '%s'", tmp_s, tmp_t);
  186. } else {
  187. syslog(LOG_INFO, "(mmap) Nie skopiowano pliku : '%s' do '%s'", tmp_s, tmp_t);
  188. }
  189. }
  190.  
  191. new_time.modtime = stat_s.st_mtime;
  192. utime(tmp_t, &new_time);
  193. }
  194. }
  195. }
  196. } else {
  197. syslog(LOG_INFO, "Niepowodzenie w otworzeniu sciezki: '%s', '%s'", source, target);
  198. }
  199.  
  200. closedir(dir_s);
  201. }
  202.  
  203. void delete(char *source, char *target) {
  204. struct dirent *dest;
  205. struct dirent *file_s;
  206. struct stat stat_s;
  207. struct stat stat_t;
  208. DIR *path_t;
  209. char tmp_s[PATH_MAX];
  210. char tmp_t[PATH_MAX];
  211.  
  212. if ((path_t = opendir(target)) != 0) {
  213.  
  214. while (dest = readdir(path_t)) {
  215. if (strcmp(dest->d_name, ".") || strcmp(dest->d_name, "..")) {
  216.  
  217.  
  218. snprintf(tmp_s, PATH_MAX, "%s/%s", source, dest->d_name);
  219. snprintf(tmp_t, PATH_MAX, "%s/%s", target, dest->d_name);
  220.  
  221. if (stat(tmp_t, &stat_t) == 0) {
  222.  
  223. if (check_path(stat_t) == 1) {
  224.  
  225. if (stat(tmp_s, &stat_s) == 0) {
  226.  
  227.  
  228. } else {
  229. if (unlink(tmp_t) == 0) {
  230. syslog(LOG_INFO, "Usunieto plik: '%s'", tmp_t);
  231. } else {
  232. syslog(LOG_ERR, "Nie udalo sie usunac pliku: '%s'", tmp_t);
  233. }
  234. }
  235. }
  236.  
  237. } else {
  238. syslog(LOG_ERR, "Podana sciezka nie istnieje '%s'", tmp_t);
  239. }
  240. }
  241. }
  242. } else {
  243. syslog(LOG_ERR, "Blad sciezki: '%s' ", target);
  244. }
  245. closedir(path_t);
  246. }
  247.  
  248. int main(int argc, char **argv) {
  249. char source[PATH_MAX];
  250. char target[PATH_MAX];
  251.  
  252. struct stat source_path;
  253. struct stat dest_path;
  254.  
  255. struct sigaction new_action, old_action;
  256.  
  257. pid_t pid, sid;
  258. int proces;
  259.  
  260.  
  261. if (check_arguments(source, target, argc, argv) == -1) {
  262. exit(-1);
  263. }
  264.  
  265. if (lstat(source, &source_path) != 0) {
  266. exit(-1);
  267. }
  268.  
  269. if (lstat(target, &dest_path) != 0) {
  270. exit(-1);
  271. }
  272.  
  273.  
  274. if (check_path(source_path) != 0)
  275. {
  276. exit(-1);
  277. }
  278.  
  279. if (check_path(dest_path) != 0)
  280. {
  281. exit(-1);
  282. }
  283.  
  284. //Proces potomny
  285.  
  286. pid = fork(); // Tworzenie procesu potomnego
  287.  
  288. if (pid < 0) {
  289. printf("Blad utworzenia procesu potomnego\n");
  290. exit(-1);
  291. }
  292.  
  293. // Proces macierzysty
  294. if (pid > 0) {
  295. return 0;
  296. }
  297.  
  298.  
  299. // Proces potomny,
  300. if (pid == 0) {
  301. proces = getpid(); // Zmiennej 'proces' przypisujemy numer procesu potomnego, aby zakończyć pracę demona poleceniem 'kill'.
  302. printf("Demon zostal uruchomiony\n");
  303. printf("\nArgumenty:\n -S:\t%d Sekund\n -P:\t%d Bajtów\n \n", sleep_time, fsiz);
  304. printf("Numer procesu: %d\nAby zakonczyc prace demona uzyj polecenia: kill <nr_procesu>\nAby wykorzystac SIGUSR1 uzyj polecenia: kill -s 10 <nr_procesu>\n",
  305. proces);
  306.  
  307.  
  308. openlog("demon", LOG_PID, LOG_DAEMON); //wpis do logu systemowego
  309.  
  310. syslog(LOG_INFO, "Demon synchronizujacy zostal uruchomiony.\n"); //wysyla informacje do logu systemowego,
  311. sid = setsid();
  312. if (sid < 0) //setsid() zwraca PID aktualnego procesu
  313. {
  314. syslog(LOG_ERR, "Wystąpił błąd przy wysylaniu informacji do logu systemowego\n");
  315. exit(-1);
  316. }
  317. umask(0);
  318.  
  319. new_action.sa_handler = signal_handling;
  320. sigfillset(&new_action.sa_mask);
  321. new_action.sa_flags = 0;
  322.  
  323.  
  324. while (1) {
  325.  
  326. switch (sigusr_flag) {
  327. case 0:
  328. syslog(LOG_INFO, "Demon wybudził się po %d sekundach.\n", sleep_time);
  329. break;
  330. case 1:
  331. syslog(LOG_INFO, "Wykryto SIGUSR1- Demon zostaje wybudzony.");
  332. break;
  333. }
  334.  
  335. delete(source, target);
  336. copy(source, target);
  337.  
  338. syslog(LOG_INFO, "Demon wykonał synchronizacje, przechodzi w stan uśpienia\n");
  339. sleep(sleep_time);
  340. }
  341. }
  342. return 0;
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement