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
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.
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]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include "inetd_impl.h"
35 extern char **environ
;
38 valid_env_var(const char *var
, const char *instance
, const char *method
)
40 char *cp
= strchr(var
, '=');
42 if (cp
== NULL
|| cp
== var
) {
45 error_msg(gettext("Invalid environment variable \"%s\" for "
46 "method %s of instance %s.\n"), var
, method
, instance
);
48 } else if (strncmp(var
, "SMF_", 4) == 0) {
51 error_msg(gettext("Invalid environment variable \"%s\" for "
52 "method %s of instance %s; \"SMF_\" prefix is reserved.\n"),
53 var
, method
, instance
);
61 find_dup(const char *var
, char **env
, const char *instance
, const char *method
)
66 for (p
= env
; *p
!= NULL
; p
++) {
67 tmp
= strchr(*p
, '=');
70 if (strncmp(*p
, var
, tmp
- *p
) == 0)
77 error_msg(gettext("Ignoring duplicate environment variable \"%s\" "
78 "for method %s of instance %s.\n"), *p
, method
, instance
);
83 * Create an environment which is appropriate for spawning an SMF aware
86 * In order to preserve the correctness of the new environment, various
87 * checks are performed:
89 * - All SMF_ entries are ignored. All SMF_ entries should be provided
91 * - Duplicates in the entry are eliminated.
92 * - Malformed entries are eliminated.
94 * Detected errors are logged but not fatal, since a single bad entry
95 * should not be enough to prevent an SMF_ functional environment from
99 set_smf_env(struct method_context
*mthd_ctxt
, instance_t
*instance
,
107 * Max. of env, three SMF_ variables, and terminating NULL.
109 nenv_size
= mthd_ctxt
->env_sz
+ 3 + 1;
111 if (instance
->config
->basic
->inherit_env
) {
112 for (p
= environ
; *p
!= NULL
; p
++)
116 nenv
= malloc(sizeof (char *) * nenv_size
);
119 (void) memset(nenv
, 0, sizeof (char *) * nenv_size
);
123 *np
= uu_msprintf("SMF_RESTARTER=%s", INETD_INSTANCE_FMRI
);
128 *np
= uu_msprintf("SMF_FMRI=%s", instance
->fmri
);
133 *np
= uu_msprintf("SMF_METHOD=%s", method
);
139 if (instance
->config
->basic
->inherit_env
) {
140 for (p
= environ
; *p
!= NULL
; p
++) {
141 if (!valid_env_var(*p
, NULL
, NULL
))
152 if (mthd_ctxt
->env
!= NULL
) {
153 for (p
= mthd_ctxt
->env
; *p
!= NULL
; p
++) {
156 if (!valid_env_var(*p
, instance
->fmri
, method
))
159 if ((dup_pos
= find_dup(*p
, nenv
, instance
->fmri
,
162 *dup_pos
= strdup(*p
);
163 if (*dup_pos
== NULL
)