configure.ac: add a check for linux/mempolicy.h
testcases/kernel/hotplug/...:
- Enable memory hotplug build and install
- Cordon off mempolicy / numa dependent logic on systems where it isn't available for compile / linux.
- Remove ad hoc / mostly incorrect move_mempages logic.

Signed-off-by: Garrett Cooper <yanegomi@gmail.com>
diff --git a/configure.ac b/configure.ac
index eeee355..52f3381 100644
--- a/configure.ac
+++ b/configure.ac
@@ -36,6 +36,7 @@
     mm.h \
     pthread.h \
     linux/genetlink.h \
+    linux/mempolicy.h \
     linux/module.h \
     linux/netlink.h \
     sys/acl.h \
diff --git a/m4/ltp-numa.m4 b/m4/ltp-numa.m4
index 375a011..e84a23e 100644
--- a/m4/ltp-numa.m4
+++ b/m4/ltp-numa.m4
@@ -24,8 +24,8 @@
 dnl
 AC_DEFUN([LTP_CHECK_SYSCALL_NUMA],
 [dnl
-AC_CHECK_HEADERS(numa.h,[
-	LTP_SYSCALL_NUMA_HEADER=yes
+AC_CHECK_HEADERS([numa.h numaif.h],[
+	LTP_SYSCALL_NUMA_HEADERS=yes
 	AC_CHECK_FUNCS(numa_alloc_onnode,numa_move_pages)
 ]
 	AC_CHECK_LIB(numa,numa_available,[
diff --git a/testcases/kernel/hotplug/Makefile b/testcases/kernel/hotplug/Makefile
new file mode 100644
index 0000000..0cde12b
--- /dev/null
+++ b/testcases/kernel/hotplug/Makefile
@@ -0,0 +1,26 @@
+#
+#    kernel/hotplug test suite Makefile.
+#
+#    Copyright (C) 2009, Cisco Systems Inc.
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program 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 General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License along
+#    with this program; if not, write to the Free Software Foundation, Inc.,
+#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Garrett Cooper, December 2009
+#
+
+top_srcdir	?= ../../..
+
+include $(top_srcdir)/include/mk/env_pre.mk
+include $(top_srcdir)/include/mk/generic_trunk_target.mk
diff --git a/testcases/kernel/hotplug/memory_hotplug/Makefile b/testcases/kernel/hotplug/memory_hotplug/Makefile
index f7965ea..894e5d4 100644
--- a/testcases/kernel/hotplug/memory_hotplug/Makefile
+++ b/testcases/kernel/hotplug/memory_hotplug/Makefile
@@ -1,47 +1,35 @@
-# Makefile for linux memory toy/tool
 #
-SHELL   = /bin/sh
+#    kernel/hotplug/memory_hotplug test suite Makefile.
+#
+#    Copyright (C) 2009, Cisco Systems Inc.
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; either version 2 of the License, or
+#    (at your option) any later version.
+#
+#    This program 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 General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License along
+#    with this program; if not, write to the Free Software Foundation, Inc.,
+#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Garrett Cooper, July 2009
+#
 
-MACH    =
+top_srcdir	?= ../../../..
 
-CMODE	= -std=gnu99
-COPT	= $(CMODE) -O2 #-non_shared
-DEFS    = -D_GNU_SOURCE -D_NEED_MIGRATE_PAGES
-INCLS   =  #-I
-CFLAGS  = $(COPT) $(DEFS) $(INCLS) $(ECFLAGS)
+include $(top_srcdir)/include/mk/testcases.mk
 
-LDOPTS	= #-dnon_shared
-LDLIBS	= -lnuma
-LDFLAGS = $(CMODE) $(LDOPTS) $(ELDFLAGS)
+CPPFLAGS	+= -I$(abs_srcdir)
 
-HDRS    = memtoy.h segment.h migrate_pages.h
+LDLIBS		+= $(NUMA_LIBS)
 
-OBJS    = memtoy.o commands.o segment.o migrate_pages.o
+MAKE_TARGETS	:= memtoy
 
-PROGS	= memtoy
+memtoy: $(patsubst $(abs_srcdir)/%.c,%.o,$(wildcard $(abs_srcdir)/*.c))
 
-#---------------------------------
-
-all:    $(PROGS)
-
-memtoy:  $(OBJS)
-	$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LDLIBS)
-
-$(OBJS):    $(HDRS)
-
-
-install:
-	@echo "install not implemented"
-
-clean:
-	-rm -f *.o core.[0-9]* Log* cscope.*
-
-clobber: clean
-	-rm -f  $(PROGS)
-
-# ------------------------------------------------
-TARBALL=memtoy-$$(cat $$_WD/version.h|grep _VERSION|sed 's/^.* "\([0-9.a-z]*\)".*$$/\1/').tar.gz
-tarball:  clobber
-	_WD=`pwd`; \
-	_WD=`basename $$_WD`; \
-	cd ..; tar czf - $$_WD  >$(TARBALL)
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/hotplug/memory_hotplug/commands.c b/testcases/kernel/hotplug/memory_hotplug/commands.c
index 29aec1a..39502f4 100644
--- a/testcases/kernel/hotplug/memory_hotplug/commands.c
+++ b/testcases/kernel/hotplug/memory_hotplug/commands.c
@@ -27,10 +27,12 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
+
+#include "config.h"
+#if HAVE_NUMA_H && HAVE_NUMAIF_H && LINUX_MEMPOLICY_H
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/mman.h>
-
 #include <ctype.h>
 #include <errno.h>
 #include <numa.h>
@@ -42,7 +44,8 @@
 #include <unistd.h>
 
 #include "memtoy.h"
-#include "migrate_pages.h"
+#include "test.h"
+#include "linux_syscall_numbers.h"
 
 #define CMD_SUCCESS 0
 #define CMD_ERROR   1
@@ -429,6 +432,10 @@
 static int
 get_current_nodeid_list(unsigned int *fromids)
 {
+	/*
+	 * FIXME (garrcoop): gcp is unitialized and shortly hereafter used in
+	 * an initialization statement..... UHHHHHHH... test writer fail?
+	 */
 	glctx_t    *gcp;
 	nodemask_t my_allowed_nodes;
 	int        nr_nodes = 0, max_node = gcp->numa_max_node;
@@ -446,11 +453,15 @@
 	 */
 	if (nr_nodes == 0)
 		fprintf(stderr, "%s:  my allowed node mask is empty !!???\n",
-
 			gcp->program_name);
 	return nr_nodes;
 }
 
+/*
+ * NOTE (garrcoop): Get rid of an -Wunused warning. This wasn't deleted because
+ * I don't know what the original intent was for this code.
+ */
+#if 0
 static void
 not_implemented()
 {
@@ -459,6 +470,7 @@
 	fprintf(stderr, "%s:  %s not implemented yet\n",
 		gcp->program_name, gcp->cmd_name);
 }
+#endif
 
 /*
  * =========================================================================
@@ -521,7 +533,7 @@
 
 		if (do_header) {
 			do_header = false;
-			printf(numa_header);
+			puts(numa_header);
 		}
 		printf("  %3d  %9ld      %8ld\n", node,
 			 node_size/(1024*1024), node_free/(1024*1024));
@@ -619,7 +631,7 @@
 	}
 
 	gettimeofday(&t_start, NULL);
-	nr_migrated = migrate_pages(getpid(), nr_from, fromids, toids);
+	nr_migrated = syscall(__NR_migrate_pages, getpid(), nr_from, fromids, toids);
 	if (nr_migrated < 0) {
 		int err = errno;
 		fprintf(stderr, "%s: migrate_pages failed - %s\n",
@@ -752,7 +764,7 @@
 
 		segment_remove(segname);
 	}
-
+	return 0;
 }
 
 /*
@@ -1174,8 +1186,10 @@
 	if (*args != '\0') {
 		cmd    = strtok_r(args, whitespace, &nextarg);
 		cmdlen = strlen(cmd);
-	} else
+	} else {
 		cmd = NULL;
+		cmdlen = 0;
+	}
 
 	for( cmdp = cmd_table; cmdp->cmd_name != NULL; ++cmdp) {
 		if (cmd == NULL ||
@@ -1319,3 +1333,4 @@
 
 	} while(1);
 }
+#endif
diff --git a/testcases/kernel/hotplug/memory_hotplug/memtoy.c b/testcases/kernel/hotplug/memory_hotplug/memtoy.c
index 9ccbc23..cb4dbbf 100644
--- a/testcases/kernel/hotplug/memory_hotplug/memtoy.c
+++ b/testcases/kernel/hotplug/memory_hotplug/memtoy.c
@@ -22,19 +22,21 @@
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
+#include <stdio.h>
+#include "config.h"
+/* Shortcut because the test requires numa and mempolicy support. */
+#if HAVE_NUMA_H && HAVE_NUMAIF_H && LINUX_MEMPOLICY_H
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/mman.h>
-
+#include <libgen.h>
 #include <errno.h>
 #include <numa.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-
 #include "memtoy.h"
 
 /*
@@ -496,7 +498,7 @@
 	 */
 	printf("memtoy pid:  %d\n", getpid());
 	vprint("%s:  pagesize = %d\n", gcp->program_name, gcp->pagesize);
-	if (gcp->numa_max_node >= 0)
+	if (gcp->numa_max_node >= 0) 
 		vprint("%s:  NUMA available - max node: %d\n",
 			gcp->program_name, gcp->numa_max_node);
 
@@ -504,6 +506,12 @@
 
 	process_commands();
 
-	exit(0);
+	return 0;
 
 }
+#else	/* ! (HAVE_NUMA_H && HAVE_NUMAIF_H) */
+int main(void) {
+	printf("System doesn't have required numa support.\n");
+	return 0;
+}
+#endif	/* HAVE_NUMA_H && HAVE_NUMAIF_H */
diff --git a/testcases/kernel/hotplug/memory_hotplug/memtoy.h b/testcases/kernel/hotplug/memory_hotplug/memtoy.h
index 5db2037..fed6a5a 100644
--- a/testcases/kernel/hotplug/memory_hotplug/memtoy.h
+++ b/testcases/kernel/hotplug/memory_hotplug/memtoy.h
@@ -26,8 +26,6 @@
 #include <sys/types.h>
 #include <sys/time.h>
 
-#include <numa.h>
-#include <numaif.h>
 #include <setjmp.h>
 #include <signal.h>
 
diff --git a/testcases/kernel/hotplug/memory_hotplug/migrate_pages.c b/testcases/kernel/hotplug/memory_hotplug/migrate_pages.c
deleted file mode 100644
index a5d64ff..0000000
--- a/testcases/kernel/hotplug/memory_hotplug/migrate_pages.c
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- *  Copyright (c) 2005 Silicon Graphics, Inc.
- *  All rights reserved.
- *
- *    Version 2 Jun 2005
- *
- *    Ray Bryant <raybry@sgi.com>
- *
- * Additions:  Lee Schermerhorn <lee.schermerhorn@hp.com>
- */
-
-#ifdef _NEED_MIGRATE_PAGES
-#include <sys/types.h>
-
-#include <stdio.h>
-#include <unistd.h>             /* For __NR_ni_syscall */
-#include <errno.h>
-
-/*
- * syscall numbers keep changings as we track newer kernels.
-TODO:  x86_64 testing -- arch dependent sys call #s
- */
-#define OLD_migrate_pages 1279	/* <= 2.6.13 */
-#ifndef __NR_migrate_pages
-#define __NR_migrate_pages 1280 // OLD_migrate_pages
-#endif
-
-int
-migrate_pages(const pid_t pid, int count, unsigned int *old_nodes, unsigned int *new_nodes)
-{
-	int ret;
-
-	ret = syscall(__NR_migrate_pages, pid, count, old_nodes, new_nodes);
-
-	/*
-	 *  If not implemented, maybe is because we're running a newer build
-	 *  of program on older kernel?  Try the old sys call #
-	 */
-	if (ret == ENOSYS && __NR_migrate_pages != OLD_migrate_pages) {
-fprintf(stderr, "%s:  trying old sys call # %d\n", __FUNCTION__, OLD_migrate_pages);
-		ret = syscall(OLD_migrate_pages, pid, count, old_nodes, new_nodes);
-	}
-
-// fprintf(stderr, "%s:  migrate_pages() returns  %d, errno = %d\n", __FUNCTION__, ret, errno);
-	return ret;
-}
-#endif
diff --git a/testcases/kernel/hotplug/memory_hotplug/migrate_pages.h b/testcases/kernel/hotplug/memory_hotplug/migrate_pages.h
deleted file mode 100644
index 4d8a7dd..0000000
--- a/testcases/kernel/hotplug/memory_hotplug/migrate_pages.h
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * memtoy - migrate_pages.h - interface to migrate_pages() system call
- *
- * until merged into upstream kernels [he says hopefully].
- */
-#ifndef _MEMTOY_MIGRATE_PAGES_
-#define _MEMTOY_MIGRATE_PAGES_
-
-#ifdef _NEED_MIGRATE_PAGES
-extern int migrate_pages(const pid_t, int, unsigned int*, unsigned int*);
-#endif
-
-// temporary?  
-#ifndef MPOL_MF_MOVE
-#define MPOL_MF_MOVE    (1<<1)  /* Move existing pages, if possible */
-#endif
-#ifndef MPOL_MF_WAIT
-#define MPOL_MF_WAIT    (1<<2)  /* Wait for existing pages to migrate */
-#endif
-
-#endif
diff --git a/testcases/kernel/hotplug/memory_hotplug/segment.c b/testcases/kernel/hotplug/memory_hotplug/segment.c
index 6e188cd..48bbe9e 100644
--- a/testcases/kernel/hotplug/memory_hotplug/segment.c
+++ b/testcases/kernel/hotplug/memory_hotplug/segment.c
@@ -26,17 +26,20 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
+
+#include "config.h"
+#if HAVE_NUMA_H && HAVE_NUMAIF_H && LINUX_MEMPOLICY_H
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/mman.h>
 #include <sys/shm.h>
 #include <sys/stat.h>
 #include <sys/time.h>
-
 #include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
 #include <numa.h>
+#include <numaif.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -124,7 +127,7 @@
 	/*
 	 * simple linear scan for first available slot
 	 */
-	for (segpp = gcp->seglist; segp = *segpp; ++segpp) {
+	for (segpp = gcp->seglist; (segp = *segpp); ++segpp) {
 		if (segp->seg_type == SEGT_NONE)
 			return segp;
 	}
@@ -213,7 +216,7 @@
 	if (segpp == NULL)
 		return;
 
-	for (; segp = *segpp; ++segpp) {
+	for (; (segp = *segpp); ++segpp) {
 		if (segp->seg_type != SEGT_SHM) {
 			continue;
 		}
@@ -472,7 +475,7 @@
 	segp->seg_start = shmat(segp->seg_shmid, NULL, 0);
 	if (segp->seg_start == MAP_FAILED) {
 		int err = errno;
-		fprintf(stderr, "%s:  failed to attach shm segment %s\n",
+		fprintf(stderr, "%s:  failed to attach shm segment %s: %s\n",
 			gcp->program_name, segp->seg_name,
 			strerror(err));
 		return SEG_ERR;
@@ -499,7 +502,7 @@
 	glctx_t   *gcp = &glctx;
 	segment_t *segp, **segpp;
 
-	for (segpp = gcp->seglist; segp = *segpp; ++segpp) {
+	for (segpp = gcp->seglist; (segp = *segpp); ++segpp) {
 		if (segp->seg_type == SEGT_NONE) {
 			if (gcp->seg_avail == NULL)
 				gcp->seg_avail = *segpp;
@@ -589,7 +592,6 @@
 static int
 show_one_segment(segment_t *segp, bool header)
 {
-	glctx_t   *gcp = &glctx;
 	char *protection, *share, *name;
 
 	switch (segp->seg_prot & (PROT_READ|PROT_WRITE)) {
@@ -618,10 +620,10 @@
 	name = (segp->seg_type == SEGT_FILE) ? segp->seg_path : segp->seg_name;
 
 	if (header)
-		printf(segment_header);
+		puts(segment_header);
 
 	if (segp->seg_start != MAP_FAILED) {
-		printf("%c 0x%016lx 0x%012lx 0x%012lx  %s  %s %s\n",
+		printf("%c 0x%p 0x%012lx 0x%012lx  %s  %s %s\n",
 			seg_type[segp->seg_type],
 			segp->seg_start,
 			segp->seg_length,
@@ -663,7 +665,7 @@
 	 * show all
 	 */
 	header = true;
-	for(segpp = gcp->seglist; segp = *segpp; ++segpp) {
+	for(segpp = gcp->seglist; (segp = *segpp); ++segpp) {
 		if (segp->seg_type != SEGT_NONE) {
 			show_one_segment(segp, header);
 			header = false;		/* first time only */
@@ -780,6 +782,9 @@
 	case SEGT_SHM:
 			//TODO:  shmdt()...
 		break;
+	/* Handle default to get rid of -Wswitch-enum */
+	default:
+		break;
 	}
 
 	segp->seg_start = MAP_FAILED;
@@ -830,6 +835,9 @@
 	case SEGT_SHM:
 		return map_shm_segment(segp);
 		break;
+	/* Handle default to get rid of -Wswitch-enum */
+	default:
+		break;
 	}
 
 	return SEG_ERR;	/* unrecognized segment type -- shouldn't happen */
@@ -963,7 +971,7 @@
 		 * start partial line
 		 */
 		int pgid2 = pgid & ~PPL_MASK;
-		printf("%12lx: ", pgid2);
+		printf("%12x: ", pgid2);
 		while (pgid2 < pgid) {
 			printf("    ");
 			++pgid2;
@@ -986,7 +994,7 @@
 		if ((pgid & PPL_MASK) == 0) {
 			if (need_nl)
 				printf("\n");
-			printf("%12lx: ", pgid);	/* start a new line */
+			printf("%12x: ", pgid);	/* start a new line */
 			need_nl = true;
 		}
 		printf(" %3d", node);
@@ -1000,3 +1008,4 @@
 
 	return SEG_OK;
 }
+#endif