Fernando RUIZ CASAS (E-mail) fernando.ruiz at ctv.es
Mon Apr 15 07:48:35 UTC 2002


You have it in the lastest snapshots.
This is the base to buid chroot() and more.
The root_rtems_filesystems and current dir are saved in a local task
variable.
When you call chroot() by example a private environment is built in order to
keep
the original rtems root filesystem.
When you call rtems_telnetd you have a private environment too.


Here the source.

A problem around the task_var_delete rtems does a crash in the rtems.
In the lastest SN this is resolved.

------------------ HERE THE libio_h PATCH---------------


/*
 *  External structures
 */
#include <rtems/userenv.h>

extern rtems_user_env_t * rtems_current_user_env;
extern rtems_user_env_t   rtems_global_user_env;

/*
 *  Instantiate a private copy of the per user information for the calling
task.
 */

rtems_status_code rtems_libio_set_private_env(void);
rtems_status_code rtems_libio_share_private_env(rtems_id task_id) ;

----------------------------------------------------------------------------
--------

rtems-ss-20020118\c\src\lib\libc\privateenv.c
------------------------------------
/*
 *  Instantatiate a private user environment for the calling thread.
 *
 *  Submitted by: fernando.ruiz at ctv.es (correo at fernando-ruiz.com)
 *
 *  COPYRIGHT (c) 1989-2000.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id: privateenv.c,v 1.3 2002/01/06 20:11:37 joel Exp $
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>	/* free */

#include <rtems.h>
#include <rtems/libio.h>
#include <rtems/libio_.h>

rtems_status_code rtems_libio_set_private_env(void) {
  rtems_status_code sc;
  rtems_id          task_id;

  sc=rtems_task_ident(RTEMS_SELF,0,&task_id);
  if (sc != RTEMS_SUCCESSFUL) return sc;

  /* Only for the first time a malloc is necesary */
  if (rtems_current_user_env==&rtems_global_user_env)

   sc =
rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,free);
   if (sc != RTEMS_SUCCESSFUL) return sc;
   rtems_current_user_env = malloc(sizeof(rtems_user_env_t));
   if (!rtems_current_user_env)
     return RTEMS_NO_MEMORY;
  };

  /* the side effect desired . chroot("/") */
  *rtems_current_user_env = rtems_global_user_env; /* get the global
values*/
  rtems_current_user_env->task_id=task_id;         /* mark the local
values*/

  return RTEMS_SUCCESSFUL;
}

/*
 *  Share a same private environment beetween two task:
 *   Task_id (remote) and RTEMS_SELF(current).
 */

rtems_status_code rtems_libio_share_private_env(rtems_id task_id) {
  rtems_status_code  sc;
  rtems_user_env_t * shared_user_env;
  rtems_id           current_task_id;

  sc=rtems_task_ident(RTEMS_SELF,0,&current_task_id);
  if (sc != RTEMS_SUCCESSFUL) return sc;

  if (rtems_current_user_env->task_id==current_task_id) {
   /* kill the current user env & task_var*/
   free(rtems_current_user_env);
   sc =
rtems_task_variable_delete(RTEMS_SELF,(void*)&rtems_current_user_env);
   if (sc != RTEMS_SUCCESSFUL) return sc;
  };

  sc = rtems_task_variable_get(task_id,(void*)&rtems_current_user_env,
		                       (void*)&shared_user_env       );
  if (sc != RTEMS_SUCCESSFUL) return sc;

  /* don't free(NULL'ed) at the task_delete. It is a shared var... */
  sc =
rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,NULL);
  if (sc != RTEMS_SUCCESSFUL) return sc;

  /* the current_user_env is the same pointer that remote env */
  rtems_current_user_env = shared_user_env;

  return RTEMS_SUCCESSFUL;
}

--------------- NOW CHROOT --------------------
/*
 *  chroot() -  Change Root Directory
 *  Author: fernando.ruiz at ctv.es
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id: chroot.c,v 1.4 2002/01/04 18:29:36 joel Exp $
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems.h>

#include <unistd.h>
#include <errno.h>

#include <rtems/libio_.h>
#include <rtems/seterr.h>

int chroot(
  const char *pathname
)
{
  int                               result;
  rtems_filesystem_location_info_t  loc;

  /* an automatic call to new private env the first time */
  if (rtems_current_user_env == &rtems_global_user_env) {
   rtems_libio_set_private_env(); /* try to set a new private env*/
   if (rtems_current_user_env == &rtems_global_user_env) /* not ok */
    rtems_set_errno_and_return_minus_one( ENOTSUP );
  };

  loc = rtems_filesystem_root;     /* save the value */

  result = chdir(pathname);
  if (result) {
    rtems_filesystem_root = loc; /* restore the value */
    rtems_set_errno_and_return_minus_one( errno );
  };
  rtems_filesystem_root = rtems_filesystem_current;

  return 0;
}

Another example that you can see in
rtems-ss-20020118\c\src\libmisc\shell\shell.c



-----Mensaje original-----
De: Joe Black [mailto:joe_black at myrealbox.com]
Enviado el: lunes, 15 de abril de 2002 3:38
Para: fernando.ruiz at ctv.es
Asunto:



	Hi, Fernando

	The file privateenv.c you mentioned in rtems mailing list is a part of
your program? Could you send me a copy of it? Thank your very much.

 ----
 Joe Black
 2002-04-15 09:26:39






More information about the users mailing list