Patrick Rohr | e7f26e2 | 2022-01-12 22:13:12 +0100 | [diff] [blame] | 1 | /* |
| 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 Rohr | 0c34e9a0 | 2022-01-17 13:59:09 +0100 | [diff] [blame] | 26 | #pragma once |
| 27 | |
Patrick Rohr | e7f26e2 | 2022-01-12 22:13:12 +0100 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <sys/utsname.h> |
| 30 | |
| 31 | #define KVER(a, b, c) (((a) << 24) + ((b) << 16) + (c)) |
| 32 | |
| 33 | namespace android { |
| 34 | |
Patrick Rohr | 0c34e9a0 | 2022-01-17 13:59:09 +0100 | [diff] [blame] | 35 | static inline unsigned kernelVersion() { |
Patrick Rohr | e7f26e2 | 2022-01-12 22:13:12 +0100 | [diff] [blame] | 36 | 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 Rohr | 0c34e9a0 | 2022-01-17 13:59:09 +0100 | [diff] [blame] | 54 | static inline bool isAtLeastKernelVersion(unsigned major, unsigned minor, |
| 55 | unsigned sub) { |
Patrick Rohr | e7f26e2 | 2022-01-12 22:13:12 +0100 | [diff] [blame] | 56 | return kernelVersion() >= KVER(major, minor, sub); |
| 57 | } |
| 58 | |
Patrick Rohr | 0c34e9a0 | 2022-01-17 13:59:09 +0100 | [diff] [blame] | 59 | } // namespace android |