diff --git a/smartmontools/ChangeLog b/smartmontools/ChangeLog
index 0612a64f06882f45b671019f86c56f5dc2a7e22e..cd35d1f00808f6c8eef1a0b4178ef3c3dcaa7cd9 100644
--- a/smartmontools/ChangeLog
+++ b/smartmontools/ChangeLog
@@ -1,5 +1,12 @@
 $Id$
 
+2022-08-06  Christian Franke  <franke@computer.org>
+
+	smartd.cpp, popen_as_ugid.cpp: Don't use 'getdtablesize()'.
+	This function is declared 'legacy' since SUS 1997 and no longer part
+	of POSIX since 2004.  Use 'sysconf(_SC_OPEN_MAX)' instead.
+	This fixes build on android (GH issues/142).
+
 2022-08-06  themylogin  <themylogin@gmail.com>
 
 	smartd.cpp: Also prevent systemd unit startup timeout in
diff --git a/smartmontools/popen_as_ugid.cpp b/smartmontools/popen_as_ugid.cpp
index 4db2f95d2bb4b980079d30486abbe3a0997acef1..2d3235a4fd250d1389934977512666dc107c7227 100644
--- a/smartmontools/popen_as_ugid.cpp
+++ b/smartmontools/popen_as_ugid.cpp
@@ -67,7 +67,7 @@ FILE * popen_as_ugid(const char * cmd, const char * mode, uid_t uid, gid_t gid)
   if (!pid) { // Child
     // Do not inherit any unneeded file descriptors
     fclose(fp);
-    for (int i = 0; i < getdtablesize(); i++) {
+    for (int i = sysconf(_SC_OPEN_MAX); --i >= 0; ) {
       if (i == pd[1] || i == sd[1])
         continue;
       close(i);
diff --git a/smartmontools/smartd.cpp b/smartmontools/smartd.cpp
index 0e0503fb6da37ff073567548cce30f4ed7395f28..b0150c43158651004b25ebc85e92efafa9e86e6b 100644
--- a/smartmontools/smartd.cpp
+++ b/smartmontools/smartd.cpp
@@ -1448,7 +1448,7 @@ static int daemon_init()
   }
 
   // close any open file descriptors
-  for (int i = getdtablesize(); --i >= 0; )
+  for (int i = sysconf(_SC_OPEN_MAX); --i >= 0; )
     close(i);
   
   // redirect any IO attempts to /dev/null and change to root directory