progress in using ByteArray+std::string

This commit is contained in:
csoler 2021-12-01 23:05:16 +01:00
parent 6a4cdcc471
commit d7afbea1dd
15 changed files with 171 additions and 858 deletions

View file

@ -120,6 +120,63 @@ std::string TorProcess::errorMessage() const
return d->errorMessage;
}
// Does a fopen, but dup all file descriptors (STDIN STDOUT and STDERR) to the
// FDs supplied by the parent process
int popen3(int fd[3],const char **const cmd)
{
int i, e;
int p[3][2];
pid_t pid;
// set all the FDs to invalid
for(i=0; i<3; i++)
p[i][0] = p[i][1] = -1;
// create the pipes
for(int i=0; i<3; i++)
if(pipe(p[i]))
goto error;
// and fork
pid = fork();
if(-1 == pid)
goto error;
// in the parent?
if(pid) {
// parent
fd[STDIN_FILENO] = p[STDIN_FILENO][1];
close(p[STDIN_FILENO][0]);
fd[STDOUT_FILENO] = p[STDOUT_FILENO][0];
close(p[STDOUT_FILENO][1]);
fd[STDERR_FILENO] = p[STDERR_FILENO][0];
close(p[STDERR_FILENO][1]);
// success
return 0;
} else {
// child
dup2(p[STDIN_FILENO][0],STDIN_FILENO);
close(p[STDIN_FILENO][1]);
dup2(p[STDOUT_FILENO][1],STDOUT_FILENO);
close(p[STDOUT_FILENO][0]);
dup2(p[STDERR_FILENO][1],STDERR_FILENO);
close(p[STDERR_FILENO][0]);
// here we try and run it
execv(*cmd,const_cast<char*const*>(cmd));
// if we are there, then we failed to launch our program
perror("Could not launch");
fprintf(stderr," \"%s\"\n",*cmd);
_exit(EXIT_FAILURE);
}
error:
// preserve original error
e = errno;
for(i=0; i<3; i++) {
close(p[i][0]);
close(p[i][1]);
}
errno = e;
return -1;
}
void TorProcess::start()
{
if (state() > NotStarted)
@ -283,12 +340,14 @@ bool TorProcessPrivate::ensureFilesExist()
std::string TorProcessPrivate::torrcPath() const
{
return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("torrc");
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("torrc");
return dataDir + "/" + "torrc";
}
std::string TorProcessPrivate::controlPortFilePath() const
{
return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("control-port");
//return QDir::toNativeSeparators(dataDir) + QDir::separator() + QStringLiteral("control-port");
return dataDir + "/" + "control-port";
}
void TorProcessPrivate::processStarted()
@ -369,3 +428,7 @@ void TorProcessPrivate::tryReadControlPort()
}
}
void TorProcess::run()
{
//
}