From eb61f834ed6ff13f3c03f8f1bd1234914905de63 Mon Sep 17 00:00:00 2001
From: chrfranke <chrfranke@4ea69e1a-61f1-4043-bf83-b5c94c648137>
Date: Sat, 6 Aug 2022 15:42:01 +0000
Subject: [PATCH] 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).

git-svn-id: https://svn.code.sf.net/p/smartmontools/code/trunk@5402 4ea69e1a-61f1-4043-bf83-b5c94c648137
---
 smartmontools/ChangeLog         | 7 +++++++
 smartmontools/popen_as_ugid.cpp | 2 +-
 smartmontools/smartd.cpp        | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/smartmontools/ChangeLog b/smartmontools/ChangeLog
index 0612a64f0..cd35d1f00 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 4db2f95d2..2d3235a4f 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 0e0503fb6..b0150c431 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
-- 
GitLab