1 | /* |
---|
2 | * Instantatiate 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.OARcorp.com/rtems/license.html. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #if HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include <rtems.h> |
---|
21 | #include <rtems/libio.h> |
---|
22 | #include <rtems/libio_.h> |
---|
23 | |
---|
24 | rtems_status_code rtems_libio_set_private_env(void) { |
---|
25 | rtems_status_code sc; |
---|
26 | rtems_id task_id; |
---|
27 | |
---|
28 | sc=rtems_task_ident(RTEMS_SELF,0,&task_id); |
---|
29 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
30 | |
---|
31 | /* Only for the first time a malloc is necesary */ |
---|
32 | if (rtems_current_user_env==&rtems_global_user_env) { |
---|
33 | sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,free); |
---|
34 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
35 | rtems_current_user_env = malloc(sizeof(rtems_user_env_t)); |
---|
36 | if (!rtems_current_user_env) |
---|
37 | return RTEMS_NO_MEMORY; |
---|
38 | }; |
---|
39 | |
---|
40 | /* the side effect desired . chroot("/") */ |
---|
41 | *rtems_current_user_env = rtems_global_user_env; /* get the global values*/ |
---|
42 | rtems_current_user_env->task_id=task_id; /* mark the local values*/ |
---|
43 | |
---|
44 | return RTEMS_SUCCESSFUL; |
---|
45 | } |
---|
46 | |
---|
47 | /* |
---|
48 | * Share a same private environment beetween two task: |
---|
49 | * Task_id (remote) and RTEMS_SELF(current). |
---|
50 | */ |
---|
51 | |
---|
52 | rtems_status_code rtems_libio_share_private_env(rtems_id task_id) { |
---|
53 | rtems_status_code sc; |
---|
54 | rtems_user_env_t * shared_user_env; |
---|
55 | rtems_id current_task_id; |
---|
56 | |
---|
57 | sc=rtems_task_ident(RTEMS_SELF,0,¤t_task_id); |
---|
58 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
59 | |
---|
60 | if (rtems_current_user_env->task_id==current_task_id) { |
---|
61 | /* kill the current user env & task_var*/ |
---|
62 | free(rtems_current_user_env); |
---|
63 | sc = rtems_task_variable_delete(RTEMS_SELF,(void*)&rtems_current_user_env); |
---|
64 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
65 | }; |
---|
66 | |
---|
67 | sc = rtems_task_variable_get(task_id,(void*)&rtems_current_user_env, |
---|
68 | (void*)&shared_user_env ); |
---|
69 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
70 | |
---|
71 | /* don't free(NULL'ed) at the task_delete. It is a shared var... */ |
---|
72 | sc = rtems_task_variable_add(RTEMS_SELF,(void*)&rtems_current_user_env,NULL); |
---|
73 | if (sc != RTEMS_SUCCESSFUL) return sc; |
---|
74 | |
---|
75 | /* the current_user_env is the same pointer that remote env */ |
---|
76 | rtems_current_user_env = shared_user_env; |
---|
77 | |
---|
78 | return RTEMS_SUCCESSFUL; |
---|
79 | } |
---|