ldconfig.in-r1 2.9 KB

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