env.c (0:68f95e015346) env.c (1573:7338e65f2666)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident "%Z%%M% %I% %E% SMI"
28
29#include <assert.h>
30#include <libuutil.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
23 * Use is subject to license terms.
24 */
25
26#pragma ident "%Z%%M% %I% %E% SMI"
27
28#include <assert.h>
29#include <libuutil.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <zone.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36
37#include "startd.h"
38
39/*
40 * This file contains functions for setting the environment for
41 * processes started by svc.startd.
42 */
43
44#define MAXCMDL 512
45#define DEF_PATH "PATH=/usr/sbin:/usr/bin"
46
47static char *ENVFILE = "/etc/default/init"; /* Default env. */
48
49static char **glob_envp; /* Array of environment strings */
50static int glob_env_n; /* Number of environment slots allocated. */
51
34#include <sys/types.h>
35#include <sys/stat.h>
36
37#include "startd.h"
38
39/*
40 * This file contains functions for setting the environment for
41 * processes started by svc.startd.
42 */
43
44#define MAXCMDL 512
45#define DEF_PATH "PATH=/usr/sbin:/usr/bin"
46
47static char *ENVFILE = "/etc/default/init"; /* Default env. */
48
49static char **glob_envp; /* Array of environment strings */
50static int glob_env_n; /* Number of environment slots allocated. */
51
52static char zonename[ZONENAME_MAX];
53
52/*
53 * init_env()
54 * A clone of the work init.c does to provide as much compatibility
55 * for startup scripts as possible.
56 */
57void
58init_env()
59{

--- 101 unchanged lines hidden (view full) ---

161 }
162 } while ((tokp = strtok(NULL, " \t")) != NULL);
163 }
164
165 startd_fclose(fp);
166
167 /* Append a null pointer to the environment array to mark its end. */
168 glob_envp[i] = NULL;
54/*
55 * init_env()
56 * A clone of the work init.c does to provide as much compatibility
57 * for startup scripts as possible.
58 */
59void
60init_env()
61{

--- 101 unchanged lines hidden (view full) ---

163 }
164 } while ((tokp = strtok(NULL, " \t")) != NULL);
165 }
166
167 startd_fclose(fp);
168
169 /* Append a null pointer to the environment array to mark its end. */
170 glob_envp[i] = NULL;
171
172 /*
173 * Get the zonename once; it is used to set SMF_ZONENAME for methods.
174 */
175 (void) getzonenamebyid(getzoneid(), zonename, sizeof (zonename));
176
169}
170
171static int
172valid_env_var(const char *var, const restarter_inst_t *inst, const char *path)
173{
174 char *cp = strchr(var, '=');
175
176 if (cp == NULL || cp == var) {

--- 68 unchanged lines hidden (view full) ---

245 const restarter_inst_t *inst, const char *method)
246{
247 char **nenv;
248 char **p, **np;
249 size_t nenv_size;
250 size_t sz;
251
252 /*
177}
178
179static int
180valid_env_var(const char *var, const restarter_inst_t *inst, const char *path)
181{
182 char *cp = strchr(var, '=');
183
184 if (cp == NULL || cp == var) {

--- 68 unchanged lines hidden (view full) ---

253 const restarter_inst_t *inst, const char *method)
254{
255 char **nenv;
256 char **p, **np;
257 size_t nenv_size;
258 size_t sz;
259
260 /*
253 * Max. of glob_env, env, three SMF_ variables,
261 * Max. of glob_env, env, four SMF_ variables,
254 * path, and terminating NULL.
255 */
262 * path, and terminating NULL.
263 */
256 nenv_size = glob_env_n + env_sz + 3 + 1 + 1;
264 nenv_size = glob_env_n + env_sz + 4 + 1 + 1;
257
258 nenv = startd_zalloc(sizeof (char *) * nenv_size);
259
260 np = nenv;
261
262 if (path != NULL) {
263 sz = strlen(path) + 1;
264 *np = startd_alloc(sz);
265 (void) strlcpy(*np, path, sz);
266 np++;
267 }
268
265
266 nenv = startd_zalloc(sizeof (char *) * nenv_size);
267
268 np = nenv;
269
270 if (path != NULL) {
271 sz = strlen(path) + 1;
272 *np = startd_alloc(sz);
273 (void) strlcpy(*np, path, sz);
274 np++;
275 }
276
269
270 if (inst) {
271 sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri);
272 *np = startd_alloc(sz);
273 (void) strlcpy(*np, "SMF_FMRI=", sz);
274 (void) strlcat(*np, inst->ri_i.i_fmri, sz);
275 np++;
276 }
277

--- 6 unchanged lines hidden (view full) ---

284 }
285
286 sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD);
287 *np = startd_alloc(sz);
288 (void) strlcpy(*np, "SMF_RESTARTER=", sz);
289 (void) strlcat(*np, SCF_SERVICE_STARTD, sz);
290 np++;
291
277 if (inst) {
278 sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri);
279 *np = startd_alloc(sz);
280 (void) strlcpy(*np, "SMF_FMRI=", sz);
281 (void) strlcat(*np, inst->ri_i.i_fmri, sz);
282 np++;
283 }
284

--- 6 unchanged lines hidden (view full) ---

291 }
292
293 sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD);
294 *np = startd_alloc(sz);
295 (void) strlcpy(*np, "SMF_RESTARTER=", sz);
296 (void) strlcat(*np, SCF_SERVICE_STARTD, sz);
297 np++;
298
299 sz = sizeof ("SMF_ZONENAME=") + strlen(zonename);
300 *np = startd_alloc(sz);
301 (void) strlcpy(*np, "SMF_ZONENAME=", sz);
302 (void) strlcat(*np, zonename, sz);
303 np++;
304
292 for (p = glob_envp; *p != NULL; p++) {
293 if (valid_env_var(*p, inst, path)) {
294 sz = strlen(*p) + 1;
295 *np = startd_alloc(sz);
296 (void) strlcpy(*np, *p, sz);
297 np++;
298 }
299 }

--- 25 unchanged lines hidden ---
305 for (p = glob_envp; *p != NULL; p++) {
306 if (valid_env_var(*p, inst, path)) {
307 sz = strlen(*p) + 1;
308 *np = startd_alloc(sz);
309 (void) strlcpy(*np, *p, sz);
310 np++;
311 }
312 }

--- 25 unchanged lines hidden ---