2015-08-30 :-)
_ [libevent][cdbiff][eject]libevent で cdbiff (動作未確認)
手元の VMWare には /var/mail/$HOME もないし CD-ROM ドライブもないので。
The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.
Currently, libevent supports /dev/poll, kqueue(2), event ports, POSIX select(2), Windows select(), poll(2), and epoll(4). The internal event mechanism is completely independent of the exposed event API, and a simple update of libevent can provide new functionality without having to redesign the applications.
非同期 API を提供するよ、システムの /dev/poll とか kqueue とか隠蔽するのでユーザーは実装を気にしなくていいよ、移植性が高くなるよ、とのこと。
環境
% uname -rsm NetBSD 7.99.21 i386
コードだけ
man 3 event_init より
The additional flag EV_PERSIST makes an event_add() persistent until event_del() has been called.
EV_PERSIST を指定するといちいち event_add しなおさなくていいそうだ。
#include <sys/time.h> #include <event.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> void handler( int fd, short event, void* arg ) { puts( __FUNCTION__ ); system( "eject cd" ); } int main( int ac, char** av ) { int fd; struct event ev; char* user = getenv( "USER" ); char filename[ FILENAME_MAX ]; snprintf( filename, strlen( filename ), "%s/%s", "/var/mail/", user ); fd = open( filename, O_RDONLY ); if( fd < 0 ) { err( EXIT_FAILURE, "open fail: %s", filename ); exit( EXIT_FAILURE ); } event_init(); event_set( &ev, fd, EV_READ | EV_PERSIST, handler, NULL ); event_add( &ev, NULL ); event_dispatch(); return 0; }
参考
- C言語 libeventの使い方 tailを作ってみる
- libeventとは - NeiNeigh's blog
- libeventの使い方 - NeiNeigh's blog
- libeventでechoサーバをつくってみた - in the mythosil ハンドラ内でさらに event_add してるのが oh yeah ってなるけどそういうもんか