diff options
| author | Siva Chandra Reddy <sivachandra@google.com> | 2022-10-10 08:35:21 +0000 |
|---|---|---|
| committer | Siva Chandra Reddy <sivachandra@google.com> | 2022-10-11 04:54:44 +0000 |
| commit | 28943d617a478cbaf032f9c7ff3c9830e62bd0af (patch) | |
| tree | 1bf5987c16f10c4c0463346c994a090a97ea9ada /libc/src/spawn | |
| parent | 66046e6e65e5dfe0b6e2222a467d07899a43b624 (diff) | |
[libc] Add POSIX functions posix_spawn_file_actions_*.
Namely, posix_spawn_file_actions_addclose,
posix_spawn_file_actions_adddup2, posix_spawn_file_actions_addopen,
posix_spawn_file_actions_destroy, posix_spawn_file_actions_init have
been added.
Reviewed By: michaelrj, lntue
Differential Revision: https://reviews.llvm.org/D135603
Diffstat (limited to 'libc/src/spawn')
| -rw-r--r-- | libc/src/spawn/CMakeLists.txt | 65 | ||||
| -rw-r--r-- | libc/src/spawn/file_actions.h | 58 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_addclose.cpp | 38 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_addclose.h | 21 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_adddup2.cpp | 40 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_adddup2.h | 21 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_addopen.cpp | 43 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_addopen.h | 22 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_destroy.cpp | 40 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_destroy.h | 20 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_init.cpp | 24 | ||||
| -rw-r--r-- | libc/src/spawn/posix_spawn_file_actions_init.h | 20 |
12 files changed, 412 insertions, 0 deletions
diff --git a/libc/src/spawn/CMakeLists.txt b/libc/src/spawn/CMakeLists.txt new file mode 100644 index 000000000000..95a0c7436d26 --- /dev/null +++ b/libc/src/spawn/CMakeLists.txt @@ -0,0 +1,65 @@ +add_header_library( + file_actions + HDRS + file_actions.h + DEPENDS + libc.include.spawn +) + +add_entrypoint_object( + posix_spawn_file_actions_init + SRCS + posix_spawn_file_actions_init.cpp + HDRS + posix_spawn_file_actions_init.h + DEPENDS + libc.include.spawn +) + +add_entrypoint_object( + posix_spawn_file_actions_destroy + SRCS + posix_spawn_file_actions_destroy.cpp + HDRS + posix_spawn_file_actions_destroy.h + DEPENDS + .file_actions + libc.include.errno + libc.include.spawn +) + +add_entrypoint_object( + posix_spawn_file_actions_adddup2 + SRCS + posix_spawn_file_actions_adddup2.cpp + HDRS + posix_spawn_file_actions_adddup2.h + DEPENDS + .file_actions + libc.include.errno + libc.include.spawn +) + +add_entrypoint_object( + posix_spawn_file_actions_addopen + SRCS + posix_spawn_file_actions_addopen.cpp + HDRS + posix_spawn_file_actions_addopen.h + DEPENDS + .file_actions + libc.include.errno + libc.include.spawn +) + +add_entrypoint_object( + posix_spawn_file_actions_addclose + SRCS + posix_spawn_file_actions_addclose.cpp + HDRS + posix_spawn_file_actions_addclose.h + DEPENDS + .file_actions + libc.include.errno + libc.include.spawn +) diff --git a/libc/src/spawn/file_actions.h b/libc/src/spawn/file_actions.h new file mode 100644 index 000000000000..11d9fb70d193 --- /dev/null +++ b/libc/src/spawn/file_actions.h @@ -0,0 +1,58 @@ +//===-- Spawn file actions -------------------------------------*- 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_FILE_ACTIONS_H +#define LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H + +#include <spawn.h> // For mode_t +#include <stdint.h> + +namespace __llvm_libc { + +struct BaseSpawnFileAction { + enum ActionType { + OPEN = 111, + CLOSE = 222, + DUP2 = 333, + }; + + ActionType type; + BaseSpawnFileAction *next; +}; + +struct SpawnFileOpenAction : public BaseSpawnFileAction { + const char *path; + int fd; + int oflag; + mode_t mode; +}; + +struct SpawnFileCloseAction : public BaseSpawnFileAction { + int fd; +}; + +struct SpawnFileDup2Action : public BaseSpawnFileAction { + int fd; + int newfd; +}; + +inline void enque_spawn_action(posix_spawn_file_actions_t *actions, + BaseSpawnFileAction *act) { + if (actions->__back != nullptr) { + auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back); + back->next = act; + actions->__back = act; + } else { + // First action is being added. + actions->__front = actions->__back = act; + } +} + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H diff --git a/libc/src/spawn/posix_spawn_file_actions_addclose.cpp b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp new file mode 100644 index 000000000000..81d3878fe81d --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_addclose.cpp @@ -0,0 +1,38 @@ +//===-- Implementation of posix_spawn_file_actions_addclose ---------------===// +// +// 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_addclose.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_addclose, + (posix_spawn_file_actions_t *__restrict actions, int fd)) { + if (actions == nullptr) + return EINVAL; + if (fd < 0) + return EBADF; + + auto *act = reinterpret_cast<SpawnFileCloseAction *>( + malloc(sizeof(SpawnFileCloseAction))); + if (act == nullptr) + return ENOMEM; + + act->type = BaseSpawnFileAction::CLOSE; + act->fd = fd; + enque_spawn_action(actions, act); + return 0; +} + +} // namespace __llvm_libc diff --git a/libc/src/spawn/posix_spawn_file_actions_addclose.h b/libc/src/spawn/posix_spawn_file_actions_addclose.h new file mode 100644 index 000000000000..5eda292760da --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_addclose.h @@ -0,0 +1,21 @@ +//===-- Impl header for posix_spawn_file_actions_addclose -------*- 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_ADDCLOSE_H +#define LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE_H + +#include <spawn.h> + +namespace __llvm_libc { + +int posix_spawn_file_actions_addclose( + posix_spawn_file_actions_t *__restrict actions, int fd); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSE_H diff --git a/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp new file mode 100644 index 000000000000..a6a7f07110e9 --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_adddup2.cpp @@ -0,0 +1,40 @@ +//===-- Implementation of posix_spawn_file_actions_adddup2 ----------------===// +// +// 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_adddup2.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_adddup2, + (posix_spawn_file_actions_t * actions, int fd, int newfd)) { + if (actions == nullptr) + return EINVAL; + if (fd < 0 || newfd < 0) + return EBADF; + + auto *act = reinterpret_cast<SpawnFileDup2Action *>( + malloc(sizeof(SpawnFileDup2Action))); + if (act == nullptr) + return ENOMEM; + + act->type = BaseSpawnFileAction::DUP2; + act->fd = fd; + act->newfd = newfd; + act->next = nullptr; + enque_spawn_action(actions, act); + return 0; +} + +} // namespace __llvm_libc diff --git a/libc/src/spawn/posix_spawn_file_actions_adddup2.h b/libc/src/spawn/posix_spawn_file_actions_adddup2.h new file mode 100644 index 000000000000..33444184f973 --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_adddup2.h @@ -0,0 +1,21 @@ +//===-- Impl header for posix_spawn_file_actions_adddup2 --------*- 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_ADDDUP2_H +#define LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2_H + +#include <spawn.h> + +namespace __llvm_libc { + +int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *actions, + int fd, int newfd); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_SPAWN_POSIX_SPAWN_FILE_ACTIONS_ADDDUP2_H diff --git a/libc/src/spawn/posix_spawn_file_actions_addopen.cpp b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp new file mode 100644 index 000000000000..ac286631c056 --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_addopen.cpp @@ -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 diff --git a/libc/src/spawn/posix_spawn_file_actions_addopen.h b/libc/src/spawn/posix_spawn_file_actions_addopen.h new file mode 100644 index 000000000000..ff757d42e514 --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_addopen.h @@ -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 diff --git a/libc/src/spawn/posix_spawn_file_actions_destroy.cpp b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp new file mode 100644 index 000000000000..83346462c95b --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_destroy.cpp @@ -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 diff --git a/libc/src/spawn/posix_spawn_file_actions_destroy.h b/libc/src/spawn/posix_spawn_file_actions_destroy.h new file mode 100644 index 000000000000..89f3c55dd99b --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_destroy.h @@ -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 diff --git a/libc/src/spawn/posix_spawn_file_actions_init.cpp b/libc/src/spawn/posix_spawn_file_actions_init.cpp new file mode 100644 index 000000000000..99502ff44962 --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_init.cpp @@ -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 diff --git a/libc/src/spawn/posix_spawn_file_actions_init.h b/libc/src/spawn/posix_spawn_file_actions_init.h new file mode 100644 index 000000000000..da354e1b0eba --- /dev/null +++ b/libc/src/spawn/posix_spawn_file_actions_init.h @@ -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 |
