Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

CArchDaemonUnix.cpp

00001 /*
00002  * synergy -- mouse and keyboard sharing utility
00003  * Copyright (C) 2002 Chris Schoeneman
00004  * 
00005  * This package is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * found in the file COPYING that should have accompanied this file.
00008  * 
00009  * This package is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
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 // CArchDaemonUnix
00026 //
00027 
00028 CArchDaemonUnix::CArchDaemonUnix()
00029 {
00030     // do nothing
00031 }
00032 
00033 CArchDaemonUnix::~CArchDaemonUnix()
00034 {
00035     // do nothing
00036 }
00037 
00038 int
00039 CArchDaemonUnix::daemonize(const char* name, DaemonFunc func)
00040 {
00041     int dummy;
00042     
00043     // fork so shell thinks we're done and so we're not a process
00044     // group leader
00045     switch (fork()) {
00046     case -1:
00047         // failed
00048         throw XArchDaemonFailed(new XArchEvalUnix(errno));
00049 
00050     case 0:
00051         // child
00052         break;
00053 
00054     default:
00055         // parent exits
00056         exit(0);
00057     }
00058 
00059     // become leader of a new session
00060     setsid();
00061 
00062     // chdir to root so we don't keep mounted filesystems points busy
00063     dummy = chdir("/");
00064 
00065     // mask off permissions for any but owner
00066     umask(077);
00067 
00068     // close open files.  we only expect stdin, stdout, stderr to be open.
00069     close(0);
00070     close(1);
00071     close(2);
00072 
00073     // attach file descriptors 0, 1, 2 to /dev/null so inadvertent use
00074     // of standard I/O safely goes in the bit bucket.
00075     open("/dev/null", O_RDONLY);
00076     open("/dev/null", O_RDWR);
00077     dummy = dup(1);
00078 
00079     // invoke function
00080     return func(1, &name);
00081 }

Generated on Fri Nov 6 00:21:13 2009 for synergy-plus by  doxygen 1.3.9.1