43 changes: 43 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_addopen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===-- Implementation of posix_spawn_file_actions_addopen ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "posix_spawn_file_actions_addopen.h"

#include "file_actions.h"
#include "src/__support/common.h"

#include <errno.h>
#include <spawn.h>
#include <stdlib.h> // For malloc

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addopen,
(posix_spawn_file_actions_t *__restrict actions, int fd,
const char *__restrict path, int oflag, mode_t mode)) {
if (actions == nullptr)
return EINVAL;
if (fd < 0)
return EBADF;

auto *act = reinterpret_cast<SpawnFileOpenAction *>(
malloc(sizeof(SpawnFileOpenAction)));
if (act == nullptr)
return ENOMEM;

act->type = BaseSpawnFileAction::OPEN;
act->fd = fd;
act->path = path;
act->oflag = oflag;
act->mode = mode;
act->next = nullptr;
enque_spawn_action(actions, act);
return 0;
}

} // namespace __llvm_libc
22 changes: 22 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_addopen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Impl header for posix_spawn_file_actions_addopen --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN_H
#define LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN_H

#include <spawn.h>

namespace __llvm_libc {

int posix_spawn_file_actions_addopen(
posix_spawn_file_actions_t *__restrict actions, int fd,
const char *__restrict path, int oflag, mode_t mode);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDOPEN_H
40 changes: 40 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_destroy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===-- Implementation of posix_spawn_file_actions_destroy ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "posix_spawn_file_actions_destroy.h"

#include "file_actions.h"
#include "src/__support/common.h"

#include <errno.h>
#include <spawn.h>
#include <stdlib.h> // For free

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_destroy,
(posix_spawn_file_actions_t * actions)) {
if (actions == nullptr)
return EINVAL;
if (actions->__front == nullptr)
return 0;

auto *act = reinterpret_cast<BaseSpawnFileAction *>(actions->__front);
actions->__front = nullptr;
actions->__back = nullptr;

while (act != nullptr) {
auto *next = act->next;
free(act);
act = next;
}

return 0;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_destroy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Impl header for posix_spawn_file_actions_destroy --------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_DESTROY_H
#define LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_DESTROY_H

#include <spawn.h>

namespace __llvm_libc {

int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *actions);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_DESTROY_H
24 changes: 24 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation of posix_spawn_file_actions_init -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "posix_spawn_file_actions_init.h"

#include "src/__support/common.h"

#include <spawn.h>

namespace __llvm_libc {

LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_init,
(posix_spawn_file_actions_t * actions)) {
actions->__front = nullptr;
actions->__back = nullptr;
return 0;
}

} // namespace __llvm_libc
20 changes: 20 additions & 0 deletions libc/src/spawn/posix_spawn_file_actions_init.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for posix_spawn_file_actions_init -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_INIT_H
#define LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_INIT_H

#include <spawn.h>

namespace __llvm_libc {

int posix_spawn_file_actions_init(posix_spawn_file_actions_t *actions);

} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_INIT_H
1 change: 1 addition & 0 deletions libc/test/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_subdirectory(dirent)
# since assert uses the signal API, we disable assert also.
# add_subdirectory(assert)
add_subdirectory(signal)
add_subdirectory(spawn)
add_subdirectory(time)

if(${LIBC_TARGET_OS} STREQUAL "linux")
Expand Down
18 changes: 18 additions & 0 deletions libc/test/src/spawn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_libc_testsuite(libc_spawn_unittests)

add_libc_unittest(
posix_spawn_file_actions_test
SUITE
libc_spawn_unittests
SRCS
posix_spawn_file_actions_test.cpp
DEPENDS
libc.include.errno
libc.include.spawn
libc.src.spawn.file_actions
libc.src.spawn.posix_spawn_file_actions_addclose
libc.src.spawn.posix_spawn_file_actions_adddup2
libc.src.spawn.posix_spawn_file_actions_addopen
libc.src.spawn.posix_spawn_file_actions_destroy
libc.src.spawn.posix_spawn_file_actions_init
)
75 changes: 75 additions & 0 deletions libc/test/src/spawn/posix_spawn_file_actions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===-- Unittests for posix_spwan_file_actions_t manipulation -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/spawn/file_actions.h"
#include "src/spawn/posix_spawn_file_actions_addclose.h"
#include "src/spawn/posix_spawn_file_actions_adddup2.h"
#include "src/spawn/posix_spawn_file_actions_addopen.h"
#include "src/spawn/posix_spawn_file_actions_destroy.h"
#include "src/spawn/posix_spawn_file_actions_init.h"
#include "utils/UnitTest/Test.h"

#include <errno.h>
#include <spawn.h>
#include <stdint.h>

TEST(LlvmLibcPosixSpawnFileActionsTest, AddActions) {
posix_spawn_file_actions_t actions;
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_init(&actions), 0);

ASSERT_EQ(uintptr_t(actions.__front), uintptr_t(nullptr));
ASSERT_EQ(uintptr_t(actions.__back), uintptr_t(nullptr));

ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_addclose(&actions, 10), 0);
ASSERT_NE(uintptr_t(actions.__front), uintptr_t(nullptr));
ASSERT_NE(uintptr_t(actions.__back), uintptr_t(nullptr));

ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_adddup2(&actions, 11, 12), 0);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_addopen(&actions, 13,
"path/to/file", 0, 0),
0);

__llvm_libc::BaseSpawnFileAction *act =
reinterpret_cast<__llvm_libc::BaseSpawnFileAction *>(actions.__front);
int action_count = 0;
while (act != nullptr) {
++action_count;
if (action_count == 1)
ASSERT_EQ(act->type, __llvm_libc::BaseSpawnFileAction::CLOSE);
if (action_count == 2)
ASSERT_EQ(act->type, __llvm_libc::BaseSpawnFileAction::DUP2);
if (action_count == 3)
ASSERT_EQ(act->type, __llvm_libc::BaseSpawnFileAction::OPEN);
act = act->next;
}
ASSERT_EQ(action_count, 3);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_destroy(&actions), 0);
}

TEST(LlvmLibcPosixSpawnFileActionsTest, InvalidActions) {
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_addclose(nullptr, 1), EINVAL);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_adddup2(nullptr, 1, 2),
EINVAL);
ASSERT_EQ(
__llvm_libc::posix_spawn_file_actions_addopen(nullptr, 1, nullptr, 0, 0),
EINVAL);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_destroy(nullptr), EINVAL);

posix_spawn_file_actions_t actions;
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_init(&actions), 0);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_addclose(&actions, -1),
EBADF);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_adddup2(&actions, -1, 2),
EBADF);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_adddup2(&actions, 1, -2),
EBADF);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_addopen(&actions, -1, nullptr,
0, 0),
EBADF);
ASSERT_EQ(__llvm_libc::posix_spawn_file_actions_destroy(&actions), 0);
}