lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <2f11576a0802090755n123c9b7dh26e0af6a2fef28af@mail.gmail.com>
Date:	Sun, 10 Feb 2008 00:55:54 +0900
From:	"KOSAKI Motohiro" <kosaki.motohiro@...fujitsu.com>
To:	linux-mm@...ck.org, linux-kernel@...r.kernel.org
Cc:	kosaki.motohiro@...fujitsu.com,
	"Marcelo Tosatti" <marcelo@...ck.org>,
	"Daniel Spang" <daniel.spang@...il.com>,
	"Rik van Riel" <riel@...hat.com>,
	"Andrew Morton" <akpm@...ux-foundation.org>,
	"Alan Cox" <alan@...rguk.ukuu.org.uk>,
	linux-fsdevel@...r.kernel.org, "Pavel Machek" <pavel@....cz>,
	"Al Boldi" <a1426z@...ab.com>,
	"Jon Masters" <jonathan@...masters.org>, "Zan Lynx" <zlynx@....org>
Subject: [sample] mem_notify v6: usage example

this is usage example of /dev/mem_notify.

Daniel Spang create original version.
kosaki add fasync related code.


Signed-off-by: Daniel Spang <daniel.spang@...il.com>
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@...fujitsu.com>

---
 Documentation/mem_notify.c |  120 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)

Index: b/Documentation/mem_notify.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ b/Documentation/mem_notify.c	2008-02-10 00:44:00.000000000 +0900
@@ -0,0 +1,120 @@
+/*
+ * Allocate 10 MB each second. Exit on notification.
+ */
+
+#define _GNU_SOURCE
+
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <poll.h>
+#include <pthread.h>
+#include <errno.h>
+#include <signal.h>
+
+int count = 0;
+int size = 10;
+
+void *do_alloc()
+{
+        for(;;) {
+                int *buffer;
+                buffer = mmap(NULL,  size*1024*1024,
+                              PROT_READ | PROT_WRITE,
+                              MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+                if (buffer == MAP_FAILED) {
+                        perror("mmap");
+                        exit(EXIT_FAILURE);
+                }
+                memset(buffer, 1 , size*1024*1024);
+
+                printf("-");
+                fflush(stdout);
+
+                count++;
+                sleep(1);
+        }
+}
+
+int wait_for_notification(struct pollfd *pfd)
+{
+        int ret;
+        read(pfd->fd, 0, 0);
+        ret = poll(pfd, 1, -1);              /* wake up when low memory */
+        if (ret == -1 && errno != EINTR) {
+                perror("poll");
+                exit(EXIT_FAILURE);
+        }
+        return ret;
+}
+
+void do_free()
+{
+	int fd;
+	struct pollfd pfd;
+
+        fd = open("/dev/mem_notify", O_RDONLY);
+        if (fd == -1) {
+                perror("open");
+                exit(EXIT_FAILURE);
+        }
+
+	pfd.fd = fd;
+        pfd.events = POLLIN;
+        for(;;)
+                if (wait_for_notification(&pfd) > 0) {
+                        printf("\nGot notification, allocated %d MB\n",
+                               size * count);
+                        exit(EXIT_SUCCESS);
+                }
+}
+
+void do_free_signal()
+{
+	int fd;
+	int flags;
+
+        fd = open("/dev/mem_notify", O_RDONLY);
+        if (fd == -1) {
+                perror("open");
+                exit(EXIT_FAILURE);
+        }
+
+	fcntl(fd, F_SETOWN, getpid());
+	fcntl(fd, F_SETSIG, SIGUSR1);
+
+	flags = fcntl(fd, F_GETFL);
+	fcntl(fd, F_SETFL, flags|FASYNC); /* when low memory, receive SIGUSR1 */
+
+	for(;;)
+		sleep(1);
+}
+
+
+void daniel_exit(int signo)
+{
+	printf("\nGot notification %d, allocated %d MB\n",
+	       signo, size * count);
+	exit(EXIT_SUCCESS);
+
+}
+
+int main(int argc, char *argv[])
+{
+        pthread_t allocator;
+
+	if(argc == 2 && (strcmp(argv[1], "-sig") == 0)) {
+		printf("run signal mode\n");
+		signal(SIGUSR1, daniel_exit);
+		pthread_create(&allocator, NULL, do_alloc, NULL);
+		do_free_signal();
+	} else {
+		printf("run poll mode\n");
+		pthread_create(&allocator, NULL, do_alloc, NULL);
+		do_free();
+	}
+	return 0;
+}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ