Subject: bug#53026: 27.2; (battery) errors in Android app Termux

From: Lucien Cartier-Tilet <[email-masked]>
Date: Wed, 05 Jan 2022 15:08:33 +0100

In the Termux application on Android, evaluating `(battery)' results in
an error due to `/sys/' not being readable on non-rooted phones.

Steps to reproduce:
1. In termux, install Emacs with `pkg install emacs'.
2. Launch Emacs with `emacs -Q'
3. In the `*scratch*' buffer, type and evaluate `(battery)'
4. The expected output due to `/sys/' being unreadable should be the
   text `Battery status not available', however it errors with the
   attached backtrace.

I made a patch I attached to this message in an attempt to prevent
this issue, but I have not had the time to test it yet on my own
phone. It is made from the current `master' branch.

Text automatically generated by Emacs on Termux using
`report-emacs-bug':

In GNU Emacs 27.2 (build 1, aarch64-unknown-linux-android)
 of 2022-01-05 built on localhost
Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Entering debugger...
Making completion list...

Configured using:
 'configure --disable-dependency-tracking
 --prefix=/data/data/com.termux/files/usr
 --libdir=/data/data/com.termux/files/usr/lib
 --sbindir=/data/data/com.termux/files/usr/bin --disable-rpath
 --disable-rpath-hack --host=aarch64-linux-android --disable-autodepend
 --with-gif=no --with-gnutls --with-jpeg=no --without-gconf
 --without-gsettings --without-lcms2 --without-x --with-png=no
 --with-tiff=no --with-xml2 --with-xpm=no --without-dbus
 --without-selinux --with-modules --with-pdumper=yes --with-dumping=none
 emacs_cv_sanitize_address=yes emacs_cv_prog_cc_no_pie=no
 ac_cv_lib_elf_elf_begin=no gl_cv_func_dup2_works=no
 ac_cv_func_setrlimit=no --disable-nls --enable-shared --enable-static
 --libexecdir=/data/data/com.termux/files/usr/libexec 'CFLAGS=
 -fstack-protector-strong -Oz' 'CPPFLAGS=
 -I/data/data/com.termux/files/usr/include'
 'LDFLAGS=-L/data/data/com.termux/files/usr/lib
 -Wl,-rpath=/data/data/com.termux/files/usr/lib	-fopenmp -static-openmp
 -Wl,--enable-new-dtags -Wl,--as-needed -Wl,-z,relro,-z,now''

Configured features:
NOTIFY INOTIFY GNUTLS LIBXML2 ZLIB MODULES THREADS PDUMPER GMP

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix:

Major mode: Debugger

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow regexp-opt sort mail-extr emacsbug message rmc puny dired
dired-loaddefs format-spec rfc822 mml mml-sec password-cache epa derived
epg epg-config gnus-util rmail tool-bar rmail-loaddefs
text-property-search time-date subr-x seq mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils help-fns radix-tree
cl-print debug backtrace help-mode easymenu find-func cl-loaddefs cl-lib
term/xterm xterm byte-opt gv bytecomp byte-compile cconv tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type tabulated-list
replace newcomment text-mode elisp-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch timer select mouse jit-lock
font-lock syntax facemenu font-core term/tty-colors frame minibuffer
cl-generic cham georgian utf-8-lang misc-lang vietnamese tibetan thai
tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek romanian
slovak czech european ethiopic indian cyrillic chinese composite
charscript charprop case-table epa-hook jka-cmpr-hook help simple abbrev
obarray cl-preloaded nadvice loaddefs button faces cus-face macroexp
files text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads inotify
multi-tty make-network-process emacs)

Memory information:
((conses 16 56686 6907)
 (symbols 48 6421 1)
 (strings 32 17408 1595)
 (string-bytes 1 567066)
 (vectors 16 8161)
 (vector-slots 8 90784 8674)
 (floats 8 29 697)
 (intervals 56 728 124)
 (buffers 1000 13))

--
Lucien “Phundrak” Cartier-Tilet

<https://phundrak.com> (Français)

<https://phundrak.com/en> (English)

Sent from GNU/Emacs
From a6ea372e02c5f8e583ea216743cf238854a8fc90 Mon Sep 17 00:00:00 2001
From: Lucien Cartier-Tilet <[email-masked]>
Date: Wed, 5 Jan 2022 15:59:33 +0100
Subject: [PATCH] ; * lisp/battery.el (battery-status-function) Fix errors in
 Termux

In Termux, neither /sys/ or /proc/ are readable on phones that are not
rooted. This patch makes Emacs verify if they are readable before it
attemps reading them.
---
 lisp/battery.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/battery.el b/lisp/battery.el
index 45334163fa..b7b81a11a1 100644
--- a/lisp/battery.el
+++ b/lisp/battery.el
@@ -96,12 +96,14 @@ Value does not include \".\" or \"..\"."
   (cond ((member battery-upower-service (dbus-list-activatable-names))
          #'battery-upower)
         ((and (eq system-type 'gnu/linux)
+              (file-readable-p "/sys/")
               (battery--find-linux-sysfs-batteries))
          #'battery-linux-sysfs)
 	((and (eq system-type 'gnu/linux)
 	      (file-directory-p "/proc/acpi/battery"))
 	 #'battery-linux-proc-acpi)
 	((and (eq system-type 'gnu/linux)
+              (file-readable-p "/proc/")
               (file-readable-p "/proc/apm"))
          #'battery-linux-proc-apm)
 	((and (eq system-type 'berkeley-unix)
-- 
2.34.1