xref: /illumos-gate/usr/src/cmd/sgs/crle/common/dump.c (revision 2a8bcb4e)
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
5*c13de8f6Sab  * Common Development and Distribution License (the "License").
6*c13de8f6Sab  * 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 /*
22*c13de8f6Sab  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include	<sys/types.h>
277c478bd9Sstevel@tonic-gate #include	<stdio.h>
287c478bd9Sstevel@tonic-gate #include	<errno.h>
297c478bd9Sstevel@tonic-gate #include	<unistd.h>
307c478bd9Sstevel@tonic-gate #include	<string.h>
317c478bd9Sstevel@tonic-gate #include	<wait.h>
327c478bd9Sstevel@tonic-gate #include	<limits.h>
337c478bd9Sstevel@tonic-gate #include	"machdep.h"
347c478bd9Sstevel@tonic-gate #include	"sgs.h"
357c478bd9Sstevel@tonic-gate #include	"rtc.h"
367c478bd9Sstevel@tonic-gate #include	"conv.h"
377c478bd9Sstevel@tonic-gate #include	"_crle.h"
387c478bd9Sstevel@tonic-gate #include	"msg.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Having gathered together any dependencies, dldump(3x) any necessary images.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * All dldump(3x) processing is carried out from the audit library.  The
447c478bd9Sstevel@tonic-gate  * temporary configuration file is read and all alternative marked files are
457c478bd9Sstevel@tonic-gate  * dumped.  If a -E application requires RTLD_REL_EXEC then that application
467c478bd9Sstevel@tonic-gate  * acts as the new process, otherwise lddstub is used.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Besides dldump(3x)'ing any images the audit library returns the address
497c478bd9Sstevel@tonic-gate  * range of the images which will used to update the configuration file.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate int
dump(Crle_desc * crle)527c478bd9Sstevel@tonic-gate dump(Crle_desc * crle)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	const char	*orgapp = (const char *)crle->c_app;
557c478bd9Sstevel@tonic-gate 	int		fildes[2], pid;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (orgapp == 0)
58*c13de8f6Sab 		orgapp = conv_lddstub(M_CLASS);
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	/*
617c478bd9Sstevel@tonic-gate 	 * Set up a pipe through which the audit library will write the image
627c478bd9Sstevel@tonic-gate 	 * address ranges.
637c478bd9Sstevel@tonic-gate 	 */
647c478bd9Sstevel@tonic-gate 	if (pipe(fildes) == -1) {
657c478bd9Sstevel@tonic-gate 		int err = errno;
667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
677c478bd9Sstevel@tonic-gate 		    crle->c_name, strerror(err));
687c478bd9Sstevel@tonic-gate 		return (1);
697c478bd9Sstevel@tonic-gate 	}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/*
727c478bd9Sstevel@tonic-gate 	 * Fork ourselves to run the application and collect its dependencies.
737c478bd9Sstevel@tonic-gate 	 */
747c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
757c478bd9Sstevel@tonic-gate 		int err = errno;
767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
777c478bd9Sstevel@tonic-gate 		    crle->c_name, strerror(err));
787c478bd9Sstevel@tonic-gate 		return (1);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	if (pid) {
827c478bd9Sstevel@tonic-gate 		/*
837c478bd9Sstevel@tonic-gate 		 * Parent. Read memory range entries from the audit library.
847c478bd9Sstevel@tonic-gate 		 * The read side of the pipe is attached to stdio to make
857c478bd9Sstevel@tonic-gate 		 * obtaining the individual dependencies easier.
867c478bd9Sstevel@tonic-gate 		 */
877c478bd9Sstevel@tonic-gate 		int	error = 0, status;
887c478bd9Sstevel@tonic-gate 		FILE	*fd;
897c478bd9Sstevel@tonic-gate 		char	buffer[PATH_MAX];
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		(void) close(fildes[1]);
927c478bd9Sstevel@tonic-gate 		if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
93*c13de8f6Sab 			char *str;
94*c13de8f6Sab 			Rtc_head *rtc = (Rtc_head *)crle->c_tempheadaddr;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 			while (fgets(buffer, PATH_MAX, fd) != NULL) {
977c478bd9Sstevel@tonic-gate 				/*
987c478bd9Sstevel@tonic-gate 				 * Make sure we recognize the message, remove
997c478bd9Sstevel@tonic-gate 				 * the newline (which allowed fgets() use) and
1007c478bd9Sstevel@tonic-gate 				 * register the memory range entry;
1017c478bd9Sstevel@tonic-gate 				 */
1027c478bd9Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
1037c478bd9Sstevel@tonic-gate 				    MSG_AUD_PRF_SIZE))
1047c478bd9Sstevel@tonic-gate 					continue;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 				str = strrchr(buffer, '\n');
1077c478bd9Sstevel@tonic-gate 				*str = '\0';
1087c478bd9Sstevel@tonic-gate 				str = buffer + MSG_AUD_PRF_SIZE;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 				if (strncmp(MSG_ORIG(MSG_AUD_RESBGN),
1117c478bd9Sstevel@tonic-gate 				    str, MSG_AUD_RESBGN_SIZE) == 0) {
1127c478bd9Sstevel@tonic-gate 					rtc->ch_resbgn =
1137c478bd9Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESBGN_SIZE,
1147c478bd9Sstevel@tonic-gate 						(char **)NULL, 0);
1157c478bd9Sstevel@tonic-gate 				} else if (strncmp(MSG_ORIG(MSG_AUD_RESEND),
1167c478bd9Sstevel@tonic-gate 				    str, MSG_AUD_RESEND_SIZE) == 0) {
1177c478bd9Sstevel@tonic-gate 					rtc->ch_resend =
1187c478bd9Sstevel@tonic-gate 					    strtoull(str + MSG_AUD_RESEND_SIZE,
1197c478bd9Sstevel@tonic-gate 						(char **)NULL, 0);
120*c13de8f6Sab 				} else {
1217c478bd9Sstevel@tonic-gate 					continue;
122*c13de8f6Sab 				}
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate 			(void) fclose(fd);
1257c478bd9Sstevel@tonic-gate 		} else
1267c478bd9Sstevel@tonic-gate 			error = errno;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		while (wait(&status) != pid)
1297c478bd9Sstevel@tonic-gate 			;
1307c478bd9Sstevel@tonic-gate 		if (status) {
1317c478bd9Sstevel@tonic-gate 			if (WIFSIGNALED(status)) {
1327c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1337c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_SYS_EXEC), crle->c_name,
1347c478bd9Sstevel@tonic-gate 				    orgapp, (WSIGMASK & status),
1357c478bd9Sstevel@tonic-gate 				    ((status & WCOREFLG) ?
1367c478bd9Sstevel@tonic-gate 				    MSG_INTL(MSG_SYS_CORE) :
1377c478bd9Sstevel@tonic-gate 				    MSG_ORIG(MSG_STR_EMPTY)));
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 			return (status);
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 		return (error);
1427c478bd9Sstevel@tonic-gate 	} else {
1437c478bd9Sstevel@tonic-gate 		char	efds[MSG_ENV_AUD_FD_SIZE + 10];
1447c478bd9Sstevel@tonic-gate 		char	eflg[MSG_ENV_AUD_FLAGS_SIZE + 10];
1457c478bd9Sstevel@tonic-gate 		char	ecnf[PATH_MAX];
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		(void) close(fildes[0]);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * Child. Set up environment variables to enable and identify
1517c478bd9Sstevel@tonic-gate 		 * auditing.
1527c478bd9Sstevel@tonic-gate 		 */
1537c478bd9Sstevel@tonic-gate 		(void) snprintf(efds, (MSG_ENV_AUD_FD_SIZE + 10),
1547c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_ENV_AUD_FD), fildes[1]);
1557c478bd9Sstevel@tonic-gate 		(void) snprintf(eflg, (MSG_ENV_AUD_FLAGS_SIZE + 10),
1567c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_ENV_AUD_FLAGS), crle->c_dlflags);
1577c478bd9Sstevel@tonic-gate 		(void) snprintf(ecnf, PATH_MAX, MSG_ORIG(MSG_ENV_LD_CONFIG),
1587c478bd9Sstevel@tonic-gate 		    crle->c_tempname);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		 * Put strings in the environment for exec().
1627c478bd9Sstevel@tonic-gate 		 * NOTE, use of automatic variables for construction of the
1637c478bd9Sstevel@tonic-gate 		 * environment variables is legitimate here, as they are local
1647c478bd9Sstevel@tonic-gate 		 * to the child process and are established solely for exec().
1657c478bd9Sstevel@tonic-gate 		 */
1667c478bd9Sstevel@tonic-gate 		if ((putenv(efds) != 0) || (putenv(eflg) != 0) ||
1677c478bd9Sstevel@tonic-gate 		    (putenv(ecnf) != 0) || (putenv(crle->c_audit) != 0) ||
1687c478bd9Sstevel@tonic-gate 		    (putenv((char *)MSG_ORIG(MSG_ENV_LD_FLAGS)) != 0)) {
1697c478bd9Sstevel@tonic-gate 			int err = errno;
1707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_PUTENV),
1717c478bd9Sstevel@tonic-gate 			    crle->c_name, strerror(err));
1727c478bd9Sstevel@tonic-gate 			return (1);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if (execlp(orgapp, orgapp, 0) == -1) {
1767c478bd9Sstevel@tonic-gate 			int err = errno;
1777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXECLP),
1787c478bd9Sstevel@tonic-gate 			    crle->c_name, orgapp, strerror(err));
1797c478bd9Sstevel@tonic-gate 			_exit(err);
1807c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	}
183b3fbe5e6Sseizo 	return (0);
1847c478bd9Sstevel@tonic-gate }
185