Newer
Older
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
(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.
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
(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 ()
"Unix-getpgrp returns the group-id of the calling process."
(int-syscall ("getpgrp")))
;;; 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.
;;; setpgrp(int int)[freebsd] is identical to setpgid and is retained
;;; for backward compatibility. setpgrp(void)[solaris] is being phased
;;; out in favor of setsid().
(defun unix-setpgrp (pid pgrp)
"Unix-setpgrp sets the process group on the process pid to
pgrp. NIL and an error number are returned upon failure."
(void-syscall (#-svr4 "setpgrp" #+svr4 "setpgid" int int) pid pgrp))
(defun unix-setpgid (pid pgrp)
"Unix-setpgid sets the process group of the process pid to
pgrp. If pgid is equal to pid, the process becomes a process
group leader. NIL and an error number are returned upon failure."
(void-syscall ("setpgid" int int) pid pgrp))
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
(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))
(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))))))
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
#+bsd
(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))
(fino (slot direct 'd-fileno)))
(declare (type (unsigned-byte 8) nlen)
(type (unsigned-byte 32) fino))
(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 string fino)))))))
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
(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))))
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
;; 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)))
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
;;
;; 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))
(def-alien-routine ("getpeername" unix-getpeername) int
(socket int)
(sockaddr (* t))
(len (* unsigned)))
(def-alien-routine ("getsockname" unix-getsockname) int
(socket int)
(sockaddr (* t))
(len (* unsigned)))
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
;;;
;;; Support for the Interval Timer (experimental)
;;;
(defconstant ITIMER-REAL 0)
(defconstant ITIMER-VIRTUAL 1)
(defconstant ITIMER-PROF 2)
(defun unix-getitimer(which)
"Unix-getitimer returns the INTERVAL and VALUE slots of one of
three system timers (:real :virtual or :profile). On success,
unix-getitimer returns 5 values,
T, it-interval-secs, it-interval-usec, it-value-secs, it-value-usec."
(declare (type (member :real :virtual :profile) which)
(values t
(unsigned-byte 29)(mod 1000000)
(unsigned-byte 29)(mod 1000000)))
(let ((which (ecase which
(:real ITIMER-REAL)
(:virtual ITIMER-VIRTUAL)
(:profile ITIMER-PROF))))
(with-alien ((itv (struct itimerval)))
(syscall* ("getitimer" int (* (struct itimerval)))
(values T
(slot (slot itv 'it-interval) 'tv-sec)
(slot (slot itv 'it-interval) 'tv-usec)
(slot (slot itv 'it-value) 'tv-sec)
(slot (slot itv 'it-value) 'tv-usec))
which (alien-sap (addr itv))))))
(defun unix-setitimer(which int-secs int-usec val-secs val-usec)
" Unix-setitimer sets the INTERVAL and VALUE slots of one of
three system timers (:real :virtual or :profile). A SIGALRM signal
will be delivered VALUE <seconds+microseconds> from now. INTERVAL,
when non-zero, is <seconds+microseconds> to be loaded each time
the timer expires. Setting INTERVAL and VALUE to zero disables
the timer. See the Unix man page for more details. On success,
unix-setitimer returns the old contents of the INTERVAL and VALUE
slots as in unix-getitimer."
(declare (type (member :real :virtual :profile) which)
(type (unsigned-byte 29) int-secs val-secs)
(type (integer 0 (1000000)) int-usec val-usec)
(values t
(unsigned-byte 29)(mod 1000000)
(unsigned-byte 29)(mod 1000000)))
(let ((which (ecase which
(:real ITIMER-REAL)
(:virtual ITIMER-VIRTUAL)
(:profile ITIMER-PROF))))
(with-alien ((itvn (struct itimerval))
(itvo (struct itimerval)))
(setf (slot (slot itvn 'it-interval) 'tv-sec ) int-secs
(slot (slot itvn 'it-interval) 'tv-usec) int-usec
(slot (slot itvn 'it-value ) 'tv-sec ) val-secs
(slot (slot itvn 'it-value ) 'tv-usec) val-usec)
(syscall* ("setitimer" int (* (struct timeval))(* (struct timeval)))
(values T
(slot (slot itvo 'it-interval) 'tv-sec)
(slot (slot itvo 'it-interval) 'tv-usec)
(slot (slot itvo 'it-value) 'tv-sec)
(slot (slot itvo 'it-value) 'tv-usec))
which (alien-sap (addr itvn))(alien-sap (addr itvo))))))