ldconfig.in 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash -e
  2. # Copyright 1999-2015 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. ROOT="/"
  5. LDSO_CONF="/etc/ld.so.conf"
  6. if [[ ! -e $LDSO_CONF ]]; then
  7. echo "$LDSO_CONF not found" >&2
  8. exit 1
  9. fi
  10. LDSO_CONF_DIR=$(dirname $LDSO_CONF)
  11. VERBOSE=0
  12. UPDATE_LINKS=1
  13. get_options() {
  14. while getopts "vnNXf:C:r:p" opt "$@"; do
  15. case $opt in
  16. v)
  17. echo "ldconfig for musl in Gentoo"
  18. VERBOSE=1
  19. ;;
  20. r)
  21. ROOT=$OPTARG
  22. ;;
  23. f)
  24. LDSO_CONF=$OPTARG
  25. ;;
  26. X)
  27. UPDATE_LINKS=0
  28. ;;
  29. \?)
  30. echo "Invalid option: -$opt" >&2
  31. exit 1
  32. ;;
  33. n|N|C|p)
  34. echo "Unimplemented option: -$opt" >&2
  35. exit 1
  36. ;;
  37. esac
  38. done
  39. if [[ $UPDATE_LINKS == 1 ]]; then
  40. echo "Updating links is not implemented."
  41. fi
  42. }
  43. repeated() {
  44. local l=$1
  45. local drs="${@:2}"
  46. for m in $drs; do
  47. [[ $m == $l ]] && return 0
  48. done
  49. return 1
  50. }
  51. expand() {
  52. # We are assuming the ld.so.conf's 'include' is not recursive
  53. local f line l
  54. local glob="$LDSO_CONF_DIR/$1"
  55. local drs="${@:2} "
  56. for f in $glob; do
  57. [[ ! -f $f ]] && continue
  58. while read line; do
  59. line=${line%%#*}
  60. line=${line//:/ }
  61. line=${line//,/ }
  62. for l in $line; do
  63. #We must add this whether or not the directory exists
  64. repeated $l $drs && continue
  65. drs+=" $l "
  66. done
  67. done < $f
  68. done
  69. echo $drs
  70. }
  71. read_ldso_conf() {
  72. local drs=" "
  73. while read line; do
  74. # Sanitize the line - see ldconfig(8) for delimiters
  75. # Note: bash read turns tabs into spaces and read already
  76. # delimits on newlines with the default $IFS
  77. line=${line%%#*} # Remove comments
  78. line=${line//:/ } # Change colon delimiter to space
  79. line=${line//,/ } # Change comma delimiter to space
  80. next=0
  81. for l in $line; do
  82. if [[ $next == 1 ]]; then
  83. next=0
  84. drs=$(expand $l $drs)
  85. elif [[ $l == "include" ]]; then
  86. next=1
  87. else
  88. # glibc's ldconfig silently skips non directories
  89. if [[ -d $l ]]; then
  90. repeated $l $drs && continue
  91. drs+=" $l "
  92. fi
  93. fi
  94. done
  95. done < $1
  96. echo $drs
  97. }
  98. sanitize() {
  99. local drs=$@
  100. repeated "/lib" $drs || drs="/lib $drs"
  101. repeated "/usr/lib" $drs || drs="/usr/lib $drs"
  102. echo $drs
  103. }
  104. get_options "$@"
  105. drs=$(read_ldso_conf "$LDSO_CONF")
  106. drs=$(sanitize $drs)
  107. ARCH=@@ARCH@@
  108. LDSO_PATH="/lib/ld-musl-${ARCH}.so.1"
  109. if [[ ! -e $LDSO_PATH ]]; then
  110. echo "$LDSO_PATH not found" >&2
  111. exit 1
  112. fi
  113. LDSO_ARCH=$(basename $LDSO_PATH)
  114. LDSO_NAME=${LDSO_ARCH%.so.1}
  115. ETC_LDSO_PATH=/etc/${LDSO_NAME}.path
  116. X=$(mktemp -p /tmp ${LDSO_NAME}.XXXXXX)
  117. for d in $drs; do
  118. echo $d >> $X
  119. done
  120. chmod 644 $X
  121. mv $X $ETC_LDSO_PATH