| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | /* |
| 3 | * License: GPLv3+ |
| 4 | * Copyright (c) 2014 Davide Madrisan <[email protected]> |
| 5 | * |
| 6 | * A library for checking the version of a running Linux kernel |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <stdio.h> |
| 23 | #include <sys/utsname.h> |
| 24 | #include <linux/version.h> |
| 25 | |
| 26 | #include "messages.h" |
| 27 | |
| 28 | unsigned int |
| 29 | linux_version (void) |
| 30 | { |
| 31 | struct utsname utsbuf; |
| 32 | int x, y, z, depth; |
| 33 | |
| 34 | if (uname (&utsbuf) == -1) |
| 35 | plugin_error (STATE_UNKNOWN, errno, "uname() failed"); |
| 36 | |
| 37 | x = y = z = 0; |
| 38 | depth = sscanf(utsbuf.release, "%d.%d.%d", &x, &y, &z); |
| 39 | if ((depth < 2) || /* non-standard for all known kernels */ |
| 40 | ((depth < 3) && (x < 3))) /* non-standard for 2.x.x kernels */ |
| 41 | plugin_error (STATE_UNKNOWN, 0, "non-standard kernel version: %s", |
| 42 | utsbuf.release); |
| 43 | |
| 44 | return KERNEL_VERSION (x, y, z); |
| 45 | } |