xref: /onnv-gate/usr/src/cmd/sgs/crle/common/dump.c (revision 1976:f0691a145b7e)
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
5*1976Sab196087  * Common Development and Distribution License (the "License").
6*1976Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1976Sab196087  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  *	Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include	<sys/types.h>
280Sstevel@tonic-gate #include	<stdio.h>
290Sstevel@tonic-gate #include	<errno.h>
300Sstevel@tonic-gate #include	<unistd.h>
310Sstevel@tonic-gate #include	<string.h>
320Sstevel@tonic-gate #include	<wait.h>
330Sstevel@tonic-gate #include	<limits.h>
340Sstevel@tonic-gate #include	"machdep.h"
350Sstevel@tonic-gate #include	"sgs.h"
360Sstevel@tonic-gate #include	"rtc.h"
370Sstevel@tonic-gate #include	"conv.h"
380Sstevel@tonic-gate #include	"_crle.h"
390Sstevel@tonic-gate #include	"msg.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * Having gathered together any dependencies, dldump(3x) any necessary images.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * All dldump(3x) processing is carried out from the audit library.  The
450Sstevel@tonic-gate  * temporary configuration file is read and all alternative marked files are
460Sstevel@tonic-gate  * dumped.  If a -E application requires RTLD_REL_EXEC then that application
470Sstevel@tonic-gate  * acts as the new process, otherwise lddstub is used.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * Besides dldump(3x)'ing any images the audit library returns the address
500Sstevel@tonic-gate  * range of the images which will used to update the configuration file.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate int
dump(Crle_desc * crle)530Sstevel@tonic-gate dump(Crle_desc * crle)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	const char	*orgapp = (const char *)crle->c_app;
560Sstevel@tonic-gate 	int		fildes[2], pid;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	if (orgapp == 0)
59*1976Sab196087 		orgapp = conv_lddstub(M_CLASS);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	/*
620Sstevel@tonic-gate 	 * Set up a pipe through which the audit library will write the image
630Sstevel@tonic-gate 	 * address ranges.
640Sstevel@tonic-gate 	 */
650Sstevel@tonic-gate 	if (pipe(fildes) == -1) {
660Sstevel@tonic-gate 		int err = errno;
670Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
680Sstevel@tonic-gate 		    crle->c_name, strerror(err));
690Sstevel@tonic-gate 		return (1);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/*
730Sstevel@tonic-gate 	 * Fork ourselves to run the application and collect its dependencies.
740Sstevel@tonic-gate 	 */
750Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
760Sstevel@tonic-gate 		int err = errno;
770Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
780Sstevel@tonic-gate 		    crle->c_name, strerror(err));
790Sstevel@tonic-gate 		return (1);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (pid) {
830Sstevel@tonic-gate 		/*
840Sstevel@tonic-gate 		 * Parent. Read memory range entries from the audit library.
850Sstevel@tonic-gate 		 * The read side of the pipe is attached to stdio to make
860Sstevel@tonic-gate 		 * obtaining the individual dependencies easier.
870Sstevel@tonic-gate 		 */
880Sstevel@tonic-gate 		int	error = 0, status;
890Sstevel@tonic-gate 		FILE	*fd;
900Sstevel@tonic-gate 		char	buffer[PATH_MAX];
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 		(void) close(fildes[1]);
930Sstevel@tonic-gate 		if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
94*1976Sab196087 			char *str;
95*1976Sab196087 			Rtc_head *rtc = (Rtc_head *)crle->c_tempheadaddr;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 			while (fgets(buffer, PATH_MAX, fd) != NULL) {
980Sstevel@tonic-gate 				/*
990Sstevel@tonic-gate 				 * Make sure we recognize the message, remove
1000Sstevel@tonic-gate 				 * the newline (which allowed fgets() use) and
1010Sstevel@tonic-gate 				 * register the memory range entry;
1020Sstevel@tonic-gate 				 */
1030Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
1040Sstevel@tonic-gate 				    MSG_AUD_PRF_SIZE))
1050Sstevel@tonic-gate 					continue;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 				str = strrchr(buffer, '\n');
1080Sstevel@tonic-gate 				*str = '\0';
1090Sstevel@tonic-gate 				str = buffer + MSG_AUD_PRF_SIZE;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_RESBGN),
1120Sstevel@tonic-gate 				    str, MSG_AUD_RESBGN_SIZE) == 0) {
1130Sstevel@tonic-gate 					rtc->ch_resbgn =
1140Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESBGN_SIZE,
1150Sstevel@tonic-gate 						(char **)NULL, 0);
1160Sstevel@tonic-gate 				} else if (strncmp(MSG_ORIG(MSG_AUD_RESEND),
1170Sstevel@tonic-gate 				    str, MSG_AUD_RESEND_SIZE) == 0) {
1180Sstevel@tonic-gate 					rtc->ch_resend =
1190Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESEND_SIZE,
1200Sstevel@tonic-gate 						(char **)NULL, 0);
121*1976Sab196087 				} else {
1220Sstevel@tonic-gate 					continue;
123*1976Sab196087 				}
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 	}
184238Sseizo 	return (0);
1850Sstevel@tonic-gate }
186