1 1.1 martin /*- 2 1.1 martin * Copyright (c) 2008 Ed Schouten <ed (at) FreeBSD.org> 3 1.1 martin * All rights reserved. 4 1.1 martin * 5 1.1 martin * Redistribution and use in source and binary forms, with or without 6 1.1 martin * modification, are permitted provided that the following conditions 7 1.1 martin * are met: 8 1.1 martin * 1. Redistributions of source code must retain the above copyright 9 1.1 martin * notice, this list of conditions and the following disclaimer. 10 1.1 martin * 2. Redistributions in binary form must reproduce the above copyright 11 1.1 martin * notice, this list of conditions and the following disclaimer in the 12 1.1 martin * documentation and/or other materials provided with the distribution. 13 1.1 martin * 14 1.1 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 1.1 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 1.1 martin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 1.1 martin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 1.1 martin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 1.1 martin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 1.1 martin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 1.1 martin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 1.1 martin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 1.1 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 1.1 martin * SUCH DAMAGE. 25 1.1 martin */ 26 1.1 martin 27 1.1 martin #include <sys/cdefs.h> 28 1.5 christos __RCSID("$NetBSD: posix_spawn_fileactions.c,v 1.5 2021/11/07 14:34:30 christos Exp $"); 29 1.1 martin 30 1.1 martin #include "namespace.h" 31 1.1 martin 32 1.1 martin #include <errno.h> 33 1.1 martin #include <fcntl.h> 34 1.1 martin #include <sched.h> 35 1.1 martin #include <signal.h> 36 1.1 martin #include <stdlib.h> 37 1.1 martin #include <string.h> 38 1.1 martin #include <unistd.h> 39 1.1 martin #include <spawn.h> 40 1.1 martin 41 1.1 martin #define MIN_SIZE 16 42 1.1 martin 43 1.1 martin /* 44 1.1 martin * File descriptor actions 45 1.1 martin */ 46 1.1 martin 47 1.1 martin int 48 1.1 martin posix_spawn_file_actions_init(posix_spawn_file_actions_t *fa) 49 1.1 martin { 50 1.1 martin if (fa == NULL) 51 1.4 martin return EINVAL; 52 1.1 martin 53 1.1 martin fa->fae = malloc(MIN_SIZE * sizeof(struct posix_spawn_file_actions_entry)); 54 1.1 martin if (fa->fae == NULL) 55 1.4 martin return ENOMEM; 56 1.1 martin fa->size = MIN_SIZE; 57 1.1 martin fa->len = 0; 58 1.1 martin 59 1.4 martin return 0; 60 1.1 martin } 61 1.1 martin 62 1.1 martin int 63 1.1 martin posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *fa) 64 1.1 martin { 65 1.2 martin unsigned int i; 66 1.1 martin 67 1.1 martin if (fa == NULL) 68 1.4 martin return EINVAL; 69 1.1 martin 70 1.1 martin for (i = 0; i < fa->len; i++) { 71 1.1 martin if (fa->fae[i].fae_action == FAE_OPEN) 72 1.1 martin free(fa->fae[i].fae_path); 73 1.1 martin } 74 1.1 martin 75 1.1 martin free(fa->fae); 76 1.4 martin return 0; 77 1.1 martin } 78 1.1 martin 79 1.1 martin static int 80 1.3 martin posix_spawn_file_actions_getentry(posix_spawn_file_actions_t *fa, 81 1.3 martin unsigned int *i) 82 1.1 martin { 83 1.3 martin posix_spawn_file_actions_entry_t *fae; 84 1.3 martin 85 1.1 martin if (fa == NULL) 86 1.4 martin return EINVAL; 87 1.1 martin 88 1.1 martin if (fa->len < fa->size) 89 1.3 martin goto out; 90 1.1 martin 91 1.3 martin fae = realloc(fa->fae, (fa->size + MIN_SIZE) * sizeof(*fa->fae)); 92 1.3 martin if (fae == NULL) 93 1.4 martin return ENOMEM; 94 1.1 martin 95 1.3 martin fa->fae = fae; 96 1.1 martin fa->size += MIN_SIZE; 97 1.1 martin 98 1.3 martin out: 99 1.3 martin *i = fa->len; 100 1.4 martin return 0; 101 1.1 martin } 102 1.1 martin 103 1.1 martin int 104 1.1 martin posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa, 105 1.1 martin int fildes, const char * __restrict path, int oflag, mode_t mode) 106 1.1 martin { 107 1.3 martin char *faepath; 108 1.3 martin unsigned int i; 109 1.3 martin int error; 110 1.1 martin 111 1.1 martin if (fildes < 0) 112 1.4 martin return EBADF; 113 1.1 martin 114 1.3 martin error = posix_spawn_file_actions_getentry(fa, &i); 115 1.3 martin if (error) 116 1.4 martin return error; 117 1.3 martin 118 1.3 martin faepath = strdup(path); 119 1.3 martin if (faepath == NULL) 120 1.4 martin return ENOMEM; 121 1.1 martin 122 1.1 martin fa->fae[i].fae_action = FAE_OPEN; 123 1.3 martin fa->fae[i].fae_path = faepath; 124 1.1 martin fa->fae[i].fae_fildes = fildes; 125 1.1 martin fa->fae[i].fae_oflag = oflag; 126 1.1 martin fa->fae[i].fae_mode = mode; 127 1.1 martin fa->len++; 128 1.1 martin 129 1.4 martin return 0; 130 1.1 martin } 131 1.1 martin 132 1.1 martin int 133 1.1 martin posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa, 134 1.1 martin int fildes, int newfildes) 135 1.1 martin { 136 1.3 martin unsigned int i; 137 1.3 martin int error; 138 1.1 martin 139 1.1 martin if (fildes < 0 || newfildes < 0) 140 1.4 martin return EBADF; 141 1.1 martin 142 1.3 martin error = posix_spawn_file_actions_getentry(fa, &i); 143 1.3 martin if (error) 144 1.4 martin return error; 145 1.1 martin 146 1.1 martin fa->fae[i].fae_action = FAE_DUP2; 147 1.1 martin fa->fae[i].fae_fildes = fildes; 148 1.1 martin fa->fae[i].fae_newfildes = newfildes; 149 1.1 martin fa->len++; 150 1.1 martin 151 1.4 martin return 0; 152 1.1 martin } 153 1.1 martin 154 1.1 martin int 155 1.1 martin posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa, 156 1.1 martin int fildes) 157 1.1 martin { 158 1.3 martin unsigned int i; 159 1.3 martin int error; 160 1.1 martin 161 1.1 martin if (fildes < 0) 162 1.4 martin return EBADF; 163 1.1 martin 164 1.3 martin error = posix_spawn_file_actions_getentry(fa, &i); 165 1.3 martin if (error) 166 1.4 martin return error; 167 1.1 martin 168 1.1 martin fa->fae[i].fae_action = FAE_CLOSE; 169 1.1 martin fa->fae[i].fae_fildes = fildes; 170 1.1 martin fa->len++; 171 1.1 martin 172 1.4 martin return 0; 173 1.1 martin } 174 1.5 christos 175 1.5 christos int 176 1.5 christos posix_spawn_file_actions_addchdir(posix_spawn_file_actions_t * __restrict fa, 177 1.5 christos const char * __restrict path) 178 1.5 christos { 179 1.5 christos char *dirpath; 180 1.5 christos unsigned int i; 181 1.5 christos int error; 182 1.5 christos 183 1.5 christos error = posix_spawn_file_actions_getentry(fa, &i); 184 1.5 christos if (error) 185 1.5 christos return error; 186 1.5 christos 187 1.5 christos dirpath = strdup(path); 188 1.5 christos if (dirpath == NULL) 189 1.5 christos return ENOMEM; 190 1.5 christos 191 1.5 christos fa->fae[i].fae_action = FAE_CHDIR; 192 1.5 christos fa->fae[i].fae_chdir_path = dirpath; 193 1.5 christos fa->fae[i].fae_fildes = -1; 194 1.5 christos fa->len++; 195 1.5 christos 196 1.5 christos return 0; 197 1.5 christos } 198 1.5 christos 199 1.5 christos int 200 1.5 christos posix_spawn_file_actions_addfchdir(posix_spawn_file_actions_t *fa, int fildes) 201 1.5 christos { 202 1.5 christos unsigned int i; 203 1.5 christos int error; 204 1.5 christos 205 1.5 christos if (fildes < 0) 206 1.5 christos return EBADF; 207 1.5 christos 208 1.5 christos error = posix_spawn_file_actions_getentry(fa, &i); 209 1.5 christos if (error) 210 1.5 christos return error; 211 1.5 christos 212 1.5 christos fa->fae[i].fae_action = FAE_FCHDIR; 213 1.5 christos fa->fae[i].fae_fildes = fildes; 214 1.5 christos fa->len++; 215 1.5 christos 216 1.5 christos return 0; 217 1.5 christos } 218