xref: /illumos-gate/usr/src/cmd/sgs/error/common/errorfilter.c (revision 28ab0ca48b3e331cbbb231b1c8325f9f24f9af95)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
229fb11590Smike_s 
239fb11590Smike_s /*
249fb11590Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
259fb11590Smike_s  * Use is subject to license terms.
269fb11590Smike_s  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <pwd.h>
319fb11590Smike_s #include <string.h>
329fb11590Smike_s #include <unistd.h>
339fb11590Smike_s #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include "error.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate char	*lint_libs[] = {
377c478bd9Sstevel@tonic-gate 	IG_FILE1,
387c478bd9Sstevel@tonic-gate 	IG_FILE2,
397c478bd9Sstevel@tonic-gate 	0
407c478bd9Sstevel@tonic-gate };
41*28ab0ca4SToomas Soome char	**names_ignored;
42*28ab0ca4SToomas Soome int	nignored;
439fb11590Smike_s 
447c478bd9Sstevel@tonic-gate extern	char	*processname;
459fb11590Smike_s 
469fb11590Smike_s static int lexsort(const void *arg1, const void *arg2);
479fb11590Smike_s 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  *	Read the file ERRORNAME of the names of functions in lint
507c478bd9Sstevel@tonic-gate  *	to ignore complaints about.
517c478bd9Sstevel@tonic-gate  */
529fb11590Smike_s void
getignored(char * auxname)539fb11590Smike_s getignored(char *auxname)
547c478bd9Sstevel@tonic-gate {
559fb11590Smike_s 	int	i;
567c478bd9Sstevel@tonic-gate 	FILE	*fyle;
577c478bd9Sstevel@tonic-gate 	char	inbuffer[256];
587c478bd9Sstevel@tonic-gate 	int	uid;
597c478bd9Sstevel@tonic-gate 	char	filename[128];
607c478bd9Sstevel@tonic-gate 	char	*username;
617c478bd9Sstevel@tonic-gate 	struct	passwd *passwdentry;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	nignored = 0;
647c478bd9Sstevel@tonic-gate 	if (auxname == 0) {	/* use the default */
657c478bd9Sstevel@tonic-gate 		if ((username = (char *)getlogin()) == NULL) {
667c478bd9Sstevel@tonic-gate 			username = "Unknown";
677c478bd9Sstevel@tonic-gate 			uid = getuid();
689fb11590Smike_s 			if ((passwdentry = getpwuid(uid)) == NULL) {
697c478bd9Sstevel@tonic-gate 				return;
707c478bd9Sstevel@tonic-gate 			}
717c478bd9Sstevel@tonic-gate 		} else {
729fb11590Smike_s 			if ((passwdentry = getpwnam(username)) == NULL)
737c478bd9Sstevel@tonic-gate 				return;
747c478bd9Sstevel@tonic-gate 		}
759fb11590Smike_s 		(void) strcpy(filename, passwdentry->pw_dir);
767c478bd9Sstevel@tonic-gate 		(void) strcat(filename, ERRORNAME);
777c478bd9Sstevel@tonic-gate 	} else
787c478bd9Sstevel@tonic-gate 		(void) strcpy(filename, auxname);
797c478bd9Sstevel@tonic-gate #ifdef FULLDEBUG
807c478bd9Sstevel@tonic-gate 	printf("Opening file \"%s\" to read names to ignore.\n",
817c478bd9Sstevel@tonic-gate 	    filename);
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 	if ((fyle = fopen(filename, "r")) == NULL) {
847c478bd9Sstevel@tonic-gate #ifdef FULLDEBUG
857c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
867c478bd9Sstevel@tonic-gate 		    processname, filename);
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate 		return;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 *	Make the first pass through the file, counting lines
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
947c478bd9Sstevel@tonic-gate 		continue;
959fb11590Smike_s 	names_ignored = Calloc(nignored+1, sizeof (char *));
969fb11590Smike_s 	(void) fclose(fyle);
977c478bd9Sstevel@tonic-gate 	if (freopen(filename, "r", fyle) == NULL) {
987c478bd9Sstevel@tonic-gate #ifdef FULLDEBUG
997c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
1007c478bd9Sstevel@tonic-gate 		    processname, filename);
1017c478bd9Sstevel@tonic-gate #endif
1027c478bd9Sstevel@tonic-gate 		nignored = 0;
1037c478bd9Sstevel@tonic-gate 		return;
1047c478bd9Sstevel@tonic-gate 	}
1059fb11590Smike_s 	for (i = 0; i < nignored && (fgets(inbuffer, 255, fyle) != NULL);
1069fb11590Smike_s 	    i++) {
1077c478bd9Sstevel@tonic-gate 		names_ignored[i] = strsave(inbuffer);
1087c478bd9Sstevel@tonic-gate 		(void) substitute(names_ignored[i], '\n', '\0');
1097c478bd9Sstevel@tonic-gate 	}
1109fb11590Smike_s 	qsort(names_ignored, nignored, sizeof (*names_ignored), lexsort);
1117c478bd9Sstevel@tonic-gate #ifdef FULLDEBUG
1127c478bd9Sstevel@tonic-gate 	printf("Names to ignore follow.\n");
1137c478bd9Sstevel@tonic-gate 	for (i = 0; i < nignored; i++) {
1147c478bd9Sstevel@tonic-gate 		printf("\tIgnore: %s\n", names_ignored[i]);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1199fb11590Smike_s static int
lexsort(const void * arg1,const void * arg2)1209fb11590Smike_s lexsort(const void *arg1, const void *arg2)
1217c478bd9Sstevel@tonic-gate {
1229fb11590Smike_s 	char **cpp1 = (char **)arg1;
1239fb11590Smike_s 	char **cpp2 = (char **)arg2;
1249fb11590Smike_s 
1257c478bd9Sstevel@tonic-gate 	return (strcmp(*cpp1, *cpp2));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1289fb11590Smike_s int
search_ignore(char * key)1299fb11590Smike_s search_ignore(char *key)
1307c478bd9Sstevel@tonic-gate {
1319fb11590Smike_s 	int	ub, lb;
1329fb11590Smike_s 	int	halfway;
1337c478bd9Sstevel@tonic-gate 	int	order;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (nignored == 0)
1367c478bd9Sstevel@tonic-gate 		return (-1);
1379fb11590Smike_s 	for (lb = 0, ub = nignored - 1; ub >= lb; /* NULL */) {
1387c478bd9Sstevel@tonic-gate 		halfway = (ub + lb)/2;
1397c478bd9Sstevel@tonic-gate 		if ((order = strcmp(key, names_ignored[halfway])) == 0)
1407c478bd9Sstevel@tonic-gate 			return (halfway);
1417c478bd9Sstevel@tonic-gate 		if (order < 0)	/* key is less than probe, throw away above */
1427c478bd9Sstevel@tonic-gate 			ub = halfway - 1;
1437c478bd9Sstevel@tonic-gate 		else
1447c478bd9Sstevel@tonic-gate 			lb = halfway + 1;
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	return (-1);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate  *	Tell if the error text is to be ignored.
1517c478bd9Sstevel@tonic-gate  *	The error must have been canonicalized, with
1527c478bd9Sstevel@tonic-gate  *	the file name the zeroth entry in the errorv,
1537c478bd9Sstevel@tonic-gate  *	and the linenumber the second.
1547c478bd9Sstevel@tonic-gate  *	Return the new categorization of the error class.
1557c478bd9Sstevel@tonic-gate  */
1569fb11590Smike_s Errorclass
discardit(Eptr errorp)1579fb11590Smike_s discardit(Eptr errorp)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int	language;
1609fb11590Smike_s 	int	i;
1617c478bd9Sstevel@tonic-gate 	Errorclass	errorclass = errorp->error_e_class;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	switch (errorclass) {
1647c478bd9Sstevel@tonic-gate 	case C_SYNC:
1657c478bd9Sstevel@tonic-gate 	case C_NONSPEC:
1669fb11590Smike_s 	case C_UNKNOWN:
1679fb11590Smike_s 		return (errorclass);
1689fb11590Smike_s 	default:
1699fb11590Smike_s 		break;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	if (errorp->error_lgtext < 2) {
1727c478bd9Sstevel@tonic-gate 		return (C_NONSPEC);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	language = errorp->error_language;
1757c478bd9Sstevel@tonic-gate 	if (language == INLINT) {
1767c478bd9Sstevel@tonic-gate 		if (errorclass != C_NONSPEC) {	/* no file */
1777c478bd9Sstevel@tonic-gate 			for (i = 0; lint_libs[i] != 0; i++) {
1789fb11590Smike_s 				if (strcmp(errorp->error_text[0],
1799fb11590Smike_s 				    lint_libs[i]) == 0) {
1807c478bd9Sstevel@tonic-gate 					return (C_DISCARD);
1817c478bd9Sstevel@tonic-gate 				}
1827c478bd9Sstevel@tonic-gate 			}
1837c478bd9Sstevel@tonic-gate 		}
1849fb11590Smike_s 		/*
1859fb11590Smike_s 		 * check if the argument to the error message
1869fb11590Smike_s 		 * is to be ignored
1879fb11590Smike_s 		 */
1887c478bd9Sstevel@tonic-gate 		if (ispunct(lastchar(errorp->error_text[2])))
1897c478bd9Sstevel@tonic-gate 			clob_last(errorp->error_text[2], '\0');
1909fb11590Smike_s 		if (search_ignore(
1919fb11590Smike_s 		    errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0) {
1927c478bd9Sstevel@tonic-gate 			return (C_NULLED);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	return (errorclass);
1967c478bd9Sstevel@tonic-gate }
197