Skip to content

Commit a36226b

Browse files
committed
fix(dist): add fallbacks to /proc/self/exe in rustup-init.sh
1 parent 54dd3d0 commit a36226b

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

rustup-init.sh

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,23 @@ main() {
177177
return "$_retval"
178178
}
179179

180-
check_proc() {
181-
# Check for /proc by looking for the /proc/self/exe link
180+
get_current_exe() {
181+
# Returns the executable used for system architecture detection
182182
# This is only run on Linux
183-
if ! test -L /proc/self/exe ; then
184-
err "fatal: Unable to find /proc/self/exe. Is /proc mounted? Installation cannot proceed without /proc."
183+
local _current_exe
184+
if test -L /proc/self/exe ; then
185+
_current_exe=/proc/self/exe
186+
else
187+
echo "Warning: Unable to find /proc/self/exe. System architecture detection might be inaccurate."
188+
if test -n "$SHELL" ; then
189+
_current_exe=$SHELL
190+
else
191+
need_cmd /bin/sh
192+
_current_exe=/bin/sh
193+
fi
194+
echo "Warning: Falling back to $_current_exe."
185195
fi
196+
RETVAL="$_current_exe"
186197
}
187198

188199
get_bitness() {
@@ -193,8 +204,9 @@ get_bitness() {
193204
# 0x02 for 64-bit.
194205
# The printf builtin on some shells like dash only supports octal
195206
# escape sequences, so we use those.
207+
local _current_exe=$1
196208
local _current_exe_head
197-
_current_exe_head=$(head -c 5 /proc/self/exe )
209+
_current_exe_head=$(head -c 5 "$_current_exe")
198210
if [ "$_current_exe_head" = "$(printf '\177ELF\001')" ]; then
199211
echo 32
200212
elif [ "$_current_exe_head" = "$(printf '\177ELF\002')" ]; then
@@ -205,27 +217,30 @@ get_bitness() {
205217
}
206218

207219
is_host_amd64_elf() {
220+
local _current_exe=$1
221+
208222
need_cmd head
209223
need_cmd tail
210224
# ELF e_machine detection without dependencies beyond coreutils.
211225
# Two-byte field at offset 0x12 indicates the CPU,
212226
# but we're interested in it being 0x3E to indicate amd64, or not that.
213227
local _current_exe_machine
214-
_current_exe_machine=$(head -c 19 /proc/self/exe | tail -c 1)
228+
_current_exe_machine=$(head -c 19 "$_current_exe" | tail -c 1)
215229
[ "$_current_exe_machine" = "$(printf '\076')" ]
216230
}
217231

218232
get_endianness() {
219-
local cputype=$1
220-
local suffix_eb=$2
221-
local suffix_el=$3
233+
local _current_exe=$1
234+
local cputype=$2
235+
local suffix_eb=$3
236+
local suffix_el=$4
222237

223238
# detect endianness without od/hexdump, like get_bitness() does.
224239
need_cmd head
225240
need_cmd tail
226241

227242
local _current_exe_endianness
228-
_current_exe_endianness="$(head -c 6 /proc/self/exe | tail -c 1)"
243+
_current_exe_endianness="$(head -c 6 "$_current_exe" | tail -c 1)"
229244
if [ "$_current_exe_endianness" = "$(printf '\001')" ]; then
230245
echo "${cputype}${suffix_el}"
231246
elif [ "$_current_exe_endianness" = "$(printf '\002')" ]; then
@@ -356,16 +371,18 @@ get_architecture() {
356371
fi
357372
fi
358373

374+
local _current_exe
359375
case "$_ostype" in
360376

361377
Android)
362378
_ostype=linux-android
363379
;;
364380

365381
Linux)
366-
check_proc
382+
get_current_exe
383+
_current_exe=$RETVAL
367384
_ostype=unknown-linux-$_clibtype
368-
_bitness=$(get_bitness)
385+
_bitness=$(get_bitness "$_current_exe")
369386
;;
370387

371388
FreeBSD)
@@ -438,14 +455,14 @@ get_architecture() {
438455
;;
439456

440457
mips)
441-
_cputype=$(get_endianness mips '' el)
458+
_cputype=$(get_endianness "$_current_exe" mips '' el)
442459
;;
443460

444461
mips64)
445462
if [ "$_bitness" -eq 64 ]; then
446463
# only n64 ABI is supported for now
447464
_ostype="${_ostype}abi64"
448-
_cputype=$(get_endianness mips64 '' el)
465+
_cputype=$(get_endianness "$_current_exe" mips64 '' el)
449466
fi
450467
;;
451468

@@ -484,7 +501,7 @@ get_architecture() {
484501
_cputype="$RUSTUP_CPUTYPE"
485502
else {
486503
# 32-bit executable for amd64 = x32
487-
if is_host_amd64_elf; then {
504+
if is_host_amd64_elf "$_current_exe"; then {
488505
echo "This host is running an x32 userland; as it stands, x32 support is poor," 1>&2
489506
echo "and there isn't a native toolchain -- you will have to install" 1>&2
490507
echo "multiarch compatibility with i686 and/or amd64, then select one" 1>&2
@@ -500,7 +517,7 @@ get_architecture() {
500517
}; fi
501518
;;
502519
mips64)
503-
_cputype=$(get_endianness mips '' el)
520+
_cputype=$(get_endianness "$_current_exe" mips '' el)
504521
;;
505522
powerpc64)
506523
_cputype=powerpc

0 commit comments

Comments
 (0)