1 //===-- Spawn file actions -------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H 10 #define LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H 11 12 #include <spawn.h> // For mode_t 13 #include <stdint.h> 14 15 namespace LIBC_NAMESPACE { 16 17 struct BaseSpawnFileAction { 18 enum ActionType { 19 OPEN = 111, 20 CLOSE = 222, 21 DUP2 = 333, 22 }; 23 24 ActionType type; 25 BaseSpawnFileAction *next; 26 add_actionBaseSpawnFileAction27 static void add_action(posix_spawn_file_actions_t *actions, 28 BaseSpawnFileAction *act) { 29 if (actions->__back != nullptr) { 30 auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back); 31 back->next = act; 32 actions->__back = act; 33 } else { 34 // First action is being added. 35 actions->__front = actions->__back = act; 36 } 37 } 38 39 protected: BaseSpawnFileActionBaseSpawnFileAction40 explicit BaseSpawnFileAction(ActionType t) : type(t), next(nullptr) {} 41 }; 42 43 struct SpawnFileOpenAction : public BaseSpawnFileAction { 44 const char *path; 45 int fd; 46 int oflag; 47 mode_t mode; 48 SpawnFileOpenActionSpawnFileOpenAction49 SpawnFileOpenAction(const char *p, int fdesc, int flags, mode_t m) 50 : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), path(p), fd(fdesc), 51 oflag(flags), mode(m) {} 52 }; 53 54 struct SpawnFileCloseAction : public BaseSpawnFileAction { 55 int fd; 56 SpawnFileCloseActionSpawnFileCloseAction57 SpawnFileCloseAction(int fdesc) 58 : BaseSpawnFileAction(BaseSpawnFileAction::CLOSE), fd(fdesc) {} 59 }; 60 61 struct SpawnFileDup2Action : public BaseSpawnFileAction { 62 int fd; 63 int newfd; 64 SpawnFileDup2ActionSpawnFileDup2Action65 SpawnFileDup2Action(int fdesc, int new_fdesc) 66 : BaseSpawnFileAction(BaseSpawnFileAction::DUP2), fd(fdesc), 67 newfd(new_fdesc) {} 68 }; 69 70 } // namespace LIBC_NAMESPACE 71 72 #endif // LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H 73