summaryrefslogtreecommitdiff
path: root/usr/src/lib/libinstzones/common/zones_utils.c
blob: 3b51d145d18c5c11fadc8ad74c0c7016efcbc466 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * 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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */



/*
 * Module:	zones.c
 * Group:	libinstzones
 * Description:	Provide "zones" interface for install consolidation code
 *
 * Public Methods:
 *
 *  _z_close_file_descriptors - close a file descriptor "a_fd" not in the
 *	list "a_fds"
 *  _z_echo - Output an interactive message if interaction is enabled
 *  _z_echoDebug - Output a debugging message if debugging is enabled
 *  _z_is_directory - determine if specified path exists and is a directory
 *  _z_program_error - Output an error message to the appropriate destinations
 *  _z_pluginCatchSigint - SIGINT/SIGHUP interrupt handler
 *  _z_running_in_global_zone - Determine if this process is running in the
 *	global zone
 *  _z_zones_are_implemented - Determine if zones are supported by the
 *	current system
 *  _z_brands_are_implemented - determine if branded zones are implemented on
 *		this system
 */

/*
 * System includes
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/param.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <stropts.h>
#include <libintl.h>
#include <locale.h>
#include <assert.h>
#include <dlfcn.h>

/*
 * local includes
 */

#include "instzones_lib.h"
#include "zones_strings.h"

/*
 * Private structures
 */

/*
 * these dynamic libraries are required in order to use the branded zones
 * functionality.  If these libraries are not available at runtime,
 * then the zones we find are assumed to be native zones.
 */

#define	BRAND1_LIBRARY	"libbrand.so.1"
#define	BRAND_LIBRARY	"libbrand.so"

/*
 * Library Function Prototypes
 */

/*
 * Local Function Prototypes
 */

static void	error_and_exit(int error_num);

static void 	(*fatal_err_func)() = &error_and_exit;

/* ----------------------- private functions -------------------------- */
/*
 * error_and_exit()
 *	Abort routine. An exit code of '2' is used by all applications
 *	to indicate a non-recoverable fatal error.
 * Parameters:
 *	error_num - error index number:
 *			ERR_MALLOC_FAIL
 * Return:
 *	none
 * Status:
 *	private
 */
static void
error_and_exit(int error_num)
{
	if (error_num == ERR_MALLOC_FAIL)
		(void) fprintf(stderr, "Allocation of memory failed\n");
	else
		(void) fprintf(stderr, "ERROR: code %d\n", error_num);
	exit(2);
}

/*
 * *****************************************************************************
 * global external (public) functions
 * *****************************************************************************
 */

/*
 * Name:	_z_close_file_descriptors
 * Description:	close a file descriptor "a_fd" not in the list "a_fds"
 *		This function is called from the fdwalk() library function.
 *		If the file descriptor passed in is NOT in the list passed in,
 *		the file is closed.
 * Arguments:	a_fds - [RO, *RO] - (void *)
 *			Pointer to list of file descriptors to keep open
 *		a_fd - [RO, *RO] - (int)
 *			File descriptor to check
 * Returns:	int
 *			0 - success
 */

int
_z_close_file_descriptors(void *a_fds, int a_fd)
{
	int	*fds;
	int	i;

	/* do not close standard input, output, or error file descriptors */

	if (a_fd == STDIN_FILENO || a_fd == STDOUT_FILENO ||
	    a_fd == STDERR_FILENO) {
		return (0);
	}

	/* if no file descriptor retention list, close this file */

	if (a_fds == (void *)NULL) {
		(void) close(a_fd);
		return (0);
	}

	/*
	 * retention list provided, skip this descriptor if its in the list
	 */

	fds = (int *)a_fds;

	for (i = 0; fds[i] != -1; i++) {
		if (fds[i] == a_fd) {
			return (0);
		}
	}

	/* this descriptor not in retention list - close this file */

	(void) close(a_fd);

	return (0);
}

/*
 * Name:	_z_echo
 * Synopsis:	Output an interactive message if interaction is enabled
 * Description:	Main method for outputting an interactive message; call to
 *		output interactive message if interation has not been disabled
 *		by a previous call to echoSetFlag(0).
 * Arguments:	format - [RO, RO*] (char *)
 *			printf-style format for debugging message to be output
 *		VARG_LIST - [RO] (?)
 *			arguments as appropriate to 'format' specified
 * Returns:	void
 */

/*PRINTFLIKE1*/
void
_z_echo(char *a_format, ...)
{
	va_list ap;
	char	message[MAX_MESSAGE_SIZE];

	/* entry assertions */

	assert(a_format != NULL);

	/* return if no progerr function registered */

	if (_z_global_data._z_echo == NULL) {
		return;
	}

	/* capture message */

	va_start(ap, a_format);
	(void) vsnprintf(message, sizeof (message), a_format, ap);
	va_end(ap);

	/* pass message to registered function */

	(_z_global_data._z_echo)("%s", message);
}

/*
 * Name:	_z_echoDebug
 * Synopsis:	Output a debugging message if debugging is enabled
 * Description:	Main method for outputting a debugging message; call to
 *		output debugging message if debugging has been enabled
 *		by a previous call to _z_echoDebugSetFlag(1).
 * Arguments:	format - [RO, RO*] (char *)
 *			printf-style format for debugging message to be output
 *		VARG_LIST - [RO] (?)
 *			arguments as appropriate to 'format' specified
 * Returns:	void
 * NOTE:	format of message will be:
 *			# [ aaa bbb ccc ] message
 *		where:	aaa - process i.d.
 *			bbb - zone i.d.
 *			ccc - name of program
 * 		for example:
 *			# [ 25685   0 pkgadd     ] unable to get package list
 */

/*PRINTFLIKE1*/
void
_z_echoDebug(char *a_format, ...)
{
	va_list ap;
	char	message[MAX_MESSAGE_SIZE];

	/* entry assertions */

	assert(a_format != NULL);

	/* return if no progerr function registered */

	if (_z_global_data._z_echo_debug == NULL) {
		return;
	}

	/* capture message */

	va_start(ap, a_format);
	(void) vsnprintf(message, sizeof (message), a_format, ap);
	va_end(ap);

	/* pass message to registered function */

	(_z_global_data._z_echo_debug)("%s", message);
}

/*
 * Name:	_z_is_directory
 * Description:	determine if specified path exists and is a directory
 * Arguments:	path - pointer to string representing the path to verify
 * returns: 0 - directory exists
 *	    1 - directory does not exist or is not a directory
 * NOTE:	errno is set appropriately
 */

int
_z_is_directory(char *path)
{
	struct stat statbuf;

	/* entry assertions */

	assert(path != NULL);
	assert(*path != '\0');

	/* return error if path does not exist */

	if (stat(path, &statbuf) != 0) {
		return (1);
	}

	/* return error if path is not a directory */

	if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
		errno = ENOTDIR;
		return (1);
	}

	/* path exists and is a directory */

	return (0);
}

/*
 * Name:	_z_pluginCatchSigint
 * Synopsis:	SIGINT/SIGHUP interrupt handler
 * Description:	Catch the "SIGINT" and "SIGHUP" signals:
 *		-> increment _z_SigReceived global variable
 *		-> propagate signal to "_z_ChildProcessId" if registered (!= -1)
 * Arguments:	signo - [RO, *RO] - (int)
 *			Signal number that was caught
 * Returns:	void
 */

void
_z_sig_trap(int a_signo)
{
	/* bump signals received count */

	_z_global_data._z_SigReceived++;

	/* if child process registered, propagate signal to child */

	if (_z_global_data._z_ChildProcessId > 0) {
		(void) kill(_z_global_data._z_ChildProcessId, a_signo);
	}
}

/*
 * Name:	_z_program_error
 * Description:	Output an error message to the appropriate destinations
 * Arguments:	format - [RO, RO*] (char *)
 *			printf-style format for debugging message to be output
 *		VARG_LIST - [RO] (?)
 *			arguments as appropriate to 'format' specified
 * Returns:	void
 * NOTE:	format of message will be:
 *			[aaa: ] ERROR: message
 *		where:	aaa - program name (if set)
 *			message - results of format and arguments
 * 		for example:
 *			ERROR: unable to get package list
 */

/*PRINTFLIKE1*/
void
_z_program_error(char *a_format, ...)
{
	va_list ap;
	char	message[MAX_MESSAGE_SIZE];

	/* entry assertions */

	assert(a_format != NULL);

	/* return if no progerr function registered */

	if (_z_global_data._z_progerr == NULL) {
		return;
	}

	/* capture message */

	va_start(ap, a_format);
	(void) vsnprintf(message, sizeof (message), a_format, ap);
	va_end(ap);

	/* pass message to registered function */

	(_z_global_data._z_progerr)(MSG_PROG_ERR, message);
}

/*
 * Name:	_z_running_in_global_zone
 * Synopsis:	Determine if this process is running in the global zone
 * Arguments:	void
 * Returns:	boolean_t
 *			== B_TRUE - this process is running in the global zone
 *			== B_FALSE - this process is running in a nonglobal zone
 */

boolean_t
_z_running_in_global_zone(void)
{
	zoneid_t	zoneid = (zoneid_t)-1;

	/*
	 * if zones are not implemented, there is no way to tell if zones
	 * are supported or not - in this case, we can only be running in the
	 * global zone (since non-global zones cannot exist) so return TRUE
	 */

	if (z_zones_are_implemented() == B_FALSE) {
		return (B_TRUE);
	}

	/* get the zone i.d. of the current zone */

	zoneid = getzoneid();

	/* return TRUE if this is the global zone i.d. */

	if (zoneid == GLOBAL_ZONEID) {
		return (B_TRUE);
	}

	/* return FALSE - not in the global zone */

	return (B_FALSE);
}

/*
 * Name:	_z_zones_are_implemented
 * Synopsis:	Determine if zones are supported by the current system
 * Arguments:	void
 * Returns:	boolean_t
 *			== B_TRUE - zones are supported
 *			== B_FALSE - zones are not supported
 */

boolean_t
_z_zones_are_implemented(void)
{
	void	*libptr = NULL;

	/* locate zone cfg library */

	libptr = dlopen(ZONECFG_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	if (libptr == (void *)NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG_LIBRARY, dlerror());
		libptr = dlopen(ZONECFG1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	}

	/* return false if library not available */

	if (libptr == (void *)NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, ZONECFG1_LIBRARY,
		    dlerror());
		return (B_FALSE);
	}

	/* library available - close handle */

	(void) dlclose(libptr);

	/* locate contract filesystem library */

	libptr = dlopen(CONTRACT_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	if (libptr == (void *)NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT_LIBRARY,
		    dlerror());
		libptr = dlopen(CONTRACT1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	}

	/* return false if library not available */

	if (libptr == (void *)NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, CONTRACT1_LIBRARY,
		    dlerror());
		return (B_FALSE);
	}

	/* library available - close handle */

	(void) dlclose(libptr);

	/* return success */

	return (B_TRUE);
}

boolean_t
_z_brands_are_implemented(void)
{
	void	*libptr;

	/* locate brand library */

	libptr = dlopen(BRAND_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	if (libptr == NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND_LIBRARY, dlerror());
		libptr = dlopen(BRAND1_LIBRARY, RTLD_NOW|RTLD_GLOBAL);
	}

	/* return false if library not available */

	if (libptr == NULL) {
		_z_echoDebug(DBG_LIBRARY_NOT_FOUND, BRAND1_LIBRARY, dlerror());
		return (B_FALSE);
	}

	/* library available - close handle */

	(void) dlclose(libptr);

	/* return success */

	return (B_TRUE);
}

/*
 * z_calloc()
 * 	Allocate 'size' bytes from the heap using calloc()
 * Parameters:
 *	size	- number of bytes to allocate
 * Return:
 *	NULL	- calloc() failure
 *	void *	- pointer to allocated structure
 * Status:
 *	public
 */
void *
_z_calloc(size_t size)
{
	void *	tmp;

	if ((tmp = (void *) malloc(size)) == NULL) {
		fatal_err_func(ERR_MALLOC_FAIL);
		return (NULL);
	}

	(void) memset(tmp, 0, size);
	return (tmp);
}

/*
 * z_malloc()
 * 	Alloc 'size' bytes from heap using malloc()
 * Parameters:
 *	size	- number of bytes to malloc
 * Return:
 *	NULL	- malloc() failure
 *	void *	- pointer to allocated structure
 * Status:
 *	public
 */
void *
_z_malloc(size_t size)
{
	void *tmp;

	if ((tmp = (void *) malloc(size)) == NULL) {
		fatal_err_func(ERR_MALLOC_FAIL);
		return (NULL);
	} else
		return (tmp);
}

/*
 * _z_realloc()
 *	Calls realloc() with the specfied parameters. _z_realloc()
 *	checks for realloc failures and adjusts the return value
 *	automatically.
 * Parameters:
 *	ptr	- pointer to existing data block
 * 	size	- number of bytes additional
 * Return:
 *	NULL	- realloc() failed
 *	void *	- pointer to realloc'd structured
 * Status:
 *	public
 */
void *
_z_realloc(void *ptr, size_t size)
{
	void *tmp;

	if ((tmp = (void *)realloc(ptr, size)) == (void *)NULL) {
		fatal_err_func(ERR_MALLOC_FAIL);
		return ((void *)NULL);
	} else
		return (tmp);
}

/*
 * z_strdup()
 *	Allocate space for the string from the heap, copy 'str' into it,
 *	and return a pointer to it.
 * Parameters:
 *	str	- string to duplicate
 * Return:
 *	NULL	- duplication failed or 'str' was NULL
 * 	char *	- pointer to newly allocated/initialized structure
 * Status:
 *	public
 */
void *
_z_strdup(char *str)
{
	char *tmp;

	if (str == NULL)
		return ((char *)NULL);

	if ((tmp = strdup(str)) == NULL) {
		fatal_err_func(ERR_MALLOC_FAIL);
		return ((char *)NULL);
	} else
		return (tmp);
}