Newer
Older
(defmacro %syscall ((name (&rest arg-types) result-type)
success-form &rest args)
`(let* ((fn (extern-alien ,name (function ,result-type ,@arg-types)))
(result (alien-funcall fn ,@args)))
(if (eql -1 result)
(values nil (unix-errno))
(defmacro syscall ((name &rest arg-types) success-form &rest args)
`(%syscall (,name (,@arg-types) int) ,success-form ,@args))
;;; Like syscall, but if it fails, signal an error instead of returing error
;;; codes. Should only be used for syscalls that will never really get an
;;; error.
;;;
(defmacro syscall* ((name &rest arg-types) success-form &rest args)
`(let ((result (alien-funcall (extern-alien ,name (function int ,@arg-types))
,@args)))
(error "Syscall ~A failed: ~A" ,name (get-unix-error-msg))
,success-form)))
(defmacro void-syscall ((name &rest arg-types) &rest args)
`(syscall (,name ,@arg-types) (values t 0) ,@args))
(defmacro int-syscall ((name &rest arg-types) &rest args)
`(syscall (,name ,@arg-types) (values result 0) ,@args))
(defmacro off-t-syscall ((name arg-types) &rest args)
`(%syscall (,name ,arg-types off-t) (values result 0) ,@args))
;;;; Memory-mapped files
(defconstant +null+ (sys:int-sap 0))
(defconstant prot_read 1) ; Readable
(defconstant prot_write 2) ; Writable
(defconstant prot_exec 4) ; Executable
(defconstant prot_none 0) ; No access
(defconstant map_shared 1) ; Changes are shared
(defconstant map_private 2) ; Changes are private
(defconstant map_fixed 16) ; Fixed, user-defined address
(defconstant map_noreserve #x40) ; Don't reserve swap space
(defconstant map_anonymous
#+solaris #x100 ; Solaris
#+linux 32 ; Linux
(defconstant ms_async 1)
(defconstant ms_sync 4)
(defconstant ms_invalidate 2)
;; The return value from mmap that means mmap failed.
(defconstant map_failed (int-sap (1- (ash 1 vm:word-bits))))
(defun unix-mmap (addr length prot flags fd offset)
(declare (type (or null system-area-pointer) addr)
(type (unsigned-byte 32) length)
(type (integer 1 7) prot)
(type (unsigned-byte 32) flags)
(type (or null unix-fd) fd)
(type file-offset offset))
;; Can't use syscall, because the address that is returned could be
;; "negative". Hence we explicitly check for mmap returning
;; MAP_FAILED.
(let ((result
(alien-funcall (extern-alien "mmap" (function system-area-pointer
system-area-pointer
size-t int int int off-t))
(or addr +null+) length prot flags (or fd -1) offset)))
(if (sap= result map_failed)
(values nil (unix-errno))
(values result 0))))
(defun unix-munmap (addr length)
(declare (type system-area-pointer addr)
(type (unsigned-byte 32) length))
(syscall ("munmap" system-area-pointer size-t) t addr length))
(defun unix-mprotect (addr length prot)
(declare (type system-area-pointer addr)
(type (unsigned-byte 32) length)
(type (integer 1 7) prot))
(syscall ("mprotect" system-area-pointer size-t int)
t addr length prot))
(defun unix-setuid (uid)
"Set the user ID of the calling process to UID.
If the calling process is the super-user, set the real
and effective user IDs, and the saved set-user-ID to UID;
if not, the effective user ID is set to UID."
(int-syscall ("setuid" uid-t) uid))
(defun unix-setgid (gid)
"Set the group ID of the calling process to GID.
If the calling process is the super-user, set the real
and effective group IDs, and the saved set-group-ID to GID;
if not, the effective group ID is set to GID."
(int-syscall ("setgid" gid-t) gid))
(defun unix-msync (addr length flags)
(declare (type system-area-pointer addr)
(type (unsigned-byte 32) length)
(type (signed-byte 32) flags))
(syscall ("msync" system-area-pointer size-t int) t addr length flags))
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
;;; Unix-access accepts a path and a mode. It returns two values the
;;; first is T if the file is accessible and NIL otherwise. The second
;;; only has meaning in the second case and is the unix errno value.
(defconstant r_ok 4 "Test for read permission")
(defconstant w_ok 2 "Test for write permission")
(defconstant x_ok 1 "Test for execute permission")
(defconstant f_ok 0 "Test for presence of file")
(defun unix-access (path mode)
"Given a file path (a string) and one of four constant modes,
unix-access returns T if the file is accessible with that
mode and NIL if not. It also returns an errno value with
NIL which determines why the file was not accessible.
The access modes are:
r_ok Read permission.
w_ok Write permission.
x_ok Execute permission.
f_ok Presence of file."
(declare (type unix-pathname path)
(type (mod 8) mode))
(void-syscall ("access" c-string int) path mode))
;;; Unix-chdir accepts a directory name and makes that the
;;; current working directory.
(defun unix-chdir (path)
"Given a file path string, unix-chdir changes the current working
directory to the one specified."
(declare (type unix-pathname path))
(void-syscall ("chdir" c-string) path))
;;; Unix-chmod accepts a path and a mode and changes the mode to the new mode.
(defconstant setuidexec #o4000 "Set user ID on execution")
(defconstant setgidexec #o2000 "Set group ID on execution")
(defconstant savetext #o1000 "Save text image after execution")
(defconstant readown #o400 "Read by owner")
(defconstant writeown #o200 "Write by owner")
(defconstant execown #o100 "Execute (search directory) by owner")
(defconstant readgrp #o40 "Read by group")
(defconstant writegrp #o20 "Write by group")
(defconstant execgrp #o10 "Execute (search directory) by group")
(defconstant readoth #o4 "Read by others")
(defconstant writeoth #o2 "Write by others")
(defconstant execoth #o1 "Execute (search directory) by others")
(defun unix-chmod (path mode)
"Given a file path string and a constant mode, unix-chmod changes the
permission mode for that file to the one specified. The new mode
can be created by logically OR'ing the following:
setuidexec Set user ID on execution.
setgidexec Set group ID on execution.
savetext Save text image after execution.
readown Read by owner.
writeown Write by owner.
execown Execute (search directory) by owner.
readgrp Read by group.
writegrp Write by group.
execgrp Execute (search directory) by group.
readoth Read by others.
writeoth Write by others.
execoth Execute (search directory) by others.
Thus #o444 and (logior unix:readown unix:readgrp unix:readoth)
are equivalent for 'mode. The octal-base is familar to Unix users.
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
It returns T on successfully completion; NIL and an error number
otherwise."
(declare (type unix-pathname path)
(type unix-file-mode mode))
(void-syscall ("chmod" c-string int) path mode))
;;; Unix-fchmod accepts a file descriptor ("fd") and a file protection mode
;;; ("mode") and changes the protection of the file described by "fd" to
;;; "mode".
(defun unix-fchmod (fd mode)
"Given an integer file descriptor and a mode (the same as those
used for unix-chmod), unix-fchmod changes the permission mode
for that file to the one specified. T is returned if the call
was successful."
(declare (type unix-fd fd)
(type unix-file-mode mode))
(void-syscall ("fchmod" int int) fd mode))
(defun unix-chown (path uid gid)
"Given a file path, an integer user-id, and an integer group-id,
unix-chown changes the owner of the file and the group of the
file to those specified. Either the owner or the group may be
left unchanged by specifying them as -1. Note: Permission will
fail if the caller is not the superuser."
(declare (type unix-pathname path)
(type (or unix-uid (integer -1 -1)) uid)
(type (or unix-gid (integer -1 -1)) gid))
(void-syscall ("chown" c-string int int) path uid gid))
;;; Unix-fchown is exactly the same as unix-chown except that the file
;;; is specified by a file-descriptor ("fd") instead of a pathname.
(defun unix-fchown (fd uid gid)
"Unix-fchown is like unix-chown, except that it accepts an integer
file descriptor instead of a file path name."
(declare (type unix-fd fd)
(type (or unix-uid (integer -1 -1)) uid)
(type (or unix-gid (integer -1 -1)) gid))
(void-syscall ("fchown" int int int) fd uid gid))
;;; Returns the maximum size (i.e. the number of array elements
;;; of the file descriptor table.
(defun unix-getdtablesize ()
"Unix-getdtablesize returns the maximum size of the file descriptor
table. (i.e. the maximum number of descriptors that can exist at
one time.)"
(int-syscall ("getdtablesize")))
;;; Unix-close accepts a file descriptor and attempts to close the file
;;; associated with it.
(defun unix-close (fd)
"Unix-close takes an integer file descriptor as an argument and
closes the file associated with it. T is returned upon successful
completion, otherwise NIL and an error number."
(declare (type unix-fd fd))
(void-syscall ("close" int) fd))
;;; Unix-creat accepts a file name and a mode. It creates a new file
;;; with name and sets it mode to mode (as for chmod).
(defun unix-creat (name mode)
"Unix-creat accepts a file name and a mode (same as those for
unix-chmod) and creates a file by that name with the specified
permission mode. It returns a file descriptor on success,
or NIL and an error number otherwise.
This interface is made obsolete by UNIX-OPEN."
(declare (type unix-pathname name)
(type unix-file-mode mode))
(int-syscall (#+solaris "creat64" #-solaris "creat" c-string int) name mode))
;;; Unix-dup returns a duplicate copy of the existing file-descriptor
;;; passed as an argument.
(defun unix-dup (fd)
"Unix-dup duplicates an existing file descriptor (given as the
argument) and return it. If FD is not a valid file descriptor, NIL
and an error number are returned."
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
(declare (type unix-fd fd))
(int-syscall ("dup" int) fd))
;;; Unix-dup2 makes the second file-descriptor describe the same file
;;; as the first. If the second file-descriptor points to an open
;;; file, it is first closed. In any case, the second should have a
;;; value which is a valid file-descriptor.
(defun unix-dup2 (fd1 fd2)
"Unix-dup2 duplicates an existing file descriptor just as unix-dup
does only the new value of the duplicate descriptor may be requested
through the second argument. If a file already exists with the
requested descriptor number, it will be closed and the number
assigned to the duplicate."
(declare (type unix-fd fd1 fd2))
(void-syscall ("dup2" int int) fd1 fd2))
;;; Unix-fcntl takes a file descriptor, an integer command
;;; number, and optional command arguments. It performs
;;; operations on the associated file and/or returns inform-
;;; ation about the file.
;;; Operations performed on file descriptors:
(defconstant F-DUPFD 0 "Duplicate a file descriptor")
(defconstant F-GETFD 1 "Get file desc. flags")
(defconstant F-SETFD 2 "Set file desc. flags")
(defconstant F-GETFL 3 "Get file flags")
(defconstant F-SETFL 4 "Set file flags")
#+svr4
(defconstant F-GETOWN 23 "Get owner")
#+linux
(defconstant F-GETLK 5 "Get lock")
#-(or linux svr4)
#+svr4
(defconstant F-SETOWN 24 "Set owner")
#+linux
(defconstant F-SETLK 6 "Set lock")
#+linux
(defconstant F-SETLKW 7 "Set lock, wait for release")
#+linux
(defconstant F-SETOWN 8 "Set owner")
(defconstant FNDELAY #-osf1 #o0004 #+osf1 #o100000 "Non-blocking reads")
(defconstant FAPPEND #-linux #o0010 #+linux #o2000 "Append on each write")
(defconstant FASYNC #-(or linux svr4) #o0100 #+svr4 #o10000 #+linux #o20000
;; doesn't exist in Linux ;-(
#-linux (defconstant FCREAT #-(or hpux svr4) #o1000 #+(or hpux svr4) #o0400
"Create if nonexistant")
#-linux (defconstant FTRUNC #-(or hpux svr4) #o2000 #+(or hpux svr4) #o1000
#-linux (defconstant FEXCL #-(or hpux svr4) #o4000 #+(or hpux svr4) #o2000
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
(defun unix-fcntl (fd cmd arg)
"Unix-fcntl manipulates file descriptors according to the
argument CMD which can be one of the following:
F-DUPFD Duplicate a file descriptor.
F-GETFD Get file descriptor flags.
F-SETFD Set file descriptor flags.
F-GETFL Get file flags.
F-SETFL Set file flags.
F-GETOWN Get owner.
F-SETOWN Set owner.
The flags that can be specified for F-SETFL are:
FNDELAY Non-blocking reads.
FAPPEND Append on each write.
FASYNC Signal pgrp when data ready.
FCREAT Create if nonexistant.
FTRUNC Truncate to zero length.
FEXCL Error if already created.
"
(declare (type unix-fd fd)
(type (unsigned-byte 32) cmd)
(type (unsigned-byte 32) arg))
(int-syscall ("fcntl" int unsigned-int unsigned-int) fd cmd arg))
;;; Unix-link creates a hard link from name2 to name1.
(defun unix-link (name1 name2)
"Unix-link creates a hard link from the file with name1 to the
file with name2."
(declare (type unix-pathname name1 name2))
(void-syscall ("link" c-string c-string) name1 name2))
;;; Unix-lseek accepts a file descriptor, an offset, and whence value.
(defconstant l_set 0 "set the file pointer")
(defconstant l_incr 1 "increment the file pointer")
(defconstant l_xtnd 2 "extend the file size")
(defun unix-lseek (fd offset whence)
"Unix-lseek accepts a file descriptor and moves the file pointer ahead
a certain offset for that file. Whence can be any of the following:
l_set Set the file pointer.
l_incr Increment the file pointer.
l_xtnd Extend the file size.
"
(declare (type unix-fd fd)
(off-t-syscall ("lseek" (int off-t int)) fd offset whence))
#+solaris
(defun unix-lseek (fd offset whence)
"Unix-lseek accepts a file descriptor and moves the file pointer ahead
a certain offset for that file. Whence can be any of the following:
l_set Set the file pointer.
l_incr Increment the file pointer.
l_xtnd Extend the file size.
"
(declare (type unix-fd fd)
(type file-offset64 offset)
(type (integer 0 2) whence))
(let ((result (alien-funcall
(extern-alien "lseek64" (function off64-t int off64-t int))
fd offset whence)))
(if (minusp result)
(progn
(values nil (unix-errno)))
;;; Unix-mkdir accepts a name and a mode and attempts to create the
;;; corresponding directory with mode mode.
(defun unix-mkdir (name mode)
"Unix-mkdir creates a new directory with the specified name and mode.
(Same as those for unix-chmod.) It returns T upon success, otherwise
NIL and an error number."
(declare (type unix-pathname name)
(type unix-file-mode mode))
(void-syscall ("mkdir" c-string int) name mode))
;;; Unix-open accepts a pathname (a simple string), flags, and mode and
;;; attempts to open file with name pathname.
(defconstant o_rdonly 0 "Read-only flag.")
(defconstant o_wronly 1 "Write-only flag.")
(defconstant o_rdwr 2 "Read-write flag.")
#+(or hpux linux svr4)
(defconstant o_ndelay #-linux 4 #+linux #o4000 "Non-blocking I/O")
(defconstant o_append #-linux #o10 #+linux #o2000 "Append flag.")
#+(or hpux svr4 linux)
(progn
(defconstant o_creat #-linux #o400 #+linux #o100 "Create if nonexistant flag.")
(defconstant o_trunc #o1000 "Truncate flag.")
(defconstant o_excl #-linux #o2000 #+linux #o200 "Error if already exists.")
(defconstant o_noctty #+linux #o400 #+hpux #o400000 #+(or irix solaris) #x800
"Don't assign controlling tty"))
#+(or hpux svr4 BSD)
(defconstant o_nonblock #+hpux #o200000 #+(or irix solaris) #x80 #+BSD #x04
"Non-blocking mode")
#+BSD
(defconstant o_ndelay o_nonblock) ; compatibility
#+linux
(progn
(defconstant o_sync #o10000 "Synchronous writes (on ext2)"))
#-(or hpux svr4 linux)
(progn
(defconstant o_creat #o1000 "Create if nonexistant flag.")
(defconstant o_trunc #o2000 "Truncate flag.")
(defconstant o_excl #o4000 "Error if already exists."))
(defun unix-open (path flags mode)
"Unix-open opens the file whose pathname is specified by path
for reading and/or writing as specified by the flags argument.
The flags argument can be:
o_rdonly Read-only flag.
o_wronly Write-only flag.
o_rdwr Read-and-write flag.
o_append Append flag.
o_creat Create-if-nonexistant flag.
o_trunc Truncate-to-size-0 flag.
If the o_creat flag is specified, then the file is created with
a permission of argument mode if the file doesn't exist. An
integer file descriptor is returned by unix-open."
(declare (type unix-pathname path)
(int-syscall (#+solaris "open64" #-solaris "open" c-string int int) path flags mode))
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
(defun unix-pipe ()
"Unix-pipe sets up a unix-piping mechanism consisting of
an input pipe and an output pipe. Unix-Pipe returns two
values: if no error occurred the first value is the pipe
to be read from and the second is can be written to. If
an error occurred the first value is NIL and the second
the unix error code."
(with-alien ((fds (array int 2)))
(syscall ("pipe" (* int))
(values (deref fds 0) (deref fds 1))
(cast fds (* int)))))
;;; Unix-read accepts a file descriptor, a buffer, and the length to read.
;;; It attempts to read len bytes from the device associated with fd
;;; and store them into the buffer. It returns the actual number of
;;; bytes read.
(defun unix-read (fd buf len)
"Unix-read attempts to read from the file described by fd into
the buffer buf until it is full. Len is the length of the buffer.
The number of bytes actually read is returned or NIL and an error
number if an error occured."
(declare (type unix-fd fd)
(type (unsigned-byte 32) len))
#+(or sunos gencgc)
;; Note: Under sunos we touch each page before doing the read to give
;; the segv handler a chance to fix the permissions. Otherwise,
;; read will return EFAULT. This also bypasses a bug in 4.1.1 in which
;; read fails with EFAULT if the page has never been touched even if
;; the permissions are okay.
;;
;; (Is this true for Solaris?)
;;
;; Also, with gencgc, the collector tries to keep raw objects like
;; strings in separate pages that are not write-protected. However,
;; this isn't always true. Thus, BUF will sometimes be
;; write-protected and the kernel doesn't like writing to
;; write-protected pages. So go through and touch each page to give
;; the segv handler a chance to unprotect the pages.
(without-gcing
(let* ((page-size (get-page-size))
(1-page-size (1- page-size))
(sap (etypecase buf
(system-area-pointer buf)
(vector (vector-sap buf))))
(end (sap+ sap len)))
(declare (type (and fixnum unsigned-byte) page-size 1-page-size)
(type system-area-pointer sap end)
(optimize (speed 3) (safety 0)))
;; Touch the beginning of every page
(do ((sap (int-sap (logand (sap-int sap)
(logxor 1-page-size (ldb (byte 32 0) -1))))
(sap+ sap page-size)))
(declare (type system-area-pointer sap))
(setf (sap-ref-8 sap 0) (sap-ref-8 sap 0)))))
(int-syscall ("read" int (* char) int) fd buf len))
(defun unix-readlink (path)
"Unix-readlink invokes the readlink system call on the file name
specified by the simple string path. It returns up to two values:
the contents of the symbolic link if the call is successful, or
NIL and the Unix error number."
(declare (type unix-pathname path))
(with-alien ((buf (array char 1024)))
(syscall ("readlink" c-string (* char) int)
(let ((string (make-string result)))
(kernel:copy-from-system-area
(alien-sap buf) 0
string (* vm:vector-data-offset vm:word-bits)
(* result vm:byte-bits))
string)
path (cast buf (* char)) 1024)))
;;; Unix-rename accepts two files names and renames the first to the second.
(defun unix-rename (name1 name2)
"Unix-rename renames the file with string name1 to the string
name2. NIL and an error code is returned if an error occured."
(declare (type unix-pathname name1 name2))
(void-syscall ("rename" c-string c-string) name1 name2))
;;; Unix-rmdir accepts a name and removes the associated directory.
(defun unix-rmdir (name)
"Unix-rmdir attempts to remove the directory name. NIL and
an error number is returned if an error occured."
(declare (type unix-pathname name))
(void-syscall ("rmdir" c-string) name))
;;; UNIX-FAST-SELECT -- public.
;;;
(defmacro unix-fast-select (num-descriptors
read-fds write-fds exception-fds
timeout-secs &optional (timeout-usecs 0))
"Perform the UNIX select(2) system call.
(declare (type (integer 0 #.FD-SETSIZE) num-descriptors)
(type (or (alien (* (struct fd-set))) null)
read-fds write-fds exception-fds)
(type (or null (unsigned-byte 31)) timeout-secs)
(type (unsigned-byte 31) timeout-usecs)
(optimize (speed 3) (safety 0) (inhibit-warnings 3)))"
`(let ((timeout-secs ,timeout-secs))
(with-alien ((tv (struct timeval)))
(when timeout-secs
(setf (slot tv 'tv-sec) timeout-secs)
(setf (slot tv 'tv-usec) ,timeout-usecs))
(int-syscall ("select" int (* (struct fd-set)) (* (struct fd-set))
(* (struct fd-set)) (* (struct timeval)))
,num-descriptors ,read-fds ,write-fds ,exception-fds
(if timeout-secs (alien-sap (addr tv)) (int-sap 0))))))
;;; Unix-select accepts sets of file descriptors and waits for an event
;;; to happen on one of them or to time out.
(defmacro num-to-fd-set (fdset num)
`(if (fixnump ,num)
(progn
(setf (deref (slot ,fdset 'fds-bits) 0) ,num)
,@(loop for index upfrom 1 below (/ fd-setsize 32)
collect `(setf (deref (slot ,fdset 'fds-bits) ,index) 0)))
(progn
,@(loop for index upfrom 0 below (/ fd-setsize 32)
collect `(setf (deref (slot ,fdset 'fds-bits) ,index)
(ldb (byte 32 ,(* index 32)) ,num))))))
(defmacro fd-set-to-num (nfds fdset)
`(if (<= ,nfds 32)
(deref (slot ,fdset 'fds-bits) 0)
(+ ,@(loop for index upfrom 0 below (/ fd-setsize 32)
collect `(ash (deref (slot ,fdset 'fds-bits) ,index)
,(* index 32))))))
(defun unix-select (nfds rdfds wrfds xpfds to-secs &optional (to-usecs 0))
"Unix-select examines the sets of descriptors passed as arguments
to see if they are ready for reading and writing. See the UNIX
Programmers Manual for more information."
(declare (type (integer 0 #.FD-SETSIZE) nfds)
(type unsigned-byte rdfds wrfds xpfds)
(type (or (unsigned-byte 31) null) to-secs)
(type (unsigned-byte 31) to-usecs)
(optimize (speed 3) (safety 0) (inhibit-warnings 3)))
(with-alien ((tv (struct timeval))
(rdf (struct fd-set))
(wrf (struct fd-set))
(xpf (struct fd-set)))
(when to-secs
(setf (slot tv 'tv-sec) to-secs)
(setf (slot tv 'tv-usec) to-usecs))
(num-to-fd-set rdf rdfds)
(num-to-fd-set wrf wrfds)
(num-to-fd-set xpf xpfds)
(macrolet ((frob (lispvar alienvar)
`(if (zerop ,lispvar)
(int-sap 0)
(alien-sap (addr ,alienvar)))))
(syscall ("select" int (* (struct fd-set)) (* (struct fd-set))
(* (struct fd-set)) (* (struct timeval)))
(values result
(fd-set-to-num nfds rdf)
(fd-set-to-num nfds wrf)
(fd-set-to-num nfds xpf))
(if to-secs (alien-sap (addr tv)) (int-sap 0))))))
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
;;; Unix-sync writes all information in core memory which has been modified
;;; to permanent storage (i.e. disk).
(defun unix-sync ()
"Unix-sync writes all information in core memory which has been
modified to disk. It returns NIL and an error code if an error
occured."
(void-syscall ("sync")))
;;; Unix-fsync writes the core-image of the file described by "fd" to
;;; permanent storage (i.e. disk).
(defun unix-fsync (fd)
"Unix-fsync writes the core image of the file described by
fd to disk."
(declare (type unix-fd fd))
(void-syscall ("fsync" int) fd))
;;; Unix-truncate accepts a file name and a new length. The file is
;;; truncated to the new length.
(defun unix-truncate (name len)
"Unix-truncate truncates the named file to the length (in
bytes) specified by len. NIL and an error number is returned
if the call is unsuccessful."
(declare (type unix-pathname name)
(type (unsigned-byte #+solaris 64 #-solaris 32) len))
(void-syscall (#+solaris "truncate64" #-solaris "truncate" c-string int) name len)
#+(and bsd x86)
(void-syscall ("truncate" c-string unsigned-long unsigned-long) name len 0))
(defun unix-ftruncate (fd len)
"Unix-ftruncate is similar to unix-truncate except that the first
argument is a file descriptor rather than a file name."
(declare (type unix-fd fd)
(type (unsigned-byte #+solaris 64 #-solaris 32) len))
(void-syscall (#+solaris "ftruncate64" #-solaris "ftruncate" int int) fd len)
#+(and bsd x86)
(void-syscall ("ftruncate" int unsigned-long unsigned-long) fd len 0))
(defun unix-symlink (name1 name2)
"Unix-symlink creates a symbolic link named name2 to the file
named name1. NIL and an error number is returned if the call
is unsuccessful."
(declare (type unix-pathname name1 name2))
(void-syscall ("symlink" c-string c-string) name1 name2))
;;; Unix-unlink accepts a name and deletes the directory entry for that
;;; name and the file if this is the last link.
(defun unix-unlink (name)
"Unix-unlink removes the directory entry for the named file.
NIL and an error code is returned if the call fails."
(declare (type unix-pathname name))
(void-syscall ("unlink" c-string) name))
;;; Unix-write accepts a file descriptor, a buffer, an offset, and the
;;; length to write. It attempts to write len bytes to the device
;;; associated with fd from the buffer starting at offset. It returns
;;; the actual number of bytes written.
(defun unix-write (fd buf offset len)
"Unix-write attempts to write a character buffer (buf) of length
len to the file described by the file descriptor fd. NIL and an
error is returned if the call is unsuccessful."
(declare (type unix-fd fd)
(type (unsigned-byte 32) offset len))
(int-syscall ("write" int (* char) int)
fd
(with-alien ((ptr (* char) (etypecase buf
((simple-array * (*))
(vector-sap buf))
(system-area-pointer
buf))))
(addr (deref ptr offset)))
len))
;;; Unix-ioctl is used to change parameters of devices in a device
;;; dependent way.
(defconstant terminal-speeds
'#(0 50 75 110 134 150 200 300 600 #+hpux 900 1200 1800 2400 #+hpux 3600
4800 #+hpux 7200 9600 19200 38400 57600 115200 230400
#+hpux 460800))
;;; from /usr/include/bsd/sgtty.h (linux)
(defconstant tty-raw #-linux #o40 #+linux 1)
(defconstant tty-crmod #-linux #o20 #+linux 4)
#-(or hpux svr4 bsd linux) (defconstant tty-echo #o10) ;; 8
(defconstant tty-cbreak #-linux #o2 #+linux 64)
#-(or linux hpux)
#+(or hpux svr4 bsd linux)
(defmacro def-enum (inc cur &rest names)
(flet ((defform (name)
(prog1 (when name `(defconstant ,name ,cur))
(setf cur (funcall inc cur 1)))))
`(progn ,@(mapcar #'defform names))))
;; Input modes. Linux: /usr/include/asm/termbits.h
(def-enum ash 1 tty-ignbrk tty-brkint tty-ignpar tty-parmrk tty-inpck
tty-istrip tty-inlcr tty-igncr tty-icrnl #-bsd tty-iuclc
tty-ixon #-bsd tty-ixany tty-ixoff #+bsd tty-ixany
#+hpux tty-ienqak #+bsd nil tty-imaxbel)
#-bsd (def-enum ash 1 tty-opost tty-olcuc tty-onlcr tty-ocrnl tty-onocr
#+bsd (def-enum ash 1 tty-opost tty-onlcr)
#-bsd (def-enum ash 1 tty-isig tty-icanon tty-xcase tty-echo tty-echoe
tty-echok tty-echonl tty-noflsh #+irix tty-iexten
#+(or sunos linux) tty-tostop tty-echoctl tty-echoprt
tty-echoke #+(or sunos svr4) tty-defecho tty-flusho
#+linux nil tty-pendin #+irix tty-tostop
#+(or sunos linux) tty-iexten)
#+bsd (def-enum ash 1 tty-echoke tty-echoe tty-echok tty-echo tty-echonl
tty-echoprt tty-echoctl tty-isig tty-icanon nil
tty-iexten)
#+bsd (defconstant tty-tostop #x00400000)
#+bsd (defconstant tty-flusho #x00800000)
#+bsd (defconstant tty-pendin #x20000000)
#+bsd (defconstant tty-noflsh #x80000000)
#+hpux (defconstant tty-tostop #o10000000000)
#+hpux (defconstant tty-iexten #o20000000000)
;; control modes
(def-enum ash #-bsd #o100 #+bsd #x400 #+hpux nil tty-cstopb
tty-cread tty-parenb tty-parodd tty-hupcl tty-clocal
#+svr4 rcv1en #+svr4 xmt1en #+(or hpux svr4) tty-loblk)
;; special control characters
#+(or hpux svr4 linux) (def-enum + 0 vintr vquit verase vkill veof
#-linux veol #-linux veol2)
#+bsd (def-enum + 0 veof veol veol2 verase nil vkill nil nil vintr vquit)
#+linux (defconstant veol 11)
#+linux (defconstant veol2 16)
(defconstant tciflush 0)
(defconstant tcoflush 1)
(defconstant tcioflush 2))
(defconstant vmin 16)
(defconstant vtime 17)
(defconstant vsusp 10)
(defconstant vstart 12)
(defconstant vstop 13)
(defconstant vdsusp 11))
#+hpux
(progn
(defconstant vmin 11)
(defconstant vtime 12)
(defconstant vsusp 13)
(defconstant vstart 14)
(defconstant vstop 15)
(defconstant vdsusp 21))
#+(or hpux bsd linux)
(progn
(defconstant tcsanow 0)
(defconstant tcsadrain 1)
(defconstant vstart 8)
(defconstant vstop 9)
(defconstant vsusp 10)
;; control modes
(defconstant tty-cbaud #o17)
(defconstant tty-csize #o60)
(defconstant tty-cs5 #o0)
(defconstant tty-cs6 #o20)
(defconstant tty-cs7 #o40)
(defconstant tty-cs8 #o60))
(progn
;; control modes
(defconstant tty-csize #x300)
(defconstant tty-cs5 #x000)
(defconstant tty-cs6 #x100)
(defconstant tty-cs7 #x200)
(defconstant tty-cs8 #x300))
#+svr4
(progn
(defconstant tcsanow #x540e)
(defconstant tcsadrain #x540f)
(defconstant tcsaflush #x5410))
#-(or (and svr4 (not irix)) linux)
(progn
(defconstant iocparm-mask #x7f) ; Freebsd: #x1fff ?
(defconstant ioc_void #x20000000)
(defconstant ioc_out #x40000000)
(defconstant ioc_in #x80000000)
(defconstant ioc_inout (logior ioc_in ioc_out)))
#-(or linux (and svr4 (not irix)))
(defmacro define-ioctl-command (name dev cmd arg &optional (parm-type :void))
(:inout ioc_inout)))
(code (logior (ash (char-code dev) 8) cmd ptype)))
(when arg
(setf code
`(logior (ash (logand (alien-size ,arg :bytes)
,iocparm-mask)
16)
,code)))
#+(and svr4 (not irix))
(defmacro define-ioctl-command (name dev cmd arg &optional (parm-type :void))
`(eval-when (eval load compile)
(defconstant ,name ,(logior (ash (char-code #\t) 8) cmd))))
#+linux
(defmacro define-ioctl-command (name dev cmd arg &optional (parm-type :void))
(declare (ignore arg parm-type))
`(eval-when (eval load compile)
(defconstant ,name ,(logior (ash (- (char-code dev) #x20) 8) cmd))))
(define-ioctl-command TIOCGETP #\t #-linux 8 #+linux #x81 (struct sgttyb) :out)
(define-ioctl-command TIOCSETP #\t #-linux 9 #+linux #x82 (struct sgttyb) :in)
(define-ioctl-command TIOCFLUSH #\t #-linux 16 #+linux #x89 int :in)
(define-ioctl-command TIOCSETC #\t #-linux 17 #+linux #x84 (struct tchars) :in)
(define-ioctl-command TIOCGETC #\t #-linux 18 #+linux #x83 (struct tchars) :out)
(define-ioctl-command TIOCGWINSZ #\t #-hpux 104 #+hpux 107 (struct winsize)
:out)
(define-ioctl-command TIOCSWINSZ #\t #-hpux 103 #+hpux 106 (struct winsize)
:in)
(define-ioctl-command TIOCNOTTY #\t #-linux 113 #+linux #x22 nil :void)
(define-ioctl-command TIOCSLTC #\t #-linux 117 #+linux #x84 (struct ltchars) :in)
(define-ioctl-command TIOCGLTC #\t #-linux 116 #+linux #x85 (struct ltchars) :out)
(define-ioctl-command TIOCSPGRP #\t #-svr4 118 #+svr4 21 int :in)
(define-ioctl-command TIOCGPGRP #\t #-svr4 119 #+svr4 20 int :out))
#+hpux
(progn
(define-ioctl-command TIOCSLTC #\T 23 (struct ltchars) :in)
(define-ioctl-command TIOCGLTC #\T 24 (struct ltchars) :out)
(define-ioctl-command TIOCSPGRP #\T 29 int :in)
(define-ioctl-command TIOCGPGRP #\T 30 int :out)
(define-ioctl-command TIOCSIGSEND #\t 93 nil))
(define-ioctl-command FIONREAD #\f #-linux 127 #+linux #x1B int :out)
(defun unix-ioctl (fd cmd arg)
"Unix-ioctl performs a variety of operations on open i/o
descriptors. See the UNIX Programmer's Manual for more
information."
(declare (type unix-fd fd)
(type (unsigned-byte 32) cmd))
(int-syscall ("ioctl" int unsigned-int (* char)) fd cmd arg))
#+(or svr4 hpux bsd linux)
(progn
(defun unix-tcgetattr (fd termios)
"Get terminal attributes."
(declare (type unix-fd fd))
(void-syscall ("tcgetattr" int (* (struct termios))) fd termios))
(defun unix-tcsetattr (fd opt termios)
"Set terminal attributes."
(declare (type unix-fd fd))
(void-syscall ("tcsetattr" int int (* (struct termios))) fd opt termios))
;; XXX rest of functions in this progn probably are present in linux, but
;; not verified.
(defun unix-cfgetospeed (termios)
"Get terminal output speed."
(multiple-value-bind (speed errno)
(int-syscall ("cfgetospeed" (* (struct termios))) termios)
(if speed
(values (svref terminal-speeds speed) 0)
(values speed errno))))
(defun unix-cfgetospeed (termios)
"Get terminal output speed."
(int-syscall ("cfgetospeed" (* (struct termios))) termios))
(defun unix-cfsetospeed (termios speed)
"Set terminal output speed."
(let ((baud (or (position speed terminal-speeds)
(error "Bogus baud rate ~S" speed))))
(void-syscall ("cfsetospeed" (* (struct termios)) int) termios baud)))
(defun unix-cfsetospeed (termios speed)
"Set terminal output speed."
(void-syscall ("cfsetospeed" (* (struct termios)) int) termios speed))
(defun unix-cfgetispeed (termios)
"Get terminal input speed."
(multiple-value-bind (speed errno)
(int-syscall ("cfgetispeed" (* (struct termios))) termios)
(if speed
(values (svref terminal-speeds speed) 0)
(values speed errno))))
(defun unix-cfgetispeed (termios)
"Get terminal input speed."
(int-syscall ("cfgetispeed" (* (struct termios))) termios))
(defun unix-cfsetispeed (termios speed)
"Set terminal input speed."
(let ((baud (or (position speed terminal-speeds)
(error "Bogus baud rate ~S" speed))))
(void-syscall ("cfsetispeed" (* (struct termios)) int) termios baud)))
(defun unix-cfsetispeed (termios speed)
"Set terminal input speed."
(void-syscall ("cfsetispeed" (* (struct termios)) int) termios speed))
(defun unix-tcsendbreak (fd duration)
"Send break"
(declare (type unix-fd fd))
(void-syscall ("tcsendbreak" int int) fd duration))
(defun unix-tcdrain (fd)
"Wait for output for finish"
(declare (type unix-fd fd))
(void-syscall ("tcdrain" int) fd))
(defun unix-tcflush (fd selector)
"See tcflush(3)"
(declare (type unix-fd fd))
(void-syscall ("tcflush" int int) fd selector))
(defun unix-tcflow (fd action)