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