[Toybox] [PATCH] probe for utmpx, shadow/getspnam, forkpty

Isaac Dunham ibid.ag at gmail.com
Fri Oct 31 00:14:05 PDT 2014


Probe for utmpx support, shadow.h/getspnam(), and pty.h/forkpty().

Android does not provide any of these headers, so probe for them at
compile time and select what to build based on presence.

--
This addresses some of the comments Jason Spiro referred to.

If someone could test this with the NDK, it would be nice. (See links below
for some info that *might* be helpful.)

#include <shadow.h> would properly be wrapped in USE_TOYBOX_SHADOW()
rather than #if defined(__ANDROID__)

The part making utmpx optional in uptime.c is rather messy.

There may well be further patches needed; an old patch series suggests
that there's a need to add swapon/swapoff/sethostname to lib/portability.c
and some other changes also:
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001991.html
(hardcodes sender's site configuration; only interesting as background)
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001992.html
(no -lutil)
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001993.html
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001994.html
(statfs for statvfs, no pty.h or sys/swap.h)
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001995.html
(no struct winsize)
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001997.html
(try to make getmuntlist work on Android)
http://lists.landley.net/pipermail/toybox-landley.net/2012-March/001999.html
(define swapon/swapoff/sethostname)

http://lists.landley.net/pipermail/toybox-landley.net/2012-March/002000.html
is apparently obsoleted by this patch and previous changes.
A couple other patches in that series are also obsolete.

Hope this helps,
Isaac Dunham

-------------- next part --------------
diff --git a/lib/portability.h b/lib/portability.h
index b7a7c79..b3c2537 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -180,7 +180,10 @@ ssize_t getline(char **lineptr, size_t *n, FILE *stream);
 #endif
 
 // Linux headers not listed by POSIX or LSB
+#ifndef __ANDROID__
 #include <shadow.h>
+#endif
+
 #include <sys/mount.h>
 #include <sys/swap.h>
 
diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh
index b50c32b..e040aea 100755
--- a/scripts/genconfig.sh
+++ b/scripts/genconfig.sh
@@ -44,6 +44,34 @@ EOF
 
     int main(int argc, char *argv[]) { return posix_fallocate(0,0,0); }
 EOF
+  
+  # Android and some other platforms miss utmpx
+  probesymbol TOYBOX_UTMPX -c << EOF
+    #include <utmpx.h>
+    #ifndef BOOT_TIME
+    #error nope
+    #endif
+    int main(int argc, char *argv[]) {
+      struct utmpx *a; 
+      if (0 != (a = getutxent())) return 0;
+      return 1;
+    }
+EOF
+
+  # Android is missing shadow.h and pty.h
+  probesymbol TOYBOX_PTY -c << EOF
+    #include <pty.h>
+    int main(int argc, char *argv[]) {
+      int master; return forkpty(&master, NULL, NULL, NULL);
+    }
+EOF
+
+  probesymbol TOYBOX_SHADOW -c << EOF
+    #include <shadow.h>
+    int main(int argc, char *argv[]) {
+      struct spwd *a = getspnam("root"); return 0;
+    }
+EOF
 }
 
 genconfig()
diff --git a/toys.h b/toys.h
index ed1fa81..86731a7 100644
--- a/toys.h
+++ b/toys.h
@@ -44,7 +44,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <utime.h>
-#include <utmpx.h>
+USE_TOYBOX_UTMPX(#include <utmpx.h>)
 
 // Posix networking
 
@@ -64,7 +64,7 @@
 #include <wctype.h>
 
 // LSB 4.1 headers
-#include <pty.h>
+USE_TOYBOX_PTY(#include <pty.h>)
 #include <sys/ioctl.h>
 #include <sys/statfs.h>
 #include <sys/sysinfo.h>
diff --git a/toys/lsb/passwd.c b/toys/lsb/passwd.c
index c92fb0d..ca98f2e 100644
--- a/toys/lsb/passwd.c
+++ b/toys/lsb/passwd.c
@@ -10,6 +10,7 @@ USE_PASSWD(NEWTOY(passwd, ">1a:dlu", TOYFLAG_STAYROOT|TOYFLAG_USR|TOYFLAG_BIN))
 config PASSWD
   bool "passwd"
   default y
+  depends on TOYBOX_SHADOW
   help
     usage: passwd [-a ALGO] [-dlu] <account name>
 
diff --git a/toys/lsb/su.c b/toys/lsb/su.c
index 268ddf7..616541b 100644
--- a/toys/lsb/su.c
+++ b/toys/lsb/su.c
@@ -10,6 +10,7 @@ USE_SU(NEWTOY(su, "lmpc:s:", TOYFLAG_BIN|TOYFLAG_ROOTONLY))
 config SU
   bool "su"
   default y
+  depends on TOYBOX_SHADOW
   help
     usage: su [-lmp] [-c CMD] [-s SHELL] [USER [ARGS...]]
 
diff --git a/toys/other/login.c b/toys/other/login.c
index 0986164..91523d4 100644
--- a/toys/other/login.c
+++ b/toys/other/login.c
@@ -10,6 +10,7 @@ USE_LOGIN(NEWTOY(login, ">1fph:", TOYFLAG_BIN))
 config LOGIN
   bool "login"
   default y
+  depends on TOYBOX_SHADOW
   help
     usage: login [-p] [-h host] [[-f] username]
 
diff --git a/toys/other/netcat.c b/toys/other/netcat.c
index 485dda1..2c1ec7b 100644
--- a/toys/other/netcat.c
+++ b/toys/other/netcat.c
@@ -26,6 +26,7 @@ config NETCAT_LISTEN
   bool "netcat server options (-let)"
   default y
   depends on NETCAT
+  depends on TOYBOX_PTY
   help
     usage: netcat [-t] [-lL COMMAND...]
 
diff --git a/toys/other/uptime.c b/toys/other/uptime.c
index adbbfc0..f584d9a 100644
--- a/toys/other/uptime.c
+++ b/toys/other/uptime.c
@@ -25,17 +25,18 @@ void uptime_main(void)
   time_t tmptime;
   struct tm * now;
   unsigned int days, hours, minutes;
-  struct utmpx *entry;
-  int users = 0;
+  USE_TOYBOX_UTMPX(struct utmpx *entry;)
+  USE_TOYBOX_UTMPX(int users = 0;)
 
   // Obtain the data we need.
   sysinfo(&info);
   time(&tmptime);
   now = localtime(&tmptime);
+
   // Obtain info about logged on users
-  setutxent();
-  while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++;
-  endutxent();
+  USE_TOYBOX_UTMPX(setutxent();)
+  USE_TOYBOX_UTMPX(while ((entry = getutxent())) if (entry->ut_type == USER_PROCESS) users++;)
+  USE_TOYBOX_UTMPX(endutxent();)
 
   // Time
   xprintf(" %02d:%02d:%02d up ", now->tm_hour, now->tm_min, now->tm_sec);
@@ -48,7 +49,7 @@ void uptime_main(void)
   if (days) xprintf("%d day%s, ", days, (days!=1)?"s":"");
   if (hours) xprintf("%2d:%02d, ", hours, minutes);
   else printf("%d min, ", minutes);
-  printf(" %d user%s, ", users, (users!=1) ? "s" : "");
+  USE_TOYBOX_UTMPX(printf(" %d user%s, ", users, (users!=1) ? "s" : "");)
   printf(" load average: %.02f, %.02f, %.02f\n", info.loads[0]/65536.0,
     info.loads[1]/65536.0, info.loads[2]/65536.0);
 }
diff --git a/toys/other/w.c b/toys/other/w.c
index c271e8b..a76c82f 100644
--- a/toys/other/w.c
+++ b/toys/other/w.c
@@ -7,6 +7,7 @@ USE_W(NEWTOY(w, NULL, TOYFLAG_USR|TOYFLAG_BIN))
 config W
   bool "w"
   default y
+  depends on TOYBOX_UTMPX
   help
     usage: w
 
diff --git a/toys/pending/sulogin.c b/toys/pending/sulogin.c
index 45f7659..e6cb831 100644
--- a/toys/pending/sulogin.c
+++ b/toys/pending/sulogin.c
@@ -13,6 +13,7 @@ USE_SULOGIN(NEWTOY(sulogin, "t#<0=0", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
 config SULOGIN
   bool "sulogin"
   default n
+  depends on TOYBOX_SHADOW
   help
     usage: sulogin [-t time] [tty]
 
diff --git a/toys/pending/telnetd.c b/toys/pending/telnetd.c
index 4198e63..f9f3422 100644
--- a/toys/pending/telnetd.c
+++ b/toys/pending/telnetd.c
@@ -8,6 +8,7 @@ USE_TELNETD(NEWTOY(telnetd, "w#<0b:p#<0>65535=23f:l:FSKi[!wi]", TOYFLAG_USR|TOYF
 config TELNETD
   bool "telnetd"
   default n
+  depends on TOYBOX_PTY
   help
     Handle incoming telnet connections
 
diff --git a/toys/posix/who.c b/toys/posix/who.c
index 2c8a2e6..876a562 100644
--- a/toys/posix/who.c
+++ b/toys/posix/who.c
@@ -14,6 +14,7 @@ USE_WHO(NEWTOY(who, "a", TOYFLAG_BIN))
 config WHO
   bool "who"
   default y
+  depends on TOYBOX_UTMPX
   help
     usage: who
 


More information about the Toybox mailing list