xref: /titanic_41/usr/src/lib/libc/i386/etc/caplib.ksh (revision cb6207858a9fcc2feaee22e626912fba281ac969)
1#!/bin/ksh
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance 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# ident	"%Z%%M%	%I%	%E% SMI"
24#
25# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29#
30# This script is called by flarcreate.sh
31#
32# Unmount all hwcap libraries (like /usr/lib/libc/libc_hwcap2.so.1)
33# and store commands needed to remount them in preexit/remount_hwcap.xxxx
34# scripts, which remounts them in the preexit phase.
35#
36
37if [ -z "$FLASH_PID" ]; then
38	echo "$0: ERROR: FLASH_PID not set in execution environment, exiting..."
39	exit 1
40fi
41if [ -z "$FLASH_DIR" ]; then
42	echo "$0: ERROR: FLASH_DIR not set in execution environment, exiting..."
43	exit 1
44fi
45
46CHMOD=/usr/bin/chmod
47ELFDUMP=/usr/ccs/bin/elfdump
48MOUNT=/usr/sbin/mount
49UMOUNT=/usr/sbin/umount
50EGREP=/usr/bin/egrep
51SED=/usr/bin/sed
52CMD_LIST="$CHMOD $ELFDUMP $MOUNT $UMOUNT $EGREP $SED"
53
54for cmd in $CMD_LIST
55do
56    if [ ! -x $cmd ]; then
57	echo "$0: ERROR: $cmd not found or not executable, exiting..."
58	exit 1
59    fi
60done
61
62#
63# Fill "LIBS" with a list of mounted libraries in the form:
64# 	MOUNTPOUNT:FILE
65# e.g.:
66#	/lib/libc.so.1:/usr/lib/libc/libc_hwcap2.so.1
67#
68LIBS=`$MOUNT | $EGREP "^/lib|^/usr/lib" | \
69    $SED -e 's:^\(/[^ ]*\) on \([^ ]*\).*$:\1@\2:'`
70
71if [ ! "$LIBS" ]; then
72	exit 0
73fi
74
75REMOUNT_DIR=${FLASH_DIR}/preexit
76REMOUNT=${REMOUNT_DIR}/remount_hwcap.${FLASH_PID}
77
78#
79# Create the flash preexit script directory for the remount scripts if it
80# doesn't already exist.
81#
82if [ ! -d $REMOUNT_DIR ]; then
83	umask 077
84	/usr/bin/mkdir $REMOUNT_DIR
85	if [ $? -ne 0 ]; then
86		echo "$0: ERROR: could not mkdir $REMOUNT_DIR, exiting..."
87		exit 1
88	fi
89fi
90
91#
92# If an old remount script by this name exists, delete it
93#
94if [ -f $REMOUNT ]; then
95	/bin/rm -f $REMOUNT
96fi
97
98umask 477
99
100cat > $REMOUNT << EOF
101#!/bin/sh
102if [ \"\$FLASH_PID\" != \"$FLASH_PID\" ]; then
103	/bin/rm -f $REMOUNT
104	exit 0
105fi
106EOF
107
108if [ $? -ne 0 ]; then
109	echo "$0: ERROR: could not create $REMOUNT, exiting..."
110	exit 1
111fi
112
113#
114# Now process each of the libraries that are mounted.  For each, find out if
115# it's a hwcap library; if it is, unmount it and write instructions to the
116# preexit script as to how to remount it.
117#
118for entry in $LIBS
119do
120	echo $entry | IFS=@ read MOUNTPOINT MOUNTLIB
121	CAPLIB=`$ELFDUMP -H $MOUNTLIB`
122	if [ \( $? -eq 0 \) -a \( -n "$CAPLIB" \) ]; then
123		$UMOUNT $MOUNTPOINT || $UMOUNT -f $MOUNTPOINT || \
124		    { echo "$0: ERROR: Could not unmount" \
125			  "$MOUNTPOINT, exiting..."; \
126		      /bin/sh $REMOUNT; /bin/rm -f $REMOUNT; exit 1; }
127
128		echo $MOUNTLIB | $EGREP -s :
129		if [ $? -eq 0 ]; then
130			MOUNTOPTS="-O"
131		else
132			MOUNTOPTS="-O -F lofs"
133		fi
134		echo "$MOUNT $MOUNTOPTS $MOUNTLIB $MOUNTPOINT" >> $REMOUNT
135	fi
136done
137
138#
139# Write final cleanup instructions to the flash preexit remount script and make
140# it executable.
141#
142echo "/bin/rm -f $REMOUNT" >> $REMOUNT
143echo "exit 0" >> $REMOUNT
144$CHMOD 0500 $REMOUNT
145exit 0
146