libc.c (2061B)
1 /* Copyright (c) 2001 Matej Pfajfar. 2 * Copyright (c) 2001-2004, Roger Dingledine. 3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. 4 * Copyright (c) 2007-2021, The Tor Project, Inc. */ 5 /* See LICENSE for licensing information */ 6 7 /** 8 * @file libc.c 9 * @brief Functions to get the name and version of the system libc. 10 **/ 11 12 #include "orconfig.h" 13 #include "lib/osinfo/libc.h" 14 #include <stdlib.h> 15 16 #ifdef HAVE_GNU_LIBC_VERSION_H 17 #include <gnu/libc-version.h> 18 #endif 19 20 #ifdef HAVE_GNU_LIBC_VERSION_H 21 #ifdef HAVE_GNU_GET_LIBC_VERSION 22 #define CHECK_LIBC_VERSION 23 #endif 24 #endif 25 26 #define STR_IMPL(x) #x 27 #define STR(x) STR_IMPL(x) 28 29 #if defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE) 30 #include <sys/param.h> 31 #endif /* defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE) */ 32 33 /** Return the name of the compile time libc. Returns NULL if we 34 * cannot identify the libc. */ 35 const char * 36 tor_libc_get_name(void) 37 { 38 #if defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE) 39 return "BSD"; 40 #endif /* defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE) */ 41 #ifdef __GLIBC__ 42 return "Glibc"; 43 #else /* !defined(__GLIBC__) */ 44 return NULL; 45 #endif /* defined(__GLIBC__) */ 46 } 47 48 /** Return a string representation of the version of the currently running 49 * version of Glibc. */ 50 const char * 51 tor_libc_get_version_str(void) 52 { 53 #ifdef __DragonFly_version 54 return STR(__DragonFly_version); 55 #endif 56 #ifdef __FreeBSD__ 57 return STR(__FreeBSD_version); 58 #endif 59 #ifdef __NetBSD_Version__ 60 return STR(__NetBSD_Version__); 61 #endif 62 #ifdef OpenBSD 63 return STR(OpenBSD); 64 #endif 65 #ifdef CHECK_LIBC_VERSION 66 const char *version = gnu_get_libc_version(); 67 if (version == NULL) 68 return "N/A"; 69 return version; 70 #else /* !defined(CHECK_LIBC_VERSION) */ 71 return "N/A"; 72 #endif /* defined(CHECK_LIBC_VERSION) */ 73 } 74 75 /** Return a string representation of the version of Glibc that was used at 76 * compilation time. */ 77 const char * 78 tor_libc_get_header_version_str(void) 79 { 80 #ifdef __GLIBC__ 81 return STR(__GLIBC__) "." STR(__GLIBC_MINOR__); 82 #else 83 return "N/A"; 84 #endif /* defined(__GLIBC__) */ 85 }