Coverage Report

Created: 2025-09-05 07:01

/src/tor/src/lib/osinfo/libc.c
Line
Count
Source (jump to first uncovered line)
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
0
#define STR_IMPL(x) #x
27
0
#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
0
{
38
#if defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE)
39
  return "BSD";
40
#endif /* defined(__BSD_VISIBLE) || defined(__NETBSD_SOURCE) */
41
0
#ifdef __GLIBC__
42
0
  return "Glibc";
43
#else /* !defined(__GLIBC__) */
44
  return NULL;
45
#endif /* defined(__GLIBC__) */
46
0
}
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
0
{
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
0
#ifdef CHECK_LIBC_VERSION
66
0
  const char *version = gnu_get_libc_version();
67
0
  if (version == NULL)
68
0
    return "N/A";
69
0
  return version;
70
#else /* !defined(CHECK_LIBC_VERSION) */
71
  return "N/A";
72
#endif /* defined(CHECK_LIBC_VERSION) */
73
0
}
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
0
{
80
0
#ifdef __GLIBC__
81
0
  return STR(__GLIBC__) "." STR(__GLIBC_MINOR__);
82
#else
83
  return "N/A";
84
#endif /* defined(__GLIBC__) */
85
0
}