xref: /onnv-gate/usr/src/cmd/saf/util.c (revision 334:249936f45018)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
25*334Sdp /*
26*334Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*334Sdp  * Use is subject to license terms.
28*334Sdp  */
290Sstevel@tonic-gate 
30*334Sdp #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
33*334Sdp #include <stdio.h>
34*334Sdp #include <stdlib.h>
35*334Sdp #include <strings.h>
36*334Sdp #include <ctype.h>
37*334Sdp #include <sys/types.h>
38*334Sdp #include <unistd.h>
39*334Sdp #include "extern.h"
40*334Sdp #include "misc.h"
41*334Sdp #include <sac.h>
42*334Sdp #include "structs.h"
43*334Sdp #ifdef SAC
44*334Sdp #include "msgs.h"
45*334Sdp #endif
460Sstevel@tonic-gate 
470Sstevel@tonic-gate char	Comment[SIZE];	/* place holder for comments */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * nexttok - return next token, essentially a strtok, but it can
520Sstevel@tonic-gate  *	deal with null fields and strtok can not
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  *	args:	str - the string to be examined, NULL if we should
550Sstevel@tonic-gate  *		      examine the remembered string
560Sstevel@tonic-gate  *		delim - the list of valid delimiters
570Sstevel@tonic-gate  *		ros - rest of string flag (1 for rest of string, 0 for
580Sstevel@tonic-gate  *		      normal processing)
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 
620Sstevel@tonic-gate char *
nexttok(str,delim,ros)630Sstevel@tonic-gate nexttok(str, delim, ros)
640Sstevel@tonic-gate char *str;
650Sstevel@tonic-gate register char *delim;
660Sstevel@tonic-gate int ros;
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	static char *savep;	/* the remembered string */
690Sstevel@tonic-gate 	register char *p;	/* pointer to start of token */
700Sstevel@tonic-gate 	register char *ep;	/* pointer to end of token */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	p = (str == NULL) ? savep : str ;
730Sstevel@tonic-gate 	if (ros)
740Sstevel@tonic-gate 		return(p);
750Sstevel@tonic-gate 	if (p == NULL)
760Sstevel@tonic-gate 		return(NULL);
770Sstevel@tonic-gate 	ep = strpbrk(p, delim);
780Sstevel@tonic-gate 	if (ep == NULL) {
790Sstevel@tonic-gate 		savep = NULL;
800Sstevel@tonic-gate 		return(p);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	savep = ep + 1;
830Sstevel@tonic-gate 	*ep = '\0';
840Sstevel@tonic-gate 	return(p);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * parse - parse a line from _sactab.  This routine will return if the parse
900Sstevel@tonic-gate  *		was successful, otherwise it will output an error and exit.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  *	args:	p - pointer to the data read from the file
930Sstevel@tonic-gate  *		sp - pointer to a structure in which the separated fields
940Sstevel@tonic-gate  *		     are placed
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  *	A line in the file has the following format:
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  *	tag:type:flags:restart_count:command_string	#comment
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate void
parse(p,sp)1030Sstevel@tonic-gate parse(p, sp)
1040Sstevel@tonic-gate register char *p;
1050Sstevel@tonic-gate register struct sactab *sp;
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	char scratch[SIZE];	/* a scratch buffer */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * get the PM tag
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	p = nexttok(p, DELIM, FALSE);
1140Sstevel@tonic-gate 	if (p == NULL) {
1150Sstevel@tonic-gate # ifdef SAC
1160Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1170Sstevel@tonic-gate # else
1180Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1190Sstevel@tonic-gate 		error("_sactab file is corrupt");
1200Sstevel@tonic-gate # endif
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	if (strlen(p) > PMTAGSIZE) {
1230Sstevel@tonic-gate 		p[PMTAGSIZE] = '\0';
1240Sstevel@tonic-gate # ifdef SAC
1250Sstevel@tonic-gate 		(void) sprintf(scratch, "tag too long, truncated to <%s>", p);
1260Sstevel@tonic-gate 		log(scratch);
1270Sstevel@tonic-gate # else
1280Sstevel@tonic-gate 		(void) fprintf(stderr, "tag too long, truncated to <%s>", p);
1290Sstevel@tonic-gate # endif
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 	(void) strcpy(sp->sc_tag, p);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * get the PM type
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1380Sstevel@tonic-gate 	if (p == NULL) {
1390Sstevel@tonic-gate # ifdef SAC
1400Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1410Sstevel@tonic-gate # else
1420Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1430Sstevel@tonic-gate 		error("_sactab file is corrupt");
1440Sstevel@tonic-gate # endif
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	if (strlen(p) > PMTYPESIZE) {
1470Sstevel@tonic-gate 		p[PMTYPESIZE] = '\0';
1480Sstevel@tonic-gate # ifdef SAC
1490Sstevel@tonic-gate 		(void) sprintf(scratch, "type too long, truncated to <%s>", p);
1500Sstevel@tonic-gate 		log(scratch);
1510Sstevel@tonic-gate # else
1520Sstevel@tonic-gate 		(void) fprintf(stderr, "type too long, truncated to <%s>", p);
1530Sstevel@tonic-gate # endif
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	(void) strcpy(sp->sc_type, p);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * get the flags
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1620Sstevel@tonic-gate 	if (p == NULL) {
1630Sstevel@tonic-gate # ifdef SAC
1640Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1650Sstevel@tonic-gate # else
1660Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1670Sstevel@tonic-gate 		error("_sactab file is corrupt");
1680Sstevel@tonic-gate # endif
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	sp->sc_flags = 0;
1710Sstevel@tonic-gate 	while (*p) {
1720Sstevel@tonic-gate 		switch (*p++) {
1730Sstevel@tonic-gate 		case 'd':
1740Sstevel@tonic-gate 			sp->sc_flags |= D_FLAG;
1750Sstevel@tonic-gate 			break;
1760Sstevel@tonic-gate 		case 'x':
1770Sstevel@tonic-gate 			sp->sc_flags |= X_FLAG;
1780Sstevel@tonic-gate 			break;
1790Sstevel@tonic-gate 		default:
1800Sstevel@tonic-gate 			(void) sprintf(scratch, "Unrecognized flag <%c>", *(p - 1));
1810Sstevel@tonic-gate # ifdef SAC
1820Sstevel@tonic-gate 			log(scratch);
1830Sstevel@tonic-gate # else
1840Sstevel@tonic-gate 			Saferrno = E_SAFERR;
1850Sstevel@tonic-gate 			error(scratch);
1860Sstevel@tonic-gate # endif
1870Sstevel@tonic-gate 			break;
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * get the restart count
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1960Sstevel@tonic-gate 	if (p == NULL) {
1970Sstevel@tonic-gate # ifdef SAC
1980Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1990Sstevel@tonic-gate # else
2000Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2010Sstevel@tonic-gate 		error("_sactab file is corrupt");
2020Sstevel@tonic-gate # endif
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	sp->sc_rsmax = atoi(p);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * get the command string
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
2110Sstevel@tonic-gate 	if (p == NULL) {
2120Sstevel@tonic-gate # ifdef SAC
2130Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
2140Sstevel@tonic-gate # else
2150Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2160Sstevel@tonic-gate 		error("_sactab file is corrupt");
2170Sstevel@tonic-gate # endif
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	if ((sp->sc_cmd = malloc((unsigned) (strlen(p) + 1))) == NULL) {
2200Sstevel@tonic-gate # ifdef SAC
2210Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2220Sstevel@tonic-gate # else
2230Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2240Sstevel@tonic-gate 		error("malloc failed");
2250Sstevel@tonic-gate # endif
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	(void) strcpy(sp->sc_cmd, p);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * remember the comment string
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if ((sp->sc_comment = malloc((unsigned) (strlen(Comment) + 1))) == NULL) {
2340Sstevel@tonic-gate # ifdef SAC
2350Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2360Sstevel@tonic-gate # else
2370Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2380Sstevel@tonic-gate 		error("malloc failed");
2390Sstevel@tonic-gate # endif
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 	(void) strcpy(sp->sc_comment, Comment);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate  * trim - remove comments, trim off trailing white space, done in place
2470Sstevel@tonic-gate  *	args:	p - string to be acted upon
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate char *
trim(p)2510Sstevel@tonic-gate trim(p)
2520Sstevel@tonic-gate register char *p;
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	register char *tp;	/* temp pointer */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * remove comments, if any, but remember them for later
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	tp = strchr(p, COMMENT);
2610Sstevel@tonic-gate 	Comment[0] = '\0';
2620Sstevel@tonic-gate 	if (tp) {
2630Sstevel@tonic-gate 		(void) strcpy(Comment, tp + 1);	/* skip the '#' */
2640Sstevel@tonic-gate 		*tp = '\0';
2650Sstevel@tonic-gate 		tp = strchr(Comment, '\n');
2660Sstevel@tonic-gate 		if (tp)
2670Sstevel@tonic-gate 			*tp ='\0';
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /*
2710Sstevel@tonic-gate  * remove trailing whitespace, if any
2720Sstevel@tonic-gate  */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
2750Sstevel@tonic-gate 		*tp = '\0';
2760Sstevel@tonic-gate 	return(p);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * pstate - put port monitor state into intelligible form for output
2820Sstevel@tonic-gate  *	SSTATE is only used by sacadm
2830Sstevel@tonic-gate  *
2840Sstevel@tonic-gate  *	args:	state - binary representation of state
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate char *
pstate(unchar state)288*334Sdp pstate(unchar state)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	switch (state) {
2910Sstevel@tonic-gate 	case NOTRUNNING:
2920Sstevel@tonic-gate 		return("NOTRUNNING");
2930Sstevel@tonic-gate 	case STARTING:
2940Sstevel@tonic-gate 		return("STARTING");
2950Sstevel@tonic-gate 	case ENABLED:
2960Sstevel@tonic-gate 		return("ENABLED");
2970Sstevel@tonic-gate 	case DISABLED:
2980Sstevel@tonic-gate 		return("DISABLED");
2990Sstevel@tonic-gate 	case STOPPING:
3000Sstevel@tonic-gate 		return("STOPPING");
3010Sstevel@tonic-gate 	case FAILED:
3020Sstevel@tonic-gate 		return("FAILED");
3030Sstevel@tonic-gate 	case UNKNOWN:
3040Sstevel@tonic-gate 		return("UNKNOWN");
3050Sstevel@tonic-gate # ifndef SAC
3060Sstevel@tonic-gate 	case SSTATE:
3070Sstevel@tonic-gate 		return("NO_SAC");
3080Sstevel@tonic-gate # endif
3090Sstevel@tonic-gate 	default:
3100Sstevel@tonic-gate # ifdef SAC
3110Sstevel@tonic-gate 		error(E_BADSTATE, EXIT);
3120Sstevel@tonic-gate # else
3130Sstevel@tonic-gate 		Saferrno = E_SAFERR;
3140Sstevel@tonic-gate 		error("Improper message from SAC\n");
3150Sstevel@tonic-gate # endif
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 	/* NOTREACHED */
318*334Sdp 	return (NULL);
3190Sstevel@tonic-gate }
320