Skip to contentSkip to conversation

Uname and kernel version check cleanup

PATCH New Peter Rajnoha 1 participant 1 message Jun 3, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Uname and kernel version check cleanup

This is just a little cleanup for uname and kernel version check - uname is called centrally. There's also a new internal function defined: kernel_version_higher_or_equal. It uses statically stored value of the current kernel version for comparison (and uses former logic of KERNEL_VERSION macro), so we don't need to bother calling uname all over the place again and again.

(The function is defined twice actually - one in libdevmapper, the other one in lvm - no need to export this from libdevmapper I think, it would be odd...)

Peter

---
 daemons/clvmd/clvmd-command.c |    4 +-
 daemons/clvmd/clvmd-gulm.c    |    3 +-
 daemons/clvmd/clvmd.c         |    4 +-
 daemons/clvmd/tcp-comms.c     |    3 +-
 lib/commands/toolcontext.c    |    5 +---
 lib/format_text/export.c      |   30 ++++--------------------
 lib/mirror/mirrored.c         |    8 +-----
 lib/misc/lvm-wrappers.c       |   49 +++++++++++++++++++++++++++++++++++++++
 lib/misc/lvm-wrappers.h       |    6 +++++
 lib/misc/util.h               |    2 -
 libdm/ioctl/libdm-iface.c     |   51 +++++++++++++++++++++++++----------------
 libdm/libdm-common.h          |    3 ++
 libdm/libdm-deptree.c         |   19 +++++----------
 13 files changed, 108 insertions(+), 79 deletions(-)
daemons/clvmd/clvmd-command.c +2 -2
index 5e3b8c1..77b6691 100644
@@ -56,7 +56,6 @@
 #include <configure.h>
 #include <pthread.h>
 #include <sys/types.h>
-#include <sys/utsname.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -74,6 +73,7 @@
 #include "locking.h"
 #include "lvm-logging.h"
 #include "lvm-functions.h"
+#include "lvm-wrappers.h"
 #include "clvmd-comms.h"
 #include "clvm.h"
 #include "clvmd.h"
@@ -111,7 +111,7 @@ int do_command(struct local_client *client, struct clvm_header *msg, int msglen,
 			*buf = new_buf;
 		}
 		if (*buf) {
-			uname(&nodeinfo);
+			get_uts(&nodeinfo);
 			*retlen = 1 + snprintf(*buf, buflen,
 					       "TEST from %s: %s v%s",
 					       nodeinfo.nodename, args,
daemons/clvmd/clvmd-gulm.c +1 -2
index 54dce4d..32081ec 100644
@@ -26,7 +26,6 @@
 
 #include <pthread.h>
 #include <sys/types.h>
-#include <sys/utsname.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -272,7 +271,7 @@ static void drop_expired_locks(char *nodename)
 
     if (!nodename)
     {
-	uname(&nodeinfo);
+	get_uts(&nodeinfo);
 	nodename = nodeinfo.nodename;
     }
 
daemons/clvmd/clvmd.c +2 -2
index 8dbd6ae..2e8eb51 100644
@@ -31,7 +31,6 @@
 #include <sys/un.h>
 #include <sys/time.h>
 #include <sys/ioctl.h>
-#include <sys/utsname.h>
 #include <netinet/in.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -50,6 +49,7 @@
 
 #include "clvmd-comms.h"
 #include "lvm-functions.h"
+#include "lvm-wrappers.h"
 #include "clvm.h"
 #include "lvm-version.h"
 #include "clvmd.h"
@@ -476,7 +476,7 @@ int main(int argc, char *argv[])
 	DEBUGLOG("Cluster ready, doing some more initialisation\n");
 
 	/* Save our CSID */
-	uname(&nodeinfo);
+	get_uts(&nodeinfo);
 	clops->get_our_csid(our_csid);
 
 	/* Initialise the FD list head */
daemons/clvmd/tcp-comms.c +1 -2
index 95fb351..8d6e077 100644
@@ -26,7 +26,6 @@
 #include <configure.h>
 #include <pthread.h>
 #include <sys/types.h>
-#include <sys/utsname.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
@@ -436,7 +435,7 @@ static int get_our_ip_address(char *addr, int *family)
 {
 	struct utsname info;
 
-	uname(&info);
+	get_uts(&info);
 	get_ip_address(info.nodename, addr);
 
 	return 0;
lib/commands/toolcontext.c +1 -4
index 7c5e379..d957cc4 100644
@@ -50,7 +50,6 @@
 
 #include <locale.h>
 #include <sys/stat.h>
-#include <sys/utsname.h>
 #include <syslog.h>
 #include <time.h>
 
@@ -1000,10 +999,8 @@ static int _init_hostname(struct cmd_context *cmd)
 {
 	struct utsname uts;
 
-	if (uname(&uts)) {
-		log_sys_error("uname", "_init_hostname");
+	if (!get_uts(&uts))
 		return 0;
-	}
 
 	if (!(cmd->hostname = dm_pool_strdup(cmd->libmem, uts.nodename))) {
 		log_error("_init_hostname: dm_pool_strdup failed");
lib/format_text/export.c +5 -25
index 5f02b5d..36be8b9 100644
@@ -24,7 +24,6 @@
 
 #include <stdarg.h>
 #include <time.h>
-#include <sys/utsname.h>
 
 struct formatter;
 typedef int (*out_with_comment_fn) (struct formatter * f, const char *comment,
@@ -68,23 +67,6 @@ struct formatter {
 	int header;		/* 1 => comments at start; 0 => end */
 };
 
-static struct utsname _utsname;
-
-static void _init(void)
-{
-	static int _initialised = 0;
-
-	if (_initialised)
-		return;
-
-	if (uname(&_utsname)) {
-		log_error("uname failed: %s", strerror(errno));
-		memset(&_utsname, 0, sizeof(_utsname));
-	}
-
-	_initialised = 1;
-}
-
 /*
  * Formatting functions.
  */
@@ -326,6 +308,9 @@ static int _print_header(struct formatter *f,
 {
 	char *buf;
 	time_t t;
+	struct utsname uts;
+
+	get_uts(&uts);
 
 	t = time(NULL);
 
@@ -341,9 +326,8 @@ static int _print_header(struct formatter *f,
 	}
 	outf(f, "description = \"%s\"", escape_double_quotes(buf, desc));
 	outnl(f);
-	outf(f, "creation_host = \"%s\"\t# %s %s %s %s %s", _utsname.nodename,
-	     _utsname.sysname, _utsname.nodename, _utsname.release,
-	     _utsname.version, _utsname.machine);
+	outf(f, "creation_host = \"%s\"\t# %s %s %s %s %s", uts.nodename,
+	     uts.sysname, uts.nodename, uts.release, uts.version, uts.machine);
 	outf(f, "creation_time = %lu\t# %s", t, ctime(&t));
 
 	return 1;
@@ -733,8 +717,6 @@ int text_vg_export_file(struct volume_group *vg, const char *desc, FILE *fp)
 	struct formatter *f;
 	int r;
 
-	_init();
-
 	if (!(f = dm_malloc(sizeof(*f))))
 		return_0;
 
@@ -758,8 +740,6 @@ int text_vg_export_raw(struct volume_group *vg, const char *desc, char **buf)
 	struct formatter *f;
 	int r = 0;
 
-	_init();
-
 	if (!(f = dm_malloc(sizeof(*f))))
 		return_0;
 
lib/mirror/mirrored.c +1 -7
index eca0377..dac97b1 100644
@@ -28,8 +28,6 @@
 #include "sharedlib.h"
 #include "str_list.h"
 
-#include <sys/utsname.h>
-
 #ifdef DMEVENTD
 #  include "libdevmapper-event.h"
 #endif
@@ -473,8 +471,6 @@ static int _mirrored_target_present(struct cmd_context *cmd,
 	uint32_t maj, min, patchlevel;
 	unsigned maj2, min2, patchlevel2;
 	char vsn[80];
-	struct utsname uts;
-	unsigned kmaj, kmin, krel;
 
 	if (!_mirrored_checked) {
 		_mirrored_present = target_present(cmd, "mirror", 1);
@@ -512,9 +508,7 @@ static int _mirrored_target_present(struct cmd_context *cmd,
 			 * The dm-log-userspace module was added to the
 			 * 2.6.31 kernel.
 			 */
-			if (!uname(&uts) &&
-			    (sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel) == 3) &&
-			    KERNEL_VERSION(kmaj, kmin, krel) < KERNEL_VERSION(2, 6, 31)) {
+			if (kernel_version_higher_or_equal(2, 6, 31) > 0) {
 				if (module_present(cmd, "log-clustered"))
 					_mirror_attributes |= MIRROR_LOG_CLUSTERED;
 			} else if (module_present(cmd, "log-userspace"))
lib/misc/lvm-wrappers.c +49 -0

55 lines not on this page · raw message

lib/misc/lvm-wrappers.h +6 -0
index 19a7f03..9ea2756 100644
@@ -16,6 +16,12 @@
 #ifndef _LVM_WRAPPERS_H
 #define _LVM_WRAPPERS_H
 
+#include <sys/utsname.h>
+
+int kernel_version_higher_or_equal(unsigned major, unsigned minor,
+				   unsigned release);
+int get_uts(struct utsname *uts);
+
 int lvm_getpagesize(void);
 
 /*
lib/misc/util.h +0 -2
index fcda369..15d2662 100644
@@ -27,6 +27,4 @@
 
 #define uninitialized_var(x) x = x
 
-#define KERNEL_VERSION(major, minor, release) (((major) << 16) + ((minor) << 8) + (release))
-
 #endif
libdm/ioctl/libdm-iface.c +31 -20

105 lines not on this page · raw message

libdm/libdm-common.h +3 -0
index fcb334f..0b1ffbd 100644
@@ -18,6 +18,9 @@
 
 #include "libdevmapper.h"
 
+int kernel_version_higher_or_equal(unsigned major, unsigned minor,
+				   unsigned release);
+
 struct target *create_target(uint64_t start,
 			     uint64_t len,
 			     const char *type, const char *params);
libdm/libdm-deptree.c +6 -13
index e1813b4..1e3085d 100644
@@ -20,7 +20,6 @@
 
 #include <stdarg.h>
 #include <sys/param.h>
-#include <sys/utsname.h>
 
 #define MAX_TARGET_PARAMSIZE 500000
 
@@ -1535,17 +1534,10 @@ static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
 	int block_on_error = 0;
 	int handle_errors = 0;
 	int dm_log_userspace = 0;
-	struct utsname uts;
 	unsigned log_parm_count;
 	int pos = 0;
 	char logbuf[DM_FORMAT_DEV_BUFSIZE];
 	const char *logtype;
-	unsigned kmaj, kmin, krel;
-
-	if (uname(&uts) == -1 || sscanf(uts.release, "%u.%u.%u", &kmaj, &kmin, &krel) != 3) {
-		log_error("Cannot read kernel release version");
-		return 0;
-	}
 
 	if ((seg->flags & DM_BLOCK_ON_ERROR)) {
 		/*
@@ -1558,9 +1550,10 @@ static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
 		 * "handle_errors" by the dm-mirror module's version
 		 * number (>= 1.12) or by the kernel version (>= 2.6.22).
 		 */
-		if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 22))
-			handle_errors = 1;
-		else
+		if ((handle_errors = kernel_version_higher_or_equal(2, 6, 22)) < 0)
+			return 0;
+
+		if (!handle_errors)
 			block_on_error = 1;
 	}
 
@@ -1577,8 +1570,8 @@ static int _mirror_emit_segment_line(struct dm_task *dmt, uint32_t major,
 		 * The dm-log-userspace module was added to the
 		 * 2.6.31 kernel.
 		 */
-		if (KERNEL_VERSION(kmaj, kmin, krel) >= KERNEL_VERSION(2, 6, 31))
-			dm_log_userspace = 1;
+		if ((dm_log_userspace = kernel_version_higher_or_equal(2, 6, 31)) < 0)
+			return 0;
 	}
 
 	/* Region size */