4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
26 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma ident "%Z%%M% %I% %E% SMI"
39 #include <sys/types.h>
45 * read_table - read in SAC's administrative file and build internal
48 * args: startflag - flag to indicate if port monitor's should be
49 * started as a side effect of reading
56 FILE *fp
; /* scratch file pointer */
57 int ret
; /* return code from check_version */
58 struct sactab
*sp
; /* working pointer to move through PM info */
61 debug("in read_table");
65 * make sure _sactab is ok
69 if ((ret
= check_version(VERSION
, SACTAB
)) == 1)
70 error(E_BADVER
, EXIT
);
72 error(E_SACOPEN
, EXIT
);
74 error(E_BADFILE
, EXIT
);
75 fp
= fopen(SACTAB
, "r");
77 error(E_SACOPEN
, EXIT
);
80 * mark all entries as invalid
83 for (sp
= Sactab
; sp
; sp
= sp
->sc_next
)
87 * build internal structures
90 while (sp
= read_entry(fp
))
91 insert(sp
, startflag
);
98 * read_entry - read an entry from _sactab
100 * args: fp - file pointer referencing _sactab
107 register struct sactab
*sp
; /* working pointer */
108 register char *p
; /* scratch pointer */
109 char buf
[SIZE
]; /* scratch buffer */
112 * retrieve a line from the file
116 if (fgets(buf
, SIZE
, fp
) == NULL
)
119 } while (*p
== '\0');
122 * allocate a list element for it and then parse the line, parsed
123 * info goes into list element
126 sp
= (struct sactab
*) calloc(1, sizeof(struct sactab
));
128 error(E_MALLOC
, EXIT
);
129 sp
->sc_sstate
= sp
->sc_lstate
= sp
->sc_pstate
= NOTRUNNING
;
130 (void) memset(sp
->sc_utid
, '\0', IDLEN
);
137 * insert - insert a sactab entry into the linked list
139 * args: sp - entry to be inserted
140 * startflag - flag to indicate if port monitor's should be
141 * started as a side effect of reading
145 insert(sp
, startflag
)
146 register struct sactab
*sp
;
149 register struct sactab
*tsp
, *savtsp
; /* scratch pointers */
150 int ret
; /* strcmp return value */
155 savtsp
= tsp
= Sactab
;
158 * find the correct place to insert this element
162 ret
= strcmp(sp
->sc_tag
, tsp
->sc_tag
);
164 (void) sprintf(Scratch
, "sp->sc_tag <%s> tsp->sc_tag <%s>, ret is %d", sp
->sc_tag
, tsp
->sc_tag
, ret
);
168 /* keep on looking */
176 * found an entry for it in the list, either a duplicate or we're
177 * rereading the file.
181 /* this is a duplicate entry, ignore it */
182 (void) sprintf(Scratch
, "Ignoring duplicate entry for <%s>", tsp
->sc_tag
);
186 /* found a valid match, replace flags & restart max only */
187 tsp
->sc_rsmax
= sp
->sc_rsmax
;
188 tsp
->sc_flags
= sp
->sc_flags
;
190 (void) sprintf(Scratch
, "replacing <%s>", sp
->sc_tag
);
193 /* this entry is "current" */
204 sp
->sc_next
= Sactab
;
208 sp
->sc_next
= savtsp
->sc_next
;
209 savtsp
->sc_next
= sp
;
212 (void) sprintf(Scratch
, "adding <%s>", sp
->sc_tag
);
216 /* this entry is "current" */
218 if (startflag
&& !(sp
->sc_flags
& X_FLAG
))
225 * either an empty list or should put element at end of list
232 savtsp
->sc_next
= sp
;
234 (void) sprintf(Scratch
, "adding <%s>", sp
->sc_tag
);
238 /* this entry is "current" */
240 if (startflag
&& !(sp
->sc_flags
& X_FLAG
))
247 * purge - purge linked list of "old" entries
254 register struct sactab
*sp
; /* working pointer */
255 register struct sactab
*savesp
, *tsp
; /* scratch pointers */
256 sigset_t cset
; /* for signal handling */
257 sigset_t tset
; /* for signal handling */
262 /* get current signal mask */
263 (void) sigprocmask(SIG_SETMASK
, NULL
, &cset
);
264 sp
= savesp
= Sactab
;
272 /* element should be removed */
273 switch (sp
->sc_sstate
) {
278 /* need to kill it */
280 (void) sigaddset(&tset
, SIGALRM
);
281 (void) sigaddset(&tset
, SIGCLD
);
282 (void) sigprocmask(SIG_SETMASK
, &tset
, NULL
);
283 if (sendsig(sp
, SIGTERM
))
284 (void) sprintf(Scratch
, "could not send SIGTERM to <%s>", sp
->sc_tag
);
286 (void) sprintf(Scratch
, "terminating <%s>", sp
->sc_tag
);
288 (void) sigdelset(&tset
, SIGALRM
);
289 (void) sigprocmask(SIG_SETMASK
, &tset
, NULL
);
292 (void) close(sp
->sc_fd
);
299 Sactab
= sp
->sc_next
;
303 savesp
->sc_next
= sp
->sc_next
;
305 (void) sprintf(Scratch
, "purging <%s>", sp
->sc_tag
);
310 free(tsp
->sc_comment
);
314 * all done cleaning up, restore signal mask
317 (void) sigprocmask(SIG_SETMASK
, &cset
, NULL
);
325 * dump_table - dump the internal SAC table, used to satisfy sacadm -l
332 register struct sactab
*sp
; /* working pointer */
333 register char *p
; /* scratch pointer */
334 register int size
; /* size of "dumped" table */
335 char **info
, **savinfo
; /* scratch pointers */
338 (void) sprintf(Scratch
, "about to 'info' malloc %d entries", Nentries
);
343 * get space for number of entries we have
348 if ((info
= (char **) malloc(Nentries
* sizeof(char *))) == NULL
) {
349 error(E_MALLOC
, CONT
);
355 * traverse the list allocating space for entries and formatting them
358 for (sp
= Sactab
; sp
; sp
= sp
->sc_next
) {
359 size
= strlen(sp
->sc_tag
) + strlen(sp
->sc_type
) + strlen(sp
->sc_cmd
) + strlen(sp
->sc_comment
) + SLOP
;
360 if ((p
= malloc((unsigned) size
)) == NULL
) {
361 error(E_MALLOC
, CONT
);
364 (void) sprintf(p
, "%s:%s:%d:%d:%d:%s:%s\n", sp
->sc_tag
, sp
->sc_type
,
365 sp
->sc_flags
, sp
->sc_rsmax
, sp
->sc_pstate
, sp
->sc_cmd
, sp
->sc_comment
);