00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "CArchDaemonUnix.h"
00016 #include "XArchUnix.h"
00017 #include <unistd.h>
00018 #include <sys/types.h>
00019 #include <sys/stat.h>
00020 #include <fcntl.h>
00021 #include <errno.h>
00022 #include <cstdlib>
00023
00024
00025
00026
00027
00028 CArchDaemonUnix::CArchDaemonUnix()
00029 {
00030
00031 }
00032
00033 CArchDaemonUnix::~CArchDaemonUnix()
00034 {
00035
00036 }
00037
00038 int
00039 CArchDaemonUnix::daemonize(const char* name, DaemonFunc func)
00040 {
00041 int dummy;
00042
00043
00044
00045 switch (fork()) {
00046 case -1:
00047
00048 throw XArchDaemonFailed(new XArchEvalUnix(errno));
00049
00050 case 0:
00051
00052 break;
00053
00054 default:
00055
00056 exit(0);
00057 }
00058
00059
00060 setsid();
00061
00062
00063 dummy = chdir("/");
00064
00065
00066 umask(077);
00067
00068
00069 close(0);
00070 close(1);
00071 close(2);
00072
00073
00074
00075 open("/dev/null", O_RDONLY);
00076 open("/dev/null", O_RDWR);
00077 dummy = dup(1);
00078
00079
00080 return func(1, &name);
00081 }