xref: /aosp_15_r20/external/llvm-libc/src/spawn/posix_spawn_file_actions_addclose.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of posix_spawn_file_actions_addclose ---------------===//
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 #include "posix_spawn_file_actions_addclose.h"
10 
11 #include "file_actions.h"
12 #include "src/__support/CPP/new.h"
13 #include "src/__support/common.h"
14 #include "src/__support/macros/config.h"
15 #include "src/errno/libc_errno.h"
16 
17 #include <spawn.h>
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(int, posix_spawn_file_actions_addclose,
22                    (posix_spawn_file_actions_t *__restrict actions, int fd)) {
23   if (actions == nullptr)
24     return EINVAL;
25   if (fd < 0)
26     return EBADF;
27 
28   AllocChecker ac;
29   auto *act = new (ac) SpawnFileCloseAction(fd);
30   if (!ac)
31     return ENOMEM;
32   BaseSpawnFileAction::add_action(actions, act);
33 
34   return 0;
35 }
36 
37 } // namespace LIBC_NAMESPACE_DECL
38