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