forked from malcolmstill/ulubis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscall.lisp
70 lines (60 loc) · 1.98 KB
/
syscall.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(defpackage :syscall
(:use :common-lisp :cffi)
(:export
poll
poll-return-event
with-pollfds
pollin
pollpri
pollout
pollerr
pollhup
pollnval
ioctl))
(in-package :syscall)
(defconstant pollin 1)
(defconstant pollpri 2)
(defconstant pollout 4)
(defconstant pollerr 8)
(defconstant pollhup 16)
(defconstant pollnval 32)
(defcstruct pollfd
(fd :int)
(events :short)
(revents :short))
(defcfun ("poll" %poll) :int
(fds :pointer)
(nfds :unsigned-long)
(timeout :int))
(defun poll (pollfds nfds timeout)
"Wait for events on file descriptors defined by POLLFDS. TIMEOUT is the time in milliseconds to wait for activity; a TIMEOUT of -1 will block indefinitely, a TIMEOUT of 0 will return immediately"
(let ((r (%poll pollfds nfds timeout)))
(when (= r -1)
(nix:posix-error))
(> r 0)))
(defun poll-return-event (pollfd)
"Access REVENT of pollfd struct"
(with-foreign-slots ((revents) pollfd (:struct pollfd))
revents))
(defmacro with-pollfds ((name &rest specs) &body body)
(let ((nfds (length specs)))
`(with-foreign-object (,name '(:struct pollfd) ,nfds)
(let (,@(loop :for i :from 0 :to (- nfds 1)
:collecting (let ((spec (nth i specs)))
`(,(first spec) (mem-aptr ,name '(:struct pollfd) ,i)))))
,@(loop :for i :from 0 :to (- nfds 1)
:collecting (let ((spec (nth i specs)))
`(with-foreign-slots ((fd events revents) ,(first spec) (:struct pollfd))
(setf fd ,(second spec))
(setf events (logior ,@(rest (rest spec))))
(setf revents 0))))
,@body))))
(defcfun ("ioctl" %ioctl-with-integer-arg) :int
(fd :int)
(request :int)
(arg :unsigned-long))
(defun ioctl (fd request &optional (arg nil argp))
(cond
((not argp) (nix:ioctl fd request))
((pointerp arg) (nix:ioctl fd request arg))
((integerp arg) (%ioctl-with-integer-arg fd request arg))))