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