library: Change linux version
Added function procps_linux_version() which used to be an exported integer instead. Also changed the method of obtaining the linux version (more correctly the os release) to use a specific procfs entry. This works for both Linux and FreeBSD.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
/*
|
||||
* Suite version information for procps-ng utilities
|
||||
* Copyright (c) 1995 Martin Schulze <joey@infodrom.north.de>
|
||||
* Amended by cblake to only export the function symbol.
|
||||
* libprocps - Library to read proc filesystem
|
||||
*
|
||||
* Modified by Albert Cahalan, ????-2003
|
||||
* Copyright (C) 1995 Martin Schulze <joey@infodrom.north.de>
|
||||
* Copyright (C) 1996 Charles Blake <cblake@bbn.com>
|
||||
* Copyright (C) 2003 Albert Cahalan
|
||||
* Copyright (C) 2015 Craig Small <csmall@enc.com.au>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
@@ -19,59 +20,44 @@
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "procps-private.h"
|
||||
#include "version.h"
|
||||
|
||||
/* Linux kernel version information for procps-ng utilities
|
||||
* Copyright (c) 1996 Charles Blake <cblake@bbn.com>
|
||||
#define PROCFS_OSRELEASE "/proc/sys/kernel/osrelease"
|
||||
|
||||
/*
|
||||
* procps_linux_version
|
||||
*
|
||||
* Return the current running Linux version release as shown in
|
||||
* 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
|
||||
*/
|
||||
#include <sys/utsname.h>
|
||||
|
||||
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
|
||||
|
||||
int linux_version_code;
|
||||
|
||||
void init_Linux_version(void) {
|
||||
int x = 0, y = 0, z = 0; /* cleared in case sscanf() < 3 */
|
||||
int version_string_depth;
|
||||
|
||||
#ifdef __linux__
|
||||
static struct utsname uts;
|
||||
|
||||
if (uname(&uts) == -1) /* failure implies impending death */
|
||||
exit(1);
|
||||
|
||||
version_string_depth = sscanf(uts.release, "%d.%d.%d", &x, &y, &z);
|
||||
#else
|
||||
PROCPS_EXPORT int procps_linux_version(void)
|
||||
{
|
||||
FILE *fp;
|
||||
char buf[256];
|
||||
unsigned int x = 0, y = 0, z = 0;
|
||||
int version_string_depth;
|
||||
|
||||
if ( (fp=fopen("/proc/version","r")) == NULL) {
|
||||
fprintf(stderr, "Cannot find /proc/version - is /proc mounted?\n");
|
||||
exit(1);
|
||||
}
|
||||
if ((fp = fopen(PROCFS_OSRELEASE, "r")) == NULL)
|
||||
return -errno;
|
||||
if (fgets(buf, 256, fp) == NULL) {
|
||||
fprintf(stderr, "Cannot read kernel version from /proc/version\n");
|
||||
fclose(fp);
|
||||
exit(1);
|
||||
fclose(fp);
|
||||
return -EIO;
|
||||
}
|
||||
fclose(fp);
|
||||
version_string_depth = sscanf(buf, "Linux version %d.%d.%d", &x, &y, &z);
|
||||
#endif /* __linux__ */
|
||||
|
||||
version_string_depth = sscanf(buf, "%u.%u.%u", &x, &y, &z);
|
||||
if ((version_string_depth < 2) || /* Non-standard for all known kernels */
|
||||
((version_string_depth < 3) && (x < 3))) /* Non-standard for 2.x.x kernels */
|
||||
#ifdef __linux__
|
||||
fprintf(stderr, /* *very* unlikely to happen by accident */
|
||||
"Non-standard uts for running kernel:\n"
|
||||
"release %s=%d.%d.%d gives version code %d\n",
|
||||
uts.release, x, y, z, LINUX_VERSION(x,y,z));
|
||||
#else
|
||||
fprintf(stderr, /* *very* unlikely to happen by accident */
|
||||
"%s=%d.%d.%d gives version code %d\n",
|
||||
buf, x, y, z, LINUX_VERSION(x,y,z));
|
||||
#endif /* __linux__ */
|
||||
linux_version_code = LINUX_VERSION(x, y, z);
|
||||
return -ERANGE;
|
||||
return LINUX_VERSION(x,y,z);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user