toybox/lib/portability.c
<<
>>
Prefs
   1/* portability.c - code to workaround the deficiencies of various platforms.
   2 *
   3 * Copyright 2012 Rob Landley <rob@landley.net>
   4 * Copyright 2012 Georgi Chorbadzhiyski <gf@unixsol.org>
   5 */
   6
   7#include "toys.h"
   8
   9// We can't fork() on nommu systems, and vfork() requires an exec() or exit()
  10// before resuming the parent (because they share a heap until then). And no,
  11// we can't implement our own clone() call that does the equivalent of fork()
  12// because nommu heaps use physical addresses so if we copy the heap all our
  13// pointers are wrong. (You need an mmu in order to map two heaps to the same
  14// address range without interfering with each other.) In the absence of
  15// a portable way to tell malloc() to start a new heap without freeing the old
  16// one, you pretty much need the exec().)
  17
  18// So we exec ourselves (via /proc/self/exe, if anybody knows a way to
  19// re-exec self without depending on the filesystem, I'm all ears),
  20// and use the arguments to signal reentry.
  21
  22#if CFG_TOYBOX_FORK
  23pid_t xfork(void)
  24{
  25  pid_t pid = fork();
  26
  27  if (pid < 0) perror_exit("fork");
  28
  29  return pid;
  30}
  31#endif
  32
  33int xgetrandom(void *buf, unsigned buflen, unsigned flags)
  34{
  35  int fd;
  36
  37#if CFG_TOYBOX_GETRANDOM
  38  if (buflen == getrandom(buf, buflen, flags&~WARN_ONLY)) return 1;
  39  if (errno!=ENOSYS && !(flags&WARN_ONLY)) perror_exit("getrandom");
  40#endif
  41  fd = xopen(flags ? "/dev/random" : "/dev/urandom",O_RDONLY|(flags&WARN_ONLY));
  42  if (fd == -1) return 0;
  43  xreadall(fd, buf, buflen);
  44  close(fd);
  45
  46  return 1;
  47}
  48
  49#if defined(__APPLE__)
  50extern char **environ;
  51
  52int clearenv(void)
  53{
  54  *environ = NULL;
  55  return 0;
  56}
  57#endif
  58
  59// Get a linked list of mount points, with stat information.
  60#if defined(__APPLE__) || defined(__FreeBSD__)
  61
  62// Not implemented for macOS.
  63// See <sys/mount.h>'s getmntinfo(3) for the BSD API.
  64
  65#else
  66
  67#include <mntent.h>
  68
  69static void octal_deslash(char *s)
  70{
  71  char *o = s;
  72
  73  while (*s) {
  74    if (*s == '\\') {
  75      int i, oct = 0;
  76
  77      for (i = 1; i < 4; i++) {
  78        if (!isdigit(s[i])) break;
  79        oct = (oct<<3)+s[i]-'0';
  80      }
  81      if (i == 4) {
  82        *o++ = oct;
  83        s += i;
  84        continue;
  85      }
  86    }
  87    *o++ = *s++;
  88  }
  89
  90  *o = 0;
  91}
  92
  93// Check if this type matches list.
  94// Odd syntax: typelist all yes = if any, typelist all no = if none.
  95
  96int mountlist_istype(struct mtab_list *ml, char *typelist)
  97{
  98  int len, skip;
  99  char *t;
 100
 101  if (!typelist) return 1;
 102
 103  skip = strncmp(typelist, "no", 2);
 104
 105  for (;;) {
 106    if (!(t = comma_iterate(&typelist, &len))) break;
 107    if (!skip) {
 108      // If one -t starts with "no", the rest must too
 109      if (strncmp(t, "no", 2)) error_exit("bad typelist");
 110      if (!strncmp(t+2, ml->type, len-2)) {
 111        skip = 1;
 112        break;
 113      }
 114    } else if (!strncmp(t, ml->type, len) && !ml->type[len]) {
 115      skip = 0;
 116      break;
 117    }
 118  }
 119
 120  return !skip;
 121}
 122
 123// Get list of mounted filesystems, including stat and statvfs info.
 124// Returns a reversed list, which is good for finding overmounts and such.
 125
 126struct mtab_list *xgetmountlist(char *path)
 127{
 128  struct mtab_list *mtlist = 0, *mt;
 129  struct mntent *me;
 130  FILE *fp;
 131  char *p = path ? path : "/proc/mounts";
 132
 133  if (!(fp = setmntent(p, "r"))) perror_exit("bad %s", p);
 134
 135  // The "test" part of the loop is done before the first time through and
 136  // again after each "increment", so putting the actual load there avoids
 137  // duplicating it. If the load was NULL, the loop stops.
 138
 139  while ((me = getmntent(fp))) {
 140    mt = xzalloc(sizeof(struct mtab_list) + strlen(me->mnt_fsname) +
 141      strlen(me->mnt_dir) + strlen(me->mnt_type) + strlen(me->mnt_opts) + 4);
 142    dlist_add_nomalloc((void *)&mtlist, (void *)mt);
 143
 144    // Collect details about mounted filesystem
 145    // Don't report errors, just leave data zeroed
 146    if (!path) {
 147      stat(me->mnt_dir, &(mt->stat));
 148      statvfs(me->mnt_dir, &(mt->statvfs));
 149    }
 150
 151    // Remember information from /proc/mounts
 152    mt->dir = stpcpy(mt->type, me->mnt_type)+1;
 153    mt->device = stpcpy(mt->dir, me->mnt_dir)+1;
 154    mt->opts = stpcpy(mt->device, me->mnt_fsname)+1;
 155    strcpy(mt->opts, me->mnt_opts);
 156
 157    octal_deslash(mt->dir);
 158    octal_deslash(mt->device);
 159  }
 160  endmntent(fp);
 161
 162  return mtlist;
 163}
 164
 165#endif
 166