Fix bug 31323

This commit is contained in:
Oleg Korshul
2020-06-23 11:15:50 +03:00
parent b95065fd70
commit d49bed47b6

View File

@ -49,6 +49,73 @@
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
extern char** environ;
char** linux_environ_get(const std::string& str_library_path = "")
{
int count = 0;
for (int i = 0; environ[i] != NULL; i++)
++count;
count += 2;
char** env = new char*[count];
for (int i = 0; i < count; i++)
env[i] = NULL;
bool is_ld_library_path = false;
for (int i = 0; environ[i] != NULL; i++)
{
std::string s = environ[i];
if (0 == s.find("LD_LIBRARY_PATH"))
{
s += (":" + str_library_path);
is_ld_library_path = true;
}
env[i] = new char[s.length() + 1];
memcpy(env[i], s.c_str(), s.length() * sizeof(char));
env[i][s.length()] = '\0';
}
if (!is_ld_library_path)
{
int index = count - 2;
std::string s = "LD_LIBRARY_PATH=";
s += str_library_path;
env[index] = new char[s.length() + 1];
memcpy(env[index], s.c_str(), s.length() * sizeof(char));
env[index][s.length()] = '\0';
}
return env;
}
void linux_environ_clear(char** data)
{
for (int i = 0; data[i] != NULL; i++)
delete [] data[i];
delete [] data;
}
void linux_environ_print(char** env)
{
std::wstring file = NSFile::GetProcessDirectory() + L"/env.log";
std::string fileA = U_TO_UTF8(file);
for (int i = 0; env[i] != NULL; i++)
{
FILE* f = fopen(fileA.c_str(), "a+");
std::string s = env[i];
// replace %%
size_t posn = 0;
while (std::string::npos != (posn = s.find("%", posn)))
{
s.replace(posn, 1, "%%");
posn += 2;
}
fprintf(f, s.c_str());
fprintf(f, "\n");
fclose(f);
}
}
#endif
class CHtmlFile_Private
@ -432,10 +499,11 @@ int CHtmlFile::Convert(const std::vector<std::wstring>& arFiles, const std::wstr
std::string::size_type posLast = sProgramm.find_last_of('/');
std::string sProgrammDir = sProgramm.substr(0, posLast);
if (std::string::npos != posLast)
sLibraryDir = "LD_LIBRARY_PATH=" + sProgrammDir + ":" + sProgrammDir + "/../";
sLibraryDir = sProgrammDir + ":" + sProgrammDir + "/../";
if (!IsLinuxXVFB())
{
sLibraryDir = "LD_LIBRARY_PATH=" + sLibraryDir;
const char* nargs[2];
nargs[0] = sXmlA.c_str();
nargs[1] = NULL;
@ -461,12 +529,17 @@ int CHtmlFile::Convert(const std::vector<std::wstring>& arFiles, const std::wstr
nargs[4] = sXmlA.c_str();
nargs[5] = NULL;
/*
const char* nenv[4];
nenv[0] = sLibraryDir.c_str();
nenv[1] = NULL;//"DISPLAY=:99";
nenv[2] = NULL;
*/
execve("/usr/bin/xvfb-run", (char * const *)nargs, (char * const *)nenv);
char** env = linux_environ_get(sLibraryDir);
//linux_environ_print(env);
execve("/usr/bin/xvfb-run", (char * const *)nargs, (char * const *)env);
linux_environ_clear(env);
exit(EXIT_SUCCESS);
}