daemon.cpp
来自「可用该程序将avi的电影文件转化为TS流」· C++ 代码 · 共 124 行
CPP
124 行
/******************************************************************************** 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 + =
减小字号Ctrl + -
显示快捷键?