source: rtems/cpukit/libcsupport/src/privateenv.c @ e22af78

4.115
Last change on this file since e22af78 was eb2c813, checked in by Joel Sherrill <joel.sherrill@…>, on 04/04/14 at 14:24:17

privateenv.c: Remove unused variable warning

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Instantiate a Private User Environment
5 *  @ingroup LibIOEnv
6 */
7
8/*
9 *  Submitted by: fernando.ruiz@ctv.es (correo@fernando-ruiz.com)
10 *
11 *  COPYRIGHT (c) 1989-2010.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.rtems.org/license/LICENSE.
17 */
18
19#if HAVE_CONFIG_H
20  #include "config.h"
21#endif
22
23#include <stdlib.h>
24
25#include <rtems/libio_.h>
26#include <rtems/score/threaddispatch.h>
27
28/**
29 *  Instantiate a private user environment for the calling thread.
30 */
31
32rtems_user_env_t * rtems_current_user_env_get(void)
33{
34  void *ptr = pthread_getspecific(rtems_current_user_env_key);
35  if (ptr == NULL) {
36    ptr = &rtems_global_user_env;
37  }
38  return (rtems_user_env_t *) ptr;
39}
40
41void rtems_libio_free_user_env(void *arg)
42{
43  rtems_user_env_t *env = arg;
44  bool uses_global_env = env == &rtems_global_user_env;
45
46  if (!uses_global_env) {
47    rtems_filesystem_global_location_release(env->current_directory);
48    rtems_filesystem_global_location_release(env->root_directory);
49    free(env);
50  }
51}
52
53static void free_user_env_protected(rtems_user_env_t *env)
54{
55  _Thread_Disable_dispatch();
56  rtems_libio_free_user_env(env);
57  _Thread_Enable_dispatch();
58}
59
60rtems_status_code rtems_libio_set_private_env(void)
61{
62  rtems_status_code sc = RTEMS_SUCCESSFUL;
63  rtems_user_env_t *old_env = rtems_current_user_env;
64  bool uses_global_env = old_env == &rtems_global_user_env;
65
66  if (uses_global_env) {
67    rtems_user_env_t *new_env = calloc(1, sizeof(*new_env));
68
69    if (new_env != NULL) {
70      *new_env = *old_env;
71      new_env->root_directory =
72        rtems_filesystem_global_location_obtain(&old_env->root_directory);
73      new_env->current_directory =
74        rtems_filesystem_global_location_obtain(&old_env->current_directory);
75
76      if (
77        !rtems_filesystem_global_location_is_null(new_env->root_directory)
78          && !rtems_filesystem_global_location_is_null(new_env->current_directory)
79      ) {
80        int eno = pthread_setspecific(
81          rtems_current_user_env_key,
82          new_env
83        );
84
85        if (eno == 0) {
86          free_user_env_protected(old_env);
87        } else {
88          sc = RTEMS_TOO_MANY;
89        }
90      } else {
91        sc = RTEMS_UNSATISFIED;
92      }
93
94      if (sc != RTEMS_SUCCESSFUL) {
95        rtems_libio_free_user_env(new_env);
96      }
97    } else {
98      sc = RTEMS_NO_MEMORY;
99    }
100  }
101
102  return sc;
103}
104
105void rtems_libio_use_global_env(void)
106{
107  rtems_user_env_t *env = rtems_current_user_env;
108  bool uses_private_env = env != &rtems_global_user_env;
109
110  if (uses_private_env) {
111    free_user_env_protected(env);
112    pthread_setspecific(rtems_current_user_env_key, NULL);
113  }
114}
Note: See TracBrowser for help on using the repository browser.