Bug 40486

Summary: [PATCH] kevent/kqueue does not work with virtual terminals
Product: Base System Reporter: Jilles Tjoelker <jilles+fbsd-bugs>
Component: kernAssignee: kbyanc
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.6-STABLE   
Hardware: Any   
OS: Any   

Description Jilles Tjoelker 2002-07-12 16:00:09 UTC
Kevent/kqueue does not work with ttyv*, but it does work with pseudo terminals.

Fix: The following patch seems to make it work, but has not been tested a lot (only
on my laptop running 4.6-RELEASE).
select(2) and poll(2) work.
	



/* Copyright (C) 2002 by Jilles Tjoelker */

#include	<sys/types.h>
#include	<sys/event.h>
#include	<sys/time.h>

#include	<err.h>
#include	<fcntl.h>
#include	<signal.h>
#include	<stdio.h>
#include	<unistd.h>


int
main(int argc, char *argv[])
{
    char buf[1024];
    int kq;
    struct kevent E;
    struct timespec TS = { 0, 0 };
    int n;

    if (argc != 1)
    {
	fprintf(stderr, "Usage: %s\n", argv[0]);
	return(1);
    }

    kq = kqueue();
    if (kq == -1)
	err(1,"kqueue");

    EV_SET(&E, STDIN_FILENO, EVFILT_READ, EV_ADD, 0, 0, NULL);
    if (kevent(kq, &E, 1, NULL, 0, &TS) == -1)
	err(1,"setup kevent");

    for (;;)
    {
	TS.tv_sec = 5;
	TS.tv_nsec = 0;
	n = kevent(kq, NULL, 0, &E, 1, &TS);
	switch (n)
	{
	    case 0:
		printf("timeout expired\n");
		break;
	    case 1:
		printf("data ready (%d)\n", E.data);
		n = read(STDIN_FILENO, buf, sizeof buf - 1);
		buf[n] = 0;
		printf("(%d) %s\n", n, buf);
		break;
	    case -1:
		err(1, "kevent");
	    default:
		printf("unexpected kevent rc %d\n", n);
	}
    }

    return 0;
}

/* vim:ts=8:cin:sw=4:kp=man\ -S3\:2\:9\:1\:4\:5\:6\:7\:8\:n
 *  */
--- kevent.c ends here -----N0H1u9F5bDsCd0srzdxaUYUlrBWEriVpIkrrJfwZedEsOkE6
Content-Type: text/plain; name="syscons.c.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="syscons.c.patch"

* Patch to:
* $FreeBSD: src/sys/dev/syscons/syscons.c,v 1.336.2.13 2002/04/08 13:37:26 sobomax Exp $
--- /sys/dev/syscons/syscons.c.orig	Mon Apr  8 15:37:26 2002
+++ /sys/dev/syscons/syscons.c	Thu Jul 11 15:48:03 2002
@@ -213,8 +213,9 @@
 	/* maj */	CDEV_MAJOR,
 	/* dump */	nodump,
 	/* psize */	nopsize,
-	/* flags */	D_TTY,
-	/* bmaj */	-1
+	/* flags */	D_TTY | D_KQFILTER,
+	/* bmaj */	-1,
+	/* kqfilter */	ttykqfilter
 };
 
 int
How-To-Repeat: Compile and run the program kevent.c
When started on a virtual terminal, it gives the following error message:
kevent: setup kevent: Operation not permitted
This error message also occurs if the program is started as root.
When started from xterm, a login via ssh or similar, it works as it should.
Comment 1 kbyanc freebsd_committer freebsd_triage 2002-10-17 06:55:18 UTC
State Changed
From-To: open->closed

Committed.  Thanks! 


Comment 2 kbyanc freebsd_committer freebsd_triage 2002-10-17 06:55:18 UTC
Responsible Changed
From-To: freebsd-bugs->kbyanc

Committed.  Thanks!