File indexing completed on 2025-05-11 08:24:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
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
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 );