1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup LibIOEnv |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Instantiate a private user environment for the calling thread. |
---|
9 | * |
---|
10 | * Submitted by: fernando.ruiz@ctv.es (correo@fernando-ruiz.com) |
---|
11 | * |
---|
12 | * COPYRIGHT (c) 1989-2010. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.com/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #define __RTEMS_VIOLATE_KERNEL_VISIBILITY__ |
---|
25 | |
---|
26 | #include <stdlib.h> |
---|
27 | |
---|
28 | #include <rtems/libio_.h> |
---|
29 | #include <rtems/score/thread.h> |
---|
30 | |
---|
31 | static void free_user_env(void *arg) |
---|
32 | { |
---|
33 | rtems_user_env_t *env = arg; |
---|
34 | bool uses_global_env = env == &rtems_global_user_env; |
---|
35 | |
---|
36 | if (!uses_global_env) { |
---|
37 | if (env->reference_count == 1) { |
---|
38 | rtems_filesystem_global_location_release(env->current_directory); |
---|
39 | rtems_filesystem_global_location_release(env->root_directory); |
---|
40 | free(env); |
---|
41 | } else { |
---|
42 | --env->reference_count; |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | static void free_user_env_protected(rtems_user_env_t *env) |
---|
48 | { |
---|
49 | _Thread_Disable_dispatch(); |
---|
50 | free_user_env(env); |
---|
51 | _Thread_Enable_dispatch(); |
---|
52 | } |
---|
53 | |
---|
54 | rtems_status_code rtems_libio_set_private_env(void) |
---|
55 | { |
---|
56 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
57 | rtems_id self_task_id = rtems_task_self(); |
---|
58 | rtems_user_env_t *old_env = rtems_current_user_env; |
---|
59 | bool uses_global_env = old_env == &rtems_global_user_env; |
---|
60 | bool uses_shared_env = old_env->task_id != self_task_id; |
---|
61 | |
---|
62 | if (uses_global_env || uses_shared_env) { |
---|
63 | rtems_user_env_t *new_env = calloc(1, sizeof(*new_env)); |
---|
64 | |
---|
65 | if (new_env != NULL) { |
---|
66 | *new_env = *old_env; |
---|
67 | new_env->reference_count = 1; |
---|
68 | new_env->task_id = self_task_id; |
---|
69 | new_env->root_directory = |
---|
70 | rtems_filesystem_global_location_obtain(&old_env->root_directory); |
---|
71 | new_env->current_directory = |
---|
72 | rtems_filesystem_global_location_obtain(&old_env->current_directory); |
---|
73 | |
---|
74 | if ( |
---|
75 | !rtems_filesystem_global_location_is_null(new_env->root_directory) |
---|
76 | && !rtems_filesystem_global_location_is_null(new_env->current_directory) |
---|
77 | ) { |
---|
78 | sc = rtems_task_variable_add( |
---|
79 | RTEMS_SELF, |
---|
80 | (void **) &rtems_current_user_env, |
---|
81 | free_user_env |
---|
82 | ); |
---|
83 | if (sc == RTEMS_SUCCESSFUL) { |
---|
84 | free_user_env_protected(old_env); |
---|
85 | rtems_current_user_env = new_env; |
---|
86 | } else { |
---|
87 | sc = RTEMS_TOO_MANY; |
---|
88 | } |
---|
89 | } else { |
---|
90 | sc = RTEMS_UNSATISFIED; |
---|
91 | } |
---|
92 | |
---|
93 | if (sc != RTEMS_SUCCESSFUL) { |
---|
94 | free_user_env(new_env); |
---|
95 | } |
---|
96 | } else { |
---|
97 | sc = RTEMS_NO_MEMORY; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | return sc; |
---|
102 | } |
---|
103 | |
---|
104 | rtems_status_code rtems_libio_share_private_env(rtems_id task_id) |
---|
105 | { |
---|
106 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
107 | rtems_id self_task_id = rtems_task_self(); |
---|
108 | |
---|
109 | if (task_id != RTEMS_SELF && self_task_id != task_id) { |
---|
110 | rtems_user_env_t *env; |
---|
111 | |
---|
112 | /* |
---|
113 | * We have to disable the thread dispatching to prevent deletion of the |
---|
114 | * environment in the meantime. |
---|
115 | */ |
---|
116 | _Thread_Disable_dispatch(); |
---|
117 | sc = rtems_task_variable_get( |
---|
118 | task_id, |
---|
119 | (void *) &rtems_current_user_env, |
---|
120 | (void *) &env |
---|
121 | ); |
---|
122 | if (sc == RTEMS_SUCCESSFUL) { |
---|
123 | ++env->reference_count; |
---|
124 | } else { |
---|
125 | sc = RTEMS_UNSATISFIED; |
---|
126 | } |
---|
127 | _Thread_Enable_dispatch(); |
---|
128 | |
---|
129 | if (sc == RTEMS_SUCCESSFUL) { |
---|
130 | sc = rtems_task_variable_add( |
---|
131 | RTEMS_SELF, |
---|
132 | (void **) &rtems_current_user_env, |
---|
133 | free_user_env |
---|
134 | ); |
---|
135 | if (sc == RTEMS_SUCCESSFUL) { |
---|
136 | free_user_env_protected(rtems_current_user_env); |
---|
137 | rtems_current_user_env = env; |
---|
138 | } else { |
---|
139 | free_user_env_protected(env); |
---|
140 | sc = RTEMS_TOO_MANY; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | return sc; |
---|
146 | } |
---|
147 | |
---|
148 | void rtems_libio_use_global_env(void) |
---|
149 | { |
---|
150 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
151 | rtems_user_env_t *env = rtems_current_user_env; |
---|
152 | bool uses_private_env = env != &rtems_global_user_env; |
---|
153 | |
---|
154 | if (uses_private_env) { |
---|
155 | sc = rtems_task_variable_delete( |
---|
156 | RTEMS_SELF, |
---|
157 | (void **) &rtems_current_user_env |
---|
158 | ); |
---|
159 | if (sc != RTEMS_SUCCESSFUL) { |
---|
160 | rtems_fatal_error_occurred(0xdeadbeef); |
---|
161 | } |
---|
162 | |
---|
163 | rtems_current_user_env = &rtems_global_user_env; |
---|
164 | } |
---|
165 | } |
---|