Skip to content

Commit

Permalink
Merge pull request #10756 from rouault/CPLSpawn_fix
Browse files Browse the repository at this point in the history
CPLSpawn() (unix): correctly return the exit() code of the process
  • Loading branch information
rouault authored Sep 10, 2024
2 parents b986fba + 4de6f48 commit feb0e99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "cpl_auto_close.h"
#include "cpl_minixml.h"
#include "cpl_quad_tree.h"
#include "cpl_spawn.h"
#include "cpl_worker_thread_pool.h"
#include "cpl_vsi_virtual.h"
#include "cpl_threadsafe_queue.hpp"
Expand Down Expand Up @@ -5261,4 +5262,28 @@ TEST_F(test_cpl, CPLUTF8ForceToASCII)
}
}

#ifndef _WIN32
TEST_F(test_cpl, CPLSpawn)
{
VSIStatBufL sStatBuf;
if (VSIStatL("/bin/true", &sStatBuf) == 0)
{
const char *const apszArgs[] = {"/bin/true", nullptr};
EXPECT_EQ(CPLSpawn(apszArgs, nullptr, nullptr, false), 0);
}
if (VSIStatL("/bin/false", &sStatBuf) == 0)
{
const char *const apszArgs[] = {"/bin/false", nullptr};
EXPECT_EQ(CPLSpawn(apszArgs, nullptr, nullptr, false), 1);
}

{
const char *const apszArgs[] = {"/i_do/not/exist", nullptr};
CPLPushErrorHandler(CPLQuietErrorHandler);
EXPECT_EQ(CPLSpawn(apszArgs, nullptr, nullptr, false), -1);
CPLPopErrorHandler();
}
}
#endif

} // namespace
8 changes: 8 additions & 0 deletions port/cpl_spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,14 @@ int CPLSpawnAsyncFinish(CPLSpawnedProcess *p, int bWait, CPL_UNUSED int bKill)
}
else
{
if (WIFEXITED(status))
{
status = WEXITSTATUS(status);
}
else
{
status = -1;
}
break;
}
}
Expand Down

0 comments on commit feb0e99

Please sign in to comment.