Loading src/dloom.cpp +105 −59 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ // along with Data Scheduler. If not, see <http://www.gnu.org/licenses/>. // #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/stat.h> Loading @@ -31,16 +32,18 @@ #include <cstdlib> #include <exception> #include <dirent.h> #include <iostream> #include <fstream> #include <stdio.h> #include <fcntl.h> #include <list> #include <tbb/tbb.h> #include <signal.h> #include "ctpl.h" #define SOCKET_NAME "dloom_socket" #define RUNNING_DIR "/tmp" #define SOCKET_FILE "/tmp/dloom.socket" #define DAEMON_LOCK_FILE "/tmp/dloom.lock" #define LOG_FILE "tmp/dloom.log" extern "C" { struct foo { Loading Loading @@ -71,76 +74,119 @@ enum job_type{ tbb::concurrent_queue<task> jobs_priority_1; tbb::concurrent_hash_map<pid_t, std::list<task_finished>> tasks_finished_map; void daemon_basic_initialization() { void log_message(const char filename[], const char message[]){ FILE *logfile; logfile=fopen(filename,"a"); if(!logfile) return; fprintf(logfile,"%s\n",message); fclose(logfile); } void signal_handler(int sig){ switch(sig) { case SIGHUP: log_message(LOG_FILE, "Hangup signal catched"); break; case SIGTERM: log_message(LOG_FILE,"terminate signal catched"); exit(0); break; } } void daemonize() { /* * --- daemon basic initalization --- * 1. Fork off praent process * 2. Exit parent * 3. Change file mode mask * 4. Check if daemon already exists * 5. Create new SID for the child process * 6. Change current working directory * 7. Close out standart file descriptor * --- Daemonize structure --- * Check if this is already a daemon * Fork off praent process * Obtain new process group * Close all descriptors * Handle standard IO * Change file mode mask * Change the current working directory * Check if daemon already exists * Manage signals */ pid_t pid, sid; /* Check if this is already a daemon */ if (getpid() == 1) return; /* Fork off the parent process */ pid = fork(); /* Fork error */ if (pid < 0) { throw std::exception(); perror("Fork"); exit(1); } /* If we got a good PID, then we can exit the parent process. */ /* Parent exits */ if (pid > 0) { return; } /* Change the file mode mask */ umask(0); /* Check if dloom already exists: * 1. Make sure folder exists /var/run/dloom * 2. Check if file dloom.txt exists */ /* Obtain new process group */ sid = setsid(); if (sid < 0) { /* Log failure */ throw std::exception(); } struct stat sb; if (stat("/var/run/dloom", &sb) == 0 && S_ISDIR(sb.st_mode)){ std::cout << "directory already existed" << std::endl; } else { /* Directory does not exist */ if(mkdir("/var/run/dloom", 0700)== -1){ std::cout << "[dloom][base_initialization] Error mkdir. Check privilages under /var/run/*" << std::endl; /* Close all descriptors */ int i; for(i=getdtablesize(); i>=0; --i){ if(close(i)<0){ perror("Close descritpors"); exit(1); } } if (access("/var/run/dloom/dloom.txt", F_OK) != -1) { /* An instance of dloom already exists. */ perror("An instance of dloom already exists. Exiting"); /* Handle standard IO */ i=open("/dev/null", O_RDWR); /* open stdin */ dup(i); /* stdout */ dup(i); /* stderr */ /* Change the file mode mask */ umask(027); /* file creation mode to 750 */ /* Change the current working directory */ if ((chdir(RUNNING_DIR)) < 0) { perror("Chdir"); exit(1); } else { /* First instance of dloom, create dloom.txt so no other instance may be initialized */ std::ofstream outfile("/var/run/dloom/dloom.txt"); outfile << pid << std::endl; } /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { /* Log failure */ throw std::exception(); } /* Check if daemon already exists: * First instance of the daemon will lock the file so that other * instances understand that an instnace is already running. */ /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log failure */ throw std::exception(); int lfp; lfp=open(DAEMON_LOCK_FILE, O_RDWR|O_CREAT, 0640); if(lfp<0){ perror("Can not open daemon lock file"); exit(1); } if(lockf(lfp, F_TLOCK, 0)<0){ perror("Another instance of this daemon already running"); exit(1); } /* record pid in lockfile */ char str[10]; sprintf(str, "%d\n", getpid()); write(lfp, str, strlen(str)); /* Manage signals */ signal(SIGCHLD,SIG_IGN); /* ignore child */ signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ signal(SIGTTOU,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGHUP,signal_handler); /* catch hangup signal */ signal(SIGTERM,signal_handler); /* catch kill signal */ /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } void comm_thread(int id){ Loading Loading @@ -309,19 +355,19 @@ void push_jobs(ctpl::thread_pool &p){ } void infinite_loop() { /* * Create thread pool * Set number of threads * Start infinite loop */ void daemon_specific_initialization() { /* set number of threads */ ctpl::thread_pool p(3); p.push(comm_thread); /* push_jobs may be done by this thread */ push_jobs(p); } int main() { daemon_basic_initialization(); daemon_specific_initialization(); daemonize(); infinite_loop(); } Loading
src/dloom.cpp +105 −59 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ // along with Data Scheduler. If not, see <http://www.gnu.org/licenses/>. // #include <stdio.h> #include <iostream> #include <sys/types.h> #include <sys/stat.h> Loading @@ -31,16 +32,18 @@ #include <cstdlib> #include <exception> #include <dirent.h> #include <iostream> #include <fstream> #include <stdio.h> #include <fcntl.h> #include <list> #include <tbb/tbb.h> #include <signal.h> #include "ctpl.h" #define SOCKET_NAME "dloom_socket" #define RUNNING_DIR "/tmp" #define SOCKET_FILE "/tmp/dloom.socket" #define DAEMON_LOCK_FILE "/tmp/dloom.lock" #define LOG_FILE "tmp/dloom.log" extern "C" { struct foo { Loading Loading @@ -71,76 +74,119 @@ enum job_type{ tbb::concurrent_queue<task> jobs_priority_1; tbb::concurrent_hash_map<pid_t, std::list<task_finished>> tasks_finished_map; void daemon_basic_initialization() { void log_message(const char filename[], const char message[]){ FILE *logfile; logfile=fopen(filename,"a"); if(!logfile) return; fprintf(logfile,"%s\n",message); fclose(logfile); } void signal_handler(int sig){ switch(sig) { case SIGHUP: log_message(LOG_FILE, "Hangup signal catched"); break; case SIGTERM: log_message(LOG_FILE,"terminate signal catched"); exit(0); break; } } void daemonize() { /* * --- daemon basic initalization --- * 1. Fork off praent process * 2. Exit parent * 3. Change file mode mask * 4. Check if daemon already exists * 5. Create new SID for the child process * 6. Change current working directory * 7. Close out standart file descriptor * --- Daemonize structure --- * Check if this is already a daemon * Fork off praent process * Obtain new process group * Close all descriptors * Handle standard IO * Change file mode mask * Change the current working directory * Check if daemon already exists * Manage signals */ pid_t pid, sid; /* Check if this is already a daemon */ if (getpid() == 1) return; /* Fork off the parent process */ pid = fork(); /* Fork error */ if (pid < 0) { throw std::exception(); perror("Fork"); exit(1); } /* If we got a good PID, then we can exit the parent process. */ /* Parent exits */ if (pid > 0) { return; } /* Change the file mode mask */ umask(0); /* Check if dloom already exists: * 1. Make sure folder exists /var/run/dloom * 2. Check if file dloom.txt exists */ /* Obtain new process group */ sid = setsid(); if (sid < 0) { /* Log failure */ throw std::exception(); } struct stat sb; if (stat("/var/run/dloom", &sb) == 0 && S_ISDIR(sb.st_mode)){ std::cout << "directory already existed" << std::endl; } else { /* Directory does not exist */ if(mkdir("/var/run/dloom", 0700)== -1){ std::cout << "[dloom][base_initialization] Error mkdir. Check privilages under /var/run/*" << std::endl; /* Close all descriptors */ int i; for(i=getdtablesize(); i>=0; --i){ if(close(i)<0){ perror("Close descritpors"); exit(1); } } if (access("/var/run/dloom/dloom.txt", F_OK) != -1) { /* An instance of dloom already exists. */ perror("An instance of dloom already exists. Exiting"); /* Handle standard IO */ i=open("/dev/null", O_RDWR); /* open stdin */ dup(i); /* stdout */ dup(i); /* stderr */ /* Change the file mode mask */ umask(027); /* file creation mode to 750 */ /* Change the current working directory */ if ((chdir(RUNNING_DIR)) < 0) { perror("Chdir"); exit(1); } else { /* First instance of dloom, create dloom.txt so no other instance may be initialized */ std::ofstream outfile("/var/run/dloom/dloom.txt"); outfile << pid << std::endl; } /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { /* Log failure */ throw std::exception(); } /* Check if daemon already exists: * First instance of the daemon will lock the file so that other * instances understand that an instnace is already running. */ /* Change the current working directory */ if ((chdir("/")) < 0) { /* Log failure */ throw std::exception(); int lfp; lfp=open(DAEMON_LOCK_FILE, O_RDWR|O_CREAT, 0640); if(lfp<0){ perror("Can not open daemon lock file"); exit(1); } if(lockf(lfp, F_TLOCK, 0)<0){ perror("Another instance of this daemon already running"); exit(1); } /* record pid in lockfile */ char str[10]; sprintf(str, "%d\n", getpid()); write(lfp, str, strlen(str)); /* Manage signals */ signal(SIGCHLD,SIG_IGN); /* ignore child */ signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ signal(SIGTTOU,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGHUP,signal_handler); /* catch hangup signal */ signal(SIGTERM,signal_handler); /* catch kill signal */ /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } void comm_thread(int id){ Loading Loading @@ -309,19 +355,19 @@ void push_jobs(ctpl::thread_pool &p){ } void infinite_loop() { /* * Create thread pool * Set number of threads * Start infinite loop */ void daemon_specific_initialization() { /* set number of threads */ ctpl::thread_pool p(3); p.push(comm_thread); /* push_jobs may be done by this thread */ push_jobs(p); } int main() { daemon_basic_initialization(); daemon_specific_initialization(); daemonize(); infinite_loop(); }