aboutsummaryrefslogtreecommitdiffstats
path: root/test/lisp
diff options
context:
space:
mode:
authorMichael Albinus2026-02-07 11:32:54 +0100
committerMichael Albinus2026-02-07 11:32:54 +0100
commit89209a83b60c87d97f0c05dbf6cb29ff3cdf3d5a (patch)
tree7eb4fe230b36d619b51eeecd6c0a9868fa268b5b /test/lisp
parente1524740bef6cee52e138a086e43988a16ed703e (diff)
downloademacs-89209a83b60c87d97f0c05dbf6cb29ff3cdf3d5a.tar.gz
emacs-89209a83b60c87d97f0c05dbf6cb29ff3cdf3d5a.zip
Support D-Bus file descriptor manipulation
* doc/misc/dbus.texi (Synchronous Methods): Adapt `dbus-call-method'. (Asynchronous Methods): Adapt `dbus-call-method-asynchronously'. (File Descriptors): New chapter, replaces Inhibitor Locks. * etc/NEWS: Replace "New D-Bus functions to support systemd inhibitor locks" by "Support D-Bus file descriptor manipulation". Presentational fixes and improvements. * lisp/net/dbus.el (dbus-call-method) (dbus-call-method-asynchronously): Adapt docstring. (dbus-list-hash-table): Return (nreverse result). (dbus-monitor-goto-serial): Declare `completion'. * src/dbusbind.c (Fdbus_message_internal, xd_read_message_1): Handle `:keep-fd'. (xd_registered_inhibitor_locks, Fdbus_make_inhibitor_lock) (Fdbus_close_inhibitor_lock, Fdbus_registered_inhibitor_locks): Delete. (xd_registered_fds): New variable. (Fdbus__fd_open, Fdbus__fd_close, Fdbus__registered_fds): New DEFUNs. (Bug#79963) (syms_of_dbusbind_for_pdumper): Initialize `xd_registered_fds'. (syms_of_dbusbind): Remove subroutines `Sdbus_make_inhibitor_lock', `Sdbus_close_inhibitor_lock' and `Sdbus_registered_inhibitor_locks'. Remove symbol `Qdbus_call_method'. Declare subroutines `Sdbus__fd_open', `Sdbus__fd_close' and `Sdbus__registered_fds'. Declare symbol `QCkeep_fd'. staticpro `xd_registered_fds'. * test/lisp/net/dbus-tests.el (dbus-test10-inhibitor-locks): Delete. (dbus-test10-keep-fd, dbus-test10-open-close-fd): New tests.
Diffstat (limited to 'test/lisp')
-rw-r--r--test/lisp/net/dbus-tests.el167
1 files changed, 117 insertions, 50 deletions
diff --git a/test/lisp/net/dbus-tests.el b/test/lisp/net/dbus-tests.el
index 53ce1929cad..f4dd9e3796b 100644
--- a/test/lisp/net/dbus-tests.el
+++ b/test/lisp/net/dbus-tests.el
@@ -2308,89 +2308,156 @@ The argument EXPECTED-ARGS is a list of expected arguments for the method."
2308 ;; Cleanup. 2308 ;; Cleanup.
2309 (dbus-unregister-service :session dbus--test-service))) 2309 (dbus-unregister-service :session dbus--test-service)))
2310 2310
2311(ert-deftest dbus-test10-inhibitor-locks () 2311(ert-deftest dbus-test10-keep-fd ()
2312 "Check `dbus-*-inhibitor-locks'." 2312 "Check D-Bus `:keep-fd' argument."
2313 :tags '(:expensive-test) 2313 :tags '(:expensive-test)
2314 (skip-unless dbus--test-enabled-system-bus) 2314 (skip-unless dbus--test-enabled-system-bus)
2315 (skip-unless (dbus-ping :system dbus--test-systemd-service 1000)) 2315 (skip-unless (dbus-ping :system dbus--test-systemd-service 1000))
2316 2316
2317 (let (lock1 lock2) 2317 (let ((what "sleep")
2318 (who "Emacs test user")
2319 (why "Test delay")
2320 (mode "delay")
2321 (fd-directory (format "/proc/%d/fd" (emacs-pid)))
2322 lock1 lock2)
2318 ;; Create inhibitor lock. 2323 ;; Create inhibitor lock.
2319 (setq lock1 (dbus-make-inhibitor-lock "sleep" "Test delay")) 2324 (setq lock1
2325 (dbus-call-method
2326 :system dbus--test-systemd-service dbus--test-systemd-path
2327 dbus--test-systemd-manager-interface "Inhibit"
2328 what who why mode))
2320 (should (natnump lock1)) 2329 (should (natnump lock1))
2321 ;; The lock is reported by systemd. 2330 ;; The lock is reported by systemd.
2322 (should 2331 (should
2323 (member 2332 (member
2324 (list "sleep" "Emacs" "Test delay" "delay" (user-uid) (emacs-pid)) 2333 (list what who why mode (user-uid) (emacs-pid))
2325 (dbus-call-method 2334 (dbus-call-method
2326 :system dbus--test-systemd-service dbus--test-systemd-path 2335 :system dbus--test-systemd-service dbus--test-systemd-path
2327 dbus--test-systemd-manager-interface "ListInhibitors"))) 2336 dbus--test-systemd-manager-interface "ListInhibitors")))
2328 ;; The lock is registered internally. 2337 ;; The lock is not registered internally.
2329 (should 2338 (should-not (assoc lock1 (dbus--registered-fds)))
2330 (member
2331 (list lock1 "sleep" "Test delay" nil)
2332 (dbus-registered-inhibitor-locks)))
2333 ;; There exist a file descriptor. 2339 ;; There exist a file descriptor.
2334 (when (file-directory-p (format "/proc/%d/fd" (emacs-pid))) 2340 (when (file-directory-p fd-directory)
2335 (should (file-symlink-p (format "/proc/%d/fd/%d" (emacs-pid) lock1)))) 2341 (should
2336 2342 (file-symlink-p
2337 ;; It is not possible to modify registered inhibitor locks on Lisp level. 2343 (expand-file-name (number-to-string lock1) fd-directory))))
2338 (setcar (assoc lock1 (dbus-registered-inhibitor-locks)) 'malicious)
2339 (should (assoc lock1 (dbus-registered-inhibitor-locks)))
2340 (should-not (assoc 'malicious (dbus-registered-inhibitor-locks)))
2341
2342 ;; Creating it again returns the same inhibitor lock.
2343 (should (= lock1 (dbus-make-inhibitor-lock "sleep" "Test delay")))
2344 2344
2345 ;; Create another inhibitor lock. 2345 ;; Create another inhibitor lock. Keep the file descriptor.
2346 (setq lock2 (dbus-make-inhibitor-lock "sleep" "Test block" 'block)) 2346 (setq lock2
2347 (dbus-call-method
2348 :system dbus--test-systemd-service dbus--test-systemd-path
2349 dbus--test-systemd-manager-interface "Inhibit" :keep-fd
2350 what who why mode))
2347 (should (natnump lock2)) 2351 (should (natnump lock2))
2348 (should-not (= lock1 lock2)) 2352 (should-not (= lock1 lock2))
2349 ;; The lock is reported by systemd. 2353 ;; The lock is reported by systemd.
2350 (should 2354 (should
2351 (member 2355 (member
2352 (list "sleep" "Emacs" "Test block" "block" (user-uid) (emacs-pid)) 2356 (list what who why mode (user-uid) (emacs-pid))
2353 (dbus-call-method 2357 (dbus-call-method
2354 :system dbus--test-systemd-service dbus--test-systemd-path 2358 :system dbus--test-systemd-service dbus--test-systemd-path
2355 dbus--test-systemd-manager-interface "ListInhibitors"))) 2359 dbus--test-systemd-manager-interface "ListInhibitors")))
2356 ;; The lock is registered internally. 2360 ;; The lock is registered internally.
2357 (should 2361 (should
2358 (member 2362 (member
2359 (list lock2 "sleep" "Test block" t) 2363 (cons lock2 dbus--test-systemd-path)
2360 (dbus-registered-inhibitor-locks))) 2364 (dbus--registered-fds)))
2361 ;; There exist a file descriptor. 2365 ;; There exist a file descriptor.
2362 (when (file-directory-p (format "/proc/%d/fd" (emacs-pid))) 2366 (when (file-directory-p fd-directory)
2363 (should (file-symlink-p (format "/proc/%d/fd/%d" (emacs-pid) lock2)))) 2367 (should
2364 2368 (file-symlink-p
2365 ;; Close the first inhibitor lock. 2369 (expand-file-name (number-to-string lock2) fd-directory))))
2366 (should (dbus-close-inhibitor-lock lock1)) 2370
2367 ;; The internal registration has gone. 2371 ;; Create another inhibitor lock via
2368 (should-not 2372 ;; `dbus-call-method-asynchronously'. Keep the file descriptor.
2369 (member 2373 (setq lock1 nil)
2370 (list lock1 "sleep" "Test delay" nil) 2374 (dbus-call-method-asynchronously
2371 (dbus-registered-inhibitor-locks))) 2375 :system dbus--test-systemd-service dbus--test-systemd-path
2372 ;; The file descriptor has been deleted. 2376 dbus--test-systemd-manager-interface "Inhibit"
2373 (when (file-directory-p (format "/proc/%d/fd" (emacs-pid))) 2377 (lambda (lock) (setq lock1 lock)) :keep-fd
2374 (should-not (file-symlink-p (format "/proc/%d/fd/%d" (emacs-pid) lock1)))) 2378 what who why mode)
2375 2379 (with-timeout (1 (dbus--test-timeout-handler))
2376 ;; Closing it again is a noop. 2380 (while (null lock1) (read-event nil nil 0.1)))
2377 (should-not (dbus-close-inhibitor-lock lock1))
2378
2379 ;; Creating it again returns (another?) inhibitor lock.
2380 (setq lock1 (dbus-make-inhibitor-lock "sleep" "Test delay"))
2381 (should (natnump lock1)) 2381 (should (natnump lock1))
2382 (should-not (= lock1 lock2))
2382 ;; The lock is registered internally. 2383 ;; The lock is registered internally.
2383 (should 2384 (should
2384 (member 2385 (member
2385 (list lock1 "sleep" "Test delay" nil) 2386 (cons lock1 dbus--test-systemd-path)
2386 (dbus-registered-inhibitor-locks))) 2387 (dbus--registered-fds)))
2387 ;; There exist a file descriptor. 2388 ;; There exist a file descriptor.
2388 (when (file-directory-p (format "/proc/%d/fd" (emacs-pid))) 2389 (when (file-directory-p fd-directory)
2389 (should (file-symlink-p (format "/proc/%d/fd/%d" (emacs-pid) lock1)))) 2390 (should
2391 (file-symlink-p
2392 (expand-file-name (number-to-string lock1) fd-directory))))
2393
2394 ;; It is not possible to modify registered inhibitor locks on Lisp level.
2395 (setcar (assoc lock1 (dbus--registered-fds)) 'malicious)
2396 (should (assoc lock1 (dbus--registered-fds)))
2397 (should-not (assoc 'malicious (dbus--registered-fds)))
2390 2398
2391 ;; Close the inhibitor locks. 2399 ;; Close the inhibitor locks.
2392 (should (dbus-close-inhibitor-lock lock1)) 2400 (should (dbus--fd-close lock1))
2393 (should (dbus-close-inhibitor-lock lock2)))) 2401 (should (dbus--fd-close lock2))
2402 ;; The internal registration has gone.
2403 (should-not
2404 (member
2405 (cons lock1 dbus--test-systemd-path)
2406 (dbus--registered-fds)))
2407 (should-not
2408 (member
2409 (cons lock2 dbus--test-systemd-path)
2410 (dbus--registered-fds)))
2411 ;; The file descriptors have been deleted.
2412 (when (file-directory-p fd-directory)
2413 (should-not
2414 (file-exists-p (expand-file-name (number-to-string lock1) fd-directory)))
2415 (should-not
2416 (file-exists-p (expand-file-name (number-to-string lock2) fd-directory))))
2417
2418 ;; Closing them again is a noop.
2419 (should-not (dbus--fd-close lock1))
2420 (should-not (dbus--fd-close lock2))))
2421
2422(ert-deftest dbus-test10-open-close-fd ()
2423 "Check D-Bus open/close a file descriptor."
2424 :tags '(:expensive-test)
2425 (skip-unless dbus--test-enabled-system-bus)
2426 (skip-unless (dbus-ping :system dbus--test-systemd-service 1000))
2427
2428 (ert-with-temp-file tmpfile
2429 (let ((fd-directory (format "/proc/%d/fd" (emacs-pid)))
2430 fd)
2431 ;; Create file descriptor.
2432 (setq fd (dbus--fd-open tmpfile))
2433 (should (natnump fd))
2434 ;; The file descriptor is registered internally.
2435 (should (member (cons fd tmpfile) (dbus--registered-fds)))
2436 ;; There exist a file descriptor file.
2437 (when (file-directory-p fd-directory)
2438 (should
2439 (file-symlink-p (expand-file-name (number-to-string fd) fd-directory)))
2440 (should
2441 (string-equal
2442 (file-truename (expand-file-name (number-to-string fd) fd-directory))
2443 tmpfile)))
2444
2445 ;; It is not possible to modify registered file descriptors on Lisp level.
2446 (setcar (assoc fd (dbus--registered-fds)) 'malicious)
2447 (should (assoc fd (dbus--registered-fds)))
2448 (should-not (assoc 'malicious (dbus--registered-fds)))
2449
2450 ;; Close the file descriptor.
2451 (should (dbus--fd-close fd))
2452 ;; The internal registration has gone.
2453 (should-not (member (cons fd tmpfile) (dbus--registered-fds)))
2454 ;; The file descriptor file has been deleted.
2455 (when (file-directory-p fd-directory)
2456 (should-not
2457 (file-exists-p (expand-file-name (number-to-string fd) fd-directory))))
2458
2459 ;; Closing it again is a noop.
2460 (should-not (dbus--fd-close fd)))))
2394 2461
2395(defun dbus-test-all (&optional interactive) 2462(defun dbus-test-all (&optional interactive)
2396 "Run all tests for \\[dbus]." 2463 "Run all tests for \\[dbus]."