xref: /onnv-gate/usr/src/cmd/saf/readtab.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 
250Sstevel@tonic-gate /*
26*334Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27*334Sdp  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
30*334Sdp #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*334Sdp #include <stdio.h>
33*334Sdp #include <stdlib.h>
34*334Sdp #include <signal.h>
35*334Sdp #include "misc.h"
36*334Sdp #include "msgs.h"
37*334Sdp #include <sac.h>
38*334Sdp #include "structs.h"
39*334Sdp #include <sys/types.h>
40*334Sdp #include <unistd.h>
41*334Sdp #include "extern.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * read_table - read in SAC's administrative file and build internal
460Sstevel@tonic-gate  *		data structures
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  *	args:	startflag - flag to indicate if port monitor's should be
490Sstevel@tonic-gate  *			    started as a side effect of reading
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate void
read_table(startflag)530Sstevel@tonic-gate read_table(startflag)
540Sstevel@tonic-gate int startflag;
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	FILE *fp;		/* scratch file pointer */
570Sstevel@tonic-gate 	int ret;		/* return code from check_version */
580Sstevel@tonic-gate 	struct sactab *sp;	/* working pointer to move through PM info */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate # ifdef DEBUG
610Sstevel@tonic-gate 	debug("in read_table");
620Sstevel@tonic-gate # endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * make sure _sactab is ok
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	Nentries = 0;
690Sstevel@tonic-gate 	if ((ret = check_version(VERSION, SACTAB)) == 1)
700Sstevel@tonic-gate 		error(E_BADVER, EXIT);
710Sstevel@tonic-gate 	else if (ret == 2)
720Sstevel@tonic-gate 		error(E_SACOPEN, EXIT);
730Sstevel@tonic-gate 	else if (ret == 3)
740Sstevel@tonic-gate 		error(E_BADFILE, EXIT);
750Sstevel@tonic-gate 	fp = fopen(SACTAB, "r");
760Sstevel@tonic-gate 	if (fp == NULL)
770Sstevel@tonic-gate 		error(E_SACOPEN, EXIT);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * mark all entries as invalid
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	for (sp = Sactab; sp; sp = sp->sc_next)
840Sstevel@tonic-gate 		sp->sc_valid = 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * build internal structures
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	while (sp = read_entry(fp))
910Sstevel@tonic-gate 		insert(sp, startflag);
920Sstevel@tonic-gate 	purge();
930Sstevel@tonic-gate 	(void) fclose(fp);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * read_entry - read an entry from _sactab
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  *	args:	fp - file pointer referencing _sactab
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate struct sactab *
read_entry(fp)1040Sstevel@tonic-gate read_entry(fp)
1050Sstevel@tonic-gate FILE *fp;
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	register struct sactab *sp;	/* working pointer */
1080Sstevel@tonic-gate 	register char *p;		/* scratch pointer */
1090Sstevel@tonic-gate 	char buf[SIZE];			/* scratch buffer */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * retrieve a line from the file
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	do {
1160Sstevel@tonic-gate 		if (fgets(buf, SIZE, fp) == NULL)
1170Sstevel@tonic-gate 			return(NULL);
1180Sstevel@tonic-gate 		p = trim(buf);
1190Sstevel@tonic-gate 	} while (*p == '\0');
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * allocate a list element for it and then parse the line, parsed
1230Sstevel@tonic-gate  * info goes into list element
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	sp = (struct sactab *) calloc(1, sizeof(struct sactab));
1270Sstevel@tonic-gate 	if (sp == NULL)
1280Sstevel@tonic-gate 		error(E_MALLOC, EXIT);
1290Sstevel@tonic-gate 	sp->sc_sstate = sp->sc_lstate = sp->sc_pstate = NOTRUNNING;
1300Sstevel@tonic-gate 	(void) memset(sp->sc_utid, '\0', IDLEN);
1310Sstevel@tonic-gate 	parse(p, sp);
1320Sstevel@tonic-gate 	return(sp);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * insert - insert a sactab entry into the linked list
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  *	args:	sp - entry to be inserted
1400Sstevel@tonic-gate  *		startflag - flag to indicate if port monitor's should be
1410Sstevel@tonic-gate  *			    started as a side effect of reading
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate void
insert(sp,startflag)1450Sstevel@tonic-gate insert(sp, startflag)
1460Sstevel@tonic-gate register struct sactab *sp;
1470Sstevel@tonic-gate int startflag;
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	register struct sactab *tsp, *savtsp;	/* scratch pointers */
1500Sstevel@tonic-gate 	int ret;				/* strcmp return value */
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate # ifdef DEBUG
1530Sstevel@tonic-gate 	debug("in insert");
1540Sstevel@tonic-gate # endif
1550Sstevel@tonic-gate 	savtsp = tsp = Sactab;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate  * find the correct place to insert this element
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	while (tsp) {
1620Sstevel@tonic-gate 		ret = strcmp(sp->sc_tag, tsp->sc_tag);
1630Sstevel@tonic-gate # ifdef DEBUG
1640Sstevel@tonic-gate 		(void) sprintf(Scratch, "sp->sc_tag <%s> tsp->sc_tag <%s>, ret is %d", sp->sc_tag, tsp->sc_tag, ret);
1650Sstevel@tonic-gate 		debug(Scratch);
1660Sstevel@tonic-gate # endif
1670Sstevel@tonic-gate 		if (ret > 0) {
1680Sstevel@tonic-gate 			/* keep on looking */
1690Sstevel@tonic-gate 			savtsp = tsp;
1700Sstevel@tonic-gate 			tsp = tsp->sc_next;
1710Sstevel@tonic-gate 			continue;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 		else if (ret == 0) {
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * found an entry for it in the list, either a duplicate or we're
1770Sstevel@tonic-gate  * rereading the file.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 			if (tsp->sc_valid) {
1810Sstevel@tonic-gate 				/* this is a duplicate entry, ignore it */
1820Sstevel@tonic-gate 				(void) sprintf(Scratch, "Ignoring duplicate entry for <%s>", tsp->sc_tag);
1830Sstevel@tonic-gate 				log(Scratch);
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			else {
1860Sstevel@tonic-gate 				/* found a valid match, replace flags & restart max only */
1870Sstevel@tonic-gate 				tsp->sc_rsmax = sp->sc_rsmax;
1880Sstevel@tonic-gate 				tsp->sc_flags = sp->sc_flags;
1890Sstevel@tonic-gate # ifdef DEBUG
1900Sstevel@tonic-gate 				(void) sprintf(Scratch, "replacing <%s>", sp->sc_tag);
1910Sstevel@tonic-gate 				debug(Scratch);
1920Sstevel@tonic-gate # endif
1930Sstevel@tonic-gate 				/* this entry is "current" */
1940Sstevel@tonic-gate 				tsp->sc_valid = 1;
1950Sstevel@tonic-gate 				Nentries++;
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			free(sp->sc_cmd);
1980Sstevel@tonic-gate 			free(sp);
1990Sstevel@tonic-gate 			return;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		else {
2020Sstevel@tonic-gate 			/* insert it here */
2030Sstevel@tonic-gate 			if (tsp == Sactab) {
2040Sstevel@tonic-gate 				sp->sc_next = Sactab;
2050Sstevel@tonic-gate 				Sactab = sp;
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 			else {
2080Sstevel@tonic-gate 				sp->sc_next = savtsp->sc_next;
2090Sstevel@tonic-gate 				savtsp->sc_next = sp;
2100Sstevel@tonic-gate 			}
2110Sstevel@tonic-gate # ifdef DEBUG
2120Sstevel@tonic-gate 			(void) sprintf(Scratch, "adding <%s>", sp->sc_tag);
2130Sstevel@tonic-gate 			debug(Scratch);
2140Sstevel@tonic-gate # endif
2150Sstevel@tonic-gate 			Nentries++;
2160Sstevel@tonic-gate 			/* this entry is "current" */
2170Sstevel@tonic-gate 			sp->sc_valid = 1;
2180Sstevel@tonic-gate 			if (startflag && !(sp->sc_flags & X_FLAG))
2190Sstevel@tonic-gate 				(void) startpm(sp);
2200Sstevel@tonic-gate 			return;
2210Sstevel@tonic-gate 		}
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate  * either an empty list or should put element at end of list
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	sp->sc_next = NULL;
2290Sstevel@tonic-gate 	if (Sactab == NULL)
2300Sstevel@tonic-gate 		Sactab = sp;
2310Sstevel@tonic-gate 	else
2320Sstevel@tonic-gate 		savtsp->sc_next = sp;
2330Sstevel@tonic-gate # ifdef DEBUG
2340Sstevel@tonic-gate 	(void) sprintf(Scratch, "adding <%s>", sp->sc_tag);
2350Sstevel@tonic-gate 	debug(Scratch);
2360Sstevel@tonic-gate # endif
2370Sstevel@tonic-gate 	++Nentries;
2380Sstevel@tonic-gate 	/* this entry is "current" */
2390Sstevel@tonic-gate 	sp->sc_valid = 1;
2400Sstevel@tonic-gate 	if (startflag && !(sp->sc_flags & X_FLAG))
2410Sstevel@tonic-gate 		(void) startpm(sp);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * purge - purge linked list of "old" entries
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate void
purge()2520Sstevel@tonic-gate purge()
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	register struct sactab *sp;		/* working pointer */
2550Sstevel@tonic-gate 	register struct sactab *savesp, *tsp;	/* scratch pointers */
2560Sstevel@tonic-gate 	sigset_t cset;				/* for signal handling */
2570Sstevel@tonic-gate 	sigset_t tset;				/* for signal handling */
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate # ifdef DEBUG
2600Sstevel@tonic-gate 	debug("in purge");
2610Sstevel@tonic-gate # endif
2620Sstevel@tonic-gate 	/* get current signal mask */
2630Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, NULL, &cset);
2640Sstevel@tonic-gate 	sp = savesp = Sactab;
2650Sstevel@tonic-gate 	while (sp) {
2660Sstevel@tonic-gate 		if (sp->sc_valid) {
2670Sstevel@tonic-gate 			savesp = sp;
2680Sstevel@tonic-gate 			sp = sp->sc_next;
2690Sstevel@tonic-gate 			continue;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/* element should be removed */
2730Sstevel@tonic-gate 		switch (sp->sc_sstate) {
2740Sstevel@tonic-gate 		case UNKNOWN:
2750Sstevel@tonic-gate 		case ENABLED:
2760Sstevel@tonic-gate 		case DISABLED:
2770Sstevel@tonic-gate 		case STARTING:
2780Sstevel@tonic-gate 			/* need to kill it */
2790Sstevel@tonic-gate 			tset = cset;
2800Sstevel@tonic-gate 			(void) sigaddset(&tset, SIGALRM);
2810Sstevel@tonic-gate 			(void) sigaddset(&tset, SIGCLD);
2820Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &tset, NULL);
2830Sstevel@tonic-gate 			if (sendsig(sp, SIGTERM))
2840Sstevel@tonic-gate 				(void) sprintf(Scratch, "could not send SIGTERM to <%s>", sp->sc_tag);
2850Sstevel@tonic-gate 			else
2860Sstevel@tonic-gate 				(void) sprintf(Scratch, "terminating <%s>", sp->sc_tag);
2870Sstevel@tonic-gate 			log(Scratch);
2880Sstevel@tonic-gate 			(void) sigdelset(&tset, SIGALRM);
2890Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &tset, NULL);
2900Sstevel@tonic-gate 			/* fall thru */
2910Sstevel@tonic-gate 		case STOPPING:
2920Sstevel@tonic-gate 			(void) close(sp->sc_fd);
2930Sstevel@tonic-gate 			/* fall thru */
2940Sstevel@tonic-gate 		case NOTRUNNING:
2950Sstevel@tonic-gate 		case FAILED:
2960Sstevel@tonic-gate 			cleanutx(sp);
2970Sstevel@tonic-gate 			tsp = sp;
2980Sstevel@tonic-gate 			if (tsp == Sactab) {
2990Sstevel@tonic-gate 				Sactab = sp->sc_next;
3000Sstevel@tonic-gate 				savesp = Sactab;
3010Sstevel@tonic-gate 			}
3020Sstevel@tonic-gate 			else
3030Sstevel@tonic-gate 				savesp->sc_next = sp->sc_next;
3040Sstevel@tonic-gate # ifdef DEBUG
3050Sstevel@tonic-gate 			(void) sprintf(Scratch, "purging <%s>", sp->sc_tag);
3060Sstevel@tonic-gate 			debug(Scratch);
3070Sstevel@tonic-gate # endif
3080Sstevel@tonic-gate 			sp = sp->sc_next;
3090Sstevel@tonic-gate 			free(tsp->sc_cmd);
3100Sstevel@tonic-gate 			free(tsp->sc_comment);
3110Sstevel@tonic-gate 			free(tsp);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * all done cleaning up, restore signal mask
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			(void) sigprocmask(SIG_SETMASK, &cset, NULL);
3180Sstevel@tonic-gate 			break;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * dump_table - dump the internal SAC table, used to satisfy sacadm -l
3260Sstevel@tonic-gate  */
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate char **
dump_table()3300Sstevel@tonic-gate dump_table()
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	register struct sactab *sp;	/* working pointer */
3330Sstevel@tonic-gate 	register char *p;		/* scratch pointer */
3340Sstevel@tonic-gate 	register int size;		/* size of "dumped" table */
3350Sstevel@tonic-gate 	char **info, **savinfo;		/* scratch pointers */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate # ifdef DEBUG
3380Sstevel@tonic-gate 	(void) sprintf(Scratch, "about to 'info' malloc %d entries", Nentries);
3390Sstevel@tonic-gate 	debug(Scratch);
3400Sstevel@tonic-gate # endif
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * get space for number of entries we have
3440Sstevel@tonic-gate  */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (Nentries == 0)
3470Sstevel@tonic-gate 		return(NULL);
3480Sstevel@tonic-gate 	if ((info = (char **) malloc(Nentries * sizeof(char *))) == NULL) {
3490Sstevel@tonic-gate 		error(E_MALLOC, CONT);
3500Sstevel@tonic-gate 		return(NULL);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	savinfo = info;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * traverse the list allocating space for entries and formatting them
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	for (sp = Sactab; sp; sp = sp->sc_next) {
3590Sstevel@tonic-gate 		size = strlen(sp->sc_tag) + strlen(sp->sc_type) + strlen(sp->sc_cmd) + strlen(sp->sc_comment) + SLOP;
3600Sstevel@tonic-gate 		if ((p = malloc((unsigned) size)) == NULL) {
3610Sstevel@tonic-gate 			error(E_MALLOC, CONT);
3620Sstevel@tonic-gate 			return(NULL);
3630Sstevel@tonic-gate 		}
3640Sstevel@tonic-gate 		(void) sprintf(p, "%s:%s:%d:%d:%d:%s:%s\n", sp->sc_tag, sp->sc_type,
3650Sstevel@tonic-gate 			sp->sc_flags, sp->sc_rsmax, sp->sc_pstate, sp->sc_cmd, sp->sc_comment);
3660Sstevel@tonic-gate 		*info++ = p;
3670Sstevel@tonic-gate # ifdef DEBUG
3680Sstevel@tonic-gate 		debug(*(info - 1));
3690Sstevel@tonic-gate # endif
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	return(savinfo);
3720Sstevel@tonic-gate }
373