From jilles@stack.nl  Fri Jul 12 07:55:10 2002
Return-Path: <jilles@stack.nl>
Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 3650C37B400
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 12 Jul 2002 07:55:10 -0700 (PDT)
Received: from mailhost.stack.nl (vaak.stack.nl [131.155.140.140])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 77C9043E4A
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 12 Jul 2002 07:55:09 -0700 (PDT)
	(envelope-from jilles@stack.nl)
Received: from toad.stack.nl (toad.stack.nl [2001:610:1108:5010:202:b3ff:fe17:9e1a])
	by mailhost.stack.nl (Postfix) with ESMTP id B6D7B3F97
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 12 Jul 2002 16:55:04 +0200 (CEST)
Received: by toad.stack.nl (Postfix, from userid 1677)
	id 5D30998D1; Fri, 12 Jul 2002 16:55:04 +0200 (CEST)
Message-Id: <20020712145504.5D30998D1@toad.stack.nl>
Date: Fri, 12 Jul 2002 16:55:04 +0200 (CEST)
From: Jilles Tjoelker <jilles+fbsd-bugs@stack.nl>
Reply-To: Jilles Tjoelker <jilles+fbsd-bugs@stack.nl>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] kevent/kqueue does not work with virtual terminals
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         40486
>Category:       kern
>Synopsis:       [PATCH] kevent/kqueue does not work with virtual terminals
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kbyanc
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jul 12 08:00:09 PDT 2002
>Closed-Date:    Wed Oct 16 23:04:30 PDT 2002
>Last-Modified:  Wed Oct 16 23:04:30 PDT 2002
>Originator:     Jilles Tjoelker
>Release:        FreeBSD 4.6-STABLE i386
>Organization:
Eindhoven University of Technology
>Environment:
System: FreeBSD toad.stack.nl 4.6-STABLE FreeBSD 4.6-STABLE #0: Sun Jun 23 17:18:50 CEST 2002 marcolz@toad.stack.nl:/toad.mnt/sources/4.x/sys/compile/toad_vwww i386
The problem is also present on my laptop with 4.6-RELEASE:
FreeBSD jaguar.stack.nl 4.6-RELEASE FreeBSD 4.6-RELEASE #1: Thu Jul 11 15:49:33
CEST 2002     jilles@jaguar.stack.nl:/usr/src/sys/compile/JAGUAR  i386


	
>Description:
Kevent/kqueue does not work with ttyv*, but it does work with pseudo terminals.
>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.

>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.
	

--- syscons.c.patch begins here ---
* 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
--- syscons.c.patch ends here ---

--- kevent.c begins here ---
/* 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 ---


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: kbyanc 
State-Changed-When: Wed Oct 16 22:55:18 PDT 2002 
State-Changed-Why:  
Committed.  Thanks! 


Responsible-Changed-From-To: freebsd-bugs->kbyanc 
Responsible-Changed-By: kbyanc 
Responsible-Changed-When: Wed Oct 16 22:55:18 PDT 2002 
Responsible-Changed-Why:  
Committed.  Thanks! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=40486 
>Unformatted:
