summaryrefslogtreecommitdiff
path: root/usr/src/cmd/sgs/crle/common/dump.c
blob: fa84ab11c9dd13031af22f79698dc8d9b9071e5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 *	Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include	<sys/types.h>
#include	<stdio.h>
#include	<errno.h>
#include	<unistd.h>
#include	<string.h>
#include	<wait.h>
#include	<limits.h>
#include	"machdep.h"
#include	"sgs.h"
#include	"rtc.h"
#include	"conv.h"
#include	"_crle.h"
#include	"msg.h"

/*
 * Having gathered together any dependencies, dldump(3x) any necessary images.
 *
 * All dldump(3x) processing is carried out from the audit library.  The
 * temporary configuration file is read and all alternative marked files are
 * dumped.  If a -E application requires RTLD_REL_EXEC then that application
 * acts as the new process, otherwise lddstub is used.
 *
 * Besides dldump(3x)'ing any images the audit library returns the address
 * range of the images which will used to update the configuration file.
 */
int
dump(Crle_desc * crle)
{
	const char	*orgapp = (const char *)crle->c_app;
	int		fildes[2], pid;

	if (orgapp == 0)
		orgapp = conv_lddstub(M_CLASS);

	/*
	 * Set up a pipe through which the audit library will write the image
	 * address ranges.
	 */
	if (pipe(fildes) == -1) {
		int err = errno;
		(void) fprintf(stderr, MSG_INTL(MSG_SYS_PIPE),
		    crle->c_name, strerror(err));
		return (1);
	}

	/*
	 * Fork ourselves to run the application and collect its dependencies.
	 */
	if ((pid = fork()) == -1) {
		int err = errno;
		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK),
		    crle->c_name, strerror(err));
		return (1);
	}

	if (pid) {
		/*
		 * Parent. Read memory range entries from the audit library.
		 * The read side of the pipe is attached to stdio to make
		 * obtaining the individual dependencies easier.
		 */
		int	error = 0, status;
		FILE	*fd;
		char	buffer[PATH_MAX];

		(void) close(fildes[1]);
		if ((fd = fdopen(fildes[0], MSG_ORIG(MSG_STR_READ))) != NULL) {
			char *str;
			Rtc_head *rtc = (Rtc_head *)crle->c_tempheadaddr;

			while (fgets(buffer, PATH_MAX, fd) != NULL) {
				/*
				 * Make sure we recognize the message, remove
				 * the newline (which allowed fgets() use) and
				 * register the memory range entry;
				 */
				if (strncmp(MSG_ORIG(MSG_AUD_PRF), buffer,
				    MSG_AUD_PRF_SIZE))
					continue;

				str = strrchr(buffer, '\n');
				*str = '\0';
				str = buffer + MSG_AUD_PRF_SIZE;

				if (strncmp(MSG_ORIG(MSG_AUD_RESBGN),
				    str, MSG_AUD_RESBGN_SIZE) == 0) {
					rtc->ch_resbgn =
					    strtoull(str + MSG_AUD_RESBGN_SIZE,
						(char **)NULL, 0);
				} else if (strncmp(MSG_ORIG(MSG_AUD_RESEND),
				    str, MSG_AUD_RESEND_SIZE) == 0) {
					rtc->ch_resend =
					    strtoull(str + MSG_AUD_RESEND_SIZE,
						(char **)NULL, 0);
				} else {
					continue;
				}
			}
			(void) fclose(fd);
		} else
			error = errno;

		while (wait(&status) != pid)
			;
		if (status) {
			if (WIFSIGNALED(status)) {
				(void) fprintf(stderr,
				    MSG_INTL(MSG_SYS_EXEC), crle->c_name,
				    orgapp, (WSIGMASK & status),
				    ((status & WCOREFLG) ?
				    MSG_INTL(MSG_SYS_CORE) :
				    MSG_ORIG(MSG_STR_EMPTY)));
			}
			return (status);
		}
		return (error);
	} else {
		char	efds[MSG_ENV_AUD_FD_SIZE + 10];
		char	eflg[MSG_ENV_AUD_FLAGS_SIZE + 10];
		char	ecnf[PATH_MAX];

		(void) close(fildes[0]);

		/*
		 * Child. Set up environment variables to enable and identify
		 * auditing.
		 */
		(void) snprintf(efds, (MSG_ENV_AUD_FD_SIZE + 10),
		    MSG_ORIG(MSG_ENV_AUD_FD), fildes[1]);
		(void) snprintf(eflg, (MSG_ENV_AUD_FLAGS_SIZE + 10),
		    MSG_ORIG(MSG_ENV_AUD_FLAGS), crle->c_dlflags);
		(void) snprintf(ecnf, PATH_MAX, MSG_ORIG(MSG_ENV_LD_CONFIG),
		    crle->c_tempname);

		/*
		 * Put strings in the environment for exec().
		 * NOTE, use of automatic variables for construction of the
		 * environment variables is legitimate here, as they are local
		 * to the child process and are established solely for exec().
		 */
		if ((putenv(efds) != 0) || (putenv(eflg) != 0) ||
		    (putenv(ecnf) != 0) || (putenv(crle->c_audit) != 0) ||
		    (putenv((char *)MSG_ORIG(MSG_ENV_LD_FLAGS)) != 0)) {
			int err = errno;
			(void) fprintf(stderr, MSG_INTL(MSG_SYS_PUTENV),
			    crle->c_name, strerror(err));
			return (1);
		}

		if (execlp(orgapp, orgapp, 0) == -1) {
			int err = errno;
			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXECLP),
			    crle->c_name, orgapp, strerror(err));
			_exit(err);
			/* NOTREACHED */
		}
	}
	return (0);
}