summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/misc/posixInt.h
blob: 8e41ca65ba74aa1ebc57be3fd3e23a0b2d7eede3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*********************************************************
 * Copyright (C) 2008-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * posixInt.h --
 *
 *	Internal definitions for the Posix wrapper module.
 */

#ifndef _POSIXINT_H_
#define _POSIXINT_H_

#define INCLUDE_ALLOW_USERLEVEL
#include "includeCheck.h"

#include "posix.h"
#include "unicode.h"
#include "hashTable.h"
#include "vm_atomic.h"
#include "util.h"
#include "str.h"


#ifndef _WIN32 // {

/*
 *----------------------------------------------------------------------
 *
 * PosixConvertToCurrent --
 *
 *      Utility function to convert a Unicode string
 *      to the current encoding.
 *
 * Results:
 *      TRUE on success.
 *      Conversion result in *out or NULL on failure.
 *      errno is untouched on success, set to UNICODE_CONVERSION_ERRNO
 *      on failure.
 *
 * Side effects:
 *      As described.
 *
 *----------------------------------------------------------------------
 */

static INLINE Bool
PosixConvertToCurrent(ConstUnicode in,   // IN: string to convert
                      char **out)        // OUT: conversion result
{
   int e = errno;
   char *p = Unicode_GetAllocBytes(in, STRING_ENCODING_DEFAULT);
   Bool success = p != NULL || in == NULL;

   if (success) {
      errno = e;
      *out = p;
   } else {
      errno = UNICODE_CONVERSION_ERRNO;
      *out = NULL;
   }
   return success;
}


/*
 *----------------------------------------------------------------------
 *
 * PosixConvertToCurrentList --
 *
 *      Utility function to convert a list of Unicode strings
 *      to the current encoding.
 *	Return NULL if list is NULL.
 *
 * Results:
 *      TRUE on success.
 *      Conversion result in *out or NULL on failure.
 *      errno is untouched on success, set to UNICODE_CONVERSION_ERRNO
 *      on failure.
 *
 * Side effects:
 *      As described.
 *
 *----------------------------------------------------------------------
 */

static INLINE Bool
PosixConvertToCurrentList(Unicode const *in,  // IN: list to convert
                          char ***out)        // OUT: conversion result
{
   int e = errno;
   char **p;
   Bool success;

   if (in == NULL) {
      p = NULL;
      success = TRUE;
   } else {
      p = Unicode_GetAllocList(in, -1, STRING_ENCODING_DEFAULT);
      success = p != NULL;
   }

   if (!success) {
      e = UNICODE_CONVERSION_ERRNO;
   }
   *out = p;
   errno = e;
   return success;
}

#endif // }


/*
 * Hash table for Posix_Getenv
 */

typedef struct PosixEnvEntry {
   Atomic_Ptr value;
   Atomic_Ptr lastValue;
} PosixEnvEntry;

static INLINE void
PosixEnvFree(void *v)
{
   // never called
   NOT_REACHED();
}


/*
 *-----------------------------------------------------------------------------
 *
 * PosixGetenvHash --
 *
 *      Save away Unicode result for Posix_Getenv() to make
 *      it persistent.
 *
 * Results:
 *      The value.
 *
 * Side effects:
 *      The passed-in value string may be saved in a hash table,
 *      or it may be freed.
 *
 *-----------------------------------------------------------------------------
 */

static INLINE_SINGLE_CALLER Unicode
PosixGetenvHash(ConstUnicode name,  // IN
                Unicode value)      // IN/OUT: may be freed
{
   static Atomic_Ptr htPtr;
   HashTable *ht;
   Unicode oldValue;
   PosixEnvEntry *e;

   /*
    * Don't need to save NULL.
    */

   if (value == NULL) {
      return value;
   }

   ht = HashTable_AllocOnce(&htPtr, 128,
			    HASH_FLAG_ATOMIC | HASH_FLAG_COPYKEY |
			       HASH_STRING_KEY,
                            PosixEnvFree);

   /*
    * We have to allow any number of concurrent getenv()'s.
    * So if the saved value is the same, don't change it,
    * and if it must change, all the threads together must
    * establish a single new value.
    *
    * On the other hand, we don't have to support concurrent
    * getenv() and setenv().
    */

   for (;;) {
      /*
       * If not in hash table, then insert and return.
       */

      if (!HashTable_Lookup(ht, name, (void **) &e)) {
	 e = Util_SafeMalloc(sizeof *e);
	 Atomic_WritePtr(&e->value, value);
	 Atomic_WritePtr(&e->lastValue, NULL);
	 if (!HashTable_Insert(ht, name, e)) {
	    free(e);
	    continue;
	 }
	 break;
      }

      /*
       * If old value is the same, then use it.
       */

      oldValue = Atomic_ReadPtr(&e->value);
      if (Str_Strcmp(oldValue, value) == 0) {
	 Unicode_Free(value);
	 value = oldValue;
	 break;
      }

      /*
       * If value has changed, use new value, but don't free old
       * value yet because somebody else may be going through this
       * same code and still using it.  Because we don't need
       * to care about concurrent setenv(), a single lastValue
       * slot is sufficient.
       */

      if (Atomic_ReadIfEqualWritePtr(&e->value, oldValue, value) == oldValue) {
	 oldValue = Atomic_ReadWritePtr(&e->lastValue, oldValue);
	 Unicode_Free(oldValue);
	 break;
      }
   }

   return value;
}

#endif // ifndef _POSIXINT_H_