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 /*
23*238Sseizo  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  *	Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<sys/types.h>
290Sstevel@tonic-gate #include	<stdio.h>
300Sstevel@tonic-gate #include	<errno.h>
310Sstevel@tonic-gate #include	<unistd.h>
320Sstevel@tonic-gate #include	<string.h>
330Sstevel@tonic-gate #include	<wait.h>
340Sstevel@tonic-gate #include	<limits.h>
350Sstevel@tonic-gate #include	"machdep.h"
360Sstevel@tonic-gate #include	"sgs.h"
370Sstevel@tonic-gate #include	"rtc.h"
380Sstevel@tonic-gate #include	"conv.h"
390Sstevel@tonic-gate #include	"_crle.h"
400Sstevel@tonic-gate #include	"msg.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Having gathered together any dependencies, dldump(3x) any necessary images.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * All dldump(3x) processing is carried out from the audit library.  The
460Sstevel@tonic-gate  * temporary configuration file is read and all alternative marked files are
470Sstevel@tonic-gate  * dumped.  If a -E application requires RTLD_REL_EXEC then that application
480Sstevel@tonic-gate  * acts as the new process, otherwise lddstub is used.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Besides dldump(3x)'ing any images the audit library returns the address
510Sstevel@tonic-gate  * range of the images which will used to update the configuration file.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate int
540Sstevel@tonic-gate dump(Crle_desc * crle)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	const char	*orgapp = (const char *)crle->c_app;
570Sstevel@tonic-gate 	int		fildes[2], pid;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (orgapp == 0)
600Sstevel@tonic-gate 		orgapp = conv_lddstub(crle->c_class);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * Set up a pipe through which the audit library will write the image
640Sstevel@tonic-gate 	 * address ranges.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	if (pipe(fildes) == -1) {
670Sstevel@tonic-gate 		int err = errno;
680Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
690Sstevel@tonic-gate 		    crle->c_name, strerror(err));
700Sstevel@tonic-gate 		return (1);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * Fork ourselves to run the application and collect its dependencies.
750Sstevel@tonic-gate 	 */
760Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
770Sstevel@tonic-gate 		int err = errno;
780Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
790Sstevel@tonic-gate 		    crle->c_name, strerror(err));
800Sstevel@tonic-gate 		return (1);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	if (pid) {
840Sstevel@tonic-gate 		/*
850Sstevel@tonic-gate 		 * Parent. Read memory range entries from the audit library.
860Sstevel@tonic-gate 		 * The read side of the pipe is attached to stdio to make
870Sstevel@tonic-gate 		 * obtaining the individual dependencies easier.
880Sstevel@tonic-gate 		 */
890Sstevel@tonic-gate 		int	error = 0, status;
900Sstevel@tonic-gate 		FILE	*fd;
910Sstevel@tonic-gate 		char	buffer[PATH_MAX];
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 		(void) close(fildes[1]);
940Sstevel@tonic-gate 		if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
950Sstevel@tonic-gate 			char		*str;
960Sstevel@tonic-gate 			Rtc_head	*rtc = (Rtc_head *)crle->c_tempaddr;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 			while (fgets(buffer, PATH_MAX, fd) != NULL) {
990Sstevel@tonic-gate 				/*
1000Sstevel@tonic-gate 				 * Make sure we recognize the message, remove
1010Sstevel@tonic-gate 				 * the newline (which allowed fgets() use) and
1020Sstevel@tonic-gate 				 * register the memory range entry;
1030Sstevel@tonic-gate 				 */
1040Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
1050Sstevel@tonic-gate 				    MSG_AUD_PRF_SIZE))
1060Sstevel@tonic-gate 					continue;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 				str = strrchr(buffer, '\n');
1090Sstevel@tonic-gate 				*str = '\0';
1100Sstevel@tonic-gate 				str = buffer + MSG_AUD_PRF_SIZE;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_RESBGN),
1130Sstevel@tonic-gate 				    str, MSG_AUD_RESBGN_SIZE) == 0) {
1140Sstevel@tonic-gate 					rtc->ch_resbgn =
1150Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESBGN_SIZE,
1160Sstevel@tonic-gate 						(char **)NULL, 0);
1170Sstevel@tonic-gate 				} else if (strncmp(MSG_ORIG(MSG_AUD_RESEND),
1180Sstevel@tonic-gate 				    str, MSG_AUD_RESEND_SIZE) == 0) {
1190Sstevel@tonic-gate 					rtc->ch_resend =
1200Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESEND_SIZE,
1210Sstevel@tonic-gate 						(char **)NULL, 0);
1220Sstevel@tonic-gate 				} else
1230Sstevel@tonic-gate 					continue;
1240Sstevel@tonic-gate 			}
1250Sstevel@tonic-gate 			(void) fclose(fd);
1260Sstevel@tonic-gate 		} else
1270Sstevel@tonic-gate 			error = errno;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 		while (wait(&status) != pid)
1300Sstevel@tonic-gate 			;
1310Sstevel@tonic-gate 		if (status) {
1320Sstevel@tonic-gate 			if (WIFSIGNALED(status)) {
1330Sstevel@tonic-gate 				(void) fprintf(stderr,
1340Sstevel@tonic-gate 				    MSG_INTL(MSG_SYS_EXEC), crle->c_name,
1350Sstevel@tonic-gate 				    orgapp, (WSIGMASK & status),
1360Sstevel@tonic-gate 				    ((status & WCOREFLG) ?
1370Sstevel@tonic-gate 				    MSG_INTL(MSG_SYS_CORE) :
1380Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_EMPTY)));
1390Sstevel@tonic-gate 			}
1400Sstevel@tonic-gate 			return (status);
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		return (error);
1430Sstevel@tonic-gate 	} else {
1440Sstevel@tonic-gate 		char	efds[MSG_ENV_AUD_FD_SIZE + 10];
1450Sstevel@tonic-gate 		char	eflg[MSG_ENV_AUD_FLAGS_SIZE + 10];
1460Sstevel@tonic-gate 		char	ecnf[PATH_MAX];
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		(void) close(fildes[0]);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 		/*
1510Sstevel@tonic-gate 		 * Child. Set up environment variables to enable and identify
1520Sstevel@tonic-gate 		 * auditing.
1530Sstevel@tonic-gate 		 */
1540Sstevel@tonic-gate 		(void) snprintf(efds, (MSG_ENV_AUD_FD_SIZE + 10),
1550Sstevel@tonic-gate 		    MSG_ORIG(MSG_ENV_AUD_FD), fildes[1]);
1560Sstevel@tonic-gate 		(void) snprintf(eflg, (MSG_ENV_AUD_FLAGS_SIZE + 10),
1570Sstevel@tonic-gate 		    MSG_ORIG(MSG_ENV_AUD_FLAGS), crle->c_dlflags);
1580Sstevel@tonic-gate 		(void) snprintf(ecnf, PATH_MAX, MSG_ORIG(MSG_ENV_LD_CONFIG),
1590Sstevel@tonic-gate 		    crle->c_tempname);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		/*
1620Sstevel@tonic-gate 		 * Put strings in the environment for exec().
1630Sstevel@tonic-gate 		 * NOTE, use of automatic variables for construction of the
1640Sstevel@tonic-gate 		 * environment variables is legitimate here, as they are local
1650Sstevel@tonic-gate 		 * to the child process and are established solely for exec().
1660Sstevel@tonic-gate 		 */
1670Sstevel@tonic-gate 		if ((putenv(efds) != 0) || (putenv(eflg) != 0) ||
1680Sstevel@tonic-gate 		    (putenv(ecnf) != 0) || (putenv(crle->c_audit) != 0) ||
1690Sstevel@tonic-gate 		    (putenv((char *)MSG_ORIG(MSG_ENV_LD_FLAGS)) != 0)) {
1700Sstevel@tonic-gate 			int err = errno;
1710Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_PUTENV),
1720Sstevel@tonic-gate 			    crle->c_name, strerror(err));
1730Sstevel@tonic-gate 			return (1);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 		if (execlp(orgapp, orgapp, 0) == -1) {
1770Sstevel@tonic-gate 			int err = errno;
1780Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXECLP),
1790Sstevel@tonic-gate 			    crle->c_name, orgapp, strerror(err));
1800Sstevel@tonic-gate 			_exit(err);
1810Sstevel@tonic-gate 			/* NOTREACHED */
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 	}
184*238Sseizo 	return (0);
1850Sstevel@tonic-gate }
186