xref: /illumos-gate/usr/src/lib/mpss/common/mpss.c (revision 1da57d55)
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
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * 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 /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <strings.h>
297c478bd9Sstevel@tonic-gate #include <sys/mman.h>
307c478bd9Sstevel@tonic-gate #include <fcntl.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
377c478bd9Sstevel@tonic-gate #include <stdarg.h>
387c478bd9Sstevel@tonic-gate #include <syslog.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <procfs.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate extern int	gmatch(const char *s, const char *p);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #pragma init(__mpssmain)
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static const char *mpssident = "mpss.so.1";
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* environment variables */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	ENV_MPSSCFGFILE		"MPSSCFGFILE"
547c478bd9Sstevel@tonic-gate #define	ENV_MPSSSTACK		"MPSSSTACK"
557c478bd9Sstevel@tonic-gate #define	ENV_MPSSHEAP		"MPSSHEAP"
567c478bd9Sstevel@tonic-gate #define	ENV_MPSSERRFILE		"MPSSERRFILE"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	MPSSHEAP	0
597c478bd9Sstevel@tonic-gate #define	MPSSSTACK	1
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* config file */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	DEF_MPSSCFGFILE		"/etc/mpss.conf"
647c478bd9Sstevel@tonic-gate #define	MAXLINELEN	MAXPATHLEN + 64
657c478bd9Sstevel@tonic-gate #define	CFGDELIMITER	':'
667c478bd9Sstevel@tonic-gate #define	ARGDELIMITER	' '
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * avoid malloc which causes certain applications to crash
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate static char		lbuf[MAXLINELEN];
727c478bd9Sstevel@tonic-gate static char		pbuf[MAXPATHLEN];
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #ifdef MPSSDEBUG
757c478bd9Sstevel@tonic-gate #define	ENV_MPSSDEBUG	"MPSSDEBUG"
767c478bd9Sstevel@tonic-gate #define	MPSSPRINT(x, y)	if (mpssdebug & x) (void) fprintf y;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static int		mpssdebug;
797c478bd9Sstevel@tonic-gate #else
807c478bd9Sstevel@tonic-gate #define	MPSSPRINT(x, y)
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
847c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
887c478bd9Sstevel@tonic-gate static void
mpsserr(FILE * fp,char * fmt,...)897c478bd9Sstevel@tonic-gate mpsserr(FILE *fp, char *fmt, ...)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	va_list		ap;
927c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
937c478bd9Sstevel@tonic-gate 	if (fp)
947c478bd9Sstevel@tonic-gate 		(void) vfprintf(fp, fmt, ap);
957c478bd9Sstevel@tonic-gate 	else
967c478bd9Sstevel@tonic-gate 		vsyslog(LOG_ERR, fmt, ap);
977c478bd9Sstevel@tonic-gate 	va_end(ap);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Return the pointer to the fully-resolved path name of the process's
1027c478bd9Sstevel@tonic-gate  * executable file obtained from the AT_SUN_EXECNAME aux vector entry.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate static const char *
mygetexecname(void)1057c478bd9Sstevel@tonic-gate mygetexecname(void)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	const char	*execname = NULL;
1087c478bd9Sstevel@tonic-gate 	static auxv_t	auxb;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * The first time through, read the initial aux vector that was
1127c478bd9Sstevel@tonic-gate 	 * passed to the process at exec(2).  Only do this once.
1137c478bd9Sstevel@tonic-gate 	 */
1147c478bd9Sstevel@tonic-gate 	int fd = open("/proc/self/auxv", O_RDONLY);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
1177c478bd9Sstevel@tonic-gate 		while (read(fd, &auxb, sizeof (auxv_t)) == sizeof (auxv_t)) {
1187c478bd9Sstevel@tonic-gate 			if (auxb.a_type == AT_SUN_EXECNAME) {
1197c478bd9Sstevel@tonic-gate 				execname = auxb.a_un.a_ptr;
1207c478bd9Sstevel@tonic-gate 				break;
1217c478bd9Sstevel@tonic-gate 			}
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 		(void) close(fd);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	return (execname);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static size_t
atosz(char * szstr)1297c478bd9Sstevel@tonic-gate atosz(char *szstr)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	size_t		sz;
1327c478bd9Sstevel@tonic-gate 	char		*endptr, c;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	sz = strtoll(szstr, &endptr, 0);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	while (c = *endptr++) {
1377c478bd9Sstevel@tonic-gate 		switch (c) {
1387c478bd9Sstevel@tonic-gate 		case 't':
1397c478bd9Sstevel@tonic-gate 		case 'T':
1407c478bd9Sstevel@tonic-gate 			sz *= 1024;
1417c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
1427c478bd9Sstevel@tonic-gate 		case 'g':
1437c478bd9Sstevel@tonic-gate 		case 'G':
1447c478bd9Sstevel@tonic-gate 			sz *= 1024;
1457c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
1467c478bd9Sstevel@tonic-gate 		case 'm':
1477c478bd9Sstevel@tonic-gate 		case 'M':
1487c478bd9Sstevel@tonic-gate 			sz *= 1024;
1497c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
1507c478bd9Sstevel@tonic-gate 		case 'k':
1517c478bd9Sstevel@tonic-gate 		case 'K':
1527c478bd9Sstevel@tonic-gate 			sz *= 1024;
1537c478bd9Sstevel@tonic-gate 		default:
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	return (sz);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	PGSZELEM	(8 * sizeof (void *))
1627c478bd9Sstevel@tonic-gate static size_t		pgsz[PGSZELEM];
1637c478bd9Sstevel@tonic-gate static int		nelem;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static int
pgszok(size_t sz)1667c478bd9Sstevel@tonic-gate pgszok(size_t sz)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	int		i;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (sz == 0)
1717c478bd9Sstevel@tonic-gate 		return (1);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelem; i++) {
1747c478bd9Sstevel@tonic-gate 		if (sz == pgsz[i])
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	return (i < nelem);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static void
pgszinit()1827c478bd9Sstevel@tonic-gate pgszinit()
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	nelem = getpagesizes(NULL, 0);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (!nelem)
1877c478bd9Sstevel@tonic-gate 		return;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (nelem > PGSZELEM)
1907c478bd9Sstevel@tonic-gate 		nelem = PGSZELEM;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	(void) getpagesizes(pgsz, nelem);
1937c478bd9Sstevel@tonic-gate #ifdef MPSSDEBUG
1947c478bd9Sstevel@tonic-gate 	pgsz[nelem] = 0x800000;
1957c478bd9Sstevel@tonic-gate 	nelem++;
1967c478bd9Sstevel@tonic-gate #endif
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
pgszset(size_t sz,uint_t flags)2017c478bd9Sstevel@tonic-gate pgszset(size_t sz, uint_t flags)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	struct memcntl_mha	mpss;
2047c478bd9Sstevel@tonic-gate 	int		rc;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	mpss.mha_cmd = (flags == MPSSHEAP) ?
2077c478bd9Sstevel@tonic-gate 	    MHA_MAPSIZE_BSSBRK: MHA_MAPSIZE_STACK;
2087c478bd9Sstevel@tonic-gate 	mpss.mha_pagesize = sz;
2097c478bd9Sstevel@tonic-gate 	mpss.mha_flags = 0;
2107c478bd9Sstevel@tonic-gate 	rc = memcntl(NULL, 0, MC_HAT_ADVISE, (caddr_t)&mpss, 0, 0);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (rc);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * check if exec name matches cfgname found in mpss cfg file.
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate static int
fnmatch(const char * execname,char * cfgname,char * cwd)2197c478bd9Sstevel@tonic-gate fnmatch(const char *execname, char *cfgname, char *cwd)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	const char	*ename;
2227c478bd9Sstevel@tonic-gate 	int		rc;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* cfgname should not have a '/' unless it begins with one */
2257c478bd9Sstevel@tonic-gate 	if (cfgname[0] == '/') {
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * if execname does not begin with a '/', prepend the
2287c478bd9Sstevel@tonic-gate 		 * current directory.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		if (execname[0] != '/') {
2317c478bd9Sstevel@tonic-gate 			ename = (const char *)strcat(cwd, execname);
2327c478bd9Sstevel@tonic-gate 		} else
2337c478bd9Sstevel@tonic-gate 			ename = execname;
2347c478bd9Sstevel@tonic-gate 	} else {	/* simple cfg name */
2357c478bd9Sstevel@tonic-gate 		if (ename = strrchr(execname, '/'))
2367c478bd9Sstevel@tonic-gate 			/* execname is a path name - get the base name */
2377c478bd9Sstevel@tonic-gate 			ename++;
2387c478bd9Sstevel@tonic-gate 		else
2397c478bd9Sstevel@tonic-gate 			ename = execname;
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 	rc = gmatch(ename, cfgname);
2427c478bd9Sstevel@tonic-gate 	MPSSPRINT(2, (stderr, "gmatch: %s %s %s %d\n",
2437c478bd9Sstevel@tonic-gate 	    cfgname, ename, execname, rc));
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	return (rc);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * Check if string matches any of exec arguments.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate static int
argmatch(char * str,FILE * errfp)2527c478bd9Sstevel@tonic-gate argmatch(char *str, FILE *errfp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	int fd;
2557c478bd9Sstevel@tonic-gate 	psinfo_t pi;
2567c478bd9Sstevel@tonic-gate 	int rc = 0;
2577c478bd9Sstevel@tonic-gate 	int arg;
2587c478bd9Sstevel@tonic-gate 	char **argv;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	fd = open("/proc/self/psinfo", O_RDONLY);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
2637c478bd9Sstevel@tonic-gate 		if (read(fd, &pi, sizeof (pi)) == sizeof (pi)) {
2647c478bd9Sstevel@tonic-gate 			argv = (char **)pi.pr_argv;
2657c478bd9Sstevel@tonic-gate 			argv++;
2667c478bd9Sstevel@tonic-gate 			MPSSPRINT(2, (stderr, "argmatch: %s ", str));
2677c478bd9Sstevel@tonic-gate 			for (arg = 1; arg < pi.pr_argc; arg++, argv++) {
2687c478bd9Sstevel@tonic-gate 				if (rc = gmatch(*argv, str)) {
2697c478bd9Sstevel@tonic-gate 					MPSSPRINT(2, (stderr, "%s ", *argv));
2707c478bd9Sstevel@tonic-gate 					break;
2717c478bd9Sstevel@tonic-gate 				}
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			MPSSPRINT(2, (stderr, "%d\n", rc));
2747c478bd9Sstevel@tonic-gate 		} else {
2757c478bd9Sstevel@tonic-gate 			mpsserr(errfp, dgettext(TEXT_DOMAIN,
2767c478bd9Sstevel@tonic-gate 			    "%s: /proc/self/psinfo read failed [%s]\n"),
2777c478bd9Sstevel@tonic-gate 			    mpssident, strerror(errno));
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 		(void) close(fd);
2807c478bd9Sstevel@tonic-gate 	} else {
2817c478bd9Sstevel@tonic-gate 		mpsserr(errfp, dgettext(TEXT_DOMAIN,
2827c478bd9Sstevel@tonic-gate 		    "%s: /proc/self/psinfo open failed [%s]\n"),
2837c478bd9Sstevel@tonic-gate 		    mpssident, strerror(errno));
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	return (rc);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static int
empty(char * str)2897c478bd9Sstevel@tonic-gate empty(char *str)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	char	c;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	while ((c = *str) == '\n' || c == ' ' || c == '\t')
2947c478bd9Sstevel@tonic-gate 		str++;
2957c478bd9Sstevel@tonic-gate 	return (*str == '\0');
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate void
__mpssmain()2997c478bd9Sstevel@tonic-gate __mpssmain()
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	static size_t	heapsz = (size_t)-1, stacksz = (size_t)-1, sz;
3027c478bd9Sstevel@tonic-gate 	char		*cfgfile, *errfile;
3037c478bd9Sstevel@tonic-gate 	const char	*execname;
3047c478bd9Sstevel@tonic-gate 	char		*cwd;
3057c478bd9Sstevel@tonic-gate 	int		cwdlen;
3067c478bd9Sstevel@tonic-gate 	FILE		*fp = NULL, *errfp = NULL;
3077c478bd9Sstevel@tonic-gate 	char		*tok, *tokheap = NULL, *tokstack = NULL, *tokarg;
3087c478bd9Sstevel@tonic-gate 	char		*str, *envheap, *envstack;
3097c478bd9Sstevel@tonic-gate 	int		lineno = 0;
3107c478bd9Sstevel@tonic-gate 	char		*locale;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * If a private error file is indicated then set the locale
3147c478bd9Sstevel@tonic-gate 	 * for error messages for the duration of this routine.
3157c478bd9Sstevel@tonic-gate 	 * Error messages destined for syslog should not be translated
3167c478bd9Sstevel@tonic-gate 	 * and thus come from the default C locale.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	if ((errfile = getenv(ENV_MPSSERRFILE)) != NULL) {
319*004388ebScasper 		errfp = fopen(errfile, "aF");
3207c478bd9Sstevel@tonic-gate 		if (errfp) {
3217c478bd9Sstevel@tonic-gate 			locale = setlocale(LC_MESSAGES, "");
3227c478bd9Sstevel@tonic-gate 		} else {
3237c478bd9Sstevel@tonic-gate 			mpsserr(NULL, dgettext(TEXT_DOMAIN,
3247c478bd9Sstevel@tonic-gate 			    "%s: cannot open error file: %s [%s]\n"),
3257c478bd9Sstevel@tonic-gate 			    mpssident, errfile, strerror(errno));
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate #ifdef MPSSDEBUG
3307c478bd9Sstevel@tonic-gate 	if (str = getenv(ENV_MPSSDEBUG))
3317c478bd9Sstevel@tonic-gate 		mpssdebug = atosz(str);
3327c478bd9Sstevel@tonic-gate #endif
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	pgszinit();
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (envstack = getenv(ENV_MPSSSTACK)) {
3377c478bd9Sstevel@tonic-gate 		sz = atosz(envstack);
3387c478bd9Sstevel@tonic-gate 		if (pgszok(sz))
3397c478bd9Sstevel@tonic-gate 			stacksz = sz;
3407c478bd9Sstevel@tonic-gate 		else
3417c478bd9Sstevel@tonic-gate 			mpsserr(errfp, dgettext(TEXT_DOMAIN,
3427c478bd9Sstevel@tonic-gate 			    "%s: invalid stack page size specified:"
3437c478bd9Sstevel@tonic-gate 			    " MPSSSTACK=%s\n"),
3447c478bd9Sstevel@tonic-gate 			    mpssident, envstack);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (envheap = getenv(ENV_MPSSHEAP)) {
3487c478bd9Sstevel@tonic-gate 		sz = atosz(envheap);
3497c478bd9Sstevel@tonic-gate 		if (pgszok(sz))
3507c478bd9Sstevel@tonic-gate 			heapsz = sz;
3517c478bd9Sstevel@tonic-gate 		else
3527c478bd9Sstevel@tonic-gate 			mpsserr(errfp, dgettext(TEXT_DOMAIN,
3537c478bd9Sstevel@tonic-gate 			    "%s: invalid heap page size specified:"
3547c478bd9Sstevel@tonic-gate 			    " MPSSHEAP=%s\n"),
3557c478bd9Sstevel@tonic-gate 			    mpssident, envheap);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Open specified cfg file or default one.
3607c478bd9Sstevel@tonic-gate 	 */
3617c478bd9Sstevel@tonic-gate 	if (cfgfile = getenv(ENV_MPSSCFGFILE)) {
362*004388ebScasper 		fp = fopen(cfgfile, "rF");
3637c478bd9Sstevel@tonic-gate 		if (!fp) {
3647c478bd9Sstevel@tonic-gate 			mpsserr(errfp, dgettext(TEXT_DOMAIN,
3657c478bd9Sstevel@tonic-gate 			    "%s: cannot open configuration file: %s [%s]\n"),
3667c478bd9Sstevel@tonic-gate 			    mpssident, cfgfile, strerror(errno));
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 	} else {
3697c478bd9Sstevel@tonic-gate 		cfgfile = DEF_MPSSCFGFILE;
370*004388ebScasper 		fp = fopen(cfgfile, "rF");
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	execname = mygetexecname();
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (fp) {
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		cwd = getcwd(pbuf, MAXPATHLEN);
3787c478bd9Sstevel@tonic-gate 		if (!cwd)
3797c478bd9Sstevel@tonic-gate 			return;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		cwd = strcat(cwd, "/");
3827c478bd9Sstevel@tonic-gate 		cwdlen = strlen(cwd);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		while (fgets(lbuf, MAXLINELEN, fp)) {
3857c478bd9Sstevel@tonic-gate 			lineno++;
3867c478bd9Sstevel@tonic-gate 			if (empty(lbuf))
3877c478bd9Sstevel@tonic-gate 				continue;
3887c478bd9Sstevel@tonic-gate 			/*
3897c478bd9Sstevel@tonic-gate 			 * Make sure line wasn't truncated.
3907c478bd9Sstevel@tonic-gate 			 */
3917c478bd9Sstevel@tonic-gate 			if (strlen(lbuf) >= MAXLINELEN - 1) {
3927c478bd9Sstevel@tonic-gate 				mpsserr(errfp, dgettext(TEXT_DOMAIN,
3937c478bd9Sstevel@tonic-gate 				    "%s: invalid entry, "
3947c478bd9Sstevel@tonic-gate 				    "line too long - cfgfile:"
3957c478bd9Sstevel@tonic-gate 				    " %s, line: %d\n"),
3967c478bd9Sstevel@tonic-gate 				    mpssident, cfgfile, lineno);
3977c478bd9Sstevel@tonic-gate 				continue;
3987c478bd9Sstevel@tonic-gate 			}
3997c478bd9Sstevel@tonic-gate 			/*
4007c478bd9Sstevel@tonic-gate 			 * parse right to left in case delimiter is
4017c478bd9Sstevel@tonic-gate 			 * in name.
4027c478bd9Sstevel@tonic-gate 			 */
4037c478bd9Sstevel@tonic-gate 			if (!(tokstack = strrchr(lbuf, CFGDELIMITER))) {
4047c478bd9Sstevel@tonic-gate 				mpsserr(errfp, dgettext(TEXT_DOMAIN,
4057c478bd9Sstevel@tonic-gate 				    "%s: no delimiters specified - cfgfile:"
4067c478bd9Sstevel@tonic-gate 				    " %s, line: %d\n"),
4077c478bd9Sstevel@tonic-gate 				    mpssident, cfgfile, lineno);
4087c478bd9Sstevel@tonic-gate 				continue;
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			/* found delimiter in lbuf */
4117c478bd9Sstevel@tonic-gate 			*tokstack++ = '\0';
4127c478bd9Sstevel@tonic-gate 			/* remove for error message */
4137c478bd9Sstevel@tonic-gate 			if (str = strrchr(tokstack, '\n'))
4147c478bd9Sstevel@tonic-gate 				*str = '\0';
4157c478bd9Sstevel@tonic-gate 			if (!(tokheap = strrchr(lbuf, CFGDELIMITER))) {
4167c478bd9Sstevel@tonic-gate 				mpsserr(errfp, dgettext(TEXT_DOMAIN,
4177c478bd9Sstevel@tonic-gate 				    "%s: invalid entry, "
4187c478bd9Sstevel@tonic-gate 				    "missing delimiter - cfgfile: %s,"
4197c478bd9Sstevel@tonic-gate 				    " line: %d\n"),
4207c478bd9Sstevel@tonic-gate 				    mpssident, cfgfile, lineno);
4217c478bd9Sstevel@tonic-gate 				continue;
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate 			*tokheap++ = '\0';
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			/* exec-args is optional */
4267c478bd9Sstevel@tonic-gate 			if (tokarg = strrchr(lbuf, ARGDELIMITER)) {
4277c478bd9Sstevel@tonic-gate 				*tokarg++ = '\0';
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 			tok = lbuf;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 			if (!fnmatch(execname, tok, cwd)) {
4337c478bd9Sstevel@tonic-gate 				tokheap = tokstack = tokarg = NULL;
4347c478bd9Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
4357c478bd9Sstevel@tonic-gate 				continue;
4367c478bd9Sstevel@tonic-gate 			}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 			if (tokarg &&
4397c478bd9Sstevel@tonic-gate 			    !empty(tokarg) &&
4407c478bd9Sstevel@tonic-gate 			    !argmatch(tokarg, errfp)) {
4417c478bd9Sstevel@tonic-gate 				tokheap = tokstack = tokarg = NULL;
4427c478bd9Sstevel@tonic-gate 				cwd[cwdlen] = '\0';
4437c478bd9Sstevel@tonic-gate 				continue;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			/* heap token */
4477c478bd9Sstevel@tonic-gate 			if (empty(tokheap)) {
4487c478bd9Sstevel@tonic-gate 				/* empty cfg entry */
4497c478bd9Sstevel@tonic-gate 				heapsz = (size_t)-1;
4507c478bd9Sstevel@tonic-gate 			} else {
4517c478bd9Sstevel@tonic-gate 				sz = atosz(tokheap);
4527c478bd9Sstevel@tonic-gate 				if (pgszok(sz))
4537c478bd9Sstevel@tonic-gate 					heapsz = sz;
4547c478bd9Sstevel@tonic-gate 				else {
4557c478bd9Sstevel@tonic-gate 					mpsserr(errfp, dgettext(TEXT_DOMAIN,
4567c478bd9Sstevel@tonic-gate 					    "%s: invalid heap page size"
4577c478bd9Sstevel@tonic-gate 					    " specified (%s) for %s - "
4587c478bd9Sstevel@tonic-gate 					    "cfgfile: %s, line: %d\n"),
4597c478bd9Sstevel@tonic-gate 					    mpssident, tokheap,
4607c478bd9Sstevel@tonic-gate 					    execname, cfgfile,
4617c478bd9Sstevel@tonic-gate 					    lineno);
4627c478bd9Sstevel@tonic-gate 					heapsz = (size_t)-1;
4637c478bd9Sstevel@tonic-gate 				}
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 			/* stack token */
4677c478bd9Sstevel@tonic-gate 			if (empty(tokstack)) {
4687c478bd9Sstevel@tonic-gate 				stacksz = (size_t)-1;
4697c478bd9Sstevel@tonic-gate 				break;
4707c478bd9Sstevel@tonic-gate 			} else {
4717c478bd9Sstevel@tonic-gate 				sz = atosz(tokstack);
4727c478bd9Sstevel@tonic-gate 				if (pgszok(sz))
4737c478bd9Sstevel@tonic-gate 					stacksz = sz;
4747c478bd9Sstevel@tonic-gate 				else {
4757c478bd9Sstevel@tonic-gate 					mpsserr(errfp, dgettext(TEXT_DOMAIN,
4767c478bd9Sstevel@tonic-gate 					    "%s: invalid stack page size"
4777c478bd9Sstevel@tonic-gate 					    " specified (%s) for %s - "
4787c478bd9Sstevel@tonic-gate 					    "cfgfile: %s, line: %d\n"),
4797c478bd9Sstevel@tonic-gate 					    mpssident, tokstack,
4807c478bd9Sstevel@tonic-gate 					    execname, cfgfile, lineno);
4817c478bd9Sstevel@tonic-gate 					stacksz = (size_t)-1;
4827c478bd9Sstevel@tonic-gate 				}
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 			break;
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if ((heapsz != (size_t)-1) && (pgszset(heapsz, MPSSHEAP) < 0))
4907c478bd9Sstevel@tonic-gate 		mpsserr(errfp, dgettext(TEXT_DOMAIN,
4917c478bd9Sstevel@tonic-gate 		    "%s: memcntl() failed [%s]: heap page size (%s)"
4927c478bd9Sstevel@tonic-gate 		    " for %s not set\n"),
4937c478bd9Sstevel@tonic-gate 		    mpssident, strerror(errno), (tokheap) ? tokheap : envheap,
4947c478bd9Sstevel@tonic-gate 		    execname);
4957c478bd9Sstevel@tonic-gate 	if ((stacksz != (size_t)-1) && (pgszset(stacksz, MPSSSTACK) < 0))
4967c478bd9Sstevel@tonic-gate 		mpsserr(errfp, dgettext(TEXT_DOMAIN,
4977c478bd9Sstevel@tonic-gate 		    "%s: memcntl() failed [%s]: stack page size (%s)"
4987c478bd9Sstevel@tonic-gate 		    " for %s not set\n"),
4997c478bd9Sstevel@tonic-gate 		    mpssident, strerror(errno), (tokstack) ? tokstack: envstack,
5007c478bd9Sstevel@tonic-gate 		    execname);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (errfp) {
5037c478bd9Sstevel@tonic-gate 		(void) fclose(errfp);
5047c478bd9Sstevel@tonic-gate 		(void) setlocale(LC_MESSAGES, locale);
5057c478bd9Sstevel@tonic-gate 	} else {
5067c478bd9Sstevel@tonic-gate 		/* close log file: no-op if nothing logged to syslog */
5077c478bd9Sstevel@tonic-gate 		closelog();
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate }
510