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