xref: /titanic_52/usr/src/cmd/sgs/crle/common/dump.c (revision 25cf1a301a396c38e8adf52c15f537b80d2483f7)
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 /*
23  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  *	Use is subject to license terms.
25  */
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include	<sys/types.h>
29 #include	<stdio.h>
30 #include	<errno.h>
31 #include	<unistd.h>
32 #include	<string.h>
33 #include	<wait.h>
34 #include	<limits.h>
35 #include	"machdep.h"
36 #include	"sgs.h"
37 #include	"rtc.h"
38 #include	"conv.h"
39 #include	"_crle.h"
40 #include	"msg.h"
41 
42 /*
43  * Having gathered together any dependencies, dldump(3x) any necessary images.
44  *
45  * All dldump(3x) processing is carried out from the audit library.  The
46  * temporary configuration file is read and all alternative marked files are
47  * dumped.  If a -E application requires RTLD_REL_EXEC then that application
48  * acts as the new process, otherwise lddstub is used.
49  *
50  * Besides dldump(3x)'ing any images the audit library returns the address
51  * range of the images which will used to update the configuration file.
52  */
53 int
54 dump(Crle_desc * crle)
55 {
56 	const char	*orgapp = (const char *)crle->c_app;
57 	int		fildes[2], pid;
58 
59 	if (orgapp == 0)
60 		orgapp = conv_lddstub(crle->c_class);
61 
62 	/*
63 	 * Set up a pipe through which the audit library will write the image
64 	 * address ranges.
65 	 */
66 	if (pipe(fildes) == -1) {
67 		int err = errno;
68 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
69 		    crle->c_name, strerror(err));
70 		return (1);
71 	}
72 
73 	/*
74 	 * Fork ourselves to run the application and collect its dependencies.
75 	 */
76 	if ((pid = fork()) == -1) {
77 		int err = errno;
78 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
79 		    crle->c_name, strerror(err));
80 		return (1);
81 	}
82 
83 	if (pid) {
84 		/*
85 		 * Parent. Read memory range entries from the audit library.
86 		 * The read side of the pipe is attached to stdio to make
87 		 * obtaining the individual dependencies easier.
88 		 */
89 		int	error = 0, status;
90 		FILE	*fd;
91 		char	buffer[PATH_MAX];
92 
93 		(void) close(fildes[1]);
94 		if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
95 			char		*str;
96 			Rtc_head	*rtc = (Rtc_head *)crle->c_tempaddr;
97 
98 			while (fgets(buffer, PATH_MAX, fd) != NULL) {
99 				/*
100 				 * Make sure we recognize the message, remove
101 				 * the newline (which allowed fgets() use) and
102 				 * register the memory range entry;
103 				 */
104 				if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
105 				    MSG_AUD_PRF_SIZE))
106 					continue;
107 
108 				str = strrchr(buffer, '\n');
109 				*str = '\0';
110 				str = buffer + MSG_AUD_PRF_SIZE;
111 
112 				if (strncmp(MSG_ORIG(MSG_AUD_RESBGN),
113 				    str, MSG_AUD_RESBGN_SIZE) == 0) {
114 					rtc->ch_resbgn =
115 					    strtoull(str + MSG_AUD_RESBGN_SIZE,
116 						(char **)NULL, 0);
117 				} else if (strncmp(MSG_ORIG(MSG_AUD_RESEND),
118 				    str, MSG_AUD_RESEND_SIZE) == 0) {
119 					rtc->ch_resend =
120 					    strtoull(str + MSG_AUD_RESEND_SIZE,
121 						(char **)NULL, 0);
122 				} else
123 					continue;
124 			}
125 			(void) fclose(fd);
126 		} else
127 			error = errno;
128 
129 		while (wait(&status) != pid)
130 			;
131 		if (status) {
132 			if (WIFSIGNALED(status)) {
133 				(void) fprintf(stderr,
134 				    MSG_INTL(MSG_SYS_EXEC), crle->c_name,
135 				    orgapp, (WSIGMASK & status),
136 				    ((status & WCOREFLG) ?
137 				    MSG_INTL(MSG_SYS_CORE) :
138 				    MSG_ORIG(MSG_STR_EMPTY)));
139 			}
140 			return (status);
141 		}
142 		return (error);
143 	} else {
144 		char	efds[MSG_ENV_AUD_FD_SIZE + 10];
145 		char	eflg[MSG_ENV_AUD_FLAGS_SIZE + 10];
146 		char	ecnf[PATH_MAX];
147 
148 		(void) close(fildes[0]);
149 
150 		/*
151 		 * Child. Set up environment variables to enable and identify
152 		 * auditing.
153 		 */
154 		(void) snprintf(efds, (MSG_ENV_AUD_FD_SIZE + 10),
155 		    MSG_ORIG(MSG_ENV_AUD_FD), fildes[1]);
156 		(void) snprintf(eflg, (MSG_ENV_AUD_FLAGS_SIZE + 10),
157 		    MSG_ORIG(MSG_ENV_AUD_FLAGS), crle->c_dlflags);
158 		(void) snprintf(ecnf, PATH_MAX, MSG_ORIG(MSG_ENV_LD_CONFIG),
159 		    crle->c_tempname);
160 
161 		/*
162 		 * Put strings in the environment for exec().
163 		 * NOTE, use of automatic variables for construction of the
164 		 * environment variables is legitimate here, as they are local
165 		 * to the child process and are established solely for exec().
166 		 */
167 		if ((putenv(efds) != 0) || (putenv(eflg) != 0) ||
168 		    (putenv(ecnf) != 0) || (putenv(crle->c_audit) != 0) ||
169 		    (putenv((char *)MSG_ORIG(MSG_ENV_LD_FLAGS)) != 0)) {
170 			int err = errno;
171 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_PUTENV),
172 			    crle->c_name, strerror(err));
173 			return (1);
174 		}
175 
176 		if (execlp(orgapp, orgapp, 0) == -1) {
177 			int err = errno;
178 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXECLP),
179 			    crle->c_name, orgapp, strerror(err));
180 			_exit(err);
181 			/* NOTREACHED */
182 		}
183 	}
184 	return (0);
185 }
186