Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  *  @file
0005  *
0006  *  @brief Instantiate a Private User Environment
0007  *  @ingroup LibIOEnv
0008  */
0009 
0010 /*
0011  * Copyright (c) 2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
0012  *
0013  *  COPYRIGHT (c) 1989-2010.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <stdlib.h>
0043 
0044 #include <rtems/libio_.h>
0045 #include <rtems/score/threadimpl.h>
0046 #include <rtems/score/userextimpl.h>
0047 #include <rtems/sysinit.h>
0048 
0049 /**
0050  *  Instantiate a private user environment for the calling thread.
0051  */
0052 
0053 static void rtems_libio_free_user_env(rtems_user_env_t *env)
0054 {
0055   bool uses_global_env = env == &rtems_global_user_env;
0056 
0057   if (!uses_global_env) {
0058     rtems_filesystem_global_location_release(env->current_directory, false);
0059     rtems_filesystem_global_location_release(env->root_directory, false);
0060     free(env);
0061   }
0062 }
0063 
0064 rtems_status_code rtems_libio_set_private_env(void)
0065 {
0066   rtems_status_code sc = RTEMS_SUCCESSFUL;
0067   rtems_user_env_t *old_env = rtems_current_user_env;
0068   bool uses_global_env = old_env == &rtems_global_user_env;
0069 
0070   if (uses_global_env) {
0071     Thread_Life_state life_state =
0072       _Thread_Set_life_protection(THREAD_LIFE_PROTECTED);
0073     rtems_user_env_t *new_env = calloc(1, sizeof(*new_env));
0074 
0075     if (new_env != NULL) {
0076       *new_env = *old_env;
0077       new_env->root_directory =
0078         rtems_filesystem_global_location_obtain(&old_env->root_directory);
0079       new_env->current_directory =
0080         rtems_filesystem_global_location_obtain(&old_env->current_directory);
0081 
0082       if (
0083         !rtems_filesystem_global_location_is_null(new_env->root_directory)
0084           && !rtems_filesystem_global_location_is_null(new_env->current_directory)
0085       ) {
0086         Thread_Control *executing = _Thread_Get_executing();
0087 
0088         executing->user_environment = new_env;
0089       } else {
0090         sc = RTEMS_UNSATISFIED;
0091       }
0092 
0093       if (sc != RTEMS_SUCCESSFUL) {
0094         rtems_libio_free_user_env(new_env);
0095       }
0096     } else {
0097       sc = RTEMS_NO_MEMORY;
0098     }
0099 
0100     _Thread_Set_life_protection(life_state);
0101   }
0102 
0103   return sc;
0104 }
0105 
0106 void rtems_libio_use_global_env(void)
0107 {
0108   rtems_user_env_t *env = rtems_current_user_env;
0109   bool uses_private_env = env != &rtems_global_user_env;
0110 
0111   if (uses_private_env) {
0112     Thread_Life_state life_state =
0113       _Thread_Set_life_protection(THREAD_LIFE_PROTECTED);
0114     Thread_Control *executing;
0115 
0116     rtems_libio_free_user_env(env);
0117     executing = _Thread_Get_executing();
0118     executing->user_environment = NULL;
0119 
0120     _Thread_Set_life_protection(life_state);
0121   }
0122 }
0123 
0124 static void rtems_libio_env_thread_terminate(Thread_Control *the_thread)
0125 {
0126   rtems_user_env_t *env = the_thread->user_environment;
0127 
0128   if (env != NULL) {
0129     rtems_libio_free_user_env(env);
0130   }
0131 }
0132 
0133 static void rtems_libio_env_thread_restart(
0134   Thread_Control *executing,
0135   Thread_Control *the_thread
0136 )
0137 {
0138   (void) executing;
0139   rtems_libio_env_thread_terminate( the_thread );
0140 }
0141 
0142 static User_extensions_Control rtems_libio_env_extensions = {
0143   .Callouts = {
0144     .thread_restart = rtems_libio_env_thread_restart,
0145     .thread_terminate = rtems_libio_env_thread_terminate
0146   }
0147 };
0148 
0149 static void rtems_libio_env_init(void)
0150 {
0151   _User_extensions_Add_API_set(&rtems_libio_env_extensions);
0152 }
0153 
0154 RTEMS_SYSINIT_ITEM(
0155   rtems_libio_env_init,
0156   RTEMS_SYSINIT_USER_ENVIRONMENT,
0157   RTEMS_SYSINIT_ORDER_MIDDLE
0158 );