ldconfig.in-r3 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/bash -e
  2. # Copyright 1999-2022 Gentoo Authors
  3. # Distributed under the terms of the GNU General Public License v2
  4. ROOT="/"
  5. EPREFIX="@GENTOO_PORTAGE_EPREFIX@"
  6. LDSO_CONF_FILE="/etc/ld.so.conf"
  7. VERBOSE=0
  8. UPDATE_LINKS=1
  9. get_options() {
  10. LDSO_CONF=""
  11. while getopts "vnNXf:C:r:p" opt "$@"; do
  12. case $opt in
  13. v)
  14. echo "ldconfig for musl in Gentoo"
  15. VERBOSE=1
  16. ;;
  17. r)
  18. ROOT=${OPTARG}
  19. ;;
  20. f)
  21. LDSO_CONF=${OPTARG}
  22. ;;
  23. X)
  24. UPDATE_LINKS=0
  25. ;;
  26. \?)
  27. echo "Invalid option: -${opt}" >&2
  28. exit 1
  29. ;;
  30. n|N|C|p)
  31. echo "Unimplemented option: -${opt}" >&2
  32. exit 1
  33. ;;
  34. esac
  35. done
  36. if [[ -z ${LDSO_CONF} ]]; then
  37. LDSO_CONF=${ROOT}${EPREFIX}${LDSO_CONF_FILE}
  38. fi
  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 "${EPREFIX}/lib" ${drs} || drs="${EPREFIX}/lib ${drs}"
  101. repeated "${EPREFIX}/usr/lib" ${drs} || drs="${EPREFIX}/usr/lib ${drs}"
  102. echo ${drs}
  103. }
  104. changed() {
  105. [[ -f ${ETC_LDSO_PATH} ]] || return 0
  106. local current=$(<${ETC_LDSO_PATH})
  107. current=${current//$'\n'/ }
  108. [[ ${current} != ${drs} ]] || return 1
  109. }
  110. get_options "$@"
  111. if [[ ! -e ${LDSO_CONF} ]]; then
  112. echo "${LDSO_CONF} not found" >&2
  113. exit 1
  114. fi
  115. LDSO_CONF_DIR=$(dirname ${LDSO_CONF})
  116. drs=$(read_ldso_conf "${LDSO_CONF}")
  117. drs=$(sanitize ${drs})
  118. ARCH=@@ARCH@@
  119. LDSO_PATH="${ROOT}${EPREFIX}/lib/ld-musl-${ARCH}.so.1"
  120. if [[ ! -e ${LDSO_PATH} ]]; then
  121. echo "${LDSO_PATH} not found" >&2
  122. exit 1
  123. fi
  124. LDSO_ARCH=$(basename ${LDSO_PATH})
  125. LDSO_NAME=${LDSO_ARCH%.so.1}
  126. ETC_LDSO_PATH="${ROOT}${EPREFIX}/etc/${LDSO_NAME}.path"
  127. changed || exit 0
  128. X=$(mktemp -p /tmp ${LDSO_NAME}.XXXXXX)
  129. for d in ${drs}; do
  130. echo ${d} >> ${X}
  131. done
  132. chmod 644 ${X}
  133. # busybox doesn't support mz -Z
  134. cp ${X} ${ETC_LDSO_PATH}
  135. rm ${X}