blob: 3be1ad293fd9ba401309567a8bf02251face11e1 [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
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010035static inline unsigned kernelVersion() {
Patrick Rohre7f26e22022-01-12 22:13:12 +010036 struct utsname buf;
37 int ret = uname(&buf);
38 if (ret)
39 return 0;
40
41 unsigned kver_major;
42 unsigned kver_minor;
43 unsigned kver_sub;
44 char discard;
45 ret = sscanf(buf.release, "%u.%u.%u%c", &kver_major, &kver_minor, &kver_sub,
46 &discard);
47 // Check the device kernel version
48 if (ret < 3)
49 return 0;
50
51 return KVER(kver_major, kver_minor, kver_sub);
52}
53
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010054static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor,
55 unsigned sub) {
Patrick Rohre7f26e22022-01-12 22:13:12 +010056 return kernelVersion() >= KVER(major, minor, sub);
57}
58
Patrick Rohr0c34e9a02022-01-17 13:59:09 +010059} // namespace android