FILE *tmpfile(const wchar_t *prefix) { std::wstring sprefix = strutil::format(L"%s.%d.", prefix, GetCurrentProcessId()); wchar_t *tmpname = _wtempnam(0, sprefix.c_str()); std::shared_ptr<wchar_t> tmpname_p(tmpname, std::free); HANDLE fh = CreateFileW(prefixed_path(tmpname).c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, 0); if (fh == INVALID_HANDLE_VALUE) throw_error(tmpname, GetLastError()); int fd = _open_osfhandle(reinterpret_cast<intptr_t>(fh), _O_BINARY|_O_RDWR); if (fd == -1) { CloseHandle(fh); util::throw_crt_error("win32::tmpfile: open_osfhandle()"); } FILE *fp = _fdopen(fd, "w+"); if (!fp) { _close(fd); util::throw_crt_error("win32::tmpfile: _fdopen()"); } return fp; }
char *load_with_mmap(const wchar_t *path, uint64_t *size) { std::wstring fullpath = prefixed_path(path); HANDLE hFile = CreateFileW(fullpath.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); if (hFile == INVALID_HANDLE_VALUE) throw_error(fullpath, GetLastError()); DWORD sizeHi, sizeLo; sizeLo = GetFileSize(hFile, &sizeHi); *size = (static_cast<uint64_t>(sizeHi) << 32) | sizeLo; HANDLE hMap = CreateFileMappingW(hFile, 0, PAGE_READONLY, 0, 0, 0); DWORD err = GetLastError(); CloseHandle(hFile); if (hMap <= 0) throw_error("CreateFileMapping", err); char *view = reinterpret_cast<char*>( MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)); CloseHandle(hMap); return view; }
void UbuntuLauncher::launchApplication(const std::string & launchInfo) { LOG4CPLUS_TRACE_METHOD(sLogger, __PRETTY_FUNCTION__ ); LOG4CPLUS_INFO(sLogger, "launch info: " + launchInfo); char rpath[PATH_MAX] = ""; char * params[] = { rpath, 0, 0, 0 }; std::string prefixed_path(PREFIX); prefixed_path += launchInfo; LOG4CPLUS_INFO(sLogger, "prefixed_path: " + prefixed_path); if (realpath(prefixed_path.c_str(), rpath) != rpath) { LOG4CPLUS_ERROR(sLogger, "realpath failed " + convertIntegerToString(errno) + std::string(strerror(errno))); return; } if (rpath != NULL) { LOG4CPLUS_INFO(sLogger, "rpath: "+ std::string(rpath)); } std::string dir = get_dir_from_path(rpath); pid_t pid = fork(); switch (pid) { case -1: LOG4CPLUS_ERROR(sLogger, "fork returned -1"); if (mpHandler) { mpHandler->launchError(launchInfo); } return; break; case 0: // changing current dir, so application will not be confused if (-1 == chdir(dir.c_str())) { int saved_errno = errno; fputs(("chdir failed: " + convertIntegerToString(saved_errno) + strerror(saved_errno)).c_str(), stderr); exit(1); } // starting application execv(rpath, params); { int saved_errno = errno; fputs(("execv failed: " + convertIntegerToString(saved_errno) + strerror(saved_errno)).c_str(), stderr); exit(1); } break; default: LOG4CPLUS_INFO(sLogger, "app launched"); if (mpHandler) { LOG4CPLUS_INFO(sLogger, "say handler : app launched"); mpHandler->launchedApp(launchInfo,pid); } break; } }