xref: /titanic_51/usr/src/cmd/uadmin/uadmin.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
558091fd8Ssetje  * Common Development and Distribution License (the "License").
658091fd8Ssetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e333042dSgww  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
316dcd8691Sgww #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
356dcd8691Sgww #include <string.h>
366dcd8691Sgww #include <strings.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
386dcd8691Sgww #include <unistd.h>
396dcd8691Sgww 
406a3b10dbStz204579 #include <bsm/adt.h>
416a3b10dbStz204579 #include <bsm/adt_event.h>
426dcd8691Sgww 
43b08d8a12Sgww #include <sys/types.h>
446dcd8691Sgww #include <sys/uadmin.h>
45b08d8a12Sgww #include <sys/wait.h>
467c478bd9Sstevel@tonic-gate 
4758091fd8Ssetje #define	SMF_RST	"/etc/svc/volatile/resetting"
48b08d8a12Sgww #define	RETRY_COUNT 15	/* number of 1 sec retries for audit(1M) to complete */
4958091fd8Ssetje 
507c478bd9Sstevel@tonic-gate static const char *Usage = "Usage: %s cmd fcn [mdep]\n";
517c478bd9Sstevel@tonic-gate 
52b08d8a12Sgww static int closeout_audit(int, int);
53b08d8a12Sgww static int turnoff_auditd(void);
546a3b10dbStz204579 static void wait_for_auqueue();
55b08d8a12Sgww static int change_audit_file(void);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate int
587c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	int cmd, fcn;
617c478bd9Sstevel@tonic-gate 	uintptr_t mdep = NULL;
627c478bd9Sstevel@tonic-gate 	sigset_t set;
636a3b10dbStz204579 	adt_session_data_t *ah;  /* audit session handle */
646a3b10dbStz204579 	adt_event_data_t *event = NULL; /* event to be generated */
656a3b10dbStz204579 	au_event_t event_id;
666a3b10dbStz204579 	enum adt_uadmin_fcn fcn_id;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (argc < 3 || argc > 4) {
697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, Usage, argv[0]);
707c478bd9Sstevel@tonic-gate 		return (1);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	(void) sigfillset(&set);
747c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &set, NULL);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	cmd = atoi(argv[1]);
777c478bd9Sstevel@tonic-gate 	fcn = atoi(argv[2]);
787c478bd9Sstevel@tonic-gate 	if (argc == 4) {	/* mdep argument given */
792df1fe9cSrandyf 		if (cmd != A_REBOOT && cmd != A_SHUTDOWN && cmd != A_DUMP &&
802df1fe9cSrandyf 		    cmd != A_FREEZE) {
817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: mdep argument not "
827c478bd9Sstevel@tonic-gate 			    "allowed for this cmd value\n", argv[0]);
837c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, Usage, argv[0]);
847c478bd9Sstevel@tonic-gate 			return (1);
857c478bd9Sstevel@tonic-gate 		} else {
867c478bd9Sstevel@tonic-gate 			mdep = (uintptr_t)argv[3];
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 
906a3b10dbStz204579 	/* set up audit session and event */
916a3b10dbStz204579 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
926a3b10dbStz204579 		(void) fprintf(stderr, "%s: can't start audit session\n",
936a3b10dbStz204579 		    argv[0]);
946a3b10dbStz204579 	}
956a3b10dbStz204579 	switch (cmd) {
966a3b10dbStz204579 	case A_SHUTDOWN:
976a3b10dbStz204579 		event_id = ADT_uadmin_shutdown;
986a3b10dbStz204579 		break;
996a3b10dbStz204579 	case A_REBOOT:
1006a3b10dbStz204579 		event_id = ADT_uadmin_reboot;
1016a3b10dbStz204579 		break;
1026a3b10dbStz204579 	case A_DUMP:
1036a3b10dbStz204579 		event_id = ADT_uadmin_dump;
1046a3b10dbStz204579 		break;
1056a3b10dbStz204579 	case A_REMOUNT:
1066a3b10dbStz204579 		event_id = ADT_uadmin_remount;
1076a3b10dbStz204579 		break;
1086a3b10dbStz204579 	case A_FREEZE:
1096a3b10dbStz204579 		event_id = ADT_uadmin_freeze;
1106a3b10dbStz204579 		break;
1116a3b10dbStz204579 	case A_FTRACE:
1126a3b10dbStz204579 		event_id = ADT_uadmin_ftrace;
1136a3b10dbStz204579 		break;
1146a3b10dbStz204579 	case A_SWAPCTL:
1156a3b10dbStz204579 		event_id = ADT_uadmin_swapctl;
1166a3b10dbStz204579 		break;
1176a3b10dbStz204579 	default:
1186a3b10dbStz204579 		event_id = 0;
1196a3b10dbStz204579 	}
1206a3b10dbStz204579 	if ((event_id != 0) &&
1216a3b10dbStz204579 	    (event = adt_alloc_event(ah, event_id)) == NULL) {
1226a3b10dbStz204579 		(void) fprintf(stderr, "%s: can't allocate audit event\n",
1236a3b10dbStz204579 		    argv[0]);
1246a3b10dbStz204579 	}
1256a3b10dbStz204579 	switch (fcn) {
1266a3b10dbStz204579 	case AD_HALT:
1276a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_HALT;
1286a3b10dbStz204579 		break;
1296a3b10dbStz204579 	case AD_POWEROFF:
1306a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_POWEROFF;
1316a3b10dbStz204579 		break;
1326a3b10dbStz204579 	case AD_BOOT:
1336a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_BOOT;
1346a3b10dbStz204579 		break;
1356a3b10dbStz204579 	case AD_IBOOT:
1366a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_IBOOT;
1376a3b10dbStz204579 		break;
1386a3b10dbStz204579 	case AD_SBOOT:
1396a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_SBOOT;
1406a3b10dbStz204579 		break;
1416a3b10dbStz204579 	case AD_SIBOOT:
1426a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_SIBOOT;
1436a3b10dbStz204579 		break;
1446a3b10dbStz204579 	case AD_NOSYNC:
1456a3b10dbStz204579 		fcn_id = ADT_UADMIN_FCN_AD_NOSYNC;
1466a3b10dbStz204579 		break;
147*19397407SSherry Moore 	case AD_FASTREBOOT:
148*19397407SSherry Moore #ifdef __i386
149*19397407SSherry Moore 		fcn_id = ADT_UADMIN_FCN_AD_FASTREBOOT;
150*19397407SSherry Moore 		mdep = NULL;	/* Ignore all arguments */
151*19397407SSherry Moore #else
152*19397407SSherry Moore 		fcn = AD_BOOT;
153*19397407SSherry Moore 		fcn_id = ADT_UADMIN_FCN_AD_BOOT;
154*19397407SSherry Moore #endif /* __i386 */
155*19397407SSherry Moore 		break;
156*19397407SSherry Moore 	case AD_FASTREBOOT_DRYRUN:
157*19397407SSherry Moore 		fcn_id = ADT_UADMIN_FCN_AD_FASTREBOOT_DRYRUN;
158*19397407SSherry Moore 		mdep = NULL;	/* Ignore all arguments */
159*19397407SSherry Moore 		break;
1606a3b10dbStz204579 	default:
1616a3b10dbStz204579 		fcn_id = 0;
1626a3b10dbStz204579 	}
1636a3b10dbStz204579 	if (cmd == A_FREEZE) {
1646a3b10dbStz204579 		switch (fcn) {
1656a3b10dbStz204579 		case AD_SUSPEND_TO_DISK:
1666a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_DISK;
1676a3b10dbStz204579 			break;
1686a3b10dbStz204579 		case AD_CHECK_SUSPEND_TO_DISK:
1696a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_DISK;
1706a3b10dbStz204579 			break;
1716a3b10dbStz204579 		case AD_FORCE:
1726a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_FORCE;
1736a3b10dbStz204579 			break;
1746a3b10dbStz204579 		case AD_SUSPEND_TO_RAM:
1756a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_SUSPEND_TO_RAM;
1766a3b10dbStz204579 			break;
1776a3b10dbStz204579 		case AD_CHECK_SUSPEND_TO_RAM:
1786a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_CHECK_SUSPEND_TO_RAM;
1796a3b10dbStz204579 			break;
1806a3b10dbStz204579 		case AD_REUSEINIT:
1816a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_REUSEINIT;
1826a3b10dbStz204579 			break;
1836a3b10dbStz204579 		case AD_REUSABLE:
1846a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_REUSABLE;
1856a3b10dbStz204579 			break;
1866a3b10dbStz204579 		case AD_REUSEFINI:
1876a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_REUSEFINI;
1886a3b10dbStz204579 			break;
1896a3b10dbStz204579 		}
1906a3b10dbStz204579 	} else if (cmd == A_FTRACE) {
1916a3b10dbStz204579 		switch (fcn) {
1926a3b10dbStz204579 		case AD_FTRACE_START:
1936a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_FTRACE_START;
1946a3b10dbStz204579 			break;
1956a3b10dbStz204579 		case AD_FTRACE_STOP:
1966a3b10dbStz204579 			fcn_id = ADT_UADMIN_FCN_AD_FTRACE_STOP;
1976a3b10dbStz204579 			break;
1986a3b10dbStz204579 		}
1996a3b10dbStz204579 	}
2006a3b10dbStz204579 
2017c478bd9Sstevel@tonic-gate 	if (geteuid() == 0) {
2026a3b10dbStz204579 		if (event != NULL) {
2036a3b10dbStz204579 			switch (cmd) {
2046a3b10dbStz204579 			case A_SHUTDOWN:
2056a3b10dbStz204579 				event->adt_uadmin_shutdown.fcn = fcn_id;
2066a3b10dbStz204579 				event->adt_uadmin_shutdown.mdep = (char *)mdep;
2076a3b10dbStz204579 				break;
2086a3b10dbStz204579 			case A_REBOOT:
2096a3b10dbStz204579 				event->adt_uadmin_reboot.fcn = fcn_id;
2106a3b10dbStz204579 				event->adt_uadmin_reboot.mdep = (char *)mdep;
2116a3b10dbStz204579 				break;
2126a3b10dbStz204579 			case A_DUMP:
2136a3b10dbStz204579 				event->adt_uadmin_dump.fcn = fcn_id;
2146a3b10dbStz204579 				event->adt_uadmin_dump.mdep = (char *)mdep;
2156a3b10dbStz204579 				break;
2166a3b10dbStz204579 			case A_REMOUNT:
2176a3b10dbStz204579 				/* no parameters */
2186a3b10dbStz204579 				break;
2196a3b10dbStz204579 			case A_FREEZE:
2206a3b10dbStz204579 				event->adt_uadmin_freeze.fcn = fcn_id;
2216a3b10dbStz204579 				event->adt_uadmin_freeze.mdep = (char *)mdep;
2226a3b10dbStz204579 				break;
2236a3b10dbStz204579 			case A_FTRACE:
2246a3b10dbStz204579 				event->adt_uadmin_ftrace.fcn = fcn_id;
2256a3b10dbStz204579 				break;
2266a3b10dbStz204579 			case A_SWAPCTL:
2276a3b10dbStz204579 				event->adt_uadmin_swapctl.fcn = fcn_id;
2286a3b10dbStz204579 				break;
2296a3b10dbStz204579 			}
2306a3b10dbStz204579 
2316a3b10dbStz204579 			if (adt_put_event(event, ADT_SUCCESS, 0) != 0) {
2326a3b10dbStz204579 				(void) fprintf(stderr,
2336a3b10dbStz204579 				    "%s: can't put audit event\n", argv[0]);
2346a3b10dbStz204579 			}
2356a3b10dbStz204579 			/*
2366a3b10dbStz204579 			 * allow audit record to be processed in the kernel
2376a3b10dbStz204579 			 * audit queue
2386a3b10dbStz204579 			 */
2396a3b10dbStz204579 			wait_for_auqueue();
2406a3b10dbStz204579 		}
2416a3b10dbStz204579 
242b08d8a12Sgww 		if (closeout_audit(cmd, fcn) == -1)
2437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: can't turn off auditd\n",
2447c478bd9Sstevel@tonic-gate 			    argv[0]);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		if (cmd == A_SHUTDOWN || cmd == A_REBOOT)
24758091fd8Ssetje 			(void) creat(SMF_RST, 0777);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2506a3b10dbStz204579 	(void) adt_free_event(event);
2517c478bd9Sstevel@tonic-gate 	if (uadmin(cmd, fcn, mdep) < 0) {
2527c478bd9Sstevel@tonic-gate 		perror("uadmin");
2537c478bd9Sstevel@tonic-gate 
25458091fd8Ssetje 		(void) unlink(SMF_RST);
25558091fd8Ssetje 
2567c478bd9Sstevel@tonic-gate 		return (1);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
259b08d8a12Sgww 	/* If returning from a suspend, audit thaw */
260b08d8a12Sgww 	if ((cmd == A_FREEZE) &&
261b08d8a12Sgww 	    ((fcn == AD_FORCE) ||
262b08d8a12Sgww 	    (fcn == AD_REUSABLE) ||
263b08d8a12Sgww 	    (fcn == AD_SUSPEND_TO_DISK) ||
264b08d8a12Sgww 	    (fcn == AD_SUSPEND_TO_RAM))) {
265b08d8a12Sgww 		if ((event = adt_alloc_event(ah, ADT_uadmin_thaw)) == NULL) {
266b08d8a12Sgww 			(void) fprintf(stderr, "%s: can't allocate thaw audit "
267b08d8a12Sgww 			    "event\n", argv[0]);
268b08d8a12Sgww 		}
269b08d8a12Sgww 		event->adt_uadmin_thaw.fcn = fcn_id;
270b08d8a12Sgww 		if (adt_put_event(event, ADT_SUCCESS, 0) != 0) {
271b08d8a12Sgww 			(void) fprintf(stderr, "%s: can't put thaw audit "
272b08d8a12Sgww 			    "event\n", argv[0]);
273b08d8a12Sgww 		}
274b08d8a12Sgww 		(void) adt_free_event(event);
275b08d8a12Sgww 	}
276b08d8a12Sgww 	(void) adt_end_session(ah);
277b08d8a12Sgww 
2787c478bd9Sstevel@tonic-gate 	return (0);
2797c478bd9Sstevel@tonic-gate }
2806a3b10dbStz204579 
2816a3b10dbStz204579 static int
282b08d8a12Sgww closeout_audit(int cmd, int fcn)
2836a3b10dbStz204579 {
284b08d8a12Sgww 	if (!adt_audit_state(AUC_AUDITING)) {
285b08d8a12Sgww 		/* auditd not running, just return */
286b08d8a12Sgww 		return (0);
287b08d8a12Sgww 	}
288e333042dSgww 	switch (cmd) {
289e333042dSgww 	case A_SHUTDOWN:
290*19397407SSherry Moore 		switch (fcn) {
291*19397407SSherry Moore 		case AD_FASTREBOOT_DRYRUN:
292*19397407SSherry Moore 			/* No system discontinuity, don't turn off auditd */
293*19397407SSherry Moore 			return (0);
294*19397407SSherry Moore 		default:
295*19397407SSherry Moore 			break;	/* For all the other shutdown functions */
296*19397407SSherry Moore 		}
297*19397407SSherry Moore 		/* FALLTHROUGH */
298e333042dSgww 	case A_REBOOT:
299e333042dSgww 	case A_DUMP:
300e333042dSgww 		/* system shutting down, turn off auditd */
301b08d8a12Sgww 		return (turnoff_auditd());
302e333042dSgww 	case A_REMOUNT:
303e333042dSgww 	case A_SWAPCTL:
304e333042dSgww 	case A_FTRACE:
305e333042dSgww 		/* No system discontinuity, don't turn off auditd */
306e333042dSgww 		return (0);
307e333042dSgww 	case A_FREEZE:
308e333042dSgww 		switch (fcn) {
309e333042dSgww 		case AD_CHECK_SUSPEND_TO_DISK:	/* AD_CHECK */
310e333042dSgww 		case AD_CHECK_SUSPEND_TO_RAM:
311e333042dSgww 		case AD_REUSEINIT:
312e333042dSgww 		case AD_REUSEFINI:
313e333042dSgww 			/* No system discontinuity, don't turn off auditd */
314e333042dSgww 			return (0);
315e333042dSgww 		case AD_REUSABLE:
316e333042dSgww 		case AD_SUSPEND_TO_DISK:	/* AD_COMPRESS */
317e333042dSgww 		case AD_SUSPEND_TO_RAM:
318e333042dSgww 		case AD_FORCE:
319e333042dSgww 			/* suspend the system, change audit files */
320b08d8a12Sgww 			return (change_audit_file());
321e333042dSgww 		default:
322b08d8a12Sgww 			return (0);	/* not an audit error */
323e333042dSgww 		}
324e333042dSgww 	default:
325b08d8a12Sgww 		return (0);	/* not an audit error */
326b08d8a12Sgww 	}
327e333042dSgww }
328e333042dSgww 
329b08d8a12Sgww static int
330b08d8a12Sgww turnoff_auditd(void)
331b08d8a12Sgww {
332b08d8a12Sgww 	int	rc;
333b08d8a12Sgww 	int	retries = RETRY_COUNT;
334b08d8a12Sgww 
335b08d8a12Sgww 	if ((rc = (int)fork()) == 0) {
3366dcd8691Sgww 		(void) execl("/usr/sbin/audit", "audit", "-t", NULL);
3376a3b10dbStz204579 		(void) fprintf(stderr, "error disabling auditd: %s\n",
3386dcd8691Sgww 		    strerror(errno));
3396dcd8691Sgww 		_exit(-1);
3406dcd8691Sgww 	} else if (rc == -1) {
3416dcd8691Sgww 		(void) fprintf(stderr, "error disabling auditd: %s\n",
3426dcd8691Sgww 		    strerror(errno));
3436a3b10dbStz204579 		return (-1);
3446a3b10dbStz204579 	}
3456a3b10dbStz204579 
3466dcd8691Sgww 	/*
3476dcd8691Sgww 	 * wait for auditd to finish its work.  auditd will change the
3486dcd8691Sgww 	 * auditstart from AUC_AUDITING (auditd up and running) to
3496dcd8691Sgww 	 * AUC_NOAUDIT.  Other states are errors, so we're done as well.
3506dcd8691Sgww 	 */
3516a3b10dbStz204579 	do {
3526dcd8691Sgww 		int	auditstate;
3536a3b10dbStz204579 
3546dcd8691Sgww 		rc = -1;
3556dcd8691Sgww 		if ((auditon(A_GETCOND, (caddr_t)&auditstate,
3566dcd8691Sgww 		    sizeof (auditstate)) == 0) &&
3576dcd8691Sgww 		    (auditstate == AUC_AUDITING)) {
3586a3b10dbStz204579 			retries--;
3596a3b10dbStz204579 			(void) sleep(1);
3606a3b10dbStz204579 		} else {
3616a3b10dbStz204579 			rc = 0;
3626a3b10dbStz204579 		}
3636dcd8691Sgww 	} while ((rc != 0) && (retries != 0));
3646a3b10dbStz204579 
3656a3b10dbStz204579 	return (rc);
3666a3b10dbStz204579 }
3676a3b10dbStz204579 
368b08d8a12Sgww static int
369b08d8a12Sgww change_audit_file(void)
370b08d8a12Sgww {
371b08d8a12Sgww 	pid_t	pid;
372b08d8a12Sgww 
373b08d8a12Sgww 	if ((pid = fork()) == 0) {
374b08d8a12Sgww 		(void) execl("/usr/sbin/audit", "audit", "-n", NULL);
375b08d8a12Sgww 		(void) fprintf(stderr, "error changing audit files: %s\n",
376b08d8a12Sgww 		    strerror(errno));
377b08d8a12Sgww 		_exit(-1);
378b08d8a12Sgww 	} else if (pid == -1) {
379b08d8a12Sgww 		(void) fprintf(stderr, "error changing audit files: %s\n",
380b08d8a12Sgww 		    strerror(errno));
381b08d8a12Sgww 		return (-1);
382b08d8a12Sgww 	} else {
383b08d8a12Sgww 		pid_t	rc;
384b08d8a12Sgww 		int	retries = RETRY_COUNT;
385b08d8a12Sgww 
386b08d8a12Sgww 		/*
387b08d8a12Sgww 		 * Wait for audit(1M) -n process to complete
388b08d8a12Sgww 		 *
389b08d8a12Sgww 		 */
390b08d8a12Sgww 		do {
391b08d8a12Sgww 			if ((rc = waitpid(pid, NULL, WNOHANG)) == pid) {
392b08d8a12Sgww 				return (0);
393b08d8a12Sgww 			} else if (rc == -1) {
394b08d8a12Sgww 				return (-1);
395b08d8a12Sgww 			} else {
396b08d8a12Sgww 				(void) sleep(1);
397b08d8a12Sgww 				retries--;
398b08d8a12Sgww 			}
399b08d8a12Sgww 
400b08d8a12Sgww 		} while (retries != 0);
401b08d8a12Sgww 	}
402b08d8a12Sgww 	return (-1);
403b08d8a12Sgww }
404b08d8a12Sgww 
4056a3b10dbStz204579 static void
4066a3b10dbStz204579 wait_for_auqueue()
4076a3b10dbStz204579 {
4086a3b10dbStz204579 	au_stat_t	au_stat;
4096a3b10dbStz204579 	int		retries = 10;
4106a3b10dbStz204579 
4116a3b10dbStz204579 	while (retries-- && auditon(A_GETSTAT, (caddr_t)&au_stat, NULL) == 0) {
4126a3b10dbStz204579 		if (au_stat.as_enqueue == au_stat.as_written) {
4136a3b10dbStz204579 			break;
4146a3b10dbStz204579 		}
4156a3b10dbStz204579 		(void) sleep(1);
4166a3b10dbStz204579 	}
4176a3b10dbStz204579 }
418