15c51f12Moriah Waterland/*
25c51f12Moriah Waterland * CDDL HEADER START
35c51f12Moriah Waterland *
45c51f12Moriah Waterland * The contents of this file are subject to the terms of the
55c51f12Moriah Waterland * Common Development and Distribution License (the "License").
65c51f12Moriah Waterland * You may not use this file except in compliance with the License.
75c51f12Moriah Waterland *
85c51f12Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f12Moriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f12Moriah Waterland * See the License for the specific language governing permissions
115c51f12Moriah Waterland * and limitations under the License.
125c51f12Moriah Waterland *
135c51f12Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f12Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f12Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f12Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f12Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f12Moriah Waterland *
195c51f12Moriah Waterland * CDDL HEADER END
205c51f12Moriah Waterland */
215c51f12Moriah Waterland
225c51f12Moriah Waterland/*
235c51f12Moriah Waterland * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
245c51f12Moriah Waterland * Use is subject to license terms.
255c51f12Moriah Waterland */
265c51f12Moriah Waterland
275c51f12Moriah Waterland
285c51f12Moriah Waterland/* unix system includes */
295c51f12Moriah Waterland
305c51f12Moriah Waterland#include <stdio.h>
315c51f12Moriah Waterland#include <stdarg.h>
325c51f12Moriah Waterland#include <stdlib.h>
335c51f12Moriah Waterland#include <string.h>
345c51f12Moriah Waterland#include <fcntl.h>
355c51f12Moriah Waterland#include <sys/types.h>
365c51f12Moriah Waterland#include <sys/stat.h>
375c51f12Moriah Waterland#include <unistd.h>
385c51f12Moriah Waterland#include <locale.h>
395c51f12Moriah Waterland#include <errno.h>
405c51f12Moriah Waterland#include <sys/param.h>
415c51f12Moriah Waterland#include <instzones_api.h>
425c51f12Moriah Waterland
435c51f12Moriah Waterland/*
445c51f12Moriah Waterland * consolidation pkg command library includes
455c51f12Moriah Waterland */
465c51f12Moriah Waterland
475c51f12Moriah Waterland#include "pkglib.h"
485c51f12Moriah Waterland
495c51f12Moriah Waterland/*
505c51f12Moriah Waterland * local pkg command library includes
515c51f12Moriah Waterland */
525c51f12Moriah Waterland
535c51f12Moriah Waterland#include "install.h"
545c51f12Moriah Waterland#include "libinst.h"
555c51f12Moriah Waterland#include "libadm.h"
565c51f12Moriah Waterland#include "messages.h"
575c51f12Moriah Waterland
585c51f12Moriah Waterland/* Should be defined by cc -D */
595c51f12Moriah Waterland#if	!defined(TEXT_DOMAIN)
605c51f12Moriah Waterland#define	TEXT_DOMAIN "SYS_TEST"
615c51f12Moriah Waterland#endif
625c51f12Moriah Waterland
635c51f12Moriah Waterland/* local static data */
645c51f12Moriah Waterland
655c51f12Moriah Waterlandstatic boolean_t	verbose = B_FALSE;
665c51f12Moriah Waterland
675c51f12Moriah Waterland/*
685c51f12Moriah Waterland * Name:	log_msg
695c51f12Moriah Waterland * Description:	Outputs messages to logging facility.
705c51f12Moriah Waterland * Scope:	public
715c51f12Moriah Waterland * Arguments:	a_type - the severity of the message
725c51f12Moriah Waterland *		a_format - the printf format, plus its arguments
735c51f12Moriah Waterland * Returns:	none
745c51f12Moriah Waterland */
755c51f12Moriah Waterland
765c51f12Moriah Waterland/*PRINTFLIKE2*/
775c51f12Moriah Waterlandvoid
785c51f12Moriah Waterlandlog_msg(LogMsgType a_type, const char *a_format, ...)
795c51f12Moriah Waterland{
805c51f12Moriah Waterland	FILE	*out;
815c51f12Moriah Waterland	char	*rstr = (char *)NULL;
825c51f12Moriah Waterland	char	bfr[1];
835c51f12Moriah Waterland	char	*prefix;
845c51f12Moriah Waterland	size_t	vres = 0;
855c51f12Moriah Waterland	va_list	ap;
865c51f12Moriah Waterland	char	*p = get_prog_name();
875c51f12Moriah Waterland
885c51f12Moriah Waterland	/* process message based on type */
895c51f12Moriah Waterland
905c51f12Moriah Waterland	switch (a_type) {
915c51f12Moriah Waterland	case LOG_MSG_ERR:
925c51f12Moriah Waterland	default:	/* treat unknown type as LOG_MSG_ERR */
935c51f12Moriah Waterland		out = stderr;
945c51f12Moriah Waterland		prefix = MSG_LOG_ERROR;
955c51f12Moriah Waterland		break;
965c51f12Moriah Waterland	case LOG_MSG_WRN:	/* warning message */
975c51f12Moriah Waterland		out = stderr;
985c51f12Moriah Waterland		prefix = MSG_LOG_WARNING;
995c51f12Moriah Waterland		break;
1005c51f12Moriah Waterland	case LOG_MSG_INFO:	/* information message */
1015c51f12Moriah Waterland		out = stdout;
1025c51f12Moriah Waterland		prefix = NULL;
1035c51f12Moriah Waterland		break;
1045c51f12Moriah Waterland	case LOG_MSG_DEBUG:	/* debugging message */
1055c51f12Moriah Waterland		if (!log_get_verbose()) {
1065c51f12Moriah Waterland			/* no debug messages if not verbose mode */
1075c51f12Moriah Waterland			return;
1085c51f12Moriah Waterland		}
1095c51f12Moriah Waterland
1105c51f12Moriah Waterland		out = stderr;
1115c51f12Moriah Waterland		prefix = NULL;
1125c51f12Moriah Waterland
1135c51f12Moriah Waterland		/* output debug prefix to match echoDebug() format */
1145c51f12Moriah Waterland
1155c51f12Moriah Waterland		(void) fprintf(stderr, "# [%6d %3d", getpid(), getzoneid());
1165c51f12Moriah Waterland
1175c51f12Moriah Waterland		if ((p != (char *)NULL) && (*p != '\0')) {
1185c51f12Moriah Waterland			fprintf(stderr, " %-11s", p);
1195c51f12Moriah Waterland		}
1205c51f12Moriah Waterland
1215c51f12Moriah Waterland		(void) fprintf(stderr, "] ");
1225c51f12Moriah Waterland		break;
1235c51f12Moriah Waterland	}
1245c51f12Moriah Waterland
1255c51f12Moriah Waterland	/* output prefix if specified */
1265c51f12Moriah Waterland
1275c51f12Moriah Waterland	if (prefix != NULL) {
1285c51f12Moriah Waterland		(void) fprintf(out, "%s: ", prefix);
1295c51f12Moriah Waterland	}
1305c51f12Moriah Waterland
1315c51f12Moriah Waterland	/* determine size of the message in bytes */
1325c51f12Moriah Waterland
1335c51f12Moriah Waterland	va_start(ap, a_format);
1345c51f12Moriah Waterland	vres = vsnprintf(bfr, 1, a_format, ap);
1355c51f12Moriah Waterland	va_end(ap);
1365c51f12Moriah Waterland
1375c51f12Moriah Waterland	/* allocate storage to hold the message */
1385c51f12Moriah Waterland
1395c51f12Moriah Waterland	rstr = (char *)malloc(vres+2);
1405c51f12Moriah Waterland
1415c51f12Moriah Waterland	/* generate the results of the printf conversion */
1425c51f12Moriah Waterland
1435c51f12Moriah Waterland	va_start(ap, a_format);
1445c51f12Moriah Waterland	vres = vsnprintf(rstr, vres+1, a_format, ap);
1455c51f12Moriah Waterland	va_end(ap);
1465c51f12Moriah Waterland
1475c51f12Moriah Waterland	/* output formatted message to appropriate destination */
1485c51f12Moriah Waterland
1495c51f12Moriah Waterland	if (fprintf(out, "%s\n", rstr) < 0) {
1505c51f12Moriah Waterland		if (out != stderr) {
1515c51f12Moriah Waterland			/*
1525c51f12Moriah Waterland			 * nothing output, try stderr as a
1535c51f12Moriah Waterland			 * last resort
1545c51f12Moriah Waterland			 */
1555c51f12Moriah Waterland			(void) fprintf(stderr, ERR_LOG_FAIL, a_format);
1565c51f12Moriah Waterland		}
1575c51f12Moriah Waterland	}
1585c51f12Moriah Waterland
1595c51f12Moriah Waterland	/* free temporary message storage */
1605c51f12Moriah Waterland
1615c51f12Moriah Waterland	free(rstr);
1625c51f12Moriah Waterland}
1635c51f12Moriah Waterland
1645c51f12Moriah Waterland/*
1655c51f12Moriah Waterland * Name:	set_verbose
1665c51f12Moriah Waterland * Description:	Turns on verbose output
1675c51f12Moriah Waterland * Scope:	public
1685c51f12Moriah Waterland * Arguments:	verbose = B_TRUE indicates verbose mode
1695c51f12Moriah Waterland * Returns:	none
1705c51f12Moriah Waterland */
1715c51f12Moriah Waterland
1725c51f12Moriah Waterlandvoid
1735c51f12Moriah Waterlandlog_set_verbose(boolean_t setting)
1745c51f12Moriah Waterland{
1755c51f12Moriah Waterland	verbose = setting;
1765c51f12Moriah Waterland}
1775c51f12Moriah Waterland
1785c51f12Moriah Waterland/*
1795c51f12Moriah Waterland * Name:	get_verbose
1805c51f12Moriah Waterland * Description:	Returns whether or not to output verbose messages
1815c51f12Moriah Waterland * Scope:	public
1825c51f12Moriah Waterland * Arguments:	none
1835c51f12Moriah Waterland * Returns:	B_TRUE - verbose messages should be output
1845c51f12Moriah Waterland */
1855c51f12Moriah Waterland
1865c51f12Moriah Waterlandboolean_t
1875c51f12Moriah Waterlandlog_get_verbose()
1885c51f12Moriah Waterland{
1895c51f12Moriah Waterland	return (verbose);
1905c51f12Moriah Waterland}