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