Exemplo n.º 1
0
CMDArgs::CMDArgs(const char* args)
{
	m_pInternal = new CMDArgInternal();

	ConvertToArgs(args, m_pInternal->m_vArgList);

	m_pInternal->m_pszArgv = new const char*[m_pInternal->m_vArgList.size()];

	for (size_t x=0; x<m_pInternal->m_vArgList.size(); x++)
		m_pInternal->m_pszArgv[x] = m_pInternal->m_vArgList[x].c_str();

	process();
}
Exemplo n.º 2
0
bool launchProcess(const char* exe, const std::map<std::string, std::string> &info)
{
	if (!exe)
		return false;

	ERROR_OUTPUT(__func__);
	std::string fullExe = expandPath(exe);

	if (!fileExists(fullExe.c_str()))
		return false;

	//we double fork so that the new process is not a child of this process
	pid_t pid = fork();

	if (pid != 0)
	{
		int status;
		waitpid(pid, &status, 0);

		if (WEXITSTATUS(status) == 0)
			return true;
		else
			return false;
	}
	else {
		pid_t pid_kill = fork();
		if (pid_kill != 0) {
			exit(0);
		}
		else {
			UTIL::FS::Path path(fullExe.c_str(), "", true);
		
			std::string workingDir;
			std::string libPath;
			std::string args;
			std::string e = path.getFullPath();
		
			typedef const std::map<std::string, std::string>::const_iterator MapIterator;
		
			MapIterator &wd = info.find("wd");
			if (wd != info.end())
				workingDir = expandPath( (*wd).second.c_str() );
		
			if (workingDir == "")
				workingDir = path.getFolderPath();
		
			MapIterator &lp = info.find("lp");
			if (lp != info.end())
				libPath = (*lp).second.c_str();
		
			MapIterator &cla = info.find("cla");
			if (cla != info.end())
				args = (*cla).second.c_str();
		
		
			gcString orgLibPath = getenv("OLD_LIB_PATH");
		
			if (orgLibPath.size() > 0)
			{
				if (libPath.size() > 0)
					libPath += ":";
		
				libPath += orgLibPath;
			}
		
			if (libPath.size() > 0)
				setenv("LD_LIBRARY_PATH", libPath.c_str(), 1);
			else
				unsetenv("LD_LIBRARY_PATH");
		
		
			std::vector<std::string> out;
			const char* argv[10] = {0};
			argv[0] = e.c_str();
		
			if (args.size() > 0)
			{
				ConvertToArgs(args.c_str(), out);
		
				for (size_t x=0; x<out.size(); x++)
				{
					if (x == 9)
						break;
		
					argv[x+1] = out[x].c_str();
				}
			}
		
			(void)chdir(workingDir.c_str());
			execv(fullExe.c_str(), (char* const*)argv);
		
			printf("Failed to execl %s [%s] error: %d\n", fullExe.c_str(), args.c_str(), errno);
			exit(-1);
		}
	}
	return false;
}