Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <syslog.h>
- #include <signal.h>
- #include <getopt.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <dirent.h>
- #include <ctype.h>
- int running = 0;
- int sleepTime=15;
- int signaL=0;
- int sizeArray=0;
- void listFilesRecursively(char *path, int size, char *array[]);
- int isFindingFile(char *s1, char *s2);
- int checkArguments(int argc, char *argv[]);
- int isNumber(char *s);
- void my_handler(int sig)
- {
- syslog(LOG_INFO, "Daemon received signal SIGUSR1\n");
- signaL = 1;
- }
- void sleepFunction(int sig)
- {
- syslog(LOG_INFO, "Daemon received signal SIGUSR2\n");
- signaL=2;
- }
- int main(int argc, char *argv[])
- {
- int pid, sid;
- struct sigaction my_action, old_action;
- char *path;
- path="/home";
- if(checkArguments(argc,argv)==0)
- {
- syslog(LOG_INFO, "Reading arguments is invalid\n");
- }
- // Forking and killing father.
- pid = fork();
- if(pid < 0){
- perror("fork"); // Fork failed.
- exit(1);
- }
- if(pid > 0) exit(0); // Father process.
- // Setting umask to zero.
- umask(0);
- // Opening logs.
- openlog("logFile", LOG_PID, LOG_DAEMON);
- syslog(LOG_INFO, "Deamon has just started running\n");
- // Getting brand new session and PID.
- sid = setsid();
- if(sid < 0){
- perror("setsid"); // setsid() failed.
- exit(3);
- }
- // Changing current directory.
- if(chdir("/") < 0){ // Set appropriately.
- perror("chdir"); // chdir() failed.
- exit(4);
- }
- //SIGNAL SIGUSR1
- my_action.sa_handler = my_handler;
- sigfillset(&my_action.sa_mask);
- my_action.sa_flags = 0;
- if (sigaction(SIGUSR1, &my_action, &old_action) < 0)
- {
- syslog(LOG_ERR, "Error with the use of SIGUSR1 signal\n");
- exit(-1);
- }
- //SIGNAL SIGUSR2 for going sleep daemon
- my_action.sa_handler = sleepFunction;
- sigfillset(&my_action.sa_mask);
- my_action.sa_flags = 0;
- if (sigaction(SIGUSR2, &my_action, &old_action) < 0)
- {
- syslog(LOG_ERR, "Error with the use of SIGUSR2 signal\n");
- exit(-1);
- }
- // Deamon begins!
- while(!running)
- {
- sleep(sleepTime); // Wait x seconds and work again.
- switch (signaL)
- {
- case 0:
- syslog(LOG_INFO, "Demon started working after %ds\n", sleepTime);
- break;
- case 1:
- {
- syslog(LOG_INFO, "Demon started working after SIGUSR1 signal\n");
- signaL = 0; //Need to reeset signaL
- break;
- }
- case 2:
- {
- syslog(LOG_INFO, "Demon going sleep after SIGUSR2 signal\n");
- signaL=0;
- sleep(sleepTime); // Wait x seconds and work again.
- break;
- }
- }
- listFilesRecursively(path,sizeArray,argv);
- syslog(LOG_INFO, "Demon has just gone to sleep");
- }
- syslog(LOG_INFO, "Demon has stopped\n");
- closelog();
- }
- void listFilesRecursively(char *basePath, int size, char *array[])
- {
- char path[1000];
- struct dirent *dp;
- DIR *dir = opendir(basePath);
- // Unable to open directory stream
- if (!dir)
- return;
- while ((dp = readdir(dir)) != NULL)
- {
- if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
- {
- if(size>1)
- {
- for (int count=1;count<size;count++)
- {
- char fullpath[1000];
- strcpy(fullpath,basePath);
- strcat(fullpath, "/");
- strcat(fullpath, dp->d_name);
- if(isFindingFile(array[count],dp->d_name)==1 && access(fullpath,R_OK)==0 && access(fullpath,W_OK)==0 && access(fullpath,X_OK)==0)
- {
- syslog(LOG_INFO, "%s | %s | %s",basePath,dp->d_name,array[count]);
- printf("%s %s \n", basePath, dp->d_name);
- }
- }
- }
- // Construct new path from our base path
- strcpy(path, basePath);
- strcat(path, "/");
- strcat(path, dp->d_name);
- listFilesRecursively(path,size,array);
- }
- }
- closedir(dir);
- }
- int isFindingFile(char *s1, char *s2)
- {
- char *word = s1;
- char *phrase = s2;
- const int wordLength = strlen(word);
- const int searchLength = strlen(phrase);
- for (int n = 0; n <= searchLength; n++)
- {
- // or phrase + n
- if (strncmp(&phrase[n], word, wordLength) == 0)
- {
- return 1;
- }
- }
- return 0;
- }
- int checkArguments(int argc, char *argv[])
- {
- int sleepingTIme;
- int pom=argc;
- if(pom>1)
- {
- if(isNumber(argv[pom-1])==1)
- {
- sleepingTIme=atoi(argv[pom-1]);
- sleepTime=sleepingTIme;
- sizeArray=pom-1;
- return 1;
- }
- else
- {
- sleepTime=15;
- sizeArray=pom;
- return 1;
- }
- }
- return 0;
- }
- int isNumber(char *s)
- {
- int n=sizeof(s);
- for (int i = 0; i < n; i++)
- {
- if (isdigit(s[i]) == 0)
- {
- return 0;
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement