summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/startd/env.c
blob: ec050dffdab64ef7f07748fbc52c27e3c0ba7c31 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <assert.h>
#include <libuutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zone.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "startd.h"

/*
 * This file contains functions for setting the environment for
 * processes started by svc.startd.
 */

#define	MAXCMDL		512
#define	DEF_PATH	"PATH=/usr/sbin:/usr/bin"

static char *ENVFILE	= "/etc/default/init"; /* Default env. */

static char **glob_envp;	/* Array of environment strings */
static int glob_env_n;		/* Number of environment slots allocated. */

static char zonename[ZONENAME_MAX];

/*
 * init_env()
 *   A clone of the work init.c does to provide as much compatibility
 *   for startup scripts as possible.
 */
void
init_env()
{
	int	i;
	char	line[MAXCMDL];
	FILE	*fp;
	int	inquotes, length, wslength;
	char	*tokp, *cp1, *cp2;
	char	**newp;

	glob_env_n = 16;
	glob_envp = startd_alloc(sizeof (*glob_envp) * glob_env_n);

	glob_envp[0] = startd_alloc((unsigned)(strlen(DEF_PATH)+2));
	(void) strcpy(glob_envp[0], DEF_PATH);

	if ((fp = fopen(ENVFILE, "r")) == NULL) {
		uu_warn("Cannot open %s. Environment not initialized.\n",
		    ENVFILE);

		glob_envp[1] = NULL;
		return;
	}

	i = 1;

	while (fgets(line, MAXCMDL - 1, fp) != NULL) {
		/*
		 * Toss newline
		 */
		length = strlen(line);
		if (line[length - 1] == '\n')
			line[length - 1] = '\0';

		/*
		 * Ignore blank or comment lines.
		 */
		if (line[0] == '#' || line[0] == '\0' ||
		    (wslength = strspn(line, " \t\n")) == strlen(line) ||
		    strchr(line, '#') == line + wslength)
			continue;

		/*
		 * First make a pass through the line and change
		 * any non-quoted semi-colons to blanks so they
		 * will be treated as token separators below.
		 */
		inquotes = 0;
		for (cp1 = line; *cp1 != '\0'; cp1++) {
			if (*cp1 == '"') {
				if (inquotes == 0)
					inquotes = 1;
				else
					inquotes = 0;
			} else if (*cp1 == ';') {
				if (inquotes == 0)
					*cp1 = ' ';
			}
		}

		/*
		 * Tokens within the line are separated by blanks
		 *  and tabs.  For each token in the line which
		 * contains a '=' we strip out any quotes and then
		 * stick the token in the environment array.
		 */
		if ((tokp = strtok(line, " \t")) == NULL)
			continue;

		do {
			cp1 = strchr(tokp, '=');
			if (cp1 == NULL || cp1 == tokp)
				continue;
			length = strlen(tokp);
			while ((cp1 = strpbrk(tokp, "\"\'")) != NULL) {
				for (cp2 = cp1; cp2 < &tokp[length]; cp2++)
					*cp2 = *(cp2 + 1);
				length--;
			}

			/*
			 * init already started us with this umask, and we
			 * handled it in startd.c, so just skip it.
			 */
			if (strncmp(tokp, "CMASK=", 6) == 0 ||
			    strncmp(tokp, "SMF_", 4) == 0)
				continue;

			glob_envp[i] = startd_alloc((unsigned)(length + 1));
			(void) strcpy(glob_envp[i], tokp);

			/*
			 * Double the environment size whenever it is
			 * full.
			 */
			if (++i == glob_env_n) {
				glob_env_n *= 2;
				newp = startd_alloc(sizeof (*glob_envp) *
				    glob_env_n);
				(void) memcpy(newp, glob_envp,
				    sizeof (*glob_envp) * glob_env_n / 2);
				startd_free(glob_envp,
				    sizeof (*glob_envp) * glob_env_n / 2);
				glob_envp = newp;
			}
		} while ((tokp = strtok(NULL, " \t")) != NULL);
	}

	startd_fclose(fp);

	/* Append a null pointer to the environment array to mark its end. */
	glob_envp[i] = NULL;

	/*
	 * Get the zonename once; it is used to set SMF_ZONENAME for methods.
	 */
	(void) getzonenamebyid(getzoneid(), zonename, sizeof (zonename));

}

static int
valid_env_var(const char *var, const restarter_inst_t *inst, const char *path)
{
	char *cp = strchr(var, '=');

	if (cp == NULL || cp == var) {
		if (inst != NULL)
			log_instance(inst, B_FALSE, "Invalid environment "
			    "variable \"%s\".", var);
		return (0);
	} else if (strncmp(var, "SMF_", 4) == 0) {
		if (inst != NULL)
			log_instance(inst, B_FALSE, "Invalid environment "
			    "variable \"%s\"; \"SMF_\" prefix is reserved.",
			    var);
		return (0);
	} else if (path != NULL && strncmp(var, "PATH=", 5) == 0) {
		return (0);
	}

	return (1);
}

static char **
find_dup(const char *var, char **env, const restarter_inst_t *inst)
{
	char **p;
	char *tmp;

	for (p = env; *p != NULL; p++) {
		assert((tmp = strchr(*p, '=')) != NULL);
		tmp++;
		if (strncmp(*p, var, tmp - *p) == 0)
			break;
	}

	if (*p == NULL)
		return (NULL);

	if (inst != NULL)
		log_instance(inst, B_FALSE, "Ignoring duplicate "
		    "environment variable \"%s\".", *p);
	return (p);
}

/*
 * Create an environment which is appropriate for spawning an SMF
 * aware process. The new environment will consist of the values from
 * the global environment as modified by the supplied (local) environment.
 *
 * In order to preserve the correctness of the new environment,
 * various checks are performed on the local environment (init_env()
 * is relied upon to ensure the global environment is correct):
 *
 * - All SMF_ entries are ignored. All SMF_ entries should be provided
 *   by this function.
 * - Duplicates in the entry are eliminated.
 * - Malformed entries are eliminated.
 *
 * Detected errors are logged as warnings to the appropriate instance
 * logfile, since a single bad entry should not be enough to prevent
 * an SMF_ functional environment from being created. The faulty entry
 * is then ignored when building the environment.
 *
 * If env is NULL, then the return is an environment which contains
 * all default values.
 *
 * If "path" is non-NULL, it will silently over-ride any previous
 * PATH environment variable.
 *
 * NB: The returned env and strings are allocated using startd_alloc().
 */
char **
set_smf_env(char **env, size_t env_sz, const char *path,
    const restarter_inst_t *inst, const char *method)
{
	char **nenv;
	char **p, **np;
	size_t nenv_size;
	size_t sz;

	/*
	 * Max. of glob_env, env, four SMF_ variables,
	 * path, and terminating NULL.
	 */
	nenv_size = glob_env_n + env_sz + 4 + 1 + 1;

	nenv = startd_zalloc(sizeof (char *) * nenv_size);

	np = nenv;

	if (path != NULL) {
		sz = strlen(path) + 1;
		*np = startd_alloc(sz);
		(void) strlcpy(*np, path, sz);
		np++;
	}

	if (inst) {
		sz = sizeof ("SMF_FMRI=") + strlen(inst->ri_i.i_fmri);
		*np = startd_alloc(sz);
		(void) strlcpy(*np, "SMF_FMRI=", sz);
		(void) strlcat(*np, inst->ri_i.i_fmri, sz);
		np++;
	}

	if (method) {
		sz = sizeof ("SMF_METHOD=") + strlen(method);
		*np = startd_alloc(sz);
		(void) strlcpy(*np, "SMF_METHOD=", sz);
		(void) strlcat(*np, method, sz);
		np++;
	}

	sz = sizeof ("SMF_RESTARTER=") + strlen(SCF_SERVICE_STARTD);
	*np = startd_alloc(sz);
	(void) strlcpy(*np, "SMF_RESTARTER=", sz);
	(void) strlcat(*np, SCF_SERVICE_STARTD, sz);
	np++;

	sz = sizeof ("SMF_ZONENAME=") + strlen(zonename);
	*np = startd_alloc(sz);
	(void) strlcpy(*np, "SMF_ZONENAME=", sz);
	(void) strlcat(*np, zonename, sz);
	np++;

	for (p = glob_envp; *p != NULL; p++) {
		if (valid_env_var(*p, inst, path)) {
			sz = strlen(*p) + 1;
			*np = startd_alloc(sz);
			(void) strlcpy(*np, *p, sz);
			np++;
		}
	}

	if (env) {
		for (p = env; *p != NULL; p++) {
			char **dup_pos;

			if (!valid_env_var(*p, inst, path))
				continue;

			if ((dup_pos = find_dup(*p, nenv, inst)) != NULL) {
				startd_free(*dup_pos, strlen(*dup_pos) + 1);
				sz = strlen(*p) + 1;
				*dup_pos = startd_alloc(sz);
				(void) strlcpy(*dup_pos, *p, sz);
			} else {
				sz = strlen(*p) + 1;
				*np = startd_alloc(sz);
				(void) strlcpy(*np, *p, sz);
				np++;
			}
		}
	}
	*np = NULL;

	return (nenv);
}