delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/02/25/01:02:58

Message-Id: <199902250602.GAA34258@out1.ibm.net>
From: "Mark E." <snowball3 AT usa DOT net>
To: djgpp-workers AT delorie DOT com
Date: Thu, 25 Feb 1999 01:03:05 -0500
MIME-Version: 1.0
Subject: updated chroot
X-mailer: Pegasus Mail for Win32 (v3.01d)
Reply-To: djgpp-workers AT delorie DOT com

Here's a new improved version of chroot. It now can emulate the 
restrictive Unix behavior of chroot or the non-restrictive behavior 
needed for Bash.

Work still needs to be done on the environmental variables.

#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>

/* Should be in a header. */
#define __CHROOT_UNIX_EMU_FLAG 1

char __djgpp_root [PATH_MAX+1] = { '\0' };
int __djgpp_root_len = 0;
unsigned int __chroot_flags = __CHROOT_UNIX_EMU_FLAG;


int
chroot (const char *path)
{
  char buf[PATH_MAX+1];
  char *to = __djgpp_root;
  char *from = buf;

  int unix_emu = __chroot_flags & __CHROOT_UNIX_EMU_FLAG;

  /* When not emulating the Unix behavior of chroot,
     allow a null root to delete the root path. */
  if (path == NULL || *path == '\0')
  {
    if (unix_emu)
    {
      errno = EINVAL;
      return -1;
    }
    __djgpp_root[0] = '\0';
    __djgpp_root_len = 0;
    return 0;
  }

  _fixpath(path, buf);

  if (unix_emu)
  {
    /* If the path returned from _fixpath isn't relative
       to __djgpp_root, then the path is rejected. */
    if (__djgpp_root_len != 0 && *buf != '/')
    {
      errno = EINVAL;
      return -1;
    }

    /* Change the cwd like Unix versions do. */
    if (chdir(buf) < 0)
      return -1;
  }
  else
  {
    /* Verify the path contains directories,
       but do not make the path the cwd. */
    if (access(buf, D_OK) < 0)
      return -1;
  }

  /* Copy in the new root path. */
  if (*buf != '/')
    __djgpp_root_len = 0;
  else  /* Append to the current root path. */
    to = __djgpp_root + __djgpp_root_len;

  while (*from)
  {
    *to = *from;
    ++to;
    ++from;
    ++__djgpp_root_len;
  }


#if 0
  if (unix_emu)
    setenv ("ROOT", __djgpp_root, 1);
  else
    setenv ("SYSROOT", __djgpp_root, 1);
#endif

  return 0;
}


--- 
Mark Elbrecht
snowball3 AT usa DOT net http://members.xoom.com/snowball3/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019