1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2012 Joyent, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <errno.h>
#include <fcntl.h>
#include <sys/fork.h>
#include <libcontract.h>
#include <libzonecfg.h>
#include <sys/contract/process.h>
#include <sys/ctfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "zdoor-int.h"
#include "zerror.h"
#define ZDOOR_FMT_STR "/var/tmp/.%s"
static int
init_template(void)
{
int fd = 0;
int err = 0;
fd = open64(CTFS_ROOT "/process/template", O_RDWR);
if (fd == -1)
return (-1);
err |= ct_tmpl_set_critical(fd, 0);
err |= ct_tmpl_set_informative(fd, 0);
err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
if (err || ct_tmpl_activate(fd)) {
(void) close(fd);
return (-1);
}
return (fd);
}
static int
contract_latest(ctid_t *id)
{
int cfd = 0;
int r = 0;
ct_stathdl_t st = {0};
ctid_t result = {0};
if ((cfd = open64(CTFS_ROOT "/process/latest", O_RDONLY)) == -1)
return (errno);
if ((r = ct_status_read(cfd, CTD_COMMON, &st)) != 0) {
(void) close(cfd);
return (r);
}
result = ct_status_get_id(st);
ct_status_free(st);
(void) close(cfd);
*id = result;
return (0);
}
static int
close_on_exec(int fd)
{
int flags = fcntl(fd, F_GETFD, 0);
if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1))
return (0);
return (-1);
}
static int
contract_open(ctid_t ctid, const char *type, const char *file, int oflag)
{
char path[PATH_MAX];
int n = 0;
int fd = 0;
if (type == NULL)
type = "all";
n = snprintf(path, PATH_MAX, CTFS_ROOT "/%s/%ld/%s", type, ctid, file);
if (n >= sizeof (path)) {
errno = ENAMETOOLONG;
return (-1);
}
fd = open64(path, oflag);
if (fd != -1) {
if (close_on_exec(fd) == -1) {
int err = errno;
(void) close(fd);
errno = err;
return (-1);
}
}
return (fd);
}
static int
contract_abandon_id(ctid_t ctid)
{
int fd = 0;
int err = 0;
fd = contract_open(ctid, "all", "ctl", O_WRONLY);
if (fd == -1)
return (errno);
err = ct_ctl_abandon(fd);
(void) close(fd);
return (err);
}
/*
* zdoor_fattach(zone,service,door,detach_only) is heavily borrowed from
* zonestatd. Basically this forks, zone_enter's the targeted zone,
* fattaches to /var/tmp/.<service> with the door you've opened.
* detach_only gets passed in on door_stop to fdetach in the targeted zone.
* Note that this code really does require all the contract calls, which are
* all the static functions preceding this (have a look at zone_enter; without
* that code zone_enter will kick back EINVAL).
*/
int
zdoor_fattach(zoneid_t zoneid, const char *service, int door, int detach_only)
{
int fd = 0;
int len = 0;
int pid = 0;
int stat = 0;
int tmpl_fd = 0;
char path[MAXPATHLEN] = {0};
ctid_t ct = -1;
if (zoneid < 0) {
zdoor_debug("zdoor_fattach: zoneid < 0");
return (ZDOOR_ARGS_ERROR);
}
if (service == NULL) {
zdoor_debug("zdoor_fattach: NULL service");
return (ZDOOR_ARGS_ERROR);
}
if ((tmpl_fd = init_template()) < 0) {
zdoor_warn("zdoor_fattach: init contract for %d:%s failed",
zoneid, service);
return (ZDOOR_ERROR);
}
len = snprintf(NULL, 0, ZDOOR_FMT_STR, service) + 1;
if (len > MAXPATHLEN)
return (ZDOOR_ARGS_ERROR);
(void) snprintf(path, len, ZDOOR_FMT_STR, service);
zdoor_info("zdoor_fattach: ensuring %s", path);
pid = fork();
if (pid < 0) {
(void) ct_tmpl_clear(tmpl_fd);
zdoor_error("zdoor_fattach: unable to fork for zone_enter: %s",
strerror(errno));
return (ZDOOR_OK);
}
if (pid == 0) {
zdoor_debug("zdoor_fattach(CHILD): starting");
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
if (zone_enter(zoneid) != 0) {
zdoor_debug("zdoor_fattach(CHILD): zone_enter fail %s",
strerror(errno));
if (errno == EINVAL) {
_exit(0);
}
_exit(1);
}
(void) fdetach(path);
(void) unlink(path);
if (detach_only) {
zdoor_debug("zdoor_fattach(CHILD): detach only, done");
_exit(0);
}
fd = open(path, O_CREAT|O_RDWR, 0644);
if (fd < 0) {
zdoor_debug("zdoor_fattach(CHILD): open failed: %s",
strerror(errno));
_exit(2);
}
if (fattach(door, path) != 0) {
zdoor_debug("zdoor_fattach(CHILD): fattach failed: %s",
strerror(errno));
_exit(3);
}
_exit(0);
}
if (contract_latest(&ct) == -1)
ct = -1;
(void) ct_tmpl_clear(tmpl_fd);
(void) close(tmpl_fd);
(void) contract_abandon_id(ct);
zdoor_debug("zdoor_fattach: waiting for child...");
while (waitpid(pid, &stat, 0) != pid)
;
if (WIFEXITED(stat) && WEXITSTATUS(stat) == 0) {
zdoor_debug(" child exited with success");
zdoor_debug("zdoor_fattach: returning ZDOOR_OK");
return (ZDOOR_OK);
}
zdoor_debug(" child exited with %d", WEXITSTATUS(stat));
zdoor_debug("zdoor_fattach: returning ZDOOR_ERROR");
return (ZDOOR_ERROR);
}
/*
* zdoor_zone_is_running(zone) returns 1 if the specified zone is running, or 0
* if it is any other state. It additionally eats any other errors it
* encounters and returns 0 upon encountering them.
*/
boolean_t
zdoor_zone_is_running(zoneid_t zoneid)
{
zone_state_t state;
char zone[ZONENAME_MAX];
if (zoneid < 0)
return (B_FALSE);
if (getzonenamebyid(zoneid, zone, ZONENAME_MAX) < 0)
return (B_FALSE);
if (!zone_get_state((char *)zone, &state) == Z_OK)
return (B_FALSE);
return (state == ZONE_STATE_RUNNING);
}
/*
* zdoor_cookie_create simply allocates and initializes
* memory. Returns NULL on any error.
*/
zdoor_cookie_t *
zdoor_cookie_create(const char *zonename, const char *service,
const void *biscuit)
{
zdoor_cookie_t *cookie = NULL;
if (zonename == NULL || service == NULL)
return (NULL);
cookie = (zdoor_cookie_t *)calloc(1, sizeof (zdoor_cookie_t));
if (cookie == NULL) {
OUT_OF_MEMORY();
return (NULL);
}
cookie->zdc_biscuit = (void *)biscuit;
cookie->zdc_zonename = strdup((char *)zonename);
if (cookie->zdc_zonename == NULL) {
zdoor_cookie_free(cookie);
OUT_OF_MEMORY();
return (NULL);
}
cookie->zdc_service = strdup((char *)service);
if (cookie->zdc_service == NULL) {
zdoor_cookie_free(cookie);
OUT_OF_MEMORY();
return (NULL);
}
return (cookie);
}
/*
* zdoor_cookie_free(cookie) cleans up any memory associated with the
* specified cookie.
*/
void
zdoor_cookie_free(zdoor_cookie_t *cookie)
{
if (cookie == NULL)
return;
if (cookie->zdc_zonename != NULL) {
free(cookie->zdc_zonename);
cookie->zdc_zonename = NULL;
}
if (cookie->zdc_service != NULL) {
free(cookie->zdc_service);
cookie->zdc_service = NULL;
}
free(cookie);
}
|