xref: /illumos-gate/usr/src/cmd/saf/util.c (revision 2a8bcb4e)
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  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
25*34e48580Sdp /*
26*34e48580Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*34e48580Sdp  * Use is subject to license terms.
28*34e48580Sdp  */
297c478bd9Sstevel@tonic-gate 
30*34e48580Sdp #include <stdio.h>
31*34e48580Sdp #include <stdlib.h>
32*34e48580Sdp #include <strings.h>
33*34e48580Sdp #include <ctype.h>
34*34e48580Sdp #include <sys/types.h>
35*34e48580Sdp #include <unistd.h>
36*34e48580Sdp #include "extern.h"
37*34e48580Sdp #include "misc.h"
38*34e48580Sdp #include <sac.h>
39*34e48580Sdp #include "structs.h"
40*34e48580Sdp #ifdef SAC
41*34e48580Sdp #include "msgs.h"
42*34e48580Sdp #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate char	Comment[SIZE];	/* place holder for comments */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * nexttok - return next token, essentially a strtok, but it can
497c478bd9Sstevel@tonic-gate  *	deal with null fields and strtok can not
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	args:	str - the string to be examined, NULL if we should
527c478bd9Sstevel@tonic-gate  *		      examine the remembered string
537c478bd9Sstevel@tonic-gate  *		delim - the list of valid delimiters
547c478bd9Sstevel@tonic-gate  *		ros - rest of string flag (1 for rest of string, 0 for
557c478bd9Sstevel@tonic-gate  *		      normal processing)
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate char *
nexttok(str,delim,ros)607c478bd9Sstevel@tonic-gate nexttok(str, delim, ros)
617c478bd9Sstevel@tonic-gate char *str;
627c478bd9Sstevel@tonic-gate register char *delim;
637c478bd9Sstevel@tonic-gate int ros;
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	static char *savep;	/* the remembered string */
667c478bd9Sstevel@tonic-gate 	register char *p;	/* pointer to start of token */
677c478bd9Sstevel@tonic-gate 	register char *ep;	/* pointer to end of token */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	p = (str == NULL) ? savep : str ;
707c478bd9Sstevel@tonic-gate 	if (ros)
717c478bd9Sstevel@tonic-gate 		return(p);
727c478bd9Sstevel@tonic-gate 	if (p == NULL)
737c478bd9Sstevel@tonic-gate 		return(NULL);
747c478bd9Sstevel@tonic-gate 	ep = strpbrk(p, delim);
757c478bd9Sstevel@tonic-gate 	if (ep == NULL) {
767c478bd9Sstevel@tonic-gate 		savep = NULL;
777c478bd9Sstevel@tonic-gate 		return(p);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 	savep = ep + 1;
807c478bd9Sstevel@tonic-gate 	*ep = '\0';
817c478bd9Sstevel@tonic-gate 	return(p);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * parse - parse a line from _sactab.  This routine will return if the parse
877c478bd9Sstevel@tonic-gate  *		was successful, otherwise it will output an error and exit.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  *	args:	p - pointer to the data read from the file
907c478bd9Sstevel@tonic-gate  *		sp - pointer to a structure in which the separated fields
917c478bd9Sstevel@tonic-gate  *		     are placed
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  *	A line in the file has the following format:
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  *	tag:type:flags:restart_count:command_string	#comment
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate void
parse(p,sp)1007c478bd9Sstevel@tonic-gate parse(p, sp)
1017c478bd9Sstevel@tonic-gate register char *p;
1027c478bd9Sstevel@tonic-gate register struct sactab *sp;
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	char scratch[SIZE];	/* a scratch buffer */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * get the PM tag
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	p = nexttok(p, DELIM, FALSE);
1117c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1127c478bd9Sstevel@tonic-gate # ifdef SAC
1137c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1147c478bd9Sstevel@tonic-gate # else
1157c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1167c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1177c478bd9Sstevel@tonic-gate # endif
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTAGSIZE) {
1207c478bd9Sstevel@tonic-gate 		p[PMTAGSIZE] = '\0';
1217c478bd9Sstevel@tonic-gate # ifdef SAC
1227c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "tag too long, truncated to <%s>", p);
1237c478bd9Sstevel@tonic-gate 		log(scratch);
1247c478bd9Sstevel@tonic-gate # else
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "tag too long, truncated to <%s>", p);
1267c478bd9Sstevel@tonic-gate # endif
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_tag, p);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * get the PM type
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1357c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1367c478bd9Sstevel@tonic-gate # ifdef SAC
1377c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1387c478bd9Sstevel@tonic-gate # else
1397c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1407c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1417c478bd9Sstevel@tonic-gate # endif
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	if (strlen(p) > PMTYPESIZE) {
1447c478bd9Sstevel@tonic-gate 		p[PMTYPESIZE] = '\0';
1457c478bd9Sstevel@tonic-gate # ifdef SAC
1467c478bd9Sstevel@tonic-gate 		(void) sprintf(scratch, "type too long, truncated to <%s>", p);
1477c478bd9Sstevel@tonic-gate 		log(scratch);
1487c478bd9Sstevel@tonic-gate # else
1497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "type too long, truncated to <%s>", p);
1507c478bd9Sstevel@tonic-gate # endif
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_type, p);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * get the flags
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1597c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1607c478bd9Sstevel@tonic-gate # ifdef SAC
1617c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1627c478bd9Sstevel@tonic-gate # else
1637c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1647c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1657c478bd9Sstevel@tonic-gate # endif
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	sp->sc_flags = 0;
1687c478bd9Sstevel@tonic-gate 	while (*p) {
1697c478bd9Sstevel@tonic-gate 		switch (*p++) {
1707c478bd9Sstevel@tonic-gate 		case 'd':
1717c478bd9Sstevel@tonic-gate 			sp->sc_flags |= D_FLAG;
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 		case 'x':
1747c478bd9Sstevel@tonic-gate 			sp->sc_flags |= X_FLAG;
1757c478bd9Sstevel@tonic-gate 			break;
1767c478bd9Sstevel@tonic-gate 		default:
1777c478bd9Sstevel@tonic-gate 			(void) sprintf(scratch, "Unrecognized flag <%c>", *(p - 1));
1787c478bd9Sstevel@tonic-gate # ifdef SAC
1797c478bd9Sstevel@tonic-gate 			log(scratch);
1807c478bd9Sstevel@tonic-gate # else
1817c478bd9Sstevel@tonic-gate 			Saferrno = E_SAFERR;
1827c478bd9Sstevel@tonic-gate 			error(scratch);
1837c478bd9Sstevel@tonic-gate # endif
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * get the restart count
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
1937c478bd9Sstevel@tonic-gate 	if (p == NULL) {
1947c478bd9Sstevel@tonic-gate # ifdef SAC
1957c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
1967c478bd9Sstevel@tonic-gate # else
1977c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
1987c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
1997c478bd9Sstevel@tonic-gate # endif
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	sp->sc_rsmax = atoi(p);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * get the command string
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	p = nexttok(NULL, DELIM, FALSE);
2087c478bd9Sstevel@tonic-gate 	if (p == NULL) {
2097c478bd9Sstevel@tonic-gate # ifdef SAC
2107c478bd9Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
2117c478bd9Sstevel@tonic-gate # else
2127c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2137c478bd9Sstevel@tonic-gate 		error("_sactab file is corrupt");
2147c478bd9Sstevel@tonic-gate # endif
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	if ((sp->sc_cmd = malloc((unsigned) (strlen(p) + 1))) == NULL) {
2177c478bd9Sstevel@tonic-gate # ifdef SAC
2187c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2197c478bd9Sstevel@tonic-gate # else
2207c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2217c478bd9Sstevel@tonic-gate 		error("malloc failed");
2227c478bd9Sstevel@tonic-gate # endif
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_cmd, p);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * remember the comment string
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if ((sp->sc_comment = malloc((unsigned) (strlen(Comment) + 1))) == NULL) {
2317c478bd9Sstevel@tonic-gate # ifdef SAC
2327c478bd9Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
2337c478bd9Sstevel@tonic-gate # else
2347c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
2357c478bd9Sstevel@tonic-gate 		error("malloc failed");
2367c478bd9Sstevel@tonic-gate # endif
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->sc_comment, Comment);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * trim - remove comments, trim off trailing white space, done in place
2447c478bd9Sstevel@tonic-gate  *	args:	p - string to be acted upon
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate char *
trim(p)2487c478bd9Sstevel@tonic-gate trim(p)
2497c478bd9Sstevel@tonic-gate register char *p;
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	register char *tp;	/* temp pointer */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * remove comments, if any, but remember them for later
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	tp = strchr(p, COMMENT);
2587c478bd9Sstevel@tonic-gate 	Comment[0] = '\0';
2597c478bd9Sstevel@tonic-gate 	if (tp) {
2607c478bd9Sstevel@tonic-gate 		(void) strcpy(Comment, tp + 1);	/* skip the '#' */
2617c478bd9Sstevel@tonic-gate 		*tp = '\0';
2627c478bd9Sstevel@tonic-gate 		tp = strchr(Comment, '\n');
2637c478bd9Sstevel@tonic-gate 		if (tp)
2647c478bd9Sstevel@tonic-gate 			*tp ='\0';
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * remove trailing whitespace, if any
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	for (tp = p + strlen(p) - 1; tp >= p && isspace(*tp); --tp)
2727c478bd9Sstevel@tonic-gate 		*tp = '\0';
2737c478bd9Sstevel@tonic-gate 	return(p);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * pstate - put port monitor state into intelligible form for output
2797c478bd9Sstevel@tonic-gate  *	SSTATE is only used by sacadm
2807c478bd9Sstevel@tonic-gate  *
2817c478bd9Sstevel@tonic-gate  *	args:	state - binary representation of state
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate char *
pstate(unchar state)285*34e48580Sdp pstate(unchar state)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	switch (state) {
2887c478bd9Sstevel@tonic-gate 	case NOTRUNNING:
2897c478bd9Sstevel@tonic-gate 		return("NOTRUNNING");
2907c478bd9Sstevel@tonic-gate 	case STARTING:
2917c478bd9Sstevel@tonic-gate 		return("STARTING");
2927c478bd9Sstevel@tonic-gate 	case ENABLED:
2937c478bd9Sstevel@tonic-gate 		return("ENABLED");
2947c478bd9Sstevel@tonic-gate 	case DISABLED:
2957c478bd9Sstevel@tonic-gate 		return("DISABLED");
2967c478bd9Sstevel@tonic-gate 	case STOPPING:
2977c478bd9Sstevel@tonic-gate 		return("STOPPING");
2987c478bd9Sstevel@tonic-gate 	case FAILED:
2997c478bd9Sstevel@tonic-gate 		return("FAILED");
3007c478bd9Sstevel@tonic-gate 	case UNKNOWN:
3017c478bd9Sstevel@tonic-gate 		return("UNKNOWN");
3027c478bd9Sstevel@tonic-gate # ifndef SAC
3037c478bd9Sstevel@tonic-gate 	case SSTATE:
3047c478bd9Sstevel@tonic-gate 		return("NO_SAC");
3057c478bd9Sstevel@tonic-gate # endif
3067c478bd9Sstevel@tonic-gate 	default:
3077c478bd9Sstevel@tonic-gate # ifdef SAC
3087c478bd9Sstevel@tonic-gate 		error(E_BADSTATE, EXIT);
3097c478bd9Sstevel@tonic-gate # else
3107c478bd9Sstevel@tonic-gate 		Saferrno = E_SAFERR;
3117c478bd9Sstevel@tonic-gate 		error("Improper message from SAC\n");
3127c478bd9Sstevel@tonic-gate # endif
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
315*34e48580Sdp 	return (NULL);
3167c478bd9Sstevel@tonic-gate }
317