aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDanny Milosavljevic <dannym@scratchpost.org>2017-07-04 02:56:02 +0200
committerDanny Milosavljevic <dannym@scratchpost.org>2017-07-12 16:15:30 +0200
commit0bc6fe323d34aabc7f51fa80b8872a2c1770d32a (patch)
tree9bdf1dd7363cf0398683faf3d7197bbce4b54fd7
parent839ee8d5477bf75764afe18d9cb2addc7c0acf05 (diff)
downloadguix-0bc6fe323d34aabc7f51fa80b8872a2c1770d32a.tar.gz
guix-0bc6fe323d34aabc7f51fa80b8872a2c1770d32a.zip
syscalls: Add network-interface-running?
* guix/build/syscalls.scm (network-interface-running?): New variable. Export it. * tests/syscalls.scm: Add test. Co-authored-by: John Darrington <jmd@gnu.org>
-rw-r--r--guix/build/syscalls.scm9
-rw-r--r--tests/syscalls.scm10
2 files changed, 19 insertions, 0 deletions
diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm
index 549612fa3c..33a23edaac 100644
--- a/guix/build/syscalls.scm
+++ b/guix/build/syscalls.scm
@@ -92,6 +92,7 @@
all-network-interface-names
network-interface-names
network-interface-netmask
+ network-interface-running?
loopback-network-interface?
network-interface-address
set-network-interface-netmask
@@ -1156,6 +1157,7 @@ bytes."
(define-as-needed IFF_UP #x1) ;Interface is up
(define-as-needed IFF_BROADCAST #x2) ;Broadcast address valid.
(define-as-needed IFF_LOOPBACK #x8) ;Is a loopback net.
+(define-as-needed IFF_RUNNING #x40) ;interface RFC2863 OPER_UP
(define IF_NAMESIZE 16) ;maximum interface name size
@@ -1330,6 +1332,13 @@ interface NAME."
(close-port sock)
(not (zero? (logand flags IFF_LOOPBACK)))))
+(define (network-interface-running? name)
+ "Return true if NAME designates a running network interface."
+ (let* ((sock (socket SOCK_STREAM AF_INET 0))
+ (flags (network-interface-flags sock name)))
+ (close-port sock)
+ (not (zero? (logand flags IFF_RUNNING)))))
+
(define-as-needed (set-network-interface-flags socket name flags)
"Set the flag of network interface NAME to FLAGS."
(let ((req (make-bytevector ifreq-struct-size)))
diff --git a/tests/syscalls.scm b/tests/syscalls.scm
index 8c048e6109..2b5c4c3be1 100644
--- a/tests/syscalls.scm
+++ b/tests/syscalls.scm
@@ -361,6 +361,16 @@
(lambda args
(system-error-errno args)))))
+(test-equal "loopback-network-interface-running?"
+ ENODEV
+ (and (network-interface-running? "lo")
+ (catch 'system-error
+ (lambda ()
+ (network-interface-running? "nonexistent")
+ #f)
+ (lambda args
+ (system-error-errno args)))))
+
(test-skip (if (zero? (getuid)) 1 0))
(test-assert "set-network-interface-flags"
(let ((sock (socket AF_INET SOCK_STREAM 0)))