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 "src/__support/macros/config.h" 13 #include <spawn.h> // For mode_t 14 #include <stdint.h> 15 16 namespace LIBC_NAMESPACE_DECL { 17 18 struct BaseSpawnFileAction { 19 enum ActionType { 20 OPEN = 111, 21 CLOSE = 222, 22 DUP2 = 333, 23 }; 24 25 ActionType type; 26 BaseSpawnFileAction *next; 27 add_actionBaseSpawnFileAction28 static void add_action(posix_spawn_file_actions_t *actions, 29 BaseSpawnFileAction *act) { 30 if (actions->__back != nullptr) { 31 auto *back = reinterpret_cast<BaseSpawnFileAction *>(actions->__back); 32 back->next = act; 33 actions->__back = act; 34 } else { 35 // First action is being added. 36 actions->__front = actions->__back = act; 37 } 38 } 39 40 protected: BaseSpawnFileActionBaseSpawnFileAction41 explicit BaseSpawnFileAction(ActionType t) : type(t), next(nullptr) {} 42 }; 43 44 struct SpawnFileOpenAction : public BaseSpawnFileAction { 45 const char *path; 46 int fd; 47 int oflag; 48 mode_t mode; 49 SpawnFileOpenActionSpawnFileOpenAction50 SpawnFileOpenAction(const char *p, int fdesc, int flags, mode_t m) 51 : BaseSpawnFileAction(BaseSpawnFileAction::OPEN), path(p), fd(fdesc), 52 oflag(flags), mode(m) {} 53 }; 54 55 struct SpawnFileCloseAction : public BaseSpawnFileAction { 56 int fd; 57 SpawnFileCloseActionSpawnFileCloseAction58 SpawnFileCloseAction(int fdesc) 59 : BaseSpawnFileAction(BaseSpawnFileAction::CLOSE), fd(fdesc) {} 60 }; 61 62 struct SpawnFileDup2Action : public BaseSpawnFileAction { 63 int fd; 64 int newfd; 65 SpawnFileDup2ActionSpawnFileDup2Action66 SpawnFileDup2Action(int fdesc, int new_fdesc) 67 : BaseSpawnFileAction(BaseSpawnFileAction::DUP2), fd(fdesc), 68 newfd(new_fdesc) {} 69 }; 70 71 } // namespace LIBC_NAMESPACE_DECL 72 73 #endif // LLVM_LIBC_SRC_SPAWN_FILE_ACTIONS_H 74