From owner-svn-src-stable-10@freebsd.org Fri May 6 17:55:12 2016 Return-Path: Delivered-To: svn-src-stable-10@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 69980B314B0; Fri, 6 May 2016 17:55:12 +0000 (UTC) (envelope-from bcr@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1DF8613C1; Fri, 6 May 2016 17:55:12 +0000 (UTC) (envelope-from bcr@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u46HtBRM050654; Fri, 6 May 2016 17:55:11 GMT (envelope-from bcr@FreeBSD.org) Received: (from bcr@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u46HtBEQ050653; Fri, 6 May 2016 17:55:11 GMT (envelope-from bcr@FreeBSD.org) Message-Id: <201605061755.u46HtBEQ050653@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bcr set sender to bcr@FreeBSD.org using -f From: Benedict Reuschling Date: Fri, 6 May 2016 17:55:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r299190 - stable/10/lib/libc/sys X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 May 2016 17:55:12 -0000 Author: bcr (doc committer) Date: Fri May 6 17:55:11 2016 New Revision: 299190 URL: https://svnweb.freebsd.org/changeset/base/299190 Log: MFC r298893: Provide an example to the kqueue man page, showing a basic usage example. Although it is an untypical example for the use of kqueue, it is better than nothing and should get people started. PR: 196844 Submitted by: fernando.apesteguia@gmail.com Reviewed by: kib Approved by: kib Differential Revision: https://reviews.freebsd.org/D6082 Modified: stable/10/lib/libc/sys/kqueue.2 Directory Properties: stable/10/ (props changed) Modified: stable/10/lib/libc/sys/kqueue.2 ============================================================================== --- stable/10/lib/libc/sys/kqueue.2 Fri May 6 17:39:12 2016 (r299189) +++ stable/10/lib/libc/sys/kqueue.2 Fri May 6 17:55:11 2016 (r299190) @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd March 29, 2015 +.Dd May 1, 2016 .Dt KQUEUE 2 .Os .Sh NAME @@ -577,6 +577,57 @@ will be set to indicate the error condit If the time limit expires, then .Fn kevent returns 0. +.Sh EXAMPLES +.Bd -literal -compact +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + struct kevent event; /* Event we want to monitor */ + struct kevent tevent; /* Event triggered */ + int kq, fd, ret; + + if (argc != 2) + err(EXIT_FAILURE, "Usage: %s path\en", argv[0]); + fd = open(argv[1], O_RDONLY); + if (fd == -1) + err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); + + /* Create kqueue. */ + kq = kqueue(); + if (kq == -1) + err(EXIT_FAILURE, "kqueue() failed"); + + /* Initialize kevent structure. */ + EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, + 0, NULL); + /* Attach event to the kqueue. */ + ret = kevent(kq, &event, 1, NULL, 0, NULL); + if (ret == -1) + err(EXIT_FAILURE, "kevent register"); + if (event.flags & EV_ERROR) + errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); + + for (;;) { + /* Sleep until something happens. */ + ret = kevent(kq, NULL, 0, &tevent, 1, NULL); + if (ret == -1) { + err(EXIT_FAILURE, "kevent wait"); + } else if (ret > 0) { + printf("Something was written in '%s'\en", argv[1]); + } + } +} +.Ed .Sh ERRORS The .Fn kqueue