Advertisement
Guest User

Untitled

a guest
May 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <syslog.h>
  5. #include <signal.h>
  6. #include <getopt.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <errno.h>
  12. #include <dirent.h>
  13. #include <ctype.h>
  14.  
  15.  
  16. int running = 0;
  17. int sleepTime=15;
  18. int signaL=0;
  19. int sizeArray=0;
  20.  
  21. void listFilesRecursively(char *path, int size, char *array[]);
  22. int isFindingFile(char *s1, char *s2);
  23. int checkArguments(int argc, char *argv[]);
  24. int isNumber(char *s);
  25.  
  26.  
  27. void my_handler(int sig)
  28. {
  29. syslog(LOG_INFO, "Daemon received signal SIGUSR1\n");
  30. signaL = 1;
  31. }
  32.  
  33. void sleepFunction(int sig)
  34. {
  35. syslog(LOG_INFO, "Daemon received signal SIGUSR2\n");
  36. signaL=2;
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.  
  42. int pid, sid;
  43.  
  44. struct sigaction my_action, old_action;
  45.  
  46. char *path;
  47. path="/home";
  48.  
  49. if(checkArguments(argc,argv)==0)
  50. {
  51. syslog(LOG_INFO, "Reading arguments is invalid\n");
  52. }
  53.  
  54.  
  55. // Forking and killing father.
  56. pid = fork();
  57. if(pid < 0){
  58. perror("fork"); // Fork failed.
  59. exit(1);
  60. }
  61. if(pid > 0) exit(0); // Father process.
  62.  
  63. // Setting umask to zero.
  64. umask(0);
  65.  
  66. // Opening logs.
  67. openlog("logFile", LOG_PID, LOG_DAEMON);
  68. syslog(LOG_INFO, "Deamon has just started running\n");
  69.  
  70. // Getting brand new session and PID.
  71. sid = setsid();
  72. if(sid < 0){
  73. perror("setsid"); // setsid() failed.
  74. exit(3);
  75. }
  76.  
  77. // Changing current directory.
  78. if(chdir("/") < 0){ // Set appropriately.
  79. perror("chdir"); // chdir() failed.
  80. exit(4);
  81. }
  82.  
  83.  
  84.  
  85. //SIGNAL SIGUSR1
  86. my_action.sa_handler = my_handler;
  87. sigfillset(&my_action.sa_mask);
  88. my_action.sa_flags = 0;
  89. if (sigaction(SIGUSR1, &my_action, &old_action) < 0)
  90. {
  91. syslog(LOG_ERR, "Error with the use of SIGUSR1 signal\n");
  92. exit(-1);
  93. }
  94.  
  95. //SIGNAL SIGUSR2 for going sleep daemon
  96. my_action.sa_handler = sleepFunction;
  97. sigfillset(&my_action.sa_mask);
  98. my_action.sa_flags = 0;
  99. if (sigaction(SIGUSR2, &my_action, &old_action) < 0)
  100. {
  101. syslog(LOG_ERR, "Error with the use of SIGUSR2 signal\n");
  102. exit(-1);
  103. }
  104.  
  105.  
  106. // Deamon begins!
  107. while(!running)
  108. {
  109.  
  110. sleep(sleepTime); // Wait x seconds and work again.
  111.  
  112. switch (signaL)
  113. {
  114. case 0:
  115. syslog(LOG_INFO, "Demon started working after %ds\n", sleepTime);
  116. break;
  117.  
  118. case 1:
  119. {
  120. syslog(LOG_INFO, "Demon started working after SIGUSR1 signal\n");
  121. signaL = 0; //Need to reeset signaL
  122. break;
  123. }
  124.  
  125. case 2:
  126. {
  127. syslog(LOG_INFO, "Demon going sleep after SIGUSR2 signal\n");
  128. signaL=0;
  129. sleep(sleepTime); // Wait x seconds and work again.
  130. break;
  131. }
  132.  
  133. }
  134. listFilesRecursively(path,sizeArray,argv);
  135.  
  136.  
  137.  
  138.  
  139.  
  140. syslog(LOG_INFO, "Demon has just gone to sleep");
  141.  
  142. }
  143.  
  144.  
  145. syslog(LOG_INFO, "Demon has stopped\n");
  146. closelog();
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. void listFilesRecursively(char *basePath, int size, char *array[])
  154. {
  155. char path[1000];
  156. struct dirent *dp;
  157. DIR *dir = opendir(basePath);
  158.  
  159.  
  160.  
  161. // Unable to open directory stream
  162. if (!dir)
  163. return;
  164.  
  165. while ((dp = readdir(dir)) != NULL)
  166. {
  167. if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
  168. {
  169. if(size>1)
  170. {
  171. for (int count=1;count<size;count++)
  172. {
  173. char fullpath[1000];
  174. strcpy(fullpath,basePath);
  175. strcat(fullpath, "/");
  176. strcat(fullpath, dp->d_name);
  177.  
  178. if(isFindingFile(array[count],dp->d_name)==1 && access(fullpath,R_OK)==0 && access(fullpath,W_OK)==0 && access(fullpath,X_OK)==0)
  179. {
  180. syslog(LOG_INFO, "%s | %s | %s",basePath,dp->d_name,array[count]);
  181. printf("%s %s \n", basePath, dp->d_name);
  182. }
  183. }
  184. }
  185. // Construct new path from our base path
  186. strcpy(path, basePath);
  187. strcat(path, "/");
  188. strcat(path, dp->d_name);
  189.  
  190. listFilesRecursively(path,size,array);
  191. }
  192. }
  193.  
  194. closedir(dir);
  195. }
  196.  
  197.  
  198. int isFindingFile(char *s1, char *s2)
  199. {
  200. char *word = s1;
  201. char *phrase = s2;
  202.  
  203. const int wordLength = strlen(word);
  204. const int searchLength = strlen(phrase);
  205.  
  206. for (int n = 0; n <= searchLength; n++)
  207. {
  208. // or phrase + n
  209. if (strncmp(&phrase[n], word, wordLength) == 0)
  210. {
  211. return 1;
  212. }
  213. }
  214. return 0;
  215.  
  216. }
  217.  
  218. int checkArguments(int argc, char *argv[])
  219. {
  220. int sleepingTIme;
  221. int pom=argc;
  222.  
  223. if(pom>1)
  224. {
  225.  
  226. if(isNumber(argv[pom-1])==1)
  227. {
  228. sleepingTIme=atoi(argv[pom-1]);
  229. sleepTime=sleepingTIme;
  230. sizeArray=pom-1;
  231. return 1;
  232. }
  233. else
  234. {
  235. sleepTime=15;
  236. sizeArray=pom;
  237. return 1;
  238. }
  239. }
  240. return 0;
  241.  
  242. }
  243.  
  244.  
  245. int isNumber(char *s)
  246. {
  247. int n=sizeof(s);
  248. for (int i = 0; i < n; i++)
  249. {
  250. if (isdigit(s[i]) == 0)
  251. {
  252. return 0;
  253. }
  254. }
  255. return 1;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement