xref: /illumos-gate/usr/src/cmd/saf/sac.c (revision 16b76d3cb933ff92018a2a75594449010192eacb)
1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 /*
26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <ctype.h>
34 #include <signal.h>
35 #include <strings.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/stropts.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42 #include <utmpx.h>
43 #include <memory.h>
44 #include "msgs.h"
45 #include "extern.h"
46 #include <sac.h>
47 #include "misc.h"
48 #include "structs.h"
49 
50 #include <security/pam_appl.h>
51 
52 #define	RESP	1		/* pollfail via no response to sanity poll */
53 #define	DEATH	2		/* pollfail via child death */
54 
55 /* signal whose dispositions will be changed */
56 
57 static struct sigaction	Sigpoll;	/* SIGPOLL */
58 static struct sigaction	Sigcld;		/* SIGCLD */
59 static struct sigaction	Sigalrm;	/* SIGALRM */
60 static sigset_t Origmask;		/* original signal mask */
61 
62 void usage(void);
63 void initialize(void);
64 void startpms(void);
65 void readutmpx(void);
66 int startpm(struct sactab *);
67 void cleanutx(struct sactab *);
68 void account(struct sactab *, pid_t);
69 void startit(struct sactab *);
70 char **mkargv(struct sactab *);
71 void pollpms(void);
72 void reap(int);
73 void pollfail(struct sactab *, int);
74 void readpipe(void);
75 int validstate(unchar);
76 int mk_cmd_pipe(void);
77 void startpoll(void);
78 
79 
80 
81 /*
82  * main - scan args for sac, initialize everything, and wait for commands
83  *	  from sacadm via the command pipe
84  */
85 
86 int
87 main(int argc, char *argv[])
88 {
89 	int c;	/* place to hold options */
90 	struct sigaction sigact;	/* for signal handling */
91 
92 	(void) sigprocmask(SIG_SETMASK, NULL, &Origmask);
93 	if (argc == 1)
94 		usage();
95 	(void) setpgrp();
96 	while ((c = getopt(argc, argv, "t:")) != -1) {
97 		switch (c) {
98 		case 't':
99 			if (Stime != 0)
100 				usage();
101 			Stime = atoi(optarg);
102 			if (Stime <= 0)
103 				usage();
104 			break;
105 		case '?':
106 			usage();
107 		}
108 	}
109 	if (optind < argc)
110 		usage();
111 
112 	initialize();
113 	sigact.sa_flags = 0;
114 	sigact.sa_handler = pollpms;
115 	(void) sigemptyset(&sigact.sa_mask);
116 	(void) sigaddset(&sigact.sa_mask, SIGALRM);
117 	(void) sigaction(SIGALRM, &sigact, &Sigalrm);
118 
119 /*
120  * minimize time spent in STARTING or UNKNOWN, pollpms() sets alarm
121  */
122 
123 	pollpms();
124 	for (;;)
125 		readpipe();
126 }
127 
128 
129 /*
130  * usage - output a usage message on the console
131  */
132 
133 void
134 usage()
135 {
136 	FILE *fp;	/* scratch file pointer */
137 
138 	fp = fopen("/dev/console", "w");
139 	if (fp)
140 		(void) fprintf(fp, "SAC: Usage: sac -t sanity_interval\n");
141 	exit(1);
142 }
143 
144 
145 /*
146  * initialize - initialization stuff
147  */
148 
149 
150 void
151 initialize()
152 {
153 	int ret;			/* return code from doconfig() */
154 	struct sigaction sigact;	/* for signal handling */
155 
156 	openlog();
157 	log("*** SAC starting ***");
158 #ifdef DEBUG
159 	opendebug();
160 	log("Debugging turned on");
161 #endif
162 	if (chdir(HOME) < 0)
163 		error(E_CHDIR, EXIT);
164 
165 /*
166  * pass an invalid fd, shouldn't be doing pushes and pops in this per-system
167  * configuration script (_sysconfig)
168  */
169 
170 	if ((ret = doconfig(-1, SYSCONFIG, 0)) != 0) {
171 		if (ret == -1)
172 			error(E_SYSCONF, EXIT);
173 		else {
174 			(void) sprintf(Scratch,
175 					"Error in _sysconfig: line %d", ret);
176 			log(Scratch);
177 			error(E_BADSYSCONF, EXIT);
178 		}
179 	}
180 
181 	sigact.sa_flags = 0;
182 	sigact.sa_handler = reap;
183 	(void) sigemptyset(&sigact.sa_mask);
184 	(void) sigaddset(&sigact.sa_mask, SIGCLD);
185 	(void) sigaction(SIGCLD, &sigact, &Sigcld);
186 
187 /*
188  * establish pipe for PMS to communicate with sac
189  */
190 
191 	if (access("_sacpipe", 0) != 0) {
192 		/* not there, create one */
193 		(void) umask(0);
194 		if (mknod("_sacpipe", S_IFIFO | 0600, 0) < 0)
195 			error(E_NOPIPE, EXIT);
196 	}
197 	Sfd = open("_sacpipe", O_RDWR);
198 	if (Sfd < 0)
199 		error(E_NOPIPE, EXIT);
200 
201 /*
202  * establish pipe for sacadm to communicate with sac
203  */
204 
205 	Cfd = mk_cmd_pipe();
206 
207 /*
208  * read in _sactab, but don't start port monitors as a by-product
209  * since we may be in recovery - start them explicitly instead
210  */
211 
212 	read_table(FALSE);
213 	startpoll();
214 	startpms();
215 }
216 
217 
218 /*
219  * startpms - start initial set of port monitors
220  */
221 
222 
223 void
224 startpms()
225 {
226 	struct sactab *sp;	/* working pointer */
227 	int rflag;			/* recovery flag */
228 	pid_t checklock();
229 
230 /*
231  * check to see if we're really a recovering SAC (if any port monitors hold
232  * locks, assume that we're in recovery), if so, start differently
233  */
234 
235 	rflag = 0;
236 	for (sp = Sactab; sp; sp = sp->sc_next) {
237 		if (checklock(sp)) {
238 			rflag = 1;
239 			sp->sc_sstate = sp->sc_pstate = UNKNOWN;
240 			sp->sc_ok = 1;
241 			sp->sc_exit = 0;
242 			(void) sprintf(Scratch, "%s/_pmpipe", sp->sc_tag);
243 			sp->sc_fd = open(Scratch, O_RDWR);
244 			if (sp->sc_fd < 0) {
245 
246 /*
247  * if we get into here, we're in deep trouble.  PM seems to be running
248  * and we're trying to recover, but we can't talk to it.  Unfortunately,
249  * there's not much that can be done other than to try and restore a
250  * sane state.  By setting sp->sc_ok to 0, this will look like a poll failure
251  * and if sp->rs_rsmax > 0, PM will be restarted.
252  */
253 
254 				(void) sprintf(Scratch,
255 				"Could not open _pmpipe for port monitor <%s>",
256 					sp->sc_tag);
257 				log(Scratch);
258 				(void) sendsig(sp, SIGTERM);
259 				sp->sc_ok = 0;
260 			}
261 		}
262 	}
263 	if (rflag) {
264 		readutmpx();
265 		log("SAC in recovery");
266 		return;
267 	}
268 
269 /*
270  * normal startup
271  */
272 
273 	for (sp = Sactab; sp; sp = sp->sc_next) {
274 		if (sp->sc_flags & X_FLAG) {
275 			/* System Administator specified don't start */
276 			continue;
277 		}
278 		(void) startpm(sp);
279 	}
280 }
281 
282 
283 /*
284  * readutmpx - read the utmpx file to find out the ids of running port
285  *		monitors (only called during a recover start up).  Note:
286  *		after a sac failure, init will inherit all of the port
287  *		monitors and should get the SIGCLD's if they die (and
288  *		will clean up).  This is mainly for stuck processes,
289  *		although init would get the SIGCLD when the stuckie gets
290  *		killed, it doesn't hurt to have the sac check.  This is
291  *		only done once.
292  *
293  */
294 
295 
296 void
297 readutmpx()
298 {
299 	struct sactab *sp;	/* working pointer */
300 	struct sactab *savesp;	/* rembered port monitor match */
301 	struct utmpx *uxp;	/* working pointer */
302 
303 	setutxent();
304 	while (uxp = getutxent()) {
305 		/* we're only interested in login processes */
306 		if (uxp->ut_type != LOGIN_PROCESS)
307 			continue;
308 		if (uxp->ut_user[sizeof (uxp->ut_user) - 1] == '\0') {
309 
310 /*
311  * possible port monitor and name is short enough to do a normal compare
312  */
313 
314 			sp = findpm(uxp->ut_user);
315 			if (sp && (sp->sc_sstate == UNKNOWN)) {
316 				/* found one */
317 				(void) memcpy(sp->sc_utid, uxp->ut_id, IDLEN);
318 				sp->sc_pid = uxp->ut_pid;
319 			}
320 		} else {
321 
322 /*
323  * possible port monitor name, but it could have been truncated.  If
324  * a match is found on a unique prefix, then it should be the correct
325  * entry.  If an ambiguity is found, ignore the entry, init will clean
326  * up the entry if it dies.
327  */
328 
329 			savesp = NULL;
330 			for (sp = Sactab; sp; sp = sp->sc_next) {
331 				if (strncmp(uxp->ut_user, sp->sc_tag,
332 				    sizeof (uxp->ut_user)) == 0) {
333 					if (savesp) {
334 						/* already found a match */
335 						savesp = NULL;
336 						(void) sprintf(Scratch,
337 						"ambiguous utmpx entry <%.8s>",
338 							sp->sc_tag);
339 						log(Scratch);
340 						break;
341 					} else {
342 						savesp = sp;
343 					}
344 				}
345 			}
346 			if (savesp && (savesp->sc_sstate == UNKNOWN)) {
347 				/* found it */
348 				(void) memcpy(savesp->sc_utid, uxp->ut_id,
349 								IDLEN);
350 				savesp->sc_pid = uxp->ut_pid;
351 			}
352 		}
353 	}
354 	endutxent();
355 }
356 
357 
358 /*
359  * startpm - start a particular PM, return code:
360  *		-1: _pid file locked
361  *		-2: any other reason
362  *
363  *	args:	sp - pointer to sac's port monitor information for
364  *		     designated port monitor
365  */
366 
367 int
368 startpm(struct sactab *sp)
369 {
370 	sigset_t cset;		/* for signal handling */
371 	sigset_t tset;		/* for signal handling */
372 	pid_t pid;		/* pid of new port monitor */
373 	pid_t checklock();
374 
375 #ifdef DEBUG
376 	debug("in startpm");
377 #endif
378 	if (checklock(sp)) {
379 		(void) sprintf(Scratch,
380 			"could not start <%s> - _pid file locked", sp->sc_tag);
381 		log(Scratch);
382 		return (-1);
383 	}
384 
385 	(void) sprintf(Scratch, "%s/_pmpipe", sp->sc_tag);
386 	if (access(Scratch, 0) != 0) {
387 		/* not there, create one */
388 		(void) umask(0);
389 		if (mknod(Scratch, S_IFIFO | 0600, 0) < 0) {
390 			(void) sprintf(Scratch,
391 		"Could not create _pmpipe for port monitor <%s>, errno is %d",
392 			sp->sc_tag, errno);
393 			log(Scratch);
394 			return (-2);
395 		}
396 	}
397 	sp->sc_fd = open(Scratch, O_RDWR);
398 	if (sp->sc_fd < 0) {
399 		(void) sprintf(Scratch,
400 		"Could not open _pmpipe for port monitor <%s>, errno is %d",
401 			sp->sc_tag, errno);
402 		log(Scratch);
403 		return (-2);
404 	}
405 
406 	/* in case child dies too quickly */
407 	(void) sigprocmask(SIG_SETMASK, NULL, &cset);
408 	tset = cset;
409 	(void) sigaddset(&tset, SIGCLD);
410 	(void) sigprocmask(SIG_SETMASK, &tset, NULL);
411 	if ((pid = fork()) < 0) {
412 		(void) sprintf(Scratch,
413 			"Could not fork port monitor <%s>", sp->sc_tag);
414 		log(Scratch);
415 		return (-2);
416 	} else if (!pid) {
417 		startit(sp);
418 		/* no return */
419 	}
420 
421 /*
422  * clean up old utmpx if its there
423  */
424 
425 	cleanutx(sp);
426 
427 /*
428  * create a utmpx entry and set initial states
429  */
430 
431 	account(sp, pid);
432 	sp->sc_pstate = STARTING;
433 	if (sp->sc_lstate == NOTRUNNING)
434 		sp->sc_sstate = (sp->sc_flags & D_FLAG) ? DISABLED : ENABLED;
435 	else
436 		sp->sc_sstate = sp->sc_lstate;
437 	sp->sc_ok = 1;
438 	sp->sc_exit = 0;
439 	sp->sc_pid = pid;
440 	/* ok to take signals now that the table is up-to-table */
441 	(void) sigprocmask(SIG_SETMASK, &cset, NULL);
442 	return (0);
443 }
444 
445 
446 /*
447  * cleanutx - clean out a utmpx record for a port monitor
448  *
449  *	args:	sp - pointer to sac's port monitor information for
450  *		     designated port monitor
451  */
452 
453 
454 void
455 cleanutx(struct sactab *sp)
456 {
457 	int i;			 /* scratch variable */
458 	int zerocheck;		  /* scratch variable */
459 	char buf[SIZE];		 /* scratch buffer */
460 	pam_handle_t *pamh;		/* PAM auth descriptor */
461 	struct utmpx ut;
462 	struct utmpx *up;
463 	int pid;
464 	char user[sizeof (up->ut_user) + 1];
465 	char ttyn[sizeof (up->ut_line) + 1];
466 	char rhost[sizeof (up->ut_host) + 1];
467 /*
468  * check to see if there is a utmpx entry to clean up (indicated by a non
469  * zero utmpx id
470  */
471 	zerocheck = 0;
472 	for (i = 0; i < IDLEN; ++i) {
473 		zerocheck += sp->sc_utid[i];
474 	}
475 	if (zerocheck == 0)
476 		return;
477 
478 	pid = sp->sc_pid;
479 	setutxent();
480 	while (up = getutxent()) {
481 		if (up->ut_pid == pid) {
482 			if (up->ut_type == DEAD_PROCESS) {
483 				/*
484 				 * Cleaned up elsewhere.
485 				 */
486 				break;
487 			}
488 			strncpy(user, up->ut_user, sizeof (up->ut_user));
489 			user[sizeof (up->ut_user)] = '\0';
490 			strncpy(ttyn, up->ut_line, sizeof (up->ut_line));
491 			ttyn[sizeof (up->ut_line)] = '\0';
492 			strncpy(rhost, up->ut_host, sizeof (up->ut_host));
493 			rhost[sizeof (up->ut_host)] = '\0';
494 
495 			if ((pam_start("sac", user, NULL, &pamh)) ==
496 								PAM_SUCCESS) {
497 				(void) pam_set_item(pamh, PAM_TTY, ttyn);
498 				(void) pam_set_item(pamh, PAM_RHOST, rhost);
499 				(void) pam_close_session(pamh, 0);
500 				pam_end(pamh, PAM_SUCCESS);
501 			}
502 
503 			up->ut_type = DEAD_PROCESS;
504 			up->ut_exit.e_termination = WTERMSIG(sp->sc_exit);
505 			up->ut_exit.e_exit = WEXITSTATUS(sp->sc_exit);
506 			if (sp->sc_utid != NULL)
507 				(void) memcpy(up->ut_id, sp->sc_utid,
508 					sizeof (up->ut_id));
509 			(void) time(&up->ut_tv.tv_sec);
510 			if (modutx(up) == NULL) {
511 				/*
512 				 * Since modutx failed we'll
513 				 * write out the new entry
514 				 * ourselves.
515 				 */
516 				(void) pututxline(up);
517 				updwtmpx("wtmpx", up);
518 			}
519 			break;
520 		}
521 	}
522 	endutxent();
523 }
524 
525 /*
526  * account - create a utmp record for a port monitor
527  *
528  *	args:	pid - process id of port monitor
529  */
530 
531 
532 void
533 account(struct sactab *sp, pid_t pid)
534 {
535 	struct utmpx utmpx;			/* prototype utmpx entry */
536 	struct utmpx *up = &utmpx;		/* and a pointer to it */
537 
538 	(void) memset(up, '\0', sizeof (utmpx));
539 	(void) strncpy(up->ut_user, sp->sc_tag, sizeof (up->ut_user));
540 	up->ut_pid = pid;
541 	up->ut_type = LOGIN_PROCESS;
542 	up->ut_id[0] = 'P';
543 	up->ut_id[1] = 'M';
544 	up->ut_id[2] = SC_WILDC;
545 	up->ut_id[3] = SC_WILDC;
546 	(void) time(&up->ut_xtime);
547 	if (makeutx(up) == NULL) {
548 		log("Could not create utmpx entry");
549 		(void) memset(sp->sc_utid, '\0', IDLEN);
550 	} else {
551 		(void) memcpy(sp->sc_utid, up->ut_id, IDLEN);
552 	}
553 }
554 
555 
556 /*
557  * startit - finish starting a particular port monitor, establish environment,
558  *		etc. (Note: this is the child at this point)
559  *
560  *	args:	sp - pointer to sac's port monitor information for
561  *		     designated port monitor
562  */
563 
564 
565 void
566 startit(struct sactab *sp)
567 {
568 	static char istate[SIZE];	/* place to put ISTATE env var. */
569 	static char pmtag[SIZE];	/* place to put PMTAG env var. */
570 	char **argvp;			/* arglist for PM */
571 	int i;				/* loop control variable */
572 	long ndesc;			/* # of file descriptors configured */
573 	int ret;			/* return value from doconfig */
574 	sigset_t cset;			/* for signal handling */
575 	sigset_t tset;			/* for signal handling */
576 
577 /*
578  * establish the home directory
579  */
580 
581 	if (chdir(sp->sc_tag) < 0) {
582 		(void) sprintf(Scratch,
583 			"Cannot chdir to <%s/%s>, port monitor not started",
584 			HOME, sp->sc_tag);
585 		log(Scratch);
586 		exit(1);
587 	}
588 
589 /*
590  * interpret the configuration script, pass an invalid fd, shouldn't be
591  * doing pushes and pops in this script
592  */
593 
594 	(void) sigprocmask(SIG_SETMASK, NULL, &cset);
595 	tset = cset;
596 	(void) sigaddset(&tset, SIGCLD);
597 	(void) sigprocmask(SIG_SETMASK, &tset, NULL);
598 	if ((ret = doconfig(-1, "_config", 0)) != 0) {
599 		if (ret == -1) {
600 			(void) sprintf(Scratch,
601 				"system error in _config script for <%s>",
602 				sp->sc_tag);
603 			log(Scratch);
604 			exit(1);
605 		} else {
606 			(void) sprintf(Scratch,
607 				"Error in _config script for <%s>: line %d",
608 				sp->sc_tag, ret);
609 			log(Scratch);
610 			exit(1);
611 		}
612 	}
613 
614 /*
615  * add the promised environment variables
616  */
617 
618 	if (sp->sc_lstate == NOTRUNNING)
619 		(void) sprintf(istate, "ISTATE=%s",
620 			(sp->sc_flags & D_FLAG) ? "disabled" : "enabled");
621 	else
622 		(void) sprintf(istate, "ISTATE=%s",
623 			(sp->sc_lstate == DISABLED) ? "disabled" : "enabled");
624 	if (putenv(istate)) {
625 		(void) sprintf(Scratch,
626 			"can't expand port monitor <%s> environment",
627 			sp->sc_tag);
628 		log(Scratch);
629 		exit(1);
630 	}
631 	(void) sprintf(pmtag, "PMTAG=%s", sp->sc_tag);
632 	if (putenv(pmtag)) {
633 		(void) sprintf(Scratch,
634 			"can't expand port monitor <%s> environment",
635 			sp->sc_tag);
636 		log(Scratch);
637 		exit(1);
638 	}
639 
640 /*
641  * build an argv
642  */
643 
644 	argvp = mkargv(sp);
645 
646 	(void) sprintf(Scratch, "starting port monitor <%s>", sp->sc_tag);
647 	log(Scratch);
648 	ndesc = ulimit(4, 0L);
649 	for (i = 0; i < ndesc; i++)
650 		(void) fcntl(i, F_SETFD, 1);
651 	/* restore orignal handlers and mask */
652 	(void) sigaction(SIGPOLL, &Sigpoll, NULL);
653 	(void) sigaction(SIGCLD, &Sigcld, NULL);
654 	(void) sigaction(SIGALRM, &Sigalrm, NULL);
655 	(void) sigprocmask(SIG_SETMASK, &Origmask, NULL);
656 	(void) execve(argvp[0], argvp, environ);
657 	(void) sprintf(Scratch, "exec of port monitor <%s> failed", sp->sc_tag);
658 	log(Scratch);
659 	exit(1);
660 }
661 
662 
663 /*
664  * mkargv - Given a pointer to a struct sactab, construct argv
665  *		for an exec system call.
666  *
667  *	args:	sp - pointer to sac's port monitor information for
668  *		     designated port montior
669  */
670 
671 
672 #define	NARGS	50	/* max # of args */
673 
674 static char *newargv[NARGS];	/* place for argv list */
675 static char *delim = " \t'\"";	/* delimiter list */
676 
677 char **
678 mkargv(struct sactab *sp)
679 {
680 	char **argvp = newargv;			/* scratch pointer */
681 	char *p = sp->sc_cmd;			/* working pointer */
682 	char delch;				/* delimiter seen */
683 	char *savep;				/* scratch pointer */
684 	char *tp;				/* scratch pointer */
685 
686 	*argvp = 0;
687 	savep = p;
688 	while (p && *p) {
689 		if (p = strpbrk(p, delim)) {
690 			switch (*p) {
691 			case ' ':
692 			case '\t':
693 				/* "normal" cases */
694 				*p++ = '\0';
695 				*argvp++ = savep;
696 				/* zap trailing white space */
697 				while (isspace(*p))
698 					p++;
699 				savep = p;
700 				break;
701 			case '"':
702 			case '\'':
703 				/* found a string */
704 				delch = *p; /* remember the delimiter */
705 				savep = ++p;
706 
707 /*
708  * We work the string in place, embedded instances of the string delimiter,
709  * i.e. \" must have the '\' removed.  Since we'd have to do a compare to
710  * decide if a copy were needed, it's less work to just do the copy, even
711  * though it is most likely unnecessary.
712  */
713 
714 				tp = p;
715 				for (;;) {
716 					if (*p == '\0') {
717 						(void) sprintf(Scratch,
718 	"invalid command line, non-terminated string for port monitor %s",
719 							sp->sc_tag);
720 						log(Scratch);
721 						exit(1);
722 					}
723 					if (*p == delch) {
724 						if (*(tp - 1) == '\\') {
725 							/* \delim */
726 							*(tp - 1) = *p;
727 							p++;
728 						} else { /* end of string */
729 							*tp = 0;
730 							*argvp++ = savep;
731 							p++;
732 						/* zap trailing white space */
733 							while (isspace(*p))
734 								p++;
735 							savep = p;
736 							break;
737 						}
738 					} else {
739 						*tp++ = *p++;
740 					}
741 				}
742 				break;
743 			default:
744 				log("Internal error in parse routine");
745 				exit(1);
746 			}
747 		}
748 		else
749 			*argvp++ = savep;
750 	}
751 	*argvp = 0;
752 	return (newargv);
753 }
754 
755 
756 /*
757  * pollpms - send out sanity polls, if sc_sstate and sc_pstate are
758  *	the same (everyone agrees on the state) or if SAC thinks PM
759  *	should be stopping, send out a status message;
760  *	otherwise, send out a message indicating the state the SAC
761  *	thinks the PM should be entering
762  */
763 
764 void
765 pollpms()
766 {
767 	struct sactab *sp;	/* working pointer */
768 	struct sacmsg sacmsg;		/* message to send to PM */
769 
770 #ifdef DEBUG
771 	debug("alarm went off");
772 #endif
773 	for (sp = Sactab; sp; sp = sp->sc_next) {
774 		if (sp->sc_pstate == NOTRUNNING || sp->sc_pstate == FAILED) {
775 			/* don't bother if no one is home */
776 			continue;
777 		}
778 		if (sp->sc_ok == 0) {
779 			/* PM has stopped responding */
780 			pollfail(sp, RESP);
781 			continue;
782 		}
783 
784 /*
785  * note - if we're in recovery, a SC_STATUS message is sent
786  * (sc_sstate = UNKNOWN and sc_pstate = UNKNOWN)
787  */
788 
789 		if (sp->sc_sstate == sp->sc_pstate) {
790 			sacmsg.sc_type = SC_STATUS;
791 			sacmsg.sc_size = 0;
792 		} else {
793 			switch (sp->sc_sstate) {
794 			case ENABLED:
795 				sacmsg.sc_type = SC_ENABLE;
796 				sacmsg.sc_size = 0;
797 				break;
798 			case DISABLED:
799 				sacmsg.sc_type = SC_DISABLE;
800 				sacmsg.sc_size = 0;
801 				break;
802 			case STARTING:
803 			case STOPPING:
804 			case NOTRUNNING:
805 			case FAILED:
806 			case UNKNOWN:
807 				/*
808 				 * if NOTRUNNING or FAILED, PM will probably
809 				 * not respond to poll, that's how we detect
810 				 * that it's gone
811 				 */
812 				sacmsg.sc_type = SC_STATUS;
813 				sacmsg.sc_size = 0;
814 				break;
815 			default:
816 				error(E_BADSTATE, EXIT);
817 			}
818 		}
819 
820 		/* send the message */
821 		sendpmmsg(sp, &sacmsg);
822 		sp->sc_ok = 0;
823 	}
824 	(void) alarm(Stime);
825 }
826 
827 
828 /*
829  * reap - clean up dead children, equivalent to a "fast" poll failure
830  *
831  *	args:	signo - signal #
832  */
833 
834 void
835 reap(int signo)
836 {
837 	struct sactab *sp;		/* working pointer */
838 	pid_t pid;			/* returned pid from wait */
839 	int status;			/* returned status from wait */
840 
841 	pid = wait(&status);
842 	for (sp = Sactab; sp; sp = sp->sc_next) {
843 		if (sp->sc_pid == pid)
844 			break;
845 	}
846 	if (sp == NULL) {
847 		/* not from a port monitor we know about */
848 		return;
849 	}
850 	sp->sc_exit = status;
851 	/* only call pollfail for "stuck" and stopping processes */
852 	if (sp->sc_pstate != NOTRUNNING && sp->sc_pstate != FAILED)
853 		pollfail(sp, DEATH);
854 }
855 
856 
857 /*
858  * pollfail - handle the case where a PM stops responding to a sanity poll
859  *
860  *	args:	sp - pointer to sac's port monitor information for
861  *		     designated port monitor
862  *		reason - RESP or DEATH (indicates why pollfail called)
863  */
864 
865 
866 void
867 pollfail(struct sactab *sp, int reason)
868 {
869 	char buf[SIZE];			/* scratch buffer */
870 	sigset_t cset;			/* for signal handling */
871 	sigset_t tset;			/* for signal handling */
872 
873 #ifdef DEBUG
874 	debug("in pollfail");
875 #endif
876 
877 /* first, remove the utmpx entry and clean up any links */
878 
879 	cleanutx(sp);
880 
881 	if (sp->sc_pstate == STOPPING) {
882 		(void) sprintf(buf, "<%s> has stopped", sp->sc_tag);
883 		log(buf);
884 		sp->sc_pstate = NOTRUNNING;
885 		sp->sc_lstate = NOTRUNNING;
886 		(void) close(sp->sc_fd);
887 	} else {
888 
889 /*
890  * PM in trouble - if it's still there, try to put it out of its misery
891  * We play with SIGCLD here to that after SIGKILL is sent, the catcher
892  * routine reap() is not called until we're ready (note: when a catcher
893  * is established for SIGCLD and any zombies are present, the signal is
894  * immediately received)
895  */
896 
897 		(void) sigprocmask(SIG_SETMASK, NULL, &cset);
898 		tset = cset;
899 		(void) sigaddset(&tset, SIGCLD);
900 		(void) sigprocmask(SIG_SETMASK, &tset, NULL);
901 		(void) sendsig(sp, SIGKILL);
902 		if (sp->sc_rscnt < sp->sc_rsmax) {
903 			/* try to restart it */
904 			if (reason == RESP)
905 				(void) sprintf(buf,
906 	"<%s> stopped responding to sanity polls - trying to restart",
907 					sp->sc_tag);
908 			else
909 				(void) sprintf(buf,
910 					"<%s> has died - trying to restart",
911 					sp->sc_tag);
912 			log(buf);
913 			sp->sc_rscnt++;
914 			(void) close(sp->sc_fd);
915 			(void) startpm(sp);
916 		} else {
917 			sp->sc_sstate = sp->sc_pstate = FAILED;
918 			(void) close(sp->sc_fd);
919 			(void) sprintf(buf, "<%s> has FAILED", sp->sc_tag);
920 			log(buf);
921 		}
922 	}
923 	(void) sigprocmask(SIG_SETMASK, &cset, NULL);
924 }
925 
926 
927 /*
928  * readpipe - read messages from _sacpipe
929  */
930 
931 
932 void
933 readpipe()
934 {
935 	struct pmmsg pmmsg;			/* incoming message */
936 	struct pmmsg *pp = &pmmsg;		/* and a pointer to it */
937 	struct sactab *sp;			/* working pointer */
938 	int ret;				/* return value from read */
939 
940 /*
941  * This routine's main purpose is to maintain the state associated with
942  * each of the known port monitors.  Because it may be confusing, following
943  * is a brief discussion of what is happening.  Three different views of
944  * a port monitor's state exist: sc_sstate, sc_pstate, and sc_lstate.
945  * sc_sstate is the state in which the sac has been instructed to place
946  * a port monitor.  sc_lstate is essentially a shadow of this field, however,
947  * it will only take on the values ENABLED, DISABLED, and NOTRUNNING.
948  * sc_lstate is used if a port monitor dies to restart it in the state in
949  * which it was last running.  sc_pstate is the last state that the port
950  * monitor reported itself in.  Note that if the administrator specifies
951  * a state change, there is a window where sc_sstate and sc_pstate will
952  * be different (until the port monitor enacts and acknowledges the change).
953  *
954  * These states interact with the polling loop to determine which message
955  * should be sent to a port monitor.  If the states agree, an SC_STATUS
956  * is sent.  If they disagree, the appropriate message to put the port
957  * monitor in the correct state is sent (SC_ENABLE or SC_DISABLE).  sc_pstate
958  * is the state that is reported back to an AC_STATUS request.  Finally,
959  * when in recovery (sc_sstate and sc_pstate both = UNKNOWN), the sac will
960  * take the port monitor's reported state as the true state.  This is the
961  * only instance in which a port monitor can cause sc_sstate to change.
962  */
963 
964 	for (;;) {
965 		if (read(Sfd, pp, sizeof (pmmsg)) < 0) {
966 			if (errno != EINTR)
967 				error(E_BADREAD, EXIT);
968 			continue;
969 		}
970 
971 		while (pp->pm_size) {
972 
973 /*
974  * there's data after the header, unfortunately, we don't understand
975  * any of it because only class 1 (no data) messages are defined.  Just
976  * flush it
977  */
978 
979 			ret = read(Sfd, Scratch,
980 				(pp->pm_size > SIZE) ? (unsigned) SIZE :
981 						(unsigned) pp->pm_size);
982 			if (ret < 0) {
983 				if (errno != EINTR)
984 					error(E_BADREAD, EXIT);
985 				continue;
986 			}
987 			else
988 				pp->pm_size -= ret;
989 		}
990 
991 		sp = findpm(pp->pm_tag);
992 		if (sp == NULL) {
993 			log("message from unknown process");
994 			continue;
995 		}
996 		switch (pp->pm_type) {
997 		case PM_UNKNOWN:
998 			(void) sprintf(Scratch,
999 				"port monitor <%s> didn't recognize message",
1000 				sp->sc_tag);
1001 			log(Scratch);
1002 			/* fall through */
1003 		case PM_STATUS:
1004 			/*
1005 			 * paranoia check, if port monitor reports garbage
1006 			 * state, pretend it said UNKNOWN
1007 			 */
1008 			if (!validstate(pp->pm_state)) {
1009 				pp->pm_state = UNKNOWN;
1010 				(void) sprintf(Scratch,
1011 				"port monitor <%s> reporting invalid state",
1012 					sp->sc_tag);
1013 				log(Scratch);
1014 			}
1015 			if (sp->sc_sstate == sp->sc_pstate) {
1016 				/* everyone agrees on the current state */
1017 				if (sp->sc_sstate == UNKNOWN) {
1018 					/* special case for recovery */
1019 					sp->sc_sstate = pp->pm_state;
1020 					sp->sc_pstate = pp->pm_state;
1021 					if (pp->pm_state == ENABLED ||
1022 						pp->pm_state == DISABLED)
1023 					/* sc_lstate NOTRUNNING by default */
1024 						sp->sc_lstate = pp->pm_state;
1025 				}
1026 				if (pp->pm_state != sp->sc_pstate) {
1027 					/*
1028 					 * something isn't right here, PM
1029 					 * changed state without orders, try
1030 					 * to restore to correct state
1031 					 */
1032 					sp->sc_pstate = pp->pm_state;
1033 				}
1034 			} else if (sp->sc_sstate == pp->pm_state) {
1035 				/* PM changed to state requested */
1036 				(void) sprintf(Scratch,
1037 			"port monitor <%s> changed state from %s to %s",
1038 					sp->sc_tag, pstate(sp->sc_pstate),
1039 					pstate(pp->pm_state));
1040 				log(Scratch);
1041 				sp->sc_pstate = pp->pm_state;
1042 			} else if (sp->sc_pstate != pp->pm_state) {
1043 				/*
1044 				 * something isn't right here, PM isn't
1045 				 * in the state it was, nor is it in the
1046 				 * state we just tried to put it in, try
1047 				 * to restore to correct state if we should
1048 				 */
1049 				if (sp->sc_pstate != STOPPING)
1050 					sp->sc_pstate = pp->pm_state;
1051 			}
1052 			break;
1053 		default:
1054 			(void) sprintf(Scratch,
1055 		"port monitor <%s> sent an invalid message - ignoring it",
1056 				sp->sc_tag);
1057 			log(Scratch);
1058 			break;
1059 		}
1060 		/* no matter what, PM did answer the poll */
1061 		sp->sc_ok = 1;
1062 		/* Note the messages it understands */
1063 		sp->sc_maxclass = pp->pm_maxclass;
1064 	}
1065 }
1066 
1067 
1068 /*
1069  * validstate - determine if arg s a valid return state from a port monitor
1070  *	return 1 if ok, 0 otherwise
1071  *
1072  *	args:	state - state to be verified
1073  */
1074 int
1075 validstate(unchar state)
1076 {
1077 	switch (state) {
1078 	case PM_ENABLED:
1079 	case PM_DISABLED:
1080 	case PM_STARTING:
1081 	case PM_STOPPING:
1082 		return (1);
1083 	default:
1084 		return (0);
1085 	}
1086 }
1087 
1088 
1089 /*
1090  * mk_cmd_pipe - create the command pipe used by sacadm
1091  */
1092 
1093 int
1094 mk_cmd_pipe()
1095 {
1096 	int fds[2];			/* pipe endpoints */
1097 	int fd;				/* scratch file descriptor */
1098 
1099 	/* make sure there is a file here to mount on */
1100 	(void) unlink(CMDPIPE);
1101 	fd = open(CMDPIPE, O_RDWR | O_CREAT, 0600);
1102 	if (fd < 0)
1103 		error(E_CMDPIPE, EXIT);
1104 	close(fd);
1105 	if (pipe(fds) < 0)
1106 		error(E_PIPE, EXIT);
1107 	if (fattach(fds[0], CMDPIPE) < 0)
1108 		error(E_FATTACH, EXIT);
1109 	return (fds[1]);
1110 }
1111 
1112 
1113 /*
1114  * startpoll - enable polling on command pipe by setting up to catch SIGPOLL
1115  */
1116 
1117 
1118 void
1119 startpoll()
1120 {
1121 	struct sigaction sigact;	/* for signal handling */
1122 
1123 	if (ioctl(Cfd, I_SETSIG, S_INPUT) < 0)
1124 		error(E_SETSIG, EXIT);
1125 	sigact.sa_flags = 0;
1126 	sigact.sa_handler = sigpoll;
1127 	(void) sigemptyset(&sigact.sa_mask);
1128 	(void) sigaddset(&sigact.sa_mask, SIGPOLL);
1129 	(void) sigaction(SIGPOLL, &sigact, &Sigpoll);
1130 }
1131