posix_spawn: add closefrom non-portable action

(cherry picked from commit a18ddf7757)
This commit is contained in:
Konstantin Belousov 2021-11-28 00:54:16 +02:00
parent ef39284e94
commit 54cdfdf12a
3 changed files with 28 additions and 0 deletions

View file

@ -92,6 +92,8 @@ int posix_spawn_file_actions_addchdir_np(posix_spawn_file_actions_t *
__restrict, const char * __restrict);
int posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t *,
int);
int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t *,
int);
#endif
/*

View file

@ -437,6 +437,7 @@ FBSD_1.6 {
FBSD_1.7 {
posix_spawn_file_actions_addchdir_np;
posix_spawn_file_actions_addclosefrom_np;
posix_spawn_file_actions_addfchdir_np;
};

View file

@ -68,6 +68,7 @@ typedef struct __posix_spawn_file_actions_entry {
FAE_CLOSE,
FAE_CHDIR,
FAE_FCHDIR,
FAE_CLOSEFROM,
} fae_action;
int fae_fildes;
@ -190,6 +191,9 @@ process_file_actions_entry(posix_spawn_file_actions_entry_t *fae)
if (fchdir(fae->fae_fildes) != 0)
return (errno);
break;
case FAE_CLOSEFROM:
closefrom(fae->fae_fildes);
break;
}
return (0);
}
@ -533,6 +537,27 @@ posix_spawn_file_actions_addfchdir_np(posix_spawn_file_actions_t *__restrict fa,
return (0);
}
int
posix_spawn_file_actions_addclosefrom_np (posix_spawn_file_actions_t *
__restrict fa, int from)
{
posix_spawn_file_actions_entry_t *fae;
if (from < 0)
return (EBADF);
/* Allocate object */
fae = malloc(sizeof(posix_spawn_file_actions_entry_t));
if (fae == NULL)
return (errno);
fae->fae_action = FAE_CLOSEFROM;
fae->fae_fildes = from;
STAILQ_INSERT_TAIL(&(*fa)->fa_list, fae, fae_list);
return (0);
}
/*
* Spawn attributes
*/