📄 daemon.cpp
字号:
/******************************************************************************** daemon.cpp:*-------------------------------------------------------------------------------* (c)1999-2002 VideoLAN* $Id: daemon.cpp,v 1.2 2002/10/07 15:01:22 sam Exp $** Authors: Jean-Paul Saman <saman@natlab.research.philips.com>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.**-------------------------------------------------------------------------------********************************************************************************/#include <stdio.h>#include <stdlib.h>#ifndef _WIN32# include <sys/time.h># include <sys/resource.h># include <sys/stat.h># include <fcntl.h># include <unistd.h>#endif#include "daemon.h"C_Daemon::C_Daemon(){ m_isDaemon = false;}int C_Daemon::StartDaemon(){ int status = -1; status = DaemonFork(); if (status != -1) status = DaemonLeaveGroup(); if (status != -1) status = DaemonFork(); if (status != -1) DaemonCloseTerminal(); m_isDaemon = (status == 0); return status;}int C_Daemon::StopDaemon(){ exit(1); return -1;}bool C_Daemon::isDaemon(){ return m_isDaemon;}int C_Daemon::DaemonFork(){ int status = -1;#ifndef _WIN32 status = fork(); switch (status) { case -1: perror((const char*)fork()); exit(1); case 0: /* child process */ break; default: /* parent process */ exit(0); }#endif return status;}int C_Daemon::DaemonLeaveGroup(){ int status = -1;#ifndef _WIN32 status = setsid(); if (-1 == status) { perror((const char*)setsid()); exit(1); }#endif return status;}void C_Daemon::DaemonCloseTerminal(){#ifndef _WIN32 int fileDesc = -1; /* * now we are in a new session and process * group than process that started the * daemon. We also have no controlling * terminal */ chdir("/"); umask(0); fileDesc = open("/dev/null", O_RDWR);/* stdin */ (void) dup(fileDesc); /* stdout */ (void) dup(fileDesc); /* stderr */#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -