another pointless yak shave: autodetect hostname

This commit is contained in:
xenia 2020-12-26 03:55:16 -05:00
parent de032b9237
commit 2d1b6e864e
3 changed files with 24 additions and 5 deletions

View File

@ -865,7 +865,8 @@
listen-addr (config-get 'listen-port integer?))) listen-addr (config-get 'listen-port integer?)))
(define public-addr (define public-addr
(match (config-get 'public-addr (or/c 'auto string?)) (match (config-get 'public-addr (or/c 'auto string?))
['auto (error "TODO auto public-addr unimplemented")] ['auto (with-handlers ([exn:fail? (lambda (_) (error "failed to autodetect hostname"))])
(get-hostname))]
[addr addr])) [addr addr]))
(define public-port (define public-port
(match (config-get 'public-port (or/c 'auto integer?)) (match (config-get 'public-port (or/c 'auto integer?))

View File

@ -86,9 +86,9 @@
;; misc ffi calls not provided by racket (because ????) ;; misc ffi calls not provided by racket (because ????)
;; racket should have fsync like cmon ;; racket should have fsync like cmon
(module+ misc-calls (module+ misc-calls
(require ffi/unsafe/port racket/match) (require ffi/unsafe/port ffi/vector racket/list racket/match racket/string)
(provide port-fsync count-cpus current-seconds-monotonic) (provide port-fsync count-cpus current-seconds-monotonic get-hostname)
;; XXX : platform-specific behavior ;; XXX : platform-specific behavior
@ -120,6 +120,24 @@
['unix (port-fsync/unix port)] ['unix (port-fsync/unix port)]
[x (error "don't know how to fsync on" x)])) [x (error "don't know how to fsync on" x)]))
(define get-hostname/unix
(let ([gethostname-call #f])
(lambda ()
(when (false? gethostname-call)
(set! gethostname-call
(get-ffi-obj/runtime "gethostname" (get-libc/init)
(_fun #:save-errno 'posix (name : (_bytes o 256)) (size : _int = 256)
-> (res : _int)
-> (if (zero? res)
(cast name _bytes _string/utf-8)
(raise (exn:fail (format "gethostname: errno ~a" (saved-errno))
(current-continuation-marks))))))))
(gethostname-call))))
(define (get-hostname)
(match (system-type 'os)
['unix (get-hostname/unix)]
[x (error "don't know how to get-hostname on" x)]))
;; this provides an actual count of the number of CPUs on the system, even without ;; this provides an actual count of the number of CPUs on the system, even without
;; --enable-futures by hooking into the underlying rktio call that gets skipped when the current ;; --enable-futures by hooking into the underlying rktio call that gets skipped when the current

View File

@ -5,7 +5,7 @@
(listen-addr "0.0.0.0") (listen-addr "0.0.0.0")
(listen-port 25446) (listen-port 25446)
;; the "public" ip (or domain name) and port of this node ;; the "public" ip (or domain name) and port of this node
;; - auto for addr selects the first interface addr (linux only) ;; - auto for addr uses /etc/hostname, if present (linux only)
;; - auto for port uses the same as listen-port ;; - auto for port uses the same as listen-port
(public-addr "127.0.0.1") (public-addr auto)
(public-port auto)) (public-port auto))