00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <signal.h>
00005 #include <pthread.h>
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008
00009 #include <hlog.h>
00010 #include <hmem.h>
00011
00012 #include "hdaemon.h"
00013 #include "hdsched.h"
00014 #include "hdmsg.h"
00015 #include "hdplacer.h"
00016 #include "hdtimer.h"
00017
00018
00020 static int init_daemon_();
00021 static void signal_handler_(int sig);
00022
00023
00024 static void signal_handler_(int sig)
00025 {
00026 switch (sig) {
00027 case SIGINT:
00028 case SIGTERM:
00029 hdmsg_notify(HDME_QUIT);
00030 hdmsg_synchronize();
00031 break;
00032 }
00033 }
00034
00035 static int init_daemon_()
00036 {
00037
00038
00039 pid_t pid;
00040 pid = fork();
00041 if (pid < 0) {
00042 return -1;
00043 } else if (pid > 0)
00044 exit(EXIT_SUCCESS);
00045
00046
00047 umask(0);
00048
00049
00050 pid_t sid;
00051 sid = setsid();
00052 if (sid < 0) {
00053 return -1;
00054 }
00055
00056
00057 if ((chdir("/")) < 0) {
00058 return -1;
00059 }
00060
00061
00062 close(STDIN_FILENO);
00063 close(STDOUT_FILENO);
00064 close(STDERR_FILENO);
00065
00066 hlog_write(HLOG_NORMAL, "Daemon initialized.\n");
00067
00068 return 0;
00069 }
00070
00071
00072 int main( int argc, const char* argv[] )
00073 {
00074
00075 signal(SIGINT, signal_handler_);
00076 signal(SIGTERM, signal_handler_);
00077
00078 if (hlog_init(DAEMON_LOG) < 0) {
00079 printf("Error. Could not init log file. Daemon is stopping.\n");
00080 exit(EXIT_FAILURE);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 static pthread_t scheduler_thread;
00091 static pthread_t placer_thread;
00092 static pthread_t message_server_thread;
00093 static pthread_t timer_thread;
00094
00095
00096 pthread_create(&scheduler_thread, NULL, hdsched_main, (void*)NULL);
00097
00098 pthread_create(&placer_thread, NULL, hdplacer_main, (void*)NULL);
00099
00100 pthread_create(&timer_thread, NULL, hdtimer_main, (void*)NULL);
00101
00102
00103 pthread_create(&message_server_thread, NULL, hdmsg_main, (void*)NULL);
00104
00105
00106 pthread_join(timer_thread, NULL);
00107 pthread_join(scheduler_thread, NULL);
00108 pthread_join(placer_thread, NULL);
00109 pthread_join(message_server_thread, NULL);
00110
00111 hlog_close();
00112
00113 exit(EXIT_SUCCESS);
00114 }
00115
00116
00117
00118
00119
00120