~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to lib/include/util.h

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-03-31 14:20:05 UTC
  • mfrom: (1.4.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110331142005-3n9red91p7ogkweo
Tags: 2011.03.28-387002-0ubuntu1
* Merge latest upstream git tag.  This has the unlocked_ioctl change
  needed to fix dkms build failures (LP: #727342)
* Changes in debian/rules:
  - work around a bug in toolbox/Makefile, where install-exec-hook is
    not happening.  This needs to get fixed the right way.
  - don't install 'vmware-user' which seems to no longer exist
  - move /etc/xdg into open-vm-toolbox (which should be done using .install)
* debian/open-vm-tools.init: add 'modprobe [-r] vmblock'. (LP: #332323)
* debian/rules and debian/open-vm-toolbox.lintian-overrides:
  - Make vmware-user-suid-wrapper suid-root (LP: #332323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
   #include <sys/types.h>
44
44
#endif
45
45
 
46
 
#ifdef __APPLE__
47
 
   #include <IOKit/IOTypes.h>
48
 
   #include <CoreFoundation/CFData.h>
49
 
   #include <CoreFoundation/CFNumber.h>
50
 
   #include <CoreFoundation/CFDictionary.h>
51
 
   #include <CoreFoundation/CFArray.h>
52
 
   #include <CoreFoundation/CFString.h>
53
 
#endif
54
 
 
55
46
#include "vm_assert.h"
56
47
#include "unicodeTypes.h"
57
48
 
58
 
#ifdef __APPLE__
59
 
   #define SYSTEM_VOL_DIR "/Volumes"
60
 
#endif
61
49
 
62
50
/*
63
51
 * Define the Util_ThreadID type.
72
60
typedef pid_t Util_ThreadID;
73
61
#endif
74
62
 
75
 
#ifdef __APPLE__
76
 
EXTERN char *Util_CFStringToUTF8CString(CFStringRef s);
77
 
EXTERN char *Util_IORegCopyStringProperty(io_object_t entry, CFStringRef property);
78
 
EXTERN Bool Util_IORegGetNumberProperty(io_object_t entry, CFStringRef property,
79
 
                                        CFNumberType type, void *val);
80
 
EXTERN Bool Util_IORegGetBooleanProperty(io_object_t entry, CFStringRef property,
81
 
                                         Bool *boolVal);
82
 
EXTERN CFDataRef Util_IORegCopyDataProperty(io_object_t entry, CFStringRef property);
83
 
EXTERN CFDictionaryRef Util_IORegCopyDictionaryProperty(io_object_t entry,
84
 
                                                        CFStringRef property);
85
 
EXTERN CFMutableDictionaryRef UtilMacos_CreateCFDictionary(
86
 
   unsigned int numPairs, ...);
87
 
EXTERN io_service_t Util_IORegGetDeviceObjectByName(const char *deviceName);
88
 
EXTERN char *Util_GetBSDName(const char *deviceName);
89
 
EXTERN char *Util_IORegGetDriveType(const char *deviceName);
90
 
EXTERN uint64 Util_GetPartitionOffset(const char *bsdDev);
91
 
EXTERN char *Util_GetMacOSDefaultVMPath();
92
 
 
93
 
/* 
94
 
 * Mac laptops without a cdrom drive (currently only the Air, but maybe
95
 
 * more in the future) provide a way to use a desktop's cdrom drive
96
 
 * remotely over the network.  These functions enumerate all such
97
 
 * remote disks currently mounted as volumes, and return a list of
98
 
 * volume to bsd name mappings.  The bsd device can be considered a
99
 
 * flat-file representation of the disk contents and can be connected
100
 
 * to our cdrom image backend.  We also include the size, since we must
101
 
 * get that from the IO registry as well, and it doesn't change while
102
 
 * the disk is mounted.
103
 
 * 
104
 
 * FYI, Apple calls this "Remote Disc" not "Remote Disk", so we follow
105
 
 * that convention here too.
106
 
 */
107
 
typedef struct RemoteDiscList {
108
 
   char                  *bsdName;
109
 
   char                  *name;
110
 
   uint64                 size; // In bytes
111
 
   struct RemoteDiscList *next;
112
 
} RemoteDiscList;
113
 
 
114
 
EXTERN RemoteDiscList *Util_GetRemoteDiscList(void);
115
 
EXTERN void Util_FreeRemoteDiscList(RemoteDiscList *list);
116
 
 
117
 
/*
118
 
 * Additional keys for disk/partition device properties.
119
 
 */
120
 
EXTERN const CFStringRef kUtilMacosDeviceSerialNumberKey;
121
 
EXTERN const CFStringRef kUtilMacosVolumeOffsetKey;
122
 
 
123
 
EXTERN CFDictionaryRef UtilMacos_CopyDiskDeviceProperties(const char *bsdDev);
124
 
EXTERN CFArrayRef UtilMacos_CopyDiskDeviceBSDNames(const char *parentDisk);
125
 
#endif // __APPLE__
126
 
 
127
63
 
128
64
EXTERN uint32 CRC_Compute(const uint8 *buf, int len);
129
65
EXTERN uint32 Util_Checksum32(const uint32 *buf, int len);
130
66
EXTERN uint32 Util_Checksum(const uint8 *buf, int len);
131
67
EXTERN uint32 Util_Checksumv(void *iov, int numEntries);
 
68
EXTERN uint32 Util_HashString(const char *str);
132
69
EXTERN Unicode Util_ExpandString(ConstUnicode fileName);
133
70
EXTERN void Util_ExitThread(int);
134
71
EXTERN NORETURN void Util_ExitProcessAbruptly(int);
191
128
 
192
129
int Util_CompareDotted(const char *s1, const char *s2);
193
130
 
 
131
/*
 
132
 * This enum defines how Util_GetOpt should handle non-option arguments:
 
133
 *
 
134
 * UTIL_NONOPT_PERMUTE: Permute argv so that all non-options are at the end.
 
135
 * UTIL_NONOPT_STOP:    Stop when first non-option argument is seen.
 
136
 * UTIL_NONOPT_ALL:     Return each non-option argument as if it were
 
137
 *                      an option with character code 1.
 
138
 */
 
139
typedef enum { UTIL_NONOPT_PERMUTE, UTIL_NONOPT_STOP, UTIL_NONOPT_ALL } Util_NonOptMode;
 
140
struct option;
 
141
int Util_GetOpt(int argc, char * const *argv, const struct option *opts,
 
142
                Util_NonOptMode mode);
 
143
 
 
144
 
194
145
#if defined(VMX86_STATS)
195
146
Bool Util_QueryCStResidency(uint32 *numCpus, uint32 *numCStates,
196
147
                            uint64 **transitns, uint64 **residency,
203
154
EXTERN Bool Util_Throttle(uint32 count);
204
155
EXTERN uint32 Util_FastRand(uint32 seed);
205
156
 
 
157
 
 
158
/*
 
159
 *-----------------------------------------------------------------------------
 
160
 *
 
161
 * Util_ValidateBytes --
 
162
 *
 
163
 *      Check that memory is filled with the specified value.
 
164
 *
 
165
 * Results:
 
166
 *      NULL   No error
 
167
 *      !NULL  First address that doesn't have the proper value
 
168
 *
 
169
 * Side effects:
 
170
 *      None.
 
171
 *
 
172
 *-----------------------------------------------------------------------------
 
173
 */
 
174
 
 
175
static INLINE void *
 
176
Util_ValidateBytes(const void *ptr,  // IN: ptr to check
 
177
                   size_t size,      // IN: size of ptr
 
178
                   uint8 byteValue)  // IN: memory must be filled with this
 
179
{
 
180
   uint8 *p;
 
181
   uint8 *end;
 
182
   uint64 bigValue;
 
183
 
 
184
   ASSERT(ptr);
 
185
 
 
186
   if (size == 0) {
 
187
      return NULL;
 
188
   }
 
189
 
 
190
   p = (uint8 *) ptr;
 
191
   end = p + size;
 
192
 
 
193
   /* Compare bytes until a "nice" boundary is achieved. */
 
194
   while ((uintptr_t) p % sizeof bigValue) {
 
195
      if (*p != byteValue) {
 
196
         return p;
 
197
      }
 
198
 
 
199
      p++;
 
200
 
 
201
      if (p == end) {
 
202
         return NULL;
 
203
      }
 
204
   }
 
205
 
 
206
   /* Compare using a "nice sized" chunk for a long as possible. */
 
207
   memset(&bigValue, (int) byteValue, sizeof bigValue);
 
208
 
 
209
   while (p + sizeof bigValue <= end) {
 
210
      if (*((uint64 *) p) != bigValue) {
 
211
         /* That's not right... let the loop below report the exact address. */
 
212
         break;
 
213
      }
 
214
 
 
215
      size -= sizeof bigValue;
 
216
      p += sizeof bigValue;
 
217
   }
 
218
 
 
219
   /* Handle any trailing bytes. */
 
220
   while (p < end) {
 
221
      if (*p != byteValue) {
 
222
         return p;
 
223
      }
 
224
 
 
225
      p++;
 
226
   }
 
227
 
 
228
   return NULL;
 
229
}
 
230
 
 
231
 
206
232
/*
207
233
 *----------------------------------------------------------------------
208
234
 *
209
235
 * Util_BufferIsEmpty --
210
236
 *
211
 
 *    Determine wether or not the buffer of 'len' bytes starting at 'base' is
212
 
 *    empty (i.e. full of zeroes)
 
237
 *    Determine if the specified buffer of 'len' bytes starting at 'base'
 
238
 *    is empty (i.e. full of zeroes).
213
239
 *
214
240
 * Results:
215
 
 *    TRUE if yes
216
 
 *    FALSE if no
 
241
 *    TRUE  Yes
 
242
 *    FALSE No
217
243
 *
218
244
 * Side effects:
219
245
 *    None
221
247
 *----------------------------------------------------------------------
222
248
 */
223
249
 
224
 
static INLINE Bool Util_BufferIsEmpty(void const *base, // IN
225
 
                                      size_t len)       // IN
 
250
static INLINE Bool
 
251
Util_BufferIsEmpty(void const *base,  // IN:
 
252
                   size_t len)        // IN:
226
253
{
227
 
   uint32 const *p32;
228
 
   uint32 const *e32;
229
 
   uint16 const *p16;
230
 
 
231
 
   ASSERT_ON_COMPILE(sizeof(uint32) == 4);
232
 
 
233
 
   p32 = (uint32 const *)base;
234
 
   e32 = p32 + len / 4;
235
 
   for (; p32 < e32; p32++) {
236
 
      if (*p32) {
237
 
         return FALSE;
238
 
      }
239
 
   }
240
 
 
241
 
   len &= 0x3;
242
 
   p16 = (uint16 const *)p32;
243
 
 
244
 
   if (len & 0x2) {
245
 
      if (*p16) {
246
 
         return FALSE;
247
 
      }
248
 
 
249
 
      p16++;
250
 
   }
251
 
 
252
 
   if (   len & 0x1
253
 
       && *(uint8 const *)p16) {
254
 
      return FALSE;
255
 
   }
256
 
 
257
 
   return TRUE;
258
 
};
 
254
   return Util_ValidateBytes(base, len, '\0') == NULL;
 
255
}
259
256
 
260
257
 
261
258
EXTERN Bool Util_MakeSureDirExistsAndAccessible(char const *path,
262
 
                                                unsigned int mode);
 
259
                                                unsigned int mode);
263
260
 
264
261
#ifdef N_PLAT_NLM
265
262
#   define DIRSEPS            "\\"
378
375
 
379
376
static INLINE void
380
377
Util_Zero(void *buf,       // OUT
381
 
          size_t bufSize)  // IN 
 
378
          size_t bufSize)  // IN
382
379
{
383
380
   if (buf != NULL) {
 
381
#if defined _WIN32 && defined USERLEVEL
 
382
      /*
 
383
       * Simple memset calls might be optimized out.  See CERT advisory
 
384
       * MSC06-C.
 
385
       */
 
386
      SecureZeroMemory(buf, bufSize);
 
387
#else
384
388
      memset(buf, 0, bufSize);
 
389
#endif
385
390
   }
386
391
}
387
392
 
403
408
 */
404
409
 
405
410
static INLINE void
406
 
Util_ZeroString(char *str)  // IN
 
411
Util_ZeroString(char *str)  // IN/OUT
407
412
{
408
413
   if (str != NULL) {
409
 
      memset(str, 0, strlen(str));
 
414
      Util_Zero(str, strlen(str));
410
415
   }
411
416
}
412
417
 
430
435
 
431
436
static INLINE void
432
437
Util_ZeroFree(void *buf,       // OUT
433
 
              size_t bufSize)  // IN 
 
438
              size_t bufSize)  // IN
434
439
{
435
440
   if (buf != NULL) {
436
 
      memset(buf, 0, bufSize);
437
 
      free(buf); 
 
441
      Util_Zero(buf, bufSize);
 
442
      free(buf);
438
443
   }
439
444
}
440
445
 
460
465
Util_ZeroFreeString(char *str)  // IN
461
466
{
462
467
   if (str != NULL) {
463
 
      Util_Zero(str, strlen(str));
464
 
      free(str); 
 
468
      Util_ZeroString(str);
 
469
      free(str);
465
470
   }
466
471
}
467
472
 
488
493
Util_ZeroFreeStringW(wchar_t *str)  // IN
489
494
{
490
495
   if (str != NULL) {
491
 
      Util_Zero(str, wcslen(str) * sizeof(wchar_t));
492
 
      free(str); 
 
496
      Util_Zero(str, wcslen(str) * sizeof *str);
 
497
      free(str);
493
498
   }
494
499
}
495
500
#endif // _WIN32
531
536
      ssize_t i;
532
537
 
533
538
      for (i = 0; i < length; i++) {
534
 
         free(list[i]);
535
 
         DEBUG_ONLY(list[i] = NULL);
 
539
         free(list[i]);
 
540
         DEBUG_ONLY(list[i] = NULL);
536
541
      }
537
542
   } else {
538
543
      void **s;
539
544
 
540
545
      for (s = list; *s != NULL; s++) {
541
 
         free(*s);
542
 
         DEBUG_ONLY(*s = NULL);
 
546
         free(*s);
 
547
         DEBUG_ONLY(*s = NULL);
543
548
      }
544
549
   }
545
550
   free(list);