blob: 9aab31d4a1d9c50e7bd6df4e4e0276c9af4761b2 [file] [log] [blame]
Patrick Rohre7f26e22022-01-12 22:13:12 +01001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// -----------------------------------------------------------------------------
18// TODO - This should be replaced with BpfUtils in bpf_headers.
19// Currently, bpf_headers contains a bunch requirements it doesn't actually provide, such as a
20// non-ndk liblog version, and some version of libbase. libtcutils does not have access to either of
21// these, so I think this will have to wait until we figure out a way around this.
22//
23// In the mean time copying verbatim from:
24// frameworks/libs/net/common/native/bpf_headers
25
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010026#pragma once
27
Patrick Rohre7f26e22022-01-12 22:13:12 +010028#include <stdio.h>
29#include <sys/utsname.h>
30
31#define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c))
32
33namespace android {
34
Maciej Żenczykowskieeb58872022-05-25 12:29:37 -070035static inline unsigned uncachedKernelVersion() {
Patrick Rohre7f26e22022-01-12 22:13:12 +010036 struct utsname buf;
Maciej Żenczykowski9a276e62022-05-27 19:38:15 +000037 if (uname(&buf)) return 0;
Patrick Rohre7f26e22022-01-12 22:13:12 +010038
Maciej Żenczykowski9a276e62022-05-27 19:38:15 +000039 unsigned kver_major = 0;
40 unsigned kver_minor = 0;
41 unsigned kver_sub = 0;
42 (void)sscanf(buf.release, "%u.%u.%u", &kver_major, &kver_minor, &kver_sub);
Patrick Rohre7f26e22022-01-12 22:13:12 +010043 return KVER(kver_major, kver_minor, kver_sub);
44}
45
Maciej Żenczykowskieeb58872022-05-25 12:29:37 -070046static unsigned kernelVersion() {
47 static unsigned kver = uncachedKernelVersion();
48 return kver;
49}
50
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010051static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor,
52 unsigned sub) {
Patrick Rohre7f26e22022-01-12 22:13:12 +010053 return kernelVersion() >= KVER(major, minor, sub);
54}
55
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010056} // namespace android