2012-03-02 17:59:36 +05:30
|
|
|
/*
|
2023-02-07 15:39:48 +05:30
|
|
|
* libproc2 - Library to read proc filesystem
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
2023-02-07 15:39:48 +05:30
|
|
|
* Copyright © 2002-2023 Craig Small <csmall@dropbear.xyz>
|
|
|
|
* Copyright © 2013-2023 Jim Warner <james.warner@comcast.net>
|
|
|
|
* Copyright © 2011-2012 Sami Kerola <kerolasa@iki.fi>
|
|
|
|
* Copyright © 2002-2003 Albert Cahalan
|
|
|
|
* Copyright © 1996 Charles Blake <cblake@bbn.com>
|
|
|
|
* Copyright © 1995 Martin Schulze <joey@infodrom.north.de>
|
2002-12-15 06:00:17 +05:30
|
|
|
*
|
2012-03-02 17:59:36 +05:30
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-02-02 04:17:29 +05:30
|
|
|
*/
|
2015-06-19 16:30:46 +05:30
|
|
|
#include <errno.h>
|
2002-02-02 04:17:29 +05:30
|
|
|
#include <stdio.h>
|
2021-01-19 11:30:00 +05:30
|
|
|
#include "misc.h"
|
2015-06-19 16:30:46 +05:30
|
|
|
#include "procps-private.h"
|
2002-02-02 04:17:29 +05:30
|
|
|
|
2022-04-11 13:00:25 +05:30
|
|
|
#ifdef __CYGWIN__
|
|
|
|
#define PROCFS_OSRELEASE "/proc/version"
|
|
|
|
#define PROCFS_OSPATTERN "%*s version %u.%u.%u"
|
|
|
|
#else
|
2015-06-19 16:30:46 +05:30
|
|
|
#define PROCFS_OSRELEASE "/proc/sys/kernel/osrelease"
|
2022-04-11 13:00:25 +05:30
|
|
|
#define PROCFS_OSPATTERN "%u.%u.%u"
|
|
|
|
#endif
|
2013-03-31 10:30:00 +05:30
|
|
|
|
2015-06-19 16:30:46 +05:30
|
|
|
/*
|
|
|
|
* procps_linux_version
|
2015-06-19 19:11:51 +05:30
|
|
|
*
|
|
|
|
* Return the current running Linux version release as shown in
|
2015-06-19 16:30:46 +05:30
|
|
|
* the procps filesystem.
|
|
|
|
*
|
|
|
|
* There are three ways you can get OS release:
|
|
|
|
* 1) /proc/sys/kernel/osrelease - returns correct version of procfs
|
|
|
|
* 2) /proc/version - returns version of kernel e.g. BSD this is wrong
|
|
|
|
* 3) uname and uts.release - same as /proc/version field #3
|
|
|
|
*
|
|
|
|
* Returns: version as an integer
|
|
|
|
* Negative value means an error
|
|
|
|
*/
|
|
|
|
PROCPS_EXPORT int procps_linux_version(void)
|
|
|
|
{
|
2012-01-09 16:27:44 +05:30
|
|
|
FILE *fp;
|
|
|
|
char buf[256];
|
2015-06-19 16:30:46 +05:30
|
|
|
unsigned int x = 0, y = 0, z = 0;
|
|
|
|
int version_string_depth;
|
2012-01-09 16:27:44 +05:30
|
|
|
|
2015-06-19 16:30:46 +05:30
|
|
|
if ((fp = fopen(PROCFS_OSRELEASE, "r")) == NULL)
|
|
|
|
return -errno;
|
2012-01-09 16:27:44 +05:30
|
|
|
if (fgets(buf, 256, fp) == NULL) {
|
2015-06-19 16:30:46 +05:30
|
|
|
fclose(fp);
|
|
|
|
return -EIO;
|
2012-01-09 16:27:44 +05:30
|
|
|
}
|
|
|
|
fclose(fp);
|
2022-04-11 13:00:25 +05:30
|
|
|
version_string_depth = sscanf(buf, PROCFS_OSPATTERN, &x, &y, &z);
|
2011-06-20 21:24:06 +05:30
|
|
|
if ((version_string_depth < 2) || /* Non-standard for all known kernels */
|
|
|
|
((version_string_depth < 3) && (x < 3))) /* Non-standard for 2.x.x kernels */
|
2015-06-19 16:30:46 +05:30
|
|
|
return -ERANGE;
|
|
|
|
return LINUX_VERSION(x,y,z);
|
2002-02-02 04:17:29 +05:30
|
|
|
}
|