summaryrefslogtreecommitdiff
path: root/usr/src/lib/brand/lx/lx_support/lx_support.c
blob: d35c68ed8d1016659263c6aec5740ccb059c2a90 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2017 Joyent, Inc.
 */

/*
 * lx_support is a small cli utility used to perform some brand-specific
 * tasks when booting, halting, or verifying a zone.  This utility is not
 * intended to be called by users - it is intended to be invoked by the
 * zones utilities.
 */

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stropts.h>
#include <sys/ioccom.h>
#include <sys/stat.h>
#include <sys/systeminfo.h>
#include <sys/types.h>
#include <sys/varargs.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>

#include <libzonecfg.h>
#include <sys/lx_brand.h>

static void lxs_err(char *msg, ...) __NORETURN;
static void usage(void) __NORETURN;

#define	CP_CMD		"/usr/bin/cp"
#define	MOUNT_CMD	"/sbin/mount"

static char *bname = NULL;
static char *zonename = NULL;
static char *zoneroot = NULL;

#if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
#endif

static void
lxs_err(char *msg, ...)
{
	char	buf[1024];
	va_list	ap;

	va_start(ap, msg);
	/*LINTED*/
	(void) vsnprintf(buf, sizeof (buf), msg, ap);
	va_end(ap);

	(void) printf("%s error: %s\n", bname, buf);

	exit(1);
	/*NOTREACHED*/
}

/*
 * Cleanup from earlier versions of the code which created a /dev/initctl FIFO.
 */
static void
lxs_remove_initctl()
{
	char		special[MAXPATHLEN];

	if (snprintf(special, sizeof (special), "%s/dev/initctl", zoneroot) >=
	    sizeof (special))
		lxs_err("%s: %s", gettext("Failed to cleanup /dev/initctl"),
		    gettext("zoneroot is too long"));

	(void) unlink(special);
}

/*
 * fsck gets really confused when run inside a zone.  Removing this file
 * prevents it from running
 */
static void
lxs_remove_autofsck()
{
	char	path[MAXPATHLEN];
	int	err;

	if (snprintf(path, MAXPATHLEN, "%s/root/.autofsck", zoneroot) >=
	    MAXPATHLEN)
		lxs_err("%s: %s", gettext("Failed to remove /.autofsck"),
		    gettext("zoneroot is too long"));

	if (unlink(path) < 0) {
		err = errno;
		if (err != ENOENT)
			lxs_err("%s: %s",
			    gettext("Failed to remove /.autofsck"),
			    strerror(err));
	}
}

/*
 * Extract any lx-supported attributes from the zone configuration file.
 */
static void
lxs_getattrs(zone_dochandle_t zdh, char **krelease)
{
	struct zone_attrtab	attrtab;
	int			err;

	/* initialize the attribute iterator */
	if (zonecfg_setattrent(zdh) != Z_OK) {
		zonecfg_fini_handle(zdh);
		lxs_err(gettext("error accessing zone configuration"));
	}

	*krelease = (char *)malloc(LX_KERN_RELEASE_MAX);
	if (*krelease == NULL)
		lxs_err(gettext("out of memory"));

	bzero(*krelease, LX_KERN_RELEASE_MAX);
	while ((err = zonecfg_getattrent(zdh, &attrtab)) == Z_OK) {
		if ((strcmp(attrtab.zone_attr_name, "kernel-version") == 0) &&
		    (zonecfg_get_attr_string(&attrtab, *krelease,
		    LX_KERN_RELEASE_MAX) != Z_OK))
			lxs_err(gettext("invalid type for zone attribute: %s"),
			    attrtab.zone_attr_name);
	}

	if (strlen(*krelease) == 0) {
		free(*krelease);
		*krelease = NULL;
	}

	/* some kind of error while looking up attributes */
	if (err != Z_NO_ENTRY)
		lxs_err(gettext("error accessing zone configuration"));
}

/*
 * Attempt to lookup the "tty" gid from within the zone's /etc/group file.
 * The gid is used to emulate existing Linux udev behavior for setting the gid
 * on a pty. If we cannot lookup the gid for some reason, we still allow
 * the zone to boot.
 *
 * Because we're reading the lx zone's /etc/group file, we have to parse it
 * ourselves, but this is simple since we only need the gid.
 */
static void
lxs_set_ttygid(zoneid_t zoneid)
{
	char	buf[MAXPATHLEN];
	FILE	*fp;
	gid_t	tty_gid = 0;

	if (snprintf(buf, sizeof (buf), "%s/root/etc/group", zoneroot) >=
	    sizeof (buf))
		return;

	if ((fp = fopen(buf, "r")) == NULL)
		return;

	/*
	 * Look for the "tty" line and get the gid. Note that this loop will
	 * properly handle a long line that won't fit into buf on a single
	 * fgets. The subsequent fgets will consume more of the line but we
	 * continue on through that. In practice this is unlikely to occur.
	 */
	while (fgets(buf, sizeof (buf), fp) != NULL) {
		char *p, *p_id;
		long val;

		/* group name */
		if ((p = strchr(buf, ':')) == NULL)
			continue;
		*p = '\0';
		if (strcmp(buf, "tty") != 0)
			continue;

		/* found tty entry, skip the "x" field */
		if ((p = strchr(p + 1, ':')) == NULL)
			goto done;
		/* gid field */
		p_id = p + 1;
		if ((p = strchr(p_id, ':')) == NULL)
			goto done;
		*p = '\0';

		errno = 0;
		val = strtol(p_id, &p, 10);
		/* Perform simple validation on the gid */
		if (errno != 0 || *p != '\0' || val > MAXUID || val < 0)
			goto done;
		tty_gid = (gid_t)val;
		break;
	}

	if (tty_gid != 0)
		(void) zone_setattr(zoneid, LX_ATTR_TTY_GID, &tty_gid,
		    sizeof (tty_gid));

done:
	(void) fclose(fp);
}

static int
lxs_boot()
{
	zoneid_t	zoneid;
	zone_dochandle_t zdh;
	char		*krelease;

	lxs_remove_initctl();
	lxs_remove_autofsck();

	if ((zdh = zonecfg_init_handle()) == NULL)
		lxs_err(gettext("unable to initialize zone handle"));

	if (zonecfg_get_handle((char *)zonename, zdh) != Z_OK) {
		zonecfg_fini_handle(zdh);
		lxs_err(gettext("unable to load zone configuration"));
	}

	/* Extract any relevant attributes from the config file. */
	lxs_getattrs(zdh, &krelease);
	zonecfg_fini_handle(zdh);

	/*
	 * Let the kernel know whether or not this zone's init process
	 * should be automatically restarted on its death.
	 */
	if ((zoneid = getzoneidbyname(zonename)) < 0)
		lxs_err(gettext("unable to get zoneid"));

	if (krelease != NULL) {
		/* Backward compatability with incomplete version attr */
		if (strcmp(krelease, "2.4") == 0) {
			krelease = "2.4.21";
		} else if (strcmp(krelease, "2.6") == 0) {
			krelease = "2.6.18";
		}

		if (zone_setattr(zoneid, LX_ATTR_KERN_RELEASE, krelease,
		    strlen(krelease)) < 0)
			lxs_err(gettext("unable to set kernel version"));
	}

	lxs_set_ttygid(zoneid);

	return (0);
}

static int
lxs_halt()
{
	return (0);
}

static int
lxs_verify(char *xmlfile)
{
	zone_dochandle_t	handle;
	char			*krelease;
	char			hostidp[HW_HOSTID_LEN];
	zone_iptype_t		iptype;

	if ((handle = zonecfg_init_handle()) == NULL)
		lxs_err(gettext("internal libzonecfg.so.1 error"), 0);

	if (zonecfg_get_xml_handle(xmlfile, handle) != Z_OK) {
		zonecfg_fini_handle(handle);
		lxs_err(gettext("zonecfg provided an invalid XML file"));
	}

	/*
	 * Check to see whether the zone has hostid emulation enabled.
	 */
	if (zonecfg_get_hostid(handle, hostidp, sizeof (hostidp)) == Z_OK) {
		zonecfg_fini_handle(handle);
		lxs_err(gettext("lx zones do not support hostid emulation"));
	}

	/*
	 * Only exclusive stack is supported.
	 */
	if (zonecfg_get_iptype(handle, &iptype) != Z_OK ||
	    iptype != ZS_EXCLUSIVE) {
		zonecfg_fini_handle(handle);
		lxs_err(gettext("lx zones do not support shared IP stacks"));
	}

	/* Extract any relevant attributes from the config file. */
	lxs_getattrs(handle, &krelease);
	zonecfg_fini_handle(handle);

	if (krelease) {
		char *pdot, *ep;
		long major_ver;

		pdot = strchr(krelease, '.');
		if (pdot != NULL)
			*pdot = '\0';
		errno = 0;
		major_ver = strtol(krelease, &ep, 10);
		if (major_ver < 2 || errno != 0 || *ep != '\0')
			lxs_err(gettext("invalid value for zone attribute: %s"),
			    "kernel-version");
		if (pdot != NULL)
			*pdot = '.';

	}
	return (0);
}

static void
usage()
{

	(void) fprintf(stderr,
	    gettext("usage:\t%s boot <zoneroot> <zonename>\n"), bname);
	(void) fprintf(stderr,
	    gettext("      \t%s halt <zoneroot> <zonename>\n"), bname);
	(void) fprintf(stderr,
	    gettext("      \t%s verify <xml file>\n\n"), bname);
	exit(1);
}

int
main(int argc, char *argv[])
{
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	bname = basename(argv[0]);

	if (argc < 3)
		usage();

	if (strcmp(argv[1], "boot") == 0) {
		if (argc != 4)
			lxs_err(gettext("usage: %s %s <zoneroot> <zonename>"),
			    bname, argv[1]);
		zoneroot = argv[2];
		zonename = argv[3];
		return (lxs_boot());
	}

	if (strcmp(argv[1], "halt") == 0) {
		if (argc != 4)
			lxs_err(gettext("usage: %s %s <zoneroot> <zonename>"),
			    bname, argv[1]);
		zoneroot = argv[2];
		zonename = argv[3];
		return (lxs_halt());
	}

	if (strcmp(argv[1], "verify") == 0) {
		if (argc != 3)
			lxs_err(gettext("usage: %s verify <xml file>"),
			    bname);
		return (lxs_verify(argv[2]));
	}

	usage();
	/*NOTREACHED*/
}