/*
* Copyright (C) 2012 Yee Young Han (http://blog.naver.com/websearch)
*
* 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 3 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 "SipPlatformDefine.h"
#include "ServerUtility.h"
#include "Log.h"
#include
#include
#ifndef WIN32
#include
#include
#include
#ifndef __APPLE__
#include
#endif
#include
#include
#include
#include
#include
#include
#endif
#include "MemoryDebug.h"
/**
* @ingroup SipPlatform
* @brief ¸®´ª½º¿¡¼ ÀÏ¹Ý »ç¿ëÀÚ·Î ¼öÁ¤ÇÑ ÈÄ, core dump ÆÄÀÏ »ý¼º °¡´ÉÇÏ°Ô ÇÑ´Ù.
*/
void SetCoreDumpEnable()
{
#ifndef WIN32
#ifndef __APPLE__
prctl(PR_SET_DUMPABLE, 1);
#endif
#endif
}
/**
* @ingroup SipPlatform
* @brief µ¥¸ó ¸ðµå·Î µ¿ÀÛÇÑ´Ù.
* @param bIsFork µ¥¸ó ¸ðµå·Î µ¿ÀÛÇϸé true ¸¦ ³Ö¾îÁÖ°í ±×·¸Áö ¾ÊÀ¸¸é false ¸¦ ³Ö¾îÁØ´Ù.
* @returns true ¸¦ ¸®ÅÏÇÑ´Ù.
*/
bool Fork( bool bIsFork )
{
#ifndef WIN32
if( bIsFork )
{
if( fork() != 0 ) exit(0);
// 1¹øÂ° ÀÚ½Ä ÇÁ·Î¼¼½º ½ÃÀÛ -> ¼¼¼Ç ¸®´õ·Î º¯°æÇÑ´Ù.
setsid();
signal( SIGHUP, SIG_IGN );
if( fork() != 0 ) exit(1);
// 2¹øÂ° ÀÚ½Ä ÇÁ·Î¼¼½º ½ÃÀÛ -> ÀÛ¾÷ µð·ºÅ丮¸¦ º¯°æÇÑ´Ù.
if( chdir( "/tmp" ) == -1 )
{
CLog::Print( LOG_ERROR, "chdir(/tmp) error" );
}
}
#endif
return true;
}
/**
* @ingroup SipPlatform
* @brief ÇÁ·Î±×·¥ ½ÇÇà »ç¿ëÀÚ¸¦ ¼öÁ¤ÇÑ´Ù.
* @param pszUserId ÇÁ·Î±×·¥ ½ÇÇà »ç¿ëÀÚ
* @returns ¼º°øÇϸé true ¸¦ ¸®ÅÏÇÏ°í ½ÇÆÐÇϸé false ¸¦ ¸®ÅÏÇÑ´Ù.
*/
bool ChangeExecuteUser( const char * pszUserId )
{
#ifndef WIN32
struct passwd sttPassWord, *psttPassWord = NULL;
char szError[255], szPassWord[1024];
if( getpwnam_r( pszUserId, &sttPassWord, szPassWord, sizeof(szPassWord), &psttPassWord ) != 0 || psttPassWord == NULL )
{
CLog::Print( LOG_ERROR, "getpwnam(%s) error(%d) - %s", pszUserId, errno, strerror_r( errno, szError, sizeof(szError) ) );
return false;
}
if( setuid( psttPassWord->pw_uid ) != 0 )
{
CLog::Print( LOG_ERROR, "seteuid(%s) error(%d) - %s", pszUserId, errno, strerror_r( errno, szError, sizeof(szError) ) );
return false;
}
if( psttPassWord->pw_dir )
{
if( chdir( psttPassWord->pw_dir ) == -1 )
{
CLog::Print( LOG_ERROR, "chdir(%s) error", psttPassWord->pw_dir );
}
#ifndef __APPLE__
prctl(PR_SET_DUMPABLE, 1);
#endif
}
#endif
return true;
}
/**
* @ingroup SipPlatform
* @brief ¾²·¹µå¸¦ ½ÃÀÛÇÑ´Ù.
* @param pszName ¾²·¹µå À̸§
* @param lpStartAddress ¾²·¹µå ÇÔ¼ö
* @param lpParameter ¾²·¹µå ÀÎÀÚ
* @returns ¼º°øÇϸé true ¸¦ ¸®ÅÏÇÏ°í ½ÇÆÐÇϸé false ¸¦ ¸®ÅÏÇÑ´Ù.
*/
#ifdef WIN32
bool StartThread( const char * pszName, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter )
{
DWORD dwThreadId;
HANDLE hThread;
hThread = CreateThread( NULL, 0, lpStartAddress, lpParameter, 0, &dwThreadId );
if( hThread == NULL )
{
return false;
}
CloseHandle( hThread );
return true;
}
#else
bool StartThread( const char * pszName, void *(*lpStartAddress)(void*), void * lpParameter )
{
pthread_t iThread;
if( pthread_create( &iThread, NULL, lpStartAddress, lpParameter ) != 0 )
{
return false;
}
pthread_detach( iThread );
return true;
}
#endif