posix_spawn_fileactions.c revision 1.2.2.2 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.2 yamt __RCSID("$NetBSD: posix_spawn_fileactions.c,v 1.2.2.2 2012/04/17 00:05:19 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.2 yamt return (-1);
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.2 yamt return (-1);
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.2 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.2 yamt return (-1);
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.2 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.2 yamt posix_spawn_file_actions_getentry(posix_spawn_file_actions_t *fa)
81 1.2.2.2 yamt {
82 1.2.2.2 yamt if (fa == NULL)
83 1.2.2.2 yamt return -1;
84 1.2.2.2 yamt
85 1.2.2.2 yamt if (fa->len < fa->size)
86 1.2.2.2 yamt return fa->len;
87 1.2.2.2 yamt
88 1.2.2.2 yamt fa->fae = realloc(fa->fae, (fa->size + MIN_SIZE) *
89 1.2.2.2 yamt sizeof(struct posix_spawn_file_actions_entry));
90 1.2.2.2 yamt
91 1.2.2.2 yamt if (fa->fae == NULL)
92 1.2.2.2 yamt return -1;
93 1.2.2.2 yamt
94 1.2.2.2 yamt fa->size += MIN_SIZE;
95 1.2.2.2 yamt
96 1.2.2.2 yamt return fa->len;
97 1.2.2.2 yamt }
98 1.2.2.2 yamt
99 1.2.2.2 yamt int
100 1.2.2.2 yamt posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * __restrict fa,
101 1.2.2.2 yamt int fildes, const char * __restrict path, int oflag, mode_t mode)
102 1.2.2.2 yamt {
103 1.2.2.2 yamt int i, error;
104 1.2.2.2 yamt
105 1.2.2.2 yamt if (fildes < 0)
106 1.2.2.2 yamt return (EBADF);
107 1.2.2.2 yamt
108 1.2.2.2 yamt i = posix_spawn_file_actions_getentry(fa);
109 1.2.2.2 yamt if (i < 0)
110 1.2.2.2 yamt return (ENOMEM);
111 1.2.2.2 yamt
112 1.2.2.2 yamt fa->fae[i].fae_action = FAE_OPEN;
113 1.2.2.2 yamt fa->fae[i].fae_path = strdup(path);
114 1.2.2.2 yamt if (fa->fae[i].fae_path == NULL) {
115 1.2.2.2 yamt error = errno;
116 1.2.2.2 yamt return (error);
117 1.2.2.2 yamt }
118 1.2.2.2 yamt fa->fae[i].fae_fildes = fildes;
119 1.2.2.2 yamt fa->fae[i].fae_oflag = oflag;
120 1.2.2.2 yamt fa->fae[i].fae_mode = mode;
121 1.2.2.2 yamt
122 1.2.2.2 yamt fa->len++;
123 1.2.2.2 yamt
124 1.2.2.2 yamt return (0);
125 1.2.2.2 yamt }
126 1.2.2.2 yamt
127 1.2.2.2 yamt int
128 1.2.2.2 yamt posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *fa,
129 1.2.2.2 yamt int fildes, int newfildes)
130 1.2.2.2 yamt {
131 1.2.2.2 yamt int i;
132 1.2.2.2 yamt
133 1.2.2.2 yamt if (fildes < 0 || newfildes < 0)
134 1.2.2.2 yamt return (EBADF);
135 1.2.2.2 yamt
136 1.2.2.2 yamt i = posix_spawn_file_actions_getentry(fa);
137 1.2.2.2 yamt if (i < 0)
138 1.2.2.2 yamt return (ENOMEM);
139 1.2.2.2 yamt
140 1.2.2.2 yamt fa->fae[i].fae_action = FAE_DUP2;
141 1.2.2.2 yamt fa->fae[i].fae_fildes = fildes;
142 1.2.2.2 yamt fa->fae[i].fae_newfildes = newfildes;
143 1.2.2.2 yamt fa->len++;
144 1.2.2.2 yamt
145 1.2.2.2 yamt return (0);
146 1.2.2.2 yamt }
147 1.2.2.2 yamt
148 1.2.2.2 yamt int
149 1.2.2.2 yamt posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *fa,
150 1.2.2.2 yamt int fildes)
151 1.2.2.2 yamt {
152 1.2.2.2 yamt int i;
153 1.2.2.2 yamt
154 1.2.2.2 yamt if (fildes < 0)
155 1.2.2.2 yamt return (EBADF);
156 1.2.2.2 yamt
157 1.2.2.2 yamt i = posix_spawn_file_actions_getentry(fa);
158 1.2.2.2 yamt if (i < 0)
159 1.2.2.2 yamt return (ENOMEM);
160 1.2.2.2 yamt
161 1.2.2.2 yamt fa->fae[i].fae_action = FAE_CLOSE;
162 1.2.2.2 yamt fa->fae[i].fae_fildes = fildes;
163 1.2.2.2 yamt fa->len++;
164 1.2.2.2 yamt
165 1.2.2.2 yamt return (0);
166 1.2.2.2 yamt }
167