detect-libc.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2017 Lovell Fuller and others.
  2. // SPDX-License-Identifier: Apache-2.0
  3. 'use strict';
  4. const childProcess = require('child_process');
  5. const { isLinux, getReport } = require('./process');
  6. const { LDD_PATH, readFile, readFileSync } = require('./filesystem');
  7. let cachedFamilyFilesystem;
  8. let cachedVersionFilesystem;
  9. const command = 'getconf GNU_LIBC_VERSION 2>&1 || true; ldd --version 2>&1 || true';
  10. let commandOut = '';
  11. const safeCommand = () => {
  12. if (!commandOut) {
  13. return new Promise((resolve) => {
  14. childProcess.exec(command, (err, out) => {
  15. commandOut = err ? ' ' : out;
  16. resolve(commandOut);
  17. });
  18. });
  19. }
  20. return commandOut;
  21. };
  22. const safeCommandSync = () => {
  23. if (!commandOut) {
  24. try {
  25. commandOut = childProcess.execSync(command, { encoding: 'utf8' });
  26. } catch (_err) {
  27. commandOut = ' ';
  28. }
  29. }
  30. return commandOut;
  31. };
  32. /**
  33. * A String constant containing the value `glibc`.
  34. * @type {string}
  35. * @public
  36. */
  37. const GLIBC = 'glibc';
  38. /**
  39. * A Regexp constant to get the GLIBC Version.
  40. * @type {string}
  41. */
  42. const RE_GLIBC_VERSION = /GLIBC\s(\d+\.\d+)/;
  43. /**
  44. * A String constant containing the value `musl`.
  45. * @type {string}
  46. * @public
  47. */
  48. const MUSL = 'musl';
  49. /**
  50. * This string is used to find if the {@link LDD_PATH} is GLIBC
  51. * @type {string}
  52. */
  53. const GLIBC_ON_LDD = GLIBC.toUpperCase();
  54. /**
  55. * This string is used to find if the {@link LDD_PATH} is musl
  56. * @type {string}
  57. */
  58. const MUSL_ON_LDD = MUSL.toLowerCase();
  59. const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-');
  60. const familyFromReport = () => {
  61. const report = getReport();
  62. if (report.header && report.header.glibcVersionRuntime) {
  63. return GLIBC;
  64. }
  65. if (Array.isArray(report.sharedObjects)) {
  66. if (report.sharedObjects.some(isFileMusl)) {
  67. return MUSL;
  68. }
  69. }
  70. return null;
  71. };
  72. const familyFromCommand = (out) => {
  73. const [getconf, ldd1] = out.split(/[\r\n]+/);
  74. if (getconf && getconf.includes(GLIBC)) {
  75. return GLIBC;
  76. }
  77. if (ldd1 && ldd1.includes(MUSL)) {
  78. return MUSL;
  79. }
  80. return null;
  81. };
  82. const getFamilyFromLddContent = (content) => {
  83. if (content.includes(MUSL_ON_LDD)) {
  84. return MUSL;
  85. }
  86. if (content.includes(GLIBC_ON_LDD)) {
  87. return GLIBC;
  88. }
  89. return null;
  90. };
  91. const familyFromFilesystem = async () => {
  92. if (cachedFamilyFilesystem !== undefined) {
  93. return cachedFamilyFilesystem;
  94. }
  95. cachedFamilyFilesystem = null;
  96. try {
  97. const lddContent = await readFile(LDD_PATH);
  98. cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
  99. } catch (e) {}
  100. return cachedFamilyFilesystem;
  101. };
  102. const familyFromFilesystemSync = () => {
  103. if (cachedFamilyFilesystem !== undefined) {
  104. return cachedFamilyFilesystem;
  105. }
  106. cachedFamilyFilesystem = null;
  107. try {
  108. const lddContent = readFileSync(LDD_PATH);
  109. cachedFamilyFilesystem = getFamilyFromLddContent(lddContent);
  110. } catch (e) {}
  111. return cachedFamilyFilesystem;
  112. };
  113. /**
  114. * Resolves with the libc family when it can be determined, `null` otherwise.
  115. * @returns {Promise<?string>}
  116. */
  117. const family = async () => {
  118. let family = null;
  119. if (isLinux()) {
  120. family = await familyFromFilesystem();
  121. if (!family) {
  122. family = familyFromReport();
  123. }
  124. if (!family) {
  125. const out = await safeCommand();
  126. family = familyFromCommand(out);
  127. }
  128. }
  129. return family;
  130. };
  131. /**
  132. * Returns the libc family when it can be determined, `null` otherwise.
  133. * @returns {?string}
  134. */
  135. const familySync = () => {
  136. let family = null;
  137. if (isLinux()) {
  138. family = familyFromFilesystemSync();
  139. if (!family) {
  140. family = familyFromReport();
  141. }
  142. if (!family) {
  143. const out = safeCommandSync();
  144. family = familyFromCommand(out);
  145. }
  146. }
  147. return family;
  148. };
  149. /**
  150. * Resolves `true` only when the platform is Linux and the libc family is not `glibc`.
  151. * @returns {Promise<boolean>}
  152. */
  153. const isNonGlibcLinux = async () => isLinux() && await family() !== GLIBC;
  154. /**
  155. * Returns `true` only when the platform is Linux and the libc family is not `glibc`.
  156. * @returns {boolean}
  157. */
  158. const isNonGlibcLinuxSync = () => isLinux() && familySync() !== GLIBC;
  159. const versionFromFilesystem = async () => {
  160. if (cachedVersionFilesystem !== undefined) {
  161. return cachedVersionFilesystem;
  162. }
  163. cachedVersionFilesystem = null;
  164. try {
  165. const lddContent = await readFile(LDD_PATH);
  166. const versionMatch = lddContent.match(RE_GLIBC_VERSION);
  167. if (versionMatch) {
  168. cachedVersionFilesystem = versionMatch[1];
  169. }
  170. } catch (e) {}
  171. return cachedVersionFilesystem;
  172. };
  173. const versionFromFilesystemSync = () => {
  174. if (cachedVersionFilesystem !== undefined) {
  175. return cachedVersionFilesystem;
  176. }
  177. cachedVersionFilesystem = null;
  178. try {
  179. const lddContent = readFileSync(LDD_PATH);
  180. const versionMatch = lddContent.match(RE_GLIBC_VERSION);
  181. if (versionMatch) {
  182. cachedVersionFilesystem = versionMatch[1];
  183. }
  184. } catch (e) {}
  185. return cachedVersionFilesystem;
  186. };
  187. const versionFromReport = () => {
  188. const report = getReport();
  189. if (report.header && report.header.glibcVersionRuntime) {
  190. return report.header.glibcVersionRuntime;
  191. }
  192. return null;
  193. };
  194. const versionSuffix = (s) => s.trim().split(/\s+/)[1];
  195. const versionFromCommand = (out) => {
  196. const [getconf, ldd1, ldd2] = out.split(/[\r\n]+/);
  197. if (getconf && getconf.includes(GLIBC)) {
  198. return versionSuffix(getconf);
  199. }
  200. if (ldd1 && ldd2 && ldd1.includes(MUSL)) {
  201. return versionSuffix(ldd2);
  202. }
  203. return null;
  204. };
  205. /**
  206. * Resolves with the libc version when it can be determined, `null` otherwise.
  207. * @returns {Promise<?string>}
  208. */
  209. const version = async () => {
  210. let version = null;
  211. if (isLinux()) {
  212. version = await versionFromFilesystem();
  213. if (!version) {
  214. version = versionFromReport();
  215. }
  216. if (!version) {
  217. const out = await safeCommand();
  218. version = versionFromCommand(out);
  219. }
  220. }
  221. return version;
  222. };
  223. /**
  224. * Returns the libc version when it can be determined, `null` otherwise.
  225. * @returns {?string}
  226. */
  227. const versionSync = () => {
  228. let version = null;
  229. if (isLinux()) {
  230. version = versionFromFilesystemSync();
  231. if (!version) {
  232. version = versionFromReport();
  233. }
  234. if (!version) {
  235. const out = safeCommandSync();
  236. version = versionFromCommand(out);
  237. }
  238. }
  239. return version;
  240. };
  241. module.exports = {
  242. GLIBC,
  243. MUSL,
  244. family,
  245. familySync,
  246. isNonGlibcLinux,
  247. isNonGlibcLinuxSync,
  248. version,
  249. versionSync
  250. };