xref: /illumos-gate/usr/src/cmd/logadm/err.c (revision 454c9e74)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b493790cSbasabi  * Common Development and Distribution License (the "License").
6b493790cSbasabi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e9a193fcSJohn.Zolnowsky@Sun.COM  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * logadm/err.c -- some basic error routines
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include "err.h"
367c478bd9Sstevel@tonic-gate 
37*454c9e74SToomas Soome jmp_buf Err_env;
38e9a193fcSJohn.Zolnowsky@Sun.COM jmp_buf	*Err_env_ptr;
397c478bd9Sstevel@tonic-gate static const char *Myname;
407c478bd9Sstevel@tonic-gate static int Exitcode;
417c478bd9Sstevel@tonic-gate static FILE *Errorfile;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * err_init -- initialize the error handling routine
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate void
err_init(const char * myname)487c478bd9Sstevel@tonic-gate err_init(const char *myname)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	char *ptr;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if ((ptr = strrchr(myname, '/')) == NULL)
537c478bd9Sstevel@tonic-gate 		Myname = myname;
547c478bd9Sstevel@tonic-gate 	else
557c478bd9Sstevel@tonic-gate 		Myname = ptr + 1;
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static const char *File;
597c478bd9Sstevel@tonic-gate static int Line;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * err_fileline -- record the filename/line number for err(EF_FILE, ...)
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate void
err_fileline(const char * file,int line)657c478bd9Sstevel@tonic-gate err_fileline(const char *file, int line)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	File = file;
687c478bd9Sstevel@tonic-gate 	Line = line;
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * err -- print an error message and return, exit or longjmp based on flags
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * this routine calls gettext() to translate the fmt string.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
777c478bd9Sstevel@tonic-gate void
err(int flags,const char * fmt,...)787c478bd9Sstevel@tonic-gate err(int flags, const char *fmt, ...)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	va_list ap;
817c478bd9Sstevel@tonic-gate 	int safe_errno = errno;
827c478bd9Sstevel@tonic-gate 	char *errno_msg = NULL;
837c478bd9Sstevel@tonic-gate 	int as_is = 0;
847c478bd9Sstevel@tonic-gate 	int jump = 0;
857c478bd9Sstevel@tonic-gate 	int warning = 0;
867c478bd9Sstevel@tonic-gate 	int fileline = 0;
877c478bd9Sstevel@tonic-gate 	char *prefix = "Error: ";
887c478bd9Sstevel@tonic-gate 	const char *intlfmt;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
917c478bd9Sstevel@tonic-gate 	intlfmt = gettext(fmt);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (flags & EF_WARN) {
947c478bd9Sstevel@tonic-gate 		warning = 1;
957c478bd9Sstevel@tonic-gate 		prefix = "Warning: ";
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 	if (flags & EF_FILE) {
987c478bd9Sstevel@tonic-gate 		fileline = 1;
997c478bd9Sstevel@tonic-gate 		Exitcode++;
1007c478bd9Sstevel@tonic-gate 	}
1017c478bd9Sstevel@tonic-gate 	if (flags & EF_SYS)
1027c478bd9Sstevel@tonic-gate 		errno_msg = strerror(safe_errno);
1037c478bd9Sstevel@tonic-gate 	if (flags & EF_JMP)
1047c478bd9Sstevel@tonic-gate 		jump = 1;
1057c478bd9Sstevel@tonic-gate 	if (flags & EF_RAW)
1067c478bd9Sstevel@tonic-gate 		as_is = 1;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/* print a copy to stderr */
1097c478bd9Sstevel@tonic-gate 	if (!as_is) {
110b493790cSbasabi 		if (Myname != NULL) {
1117c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", Myname);
1127c478bd9Sstevel@tonic-gate 			if (Errorfile)
1137c478bd9Sstevel@tonic-gate 				(void) fprintf(Errorfile, "%s: ", Myname);
1147c478bd9Sstevel@tonic-gate 		}
1157c478bd9Sstevel@tonic-gate 		if (fileline && File) {
1167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s line %d: ", File, Line);
1177c478bd9Sstevel@tonic-gate 			if (Errorfile)
1187c478bd9Sstevel@tonic-gate 				(void) fprintf(Errorfile,
1197c478bd9Sstevel@tonic-gate 				    "%s line %d: ", File, Line);
1207c478bd9Sstevel@tonic-gate 		}
1217c478bd9Sstevel@tonic-gate 		(void) fputs(gettext(prefix), stderr);
1227c478bd9Sstevel@tonic-gate 		if (Errorfile)
1237c478bd9Sstevel@tonic-gate 			(void) fputs(gettext(prefix), Errorfile);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, intlfmt, ap);
1267c478bd9Sstevel@tonic-gate 	if (Errorfile)
1277c478bd9Sstevel@tonic-gate 		(void) vfprintf(Errorfile, intlfmt, ap);
128b493790cSbasabi 	if (errno_msg != NULL) {
1297c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", errno_msg);
1307c478bd9Sstevel@tonic-gate 		if (Errorfile)
1317c478bd9Sstevel@tonic-gate 			(void) fprintf(Errorfile, ": %s", errno_msg);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	if (!as_is) {
1347c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
1357c478bd9Sstevel@tonic-gate 		if (Errorfile)
1367c478bd9Sstevel@tonic-gate 			(void) fprintf(Errorfile, "\n");
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
1397c478bd9Sstevel@tonic-gate 	if (Errorfile)
1407c478bd9Sstevel@tonic-gate 		(void) fflush(Errorfile);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	va_end(ap);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (jump)
145e9a193fcSJohn.Zolnowsky@Sun.COM 		longjmp(*Err_env_ptr, 1);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (!warning && !fileline) {
1487c478bd9Sstevel@tonic-gate 		err_done(1);
1497c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * out -- print a message and return
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  * this routine calls gettext() to translate the fmt string.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1597c478bd9Sstevel@tonic-gate void
out(const char * fmt,...)1607c478bd9Sstevel@tonic-gate out(const char *fmt, ...)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	va_list ap;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	(void) vfprintf(stdout, gettext(fmt), ap);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	va_end(ap);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	CHUNKSIZE 8192		/* for copying stderr */
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * err_fromfd -- copy data from fd to stderr
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate void
err_fromfd(int fd)1767c478bd9Sstevel@tonic-gate err_fromfd(int fd)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	char buf[CHUNKSIZE];
1797c478bd9Sstevel@tonic-gate 	int count;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	while ((count = read(fd, buf, CHUNKSIZE)) > 0) {
1827c478bd9Sstevel@tonic-gate 		(void) fwrite(buf, 1, count, stderr);
1837c478bd9Sstevel@tonic-gate 		if (Errorfile)
1847c478bd9Sstevel@tonic-gate 			(void) fwrite(buf, 1, count, Errorfile);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
1877c478bd9Sstevel@tonic-gate 	if (Errorfile)
1887c478bd9Sstevel@tonic-gate 		(void) fflush(Errorfile);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * err_done -- exit the program
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate void
err_done(int exitcode)1957c478bd9Sstevel@tonic-gate err_done(int exitcode)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	/* send error mail if applicable */
1987c478bd9Sstevel@tonic-gate 	err_mailto(NULL);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (exitcode)
2017c478bd9Sstevel@tonic-gate 		exit(exitcode);
2027c478bd9Sstevel@tonic-gate 	else
2037c478bd9Sstevel@tonic-gate 		exit(Exitcode);
2047c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #define	MAXLINE	8192	/* for tmp file line buffer */
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * err_mailto -- arrange for error output to be mailed to someone
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate void
err_mailto(const char * recipient)2127c478bd9Sstevel@tonic-gate err_mailto(const char *recipient)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	static const char *lastrecipient;
2157c478bd9Sstevel@tonic-gate 	static char mailcmd[] = "/bin/mailx -s 'logadm error output'";
2167c478bd9Sstevel@tonic-gate 	char *cmd;
2177c478bd9Sstevel@tonic-gate 	int len;
2187c478bd9Sstevel@tonic-gate 	FILE *pfp;
2197c478bd9Sstevel@tonic-gate 	char line[MAXLINE];
2207c478bd9Sstevel@tonic-gate 
221b493790cSbasabi 	if (lastrecipient != NULL) {
222b493790cSbasabi 		if (recipient != NULL &&
223b493790cSbasabi 		    strcmp(recipient, lastrecipient) == 0)
2247c478bd9Sstevel@tonic-gate 			return;		/* keep going, same recipient */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		/* stop saving output for lastrecipient and send message */
2277c478bd9Sstevel@tonic-gate 		if (ftell(Errorfile)) {
2287c478bd9Sstevel@tonic-gate 			rewind(Errorfile);
2297c478bd9Sstevel@tonic-gate 			len = strlen(lastrecipient) + strlen(mailcmd) + 2;
2307c478bd9Sstevel@tonic-gate 			cmd = MALLOC(len);
2317c478bd9Sstevel@tonic-gate 			(void) snprintf(cmd, len, "%s %s",
2327c478bd9Sstevel@tonic-gate 			    mailcmd, lastrecipient);
2337c478bd9Sstevel@tonic-gate 			if ((pfp = popen(cmd, "w")) == NULL)
2347c478bd9Sstevel@tonic-gate 				err(EF_SYS, "popen to mailx");
2357c478bd9Sstevel@tonic-gate 			while (fgets(line, MAXLINE, Errorfile) != NULL)
2367c478bd9Sstevel@tonic-gate 				(void) fputs(line, pfp);
2377c478bd9Sstevel@tonic-gate 			(void) pclose(pfp);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 		(void) fclose(Errorfile);
2407c478bd9Sstevel@tonic-gate 		Errorfile = NULL;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
243b493790cSbasabi 	if (recipient != NULL) {
2447c478bd9Sstevel@tonic-gate 		/* start saving error output for this recipient */
2457c478bd9Sstevel@tonic-gate 		if ((Errorfile = tmpfile()) == NULL)
2467c478bd9Sstevel@tonic-gate 			err(EF_SYS, "tmpfile");
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	lastrecipient = recipient;
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * err_malloc -- a malloc() with checks
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * this routine is typically called via the MALLOC() macro in err.h
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate void *
err_malloc(int nbytes,const char * fname,int line)2577c478bd9Sstevel@tonic-gate err_malloc(int nbytes, const char *fname, int line)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	void *retval = malloc(nbytes);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (retval == NULL)
2627c478bd9Sstevel@tonic-gate 		err(0, "%s:%d: out of memory", fname, line);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	return (retval);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * err_realloc -- a realloc() with checks
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  * this routine is typically called via the REALLOC() macro in err.h
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate void *
err_realloc(void * ptr,int nbytes,const char * fname,int line)2737c478bd9Sstevel@tonic-gate err_realloc(void *ptr, int nbytes, const char *fname, int line)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	void *retval = realloc(ptr, nbytes);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (retval == NULL)
2787c478bd9Sstevel@tonic-gate 		err(0, "%s:%d: out of memory", fname, line);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (retval);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * err_strdup -- a strdup() with checks
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * this routine is typically called via the STRDUP() macro in err.h
2877c478bd9Sstevel@tonic-gate  */
2887c478bd9Sstevel@tonic-gate char *
err_strdup(const char * ptr,const char * fname,int line)2897c478bd9Sstevel@tonic-gate err_strdup(const char *ptr, const char *fname, int line)
2907c478bd9Sstevel@tonic-gate {
291b493790cSbasabi 	char *retval = NULL;
292b493790cSbasabi 
293b493790cSbasabi 	if (ptr != NULL) {
294b493790cSbasabi 		retval = strdup(ptr);
295b493790cSbasabi 		if (retval == NULL)
296b493790cSbasabi 			err(0, "%s:%d: out of memory", fname, line);
297b493790cSbasabi 	} else
298b493790cSbasabi 		err(0, "%s:%d: could not strdup", fname, line);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	return (retval);
302b493790cSbasabi 
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate /*
3067c478bd9Sstevel@tonic-gate  * err_free -- a free() with checks
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * this routine is typically called via the FREE() macro in err.h
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
3117c478bd9Sstevel@tonic-gate void
err_free(void * ptr,const char * fname,int line)3127c478bd9Sstevel@tonic-gate err_free(void *ptr, const char *fname, int line)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	/* nothing to check in this version */
3157c478bd9Sstevel@tonic-gate 	free(ptr);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * err_exitcode -- set an error exit code for when done(0) is called
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate void
err_exitcode(int exitcode)3227c478bd9Sstevel@tonic-gate err_exitcode(int exitcode)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	Exitcode = exitcode;
3257c478bd9Sstevel@tonic-gate }
326