From 3c2377ca159861e6eb8c08f68e80292babcb5473 Mon Sep 17 00:00:00 2001 From: Craig Small Date: Mon, 9 Jan 2012 21:57:44 +1100 Subject: [PATCH] Linux version for non-linux systems On non-linux systems, uts.version provides the version of that specific kernel (FreeBSD or Hurd version, for example) and not the emulated procfs pseudo version. On those systems we need to directly read /proc/version and parse the string. This change replaces Debian patches gnu-kbsd-version and complain_unmounted_proc patches. --- proc/version.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/proc/version.c b/proc/version.c index e7139cc4..593f4f4e 100644 --- a/proc/version.c +++ b/proc/version.c @@ -28,20 +28,43 @@ void display_version(void) { int linux_version_code; void init_Linux_version(void) { - static struct utsname uts; 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 + FILE *fp; + char buf[256]; + + if ( (fp=fopen("/proc/version","r")) == NULL) { + fprintf(stderr, "Cannot find /proc/version - is /proc mounted?\n"); + exit(1); + } + if (fgets(buf, 256, fp) == NULL) { + fprintf(stderr, "Cannot read kernel version from /proc/version\n"); + fclose(fp); + exit(1); + } + fclose(fp); + version_string_depth = sscanf(buf, "Linux version %d.%d.%d", &x, &y, &z); +#endif /* __linux__ */ 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 + "%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); }