Newer
Older
`(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))))))
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
;;; 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 32) len))
(void-syscall ("truncate" c-string int) name len))
(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 32) len))
(void-syscall ("ftruncate" int int) fd len))
(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 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
'#(nil 50 75 110 nil 150 200 300 600 1200 1800 2400 4800 9600 nil nil))
(defconstant tty-raw #o40)
(defconstant tty-crmod #o20)
(defconstant tty-echo #o10)
(defconstant tty-lcase #o4)
(progn
(defconstant tty-icanon #o2)
(defconstant tty-icrnl #o400)
(defconstant tty-ocrnl #o10)
(defconstant veof 4)
(defconstant vintr 0)
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
(defconstant vquit 1))
#+hpux
(progn
(defconstant vdsusp 21)
(defconstant vstart 14)
(defconstant vstop 15)
(defconstant vsusp 13)
(defconstant vmin 11)
(defconstant vtime 12)
(defconstant tcsaflush 2))
#+svr4
(progn
(defconstant vdsusp 11)
(defconstant vstart 8)
(defconstant vstop 9)
(defconstant vsusp 10)
(defconstant vmin 4)
(defconstant vtime 5)
(defconstant tcsaflush #x5410))
(defconstant iocparm-mask #x7f)
(defconstant ioc_void #x20000000)
(defconstant ioc_out #x40000000)
(defconstant ioc_in #x80000000)
(defconstant ioc_inout (logior ioc_in ioc_out))
(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)))
(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))))
(define-ioctl-command TIOCGETP #\t 8 (struct sgttyb) :out)
(define-ioctl-command TIOCSETP #\t 9 (struct sgttyb) :in)
(define-ioctl-command TIOCFLUSH #\t 16 int :in)
(define-ioctl-command TIOCSETC #\t 17 (struct tchars) :in)
(define-ioctl-command TIOCGETC #\t 18 (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)
#-hpux
(progn
(define-ioctl-command TIOCSLTC #\t 117 (struct ltchars) :in)
(define-ioctl-command TIOCGLTC #\t 116 (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))
(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))
(void-syscall ("ioctl" int unsigned-int (* char)) fd cmd arg))
(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))
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
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
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
(defun tcsetpgrp (fd pgrp)
"Set the tty-process-group for the unix file-descriptor FD to PGRP."
(alien:with-alien ((alien-pgrp c-call:int pgrp))
(unix-ioctl fd
tiocspgrp
(alien:alien-sap (alien:addr alien-pgrp)))))
(defun tcgetpgrp (fd)
"Get the tty-process-group for the unix file-descriptor FD."
(alien:with-alien ((alien-pgrp c-call:int))
(multiple-value-bind (ok err)
(unix-ioctl fd
tiocgpgrp
(alien:alien-sap (alien:addr alien-pgrp)))
(if ok
(values alien-pgrp nil)
(values nil err)))))
(defun tty-process-group (&optional fd)
"Get the tty-process-group for the unix file-descriptor FD. If not supplied,
FD defaults to /dev/tty."
(if fd
(tcgetpgrp fd)
(multiple-value-bind (tty-fd errno)
(unix-open "/dev/tty" o_rdwr 0)
(cond (tty-fd
(multiple-value-prog1
(tcgetpgrp tty-fd)
(unix-close tty-fd)))
(t
(values nil errno))))))
(defun %set-tty-process-group (pgrp &optional fd)
"Set the tty-process-group for the unix file-descriptor FD to PGRP. If not
supplied, FD defaults to /dev/tty."
(let ((old-sigs
(unix-sigblock
(sigmask :sigttou :sigttin :sigtstp :sigchld))))
(declare (type (unsigned-byte 32) old-sigs))
(unwind-protect
(if fd
(tcsetpgrp fd pgrp)
(multiple-value-bind (tty-fd errno)
(unix-open "/dev/tty" o_rdwr 0)
(cond (tty-fd
(multiple-value-prog1
(tcsetpgrp tty-fd pgrp)
(unix-close tty-fd)))
(t
(values nil errno)))))
(unix-sigsetmask old-sigs))))
(defsetf tty-process-group (&optional fd) (pgrp)
"Set the tty-process-group for the unix file-descriptor FD to PGRP. If not
supplied, FD defaults to /dev/tty."
`(%set-tty-process-group ,pgrp ,fd))
;;; Socket options.
#+hpux
(define-ioctl-command SIOCSPGRP #\s 8 int :in)
#+hpux
(defun siocspgrp (fd pgrp)
"Set the socket process-group for the unix file-descriptor FD to PGRP."
(alien:with-alien ((alien-pgrp c-call:int pgrp))
(unix-ioctl fd
siocspgrp
(alien:alien-sap (alien:addr alien-pgrp)))))
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
;;; Unix-exit terminates a program.
(defun unix-exit (&optional (code 0))
"Unix-exit terminates the current process with an optional
error code. If successful, the call doesn't return. If
unsuccessful, the call returns NIL and an error number."
(declare (type (signed-byte 32) code))
(void-syscall ("exit" int) code))
;;; STAT and friends.
(defmacro extract-stat-results (buf)
`(values T
(slot ,buf 'st-dev)
(slot ,buf 'st-ino)
(slot ,buf 'st-mode)
(slot ,buf 'st-nlink)
(slot ,buf 'st-uid)
(slot ,buf 'st-gid)
(slot ,buf 'st-rdev)
(slot ,buf 'st-size)
#-svr4 (slot ,buf 'st-atime)
#+svr4 (slot (slot ,buf 'st-atime) 'tv-sec)
#-svr4 (slot ,buf 'st-mtime)
#+svr4 (slot (slot ,buf 'st-mtime) 'tv-sec)
#-svr4 (slot ,buf 'st-ctime)
#+svr4 (slot (slot ,buf 'st-ctime) 'tv-sec)
(slot ,buf 'st-blksize)
(slot ,buf 'st-blocks)))
(defun unix-stat (name)
"Unix-stat retrieves information about the specified
file returning them in the form of multiple values.
See the UNIX Programmer's Manual for a description
of the values returned. If the call fails, then NIL
and an error number is returned instead."
(declare (type unix-pathname name))
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
(with-alien ((buf (struct stat)))
(syscall ("stat" c-string (* (struct stat)))
(extract-stat-results buf)
name (addr buf))))
(defun unix-lstat (name)
"Unix-lstat is similar to unix-stat except the specified
file must be a symbolic link."
(declare (type unix-pathname name))
(with-alien ((buf (struct stat)))
(syscall ("lstat" c-string (* (struct stat)))
(extract-stat-results buf)
name (addr buf))))
(defun unix-fstat (fd)
"Unix-fstat is similar to unix-stat except the file is specified
by the file descriptor fd."
(declare (type unix-fd fd))
(with-alien ((buf (struct stat)))
(syscall ("fstat" int (* (struct stat)))
(extract-stat-results buf)
fd (addr buf))))
(defconstant rusage_self 0 "The calling process.")
(defconstant rusage_children -1 "Terminated child processes.")
(declaim (inline unix-fast-getrusage))
(defun unix-fast-getrusage (who)
"Like call getrusage, but return only the system and user time, and returns
the seconds and microseconds as separate values."
(declare (values (member t)
(unsigned-byte 31) (mod 1000000)
(unsigned-byte 31) (mod 1000000)))
(with-alien ((usage (struct rusage)))
(syscall* ("getrusage" int (* (struct rusage)))
(values t
(slot (slot usage 'ru-utime) 'tv-sec)
(slot (slot usage 'ru-utime) 'tv-usec)
(slot (slot usage 'ru-stime) 'tv-sec)
(slot (slot usage 'ru-stime) 'tv-usec))
who (addr usage))))
(defun unix-getrusage (who)
"Unix-getrusage returns information about the resource usage
of the process specified by who. Who can be either the
current process (rusage_self) or all of the terminated
child processes (rusage_children). NIL and an error number
is returned if the call fails."
(with-alien ((usage (struct rusage)))
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
(syscall* ("getrusage" int (* (struct rusage)))
(values t
(+ (* (slot (slot usage 'ru-utime) 'tv-sec) 1000000)
(slot (slot usage 'ru-utime) 'tv-usec))
(+ (* (slot (slot usage 'ru-stime) 'tv-sec) 1000000)
(slot (slot usage 'ru-stime) 'tv-usec))
(slot usage 'ru-maxrss)
(slot usage 'ru-ixrss)
(slot usage 'ru-idrss)
(slot usage 'ru-isrss)
(slot usage 'ru-minflt)
(slot usage 'ru-majflt)
(slot usage 'ru-nswap)
(slot usage 'ru-inblock)
(slot usage 'ru-oublock)
(slot usage 'ru-msgsnd)
(slot usage 'ru-msgrcv)
(slot usage 'ru-nsignals)
(slot usage 'ru-nvcsw)
(slot usage 'ru-nivcsw))
who (addr usage))))
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
;; Requires call to tzset() in main.
;; Don't use this now: we
#+svr4
(progn
(def-alien-variable ("daylight" unix-daylight) int)
(def-alien-variable ("timezone" unix-timezone) time-t)
(def-alien-variable ("altzone" unix-altzone) time-t)
(def-alien-variable ("tzname" unix-tzname) (array c-string 2))
(def-alien-routine get-timezone c-call:void
(when c-call:long :in)
(minutes-west c-call:int :out)
(daylight-savings-p alien:boolean :out))
(defun unix-get-minutes-west (secs)
(multiple-value-bind (ignore minutes dst) (get-timezone secs)
(declare (ignore ignore) (ignore dst))
(values minutes))
)
(defun unix-get-timezone (secs)
(multiple-value-bind (ignore minutes dst) (get-timezone secs)
(declare (ignore ignore) (ignore minutes))
(values (deref unix-tzname (if dst 1 0)))
) )
)
(declaim (inline unix-gettimeofday))
(defun unix-gettimeofday ()
"If it works, unix-gettimeofday returns 5 values: T, the seconds and
microseconds of the current time of day, the timezone (in minutes west
of Greenwich), and a daylight-savings flag. If it doesn't work, it
returns NIL and the errno."
(with-alien ((tv (struct timeval))
#-svr4 (tz (struct timezone)))
(syscall* ("gettimeofday" (* (struct timeval)) #-svr4 (* (struct timezone)))
(values T
(slot tv 'tv-sec)
(slot tv 'tv-usec)
#-svr4 (slot tz 'tz-minuteswest)
#+svr4 (unix-get-minutes-west (slot tv 'tv-sec))
#-svr4 (slot tz 'tz-dsttime)
#+svr4 (unix-get-timezone (slot tv 'tv-sec))
)
(addr tv)
;;; Unix-utimes changes the accessed and updated times on UNIX
;;; files. The first argument is the filename (a string) and
;;; the second argument is a list of the 4 times- accessed and
;;; updated seconds and microseconds.
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
(defun unix-utimes (file atime-sec atime-usec mtime-sec mtime-usec)
"Unix-utimes sets the 'last-accessed' and 'last-updated'
times on a specified file. NIL and an error number is
returned if the call is unsuccessful."
(declare (type unix-pathname file)
(type (alien unsigned-long)
atime-sec atime-usec
mtime-sec mtime-usec))
(with-alien ((tvp (array (struct timeval) 2)))
(setf (slot (deref tvp 0) 'tv-sec) atime-sec)
(setf (slot (deref tvp 0) 'tv-usec) atime-usec)
(setf (slot (deref tvp 1) 'tv-sec) mtime-sec)
(setf (slot (deref tvp 1) 'tv-usec) mtime-usec)
(void-syscall ("utimes" c-string (* (struct timeval)))
file
(cast tvp (* (struct timeval))))))
;;; Unix-setreuid sets the real and effective user-id's of the current
;;; process to the arguments "ruid" and "euid", respectively. Usage is
;;; restricted for anyone but the super-user. Setting either "ruid" or
;;; "euid" to -1 makes the system use the current id instead.
(defun unix-setreuid (ruid euid)
"Unix-setreuid sets the real and effective user-id's of the current
process to the specified ones. NIL and an error number is returned
if the call fails."
(void-syscall ("setreuid" int int) ruid euid))
;;; Unix-setregid sets the real and effective group-id's of the current
;;; process to the arguments "rgid" and "egid", respectively. Usage is
;;; restricted for anyone but the super-user. Setting either "rgid" or
;;; "egid" to -1 makes the system use the current id instead.
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
(defun unix-setregid (rgid egid)
"Unix-setregid sets the real and effective group-id's of the current
process process to the specified ones. NIL and an error number is
returned if the call fails."
(void-syscall ("setregid" int int) rgid egid))
(def-alien-routine ("getpid" unix-getpid) int
"Unix-getpid returns the process-id of the current process.")
(def-alien-routine ("getppid" unix-getppid) int
"Unix-getppid returns the process-id of the parent of the current process.")
(def-alien-routine ("getgid" unix-getgid) int
"Unix-getgid returns the real group-id of the current process.")
(def-alien-routine ("getegid" unix-getegid) int
"Unix-getegid returns the effective group-id of the current process.")
;;; Unix-getpgrp returns the group-id associated with the
;;; process whose process-id is specified as an argument.
;;; As usual, if the process-id is 0, it refers to the current
;;; process.
(defun unix-getpgrp (pid)
"Unix-getpgrp returns the group-id of the process associated
with pid."
(int-syscall ("getpgrp" int) pid))
;;; Unix-setpgrp sets the group-id of the process specified by
;;; "pid" to the value of "pgrp". The process must either have
;;; the same effective user-id or be a super-user process.
(defun unix-setpgrp (pid pgrp)
"Unix-setpgrp sets the process group on the process pid to
pgrp. NIL and an error number is returned upon failure."
(void-syscall (#-svr4 "setpgrp" #+svr4 "setpgid" int int) pid pgrp))
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
(def-alien-routine ("getuid" unix-getuid) int
"Unix-getuid returns the real user-id associated with the
current process.")
;;; Unix-getpagesize returns the number of bytes in the system page.
(defun unix-getpagesize ()
"Unix-getpagesize returns the number of bytes in a system page."
(int-syscall ("getpagesize")))
(defun unix-gethostname ()
"Unix-gethostname returns the name of the host machine as a string."
(with-alien ((buf (array char 256)))
(syscall ("gethostname" (* char) int)
(cast buf c-string)
(cast buf (* char)) 256)))
(def-alien-routine ("gethostid" unix-gethostid) unsigned-long
"Unix-gethostid returns a 32-bit integer which provides unique
identification for the host machine.")
(defun unix-fork ()
"Executes the unix fork system call. Returns 0 in the child and the pid
of the child in the parent if it works, or NIL and an error number if it
doesn't work."
(int-syscall ("fork")))
;;; Operations on Unix Directories.
(export '(open-dir read-dir close-dir))
(defstruct (directory
(:print-function %print-directory))
name
(dir-struct (required-argument) :type system-area-pointer))
(defun %print-directory (dir stream depth)
(declare (ignore depth))
(format stream "#<Directory ~S>" (directory-name dir)))
(defun open-dir (pathname)
(declare (type unix-pathname pathname))
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
(let ((kind (unix-file-kind pathname)))
(case kind
(:directory
(let ((dir-struct
(alien-funcall (extern-alien "opendir"
(function system-area-pointer
c-string))
pathname)))
(if (zerop (sap-int dir-struct))
(values nil unix-errno)
(make-directory :name pathname :dir-struct dir-struct))))
((nil)
(values nil enoent))
(t
(values nil enotdir)))))
(defun read-dir (dir)
(declare (type directory dir))
(let ((daddr (alien-funcall (extern-alien "readdir"
(function system-area-pointer
system-area-pointer))
(directory-dir-struct dir))))
(declare (type system-area-pointer daddr))
(if (zerop (sap-int daddr))
nil
(with-alien ((direct (* (struct direct)) daddr))
(let ((nlen (slot direct 'd-namlen))
(ino (slot direct 'd-ino)))
(declare (type (unsigned-byte 16) nlen))
(let ((string (make-string nlen)))
(kernel:copy-from-system-area
(alien-sap (addr (slot direct 'd-name))) 0
string (* vm:vector-data-offset vm:word-bits)
(* nlen vm:byte-bits))
(values (cast (slot direct 'd-name) c-string)
(slot direct 'd-ino))))))
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
(defun close-dir (dir)
(declare (type directory dir))
(alien-funcall (extern-alien "closedir"
(function void system-area-pointer))
(directory-dir-struct dir))
nil)
(defun unix-current-directory ()
(with-alien ((buf (array char 1024)))
(values (not (zerop (alien-funcall (extern-alien "getwd"
(function int (* char)))
(cast buf (* char)))))
(cast buf c-string))))
;;;; Support routines for dealing with unix pathnames.
(export '(unix-file-kind unix-maybe-prepend-current-directory
unix-resolve-links unix-simplify-pathname))
(defun unix-file-kind (name &optional check-for-links)
"Returns either :file, :directory, :link, :special, or NIL."
(declare (simple-string name))
(multiple-value-bind (res dev ino mode)
(if check-for-links
(unix-lstat name)
(unix-stat name))
(declare (type (or fixnum null) mode)
(ignore dev ino))
(when res
(let ((kind (logand mode s-ifmt)))
(cond ((eql kind s-ifdir) :directory)
((eql kind s-ifreg) :file)
((eql kind s-iflnk) :link)
(t :special))))))
(defun unix-maybe-prepend-current-directory (name)
(declare (simple-string name))
(if (and (> (length name) 0) (char= (schar name 0) #\/))
name
(multiple-value-bind (win dir) (unix-current-directory)
(if win
(concatenate 'simple-string dir "/" name)
name))))
(defun unix-resolve-links (pathname)
"Returns the pathname with all symbolic links resolved."
(declare (simple-string pathname))
(let ((len (length pathname))
(pending pathname))
(declare (fixnum len) (simple-string pending))
(if (zerop len)
pathname
(let ((result (make-string 1024 :initial-element (code-char 0)))
(fill-ptr 0)
(name-start 0))
(loop
(let* ((name-end (or (position #\/ pending :start name-start) len))
(new-fill-ptr (+ fill-ptr (- name-end name-start))))
(replace result pending
:start1 fill-ptr
:end1 new-fill-ptr
:start2 name-start
:end2 name-end)
(let ((kind (unix-file-kind (if (zerop name-end) "/" result) t)))
(unless kind (return nil))
(cond ((eq kind :link)
(multiple-value-bind (link err) (unix-readlink result)
(unless link
(error "Error reading link ~S: ~S"
(subseq result 0 fill-ptr)
(get-unix-error-msg err)))
(cond ((or (zerop (length link))
(char/= (schar link 0) #\/))
;; It's a relative link
(fill result (code-char 0)
:start fill-ptr
:end new-fill-ptr))
((string= result "/../" :end1 4)
;; It's across the super-root.
(let ((slash (or (position #\/ result :start 4)
0)))
(fill result (code-char 0)
:start slash
:end new-fill-ptr)
(setf fill-ptr slash)))
(t
;; It's absolute.
(and (> (length link) 0)
(char= (schar link 0) #\/))
(fill result (code-char 0) :end new-fill-ptr)
(setf fill-ptr 0)))
(setf pending
(if (= name-end len)
link
(concatenate 'simple-string
link
(subseq pending name-end))))
(setf len (length pending))
(setf name-start 0)))
((= name-end len)
(return (subseq result 0 new-fill-ptr)))
((eq kind :directory)
(setf (schar result new-fill-ptr) #\/)
(setf fill-ptr (1+ new-fill-ptr))
(setf name-start (1+ name-end)))
(t
(return nil))))))))))
(defun unix-simplify-pathname (src)
(declare (simple-string src))
(let* ((src-len (length src))
(dst (make-string src-len))
(dst-len 0)
(dots 0)
(last-slash nil))
(macrolet ((deposit (char)
`(progn
(setf (schar dst dst-len) ,char)
(incf dst-len))))
(dotimes (src-index src-len)
(let ((char (schar src src-index)))
(cond ((char= char #\.)
(when dots
(incf dots))
(deposit char))
((char= char #\/)
(case dots
(0
;; Either ``/...' or ``...//...'
(unless last-slash
(setf last-slash dst-len)
(deposit char)))
(1
;; Either ``./...'' or ``..././...''
(decf dst-len))
(2
;; We've found ..
(cond
((and last-slash (not (zerop last-slash)))
;; There is something before this ..
(let ((prev-prev-slash
(position #\/ dst :end last-slash :from-end t)))
(cond ((and (= (+ (or prev-prev-slash 0) 2)
last-slash)
(char= (schar dst (- last-slash 2)) #\.)
(char= (schar dst (1- last-slash)) #\.))
;; The something before this .. is another ..
(deposit char)
(setf last-slash dst-len))
(t
;; The something is some random dir.
(setf dst-len
(if prev-prev-slash
(1+ prev-prev-slash)
0))
(setf last-slash prev-prev-slash)))))
(t
;; There is nothing before this .., so we need to keep it
(setf last-slash dst-len)
(deposit char))))
(t
;; Something other than a dot between slashes.
(setf last-slash dst-len)
(deposit char)))
(setf dots 0))
(t
(setf dots nil)
(setf (schar dst dst-len) char)
(incf dst-len))))))
(when (and last-slash (not (zerop last-slash)))
(case dots
(1
;; We've got ``foobar/.''
(decf dst-len))
(2
;; We've got ``foobar/..''
(unless (and (>= last-slash 2)
(char= (schar dst (1- last-slash)) #\.)
(char= (schar dst (- last-slash 2)) #\.)
(or (= last-slash 2)
(char= (schar dst (- last-slash 3)) #\/)))
(let ((prev-prev-slash
(position #\/ dst :end last-slash :from-end t)))
(if prev-prev-slash
(setf dst-len (1+ prev-prev-slash))
(return-from unix-simplify-pathname "./")))))))
(cond ((zerop dst-len)
"./")
((= dst-len src-len)
dst)
(t
(subseq dst 0 dst-len)))))
;;;; Other random routines.
(def-alien-routine ("isatty" unix-isatty) boolean
"Accepts a Unix file descriptor and returns T if the device
associated with it is a terminal."
(fd int))
(def-alien-routine ("ttyname" unix-ttyname) c-string
(fd int))
;;;; UNIX-EXECVE
(defun unix-execve (program &optional arg-list
(environment *environment-list*))
"Executes the Unix execve system call. If the system call suceeds, lisp
will no longer be running in this process. If the system call fails this
function returns two values: NIL and an error code. Arg-list should be a
list of simple-strings which are passed as arguments to the exec'ed program.
Environment should be an a-list mapping symbols to simple-strings which this
function bashes together to form the environment for the exec'ed program."
(check-type program simple-string)
(let ((env-list (let ((envlist nil))
(dolist (cons environment)
(push (if (cdr cons)
(concatenate 'simple-string
(string (car cons)) "="
(cdr cons))
(car cons))
envlist))
envlist)))
(sub-unix-execve program arg-list env-list)))
(defmacro round-bytes-to-words (n)
`(logand (the fixnum (+ (the fixnum ,n) 3)) (lognot 3)))
;;;
;;; STRING-LIST-TO-C-STRVEC -- Internal
;;;
;;; STRING-LIST-TO-C-STRVEC is a function which takes a list of
;;; simple-strings and constructs a C-style string vector (strvec) --
;;; a null-terminated array of pointers to null-terminated strings.
;;; This function returns two values: a sap and a byte count. When the
;;; memory is no longer needed it should be deallocated with
;;; vm_deallocate.
;;;
(defun string-list-to-c-strvec (string-list)
;;
;; Make a pass over string-list to calculate the amount of memory
;; needed to hold the strvec.
(let ((string-bytes 0)
(vec-bytes (* 4 (1+ (length string-list)))))
(declare (fixnum string-bytes vec-bytes))
(dolist (s string-list)
(check-type s simple-string)
(incf string-bytes (round-bytes-to-words (1+ (length s)))))
;;
;; Now allocate the memory and fill it in.
(let* ((total-bytes (+ string-bytes vec-bytes))
(vec-sap (system:allocate-system-memory total-bytes))
(string-sap (sap+ vec-sap vec-bytes))
(i 0))
(declare (type (and unsigned-byte fixnum) total-bytes i)
(type system:system-area-pointer vec-sap string-sap))
(dolist (s string-list)
(declare (simple-string s))
(let ((n (length s)))
;;
;; Blast the string into place
(kernel:copy-to-system-area (the simple-string s)
(* vm:vector-data-offset vm:word-bits)
string-sap 0
(* (1+ n) vm:byte-bits))
;;
;; Blast the pointer to the string into place
(setf (sap-ref-sap vec-sap i) string-sap)
(setf string-sap (sap+ string-sap (round-bytes-to-words (1+ n))))
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
;; Blast in last null pointer
(setf (sap-ref-sap vec-sap i) (int-sap 0))
(values vec-sap total-bytes))))
(defun sub-unix-execve (program arg-list env-list)
(let ((argv nil)
(argv-bytes 0)
(envp nil)
(envp-bytes 0)
result error-code)
(unwind-protect
(progn
;; Blast the stuff into the proper format
(multiple-value-setq
(argv argv-bytes)
(string-list-to-c-strvec arg-list))
(multiple-value-setq
(envp envp-bytes)
(string-list-to-c-strvec env-list))
;;
;; Now do the system call
(multiple-value-setq
(result error-code)
(int-syscall ("execve"
(* char) system-area-pointer system-area-pointer)
(vector-sap program) argv envp)))
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
;;
;; Deallocate memory
(when argv
(system:deallocate-system-memory argv argv-bytes))
(when envp
(system:deallocate-system-memory envp envp-bytes)))
(values result error-code)))
;;;; Socket support.
(def-alien-routine ("socket" unix-socket) int
(domain int)
(type int)
(protocol int))
(def-alien-routine ("connect" unix-connect) int
(socket int)
(sockaddr (* t))
(len int))
(def-alien-routine ("bind" unix-bind) int
(socket int)
(sockaddr (* t))
(len int))
(def-alien-routine ("listen" unix-listen) int
(socket int)
(backlog int))
(def-alien-routine ("accept" unix-accept) int
(socket int)
(sockaddr (* t))
(len int :in-out))
(def-alien-routine ("recv" unix-recv) int
(fd int)
(buffer c-string)
(length int)
(flags int))
(def-alien-routine ("send" unix-send) int
(fd int)
(buffer c-string)
(length int)
(flags int))