1 | /* |
---|
2 | * Instantiate a private user environment for the calling thread. |
---|
3 | * |
---|
4 | * Submitted by: fernando.ruiz@ctv.es (correo@fernando-ruiz.com) |
---|
5 | * |
---|
6 | * COPYRIGHT (c) 1989-2000. |
---|
7 | * On-Line Applications Research Corporation (OAR). |
---|
8 | * |
---|
9 | * The license and distribution terms for this file may be |
---|
10 | * found in the file LICENSE in this distribution or at |
---|
11 | * http://www.rtems.com/license/LICENSE. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <stdlib.h> /* free */ |
---|
21 | |
---|
22 | #include <rtems.h> |
---|
23 | #include <rtems/chain.h> |
---|
24 | #include <rtems/libio.h> |
---|
25 | #include <rtems/libio_.h> |
---|
26 | |
---|
27 | /* cleanup a user environment |
---|
28 | * NOTE: this must be called with |
---|
29 | * thread dispatching disabled! |
---|
30 | */ |
---|
31 | static void |
---|
32 | free_user_env(void *venv) |
---|
33 | { |
---|
34 | rtems_user_env_t *env = (rtems_user_env_t*) venv ; |
---|
35 | |
---|
36 | if (env != &rtems_global_user_env |
---|
37 | #ifdef HAVE_USERENV_REFCNT |
---|
38 | && --env->refcnt <= 0 |
---|
39 | #endif |
---|
40 | ) { |
---|
41 | rtems_filesystem_freenode( &env->current_directory); |
---|
42 | rtems_filesystem_freenode( &env->root_directory); |
---|
43 | free(env); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | rtems_status_code rtems_libio_set_private_env(void) { |
---|
48 | rtems_status_code sc; |
---|
49 | rtems_id task_id; |
---|
50 | rtems_filesystem_location_info_t loc; |
---|
51 | |
---|
52 | sc=rtems_task_ident(RTEMS_SELF,0,&task_id); |
---|
53 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
54 | |
---|
55 | /* Only for the first time a malloc is necesary */ |
---|
56 | if (rtems_current_user_env==&rtems_global_user_env) { |
---|
57 | rtems_user_env_t *tmp = malloc(sizeof(rtems_user_env_t)); |
---|
58 | if (!tmp) |
---|
59 | return RTEMS_NO_MEMORY; |
---|
60 | |
---|
61 | #ifdef HAVE_USERENV_REFCNT |
---|
62 | tmp->refcnt = 1; |
---|
63 | #endif |
---|
64 | |
---|
65 | sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,(void(*)(void *))free_user_env); |
---|
66 | if (sc != RTEMS_SUCCESSFUL) { |
---|
67 | /* don't use free_user_env because the pathlocs are |
---|
68 | * not initialized yet |
---|
69 | */ |
---|
70 | free(tmp); |
---|
71 | return sc; |
---|
72 | } |
---|
73 | rtems_current_user_env = tmp; |
---|
74 | }; |
---|
75 | |
---|
76 | *rtems_current_user_env = rtems_global_user_env; /* get the global values*/ |
---|
77 | rtems_current_user_env->task_id=task_id; /* mark the local values*/ |
---|
78 | |
---|
79 | /* Clone the pathlocs. In contrast to most other |
---|
80 | * code we must _not_ free the original locs because |
---|
81 | * what we are trying to do here is forking off |
---|
82 | * clones. The reason is a pathloc can be allocated by the |
---|
83 | * file system and needs to be freed when deleting the environment. |
---|
84 | */ |
---|
85 | |
---|
86 | rtems_filesystem_evaluate_path("/", 1, 0, &loc, 0); |
---|
87 | rtems_filesystem_root = loc; |
---|
88 | rtems_filesystem_evaluate_path("/", 1, 0, &loc, 0); |
---|
89 | rtems_filesystem_current = loc; |
---|
90 | |
---|
91 | return RTEMS_SUCCESSFUL; |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * Share a same private environment beetween two task: |
---|
96 | * Task_id (remote) and RTEMS_SELF(current). |
---|
97 | */ |
---|
98 | |
---|
99 | /* NOTE: |
---|
100 | * |
---|
101 | * THIS CODE HAS NO PROTECTION IMPLEMENTED |
---|
102 | * |
---|
103 | * Tasks who wish to share their environments must |
---|
104 | * |
---|
105 | * a) assert that no participants are concurrently |
---|
106 | * executing |
---|
107 | * libio_share_private_env() and/or libio_set_private_env() |
---|
108 | * |
---|
109 | * b) mutex access to rtems_filesystem_current, rtems_filesytem_root |
---|
110 | * while changing any of those (chdir(), chroot()). |
---|
111 | */ |
---|
112 | |
---|
113 | #ifndef HAVE_USERENV_REFCNT |
---|
114 | rtems_status_code rtems_libio_share_private_env(rtems_id task_id) { |
---|
115 | rtems_status_code sc; |
---|
116 | rtems_user_env_t * shared_user_env; |
---|
117 | rtems_id current_task_id; |
---|
118 | |
---|
119 | sc=rtems_task_ident(RTEMS_SELF,0,¤t_task_id); |
---|
120 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
121 | |
---|
122 | if (rtems_current_user_env->task_id==current_task_id) { |
---|
123 | /* kill the current user env & task_var*/ |
---|
124 | rtems_user_env_t *tmp = rtems_current_user_env; |
---|
125 | sc = rtems_task_variable_delete(RTEMS_SELF,(void*)&rtems_current_user_env); |
---|
126 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
127 | free_user_env(tmp); |
---|
128 | }; |
---|
129 | |
---|
130 | /* AT THIS POINT, rtems_current_user_env is DANGLING */ |
---|
131 | |
---|
132 | sc = rtems_task_variable_get(task_id,(void*)&rtems_current_user_env, |
---|
133 | (void*)&shared_user_env ); |
---|
134 | if (sc != RTEMS_SUCCESSFUL) |
---|
135 | goto bailout; |
---|
136 | |
---|
137 | sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,free_user_env); |
---|
138 | if (sc != RTEMS_SUCCESSFUL) |
---|
139 | goto bailout; |
---|
140 | |
---|
141 | /* the current_user_env is the same pointer that remote env */ |
---|
142 | rtems_current_user_env = shared_user_env; |
---|
143 | |
---|
144 | /* increase the reference count */ |
---|
145 | #ifdef HAVE_USERENV_REFCNT |
---|
146 | rtems_current_user_env->refcnt++; |
---|
147 | #endif |
---|
148 | |
---|
149 | return RTEMS_SUCCESSFUL; |
---|
150 | |
---|
151 | bailout: |
---|
152 | /* fallback to the global env */ |
---|
153 | rtems_current_user_env = &rtems_global_user_env; |
---|
154 | return sc; |
---|
155 | } |
---|
156 | #endif |
---|