no need close before dup2

Signed-off-by: Caoruihong <crh.cao@huawei.com>
This commit is contained in:
Caoruihong 2024-05-08 23:50:53 +08:00
parent 0036631e75
commit 1a02b6955e

View File

@ -255,11 +255,7 @@ static int SetUidGid(const AppSpawnMgr *content, const AppSpawningCtx *property)
static int32_t SetFileDescriptors(const AppSpawnMgr *content, const AppSpawningCtx *property)
{
#ifndef APPSPAWN_TEST
// close stdin stdout stderr
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
// redirect to /dev/null
// redirect stdin stdout stderr to /dev/null
int devNullFd = open(DEVICE_NULL_STR, O_RDWR);
if (devNullFd == -1) {
APPSPAWN_LOGE("open dev_null error: %{public}d", errno);
@ -270,18 +266,21 @@ static int32_t SetFileDescriptors(const AppSpawnMgr *content, const AppSpawningC
if (dup2(devNullFd, STDIN_FILENO) == -1) {
APPSPAWN_LOGE("dup2 STDIN error: %{public}d", errno);
return (-errno);
};
}
// stdout
if (dup2(devNullFd, STDOUT_FILENO) == -1) {
APPSPAWN_LOGE("dup2 STDOUT error: %{public}d", errno);
return (-errno);
};
}
// stderr
if (dup2(devNullFd, STDERR_FILENO) == -1) {
APPSPAWN_LOGE("dup2 STDERR error: %{public}d", errno);
return (-errno);
};
}
if (devNullFd > STDERR_FILENO) {
close(devNullFd);
}
#endif
return 0;
}