Debian Codegraph @

Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c

raw symbols SCIP
1 /*
2 * gen_uuid.c --- generate a DCE-compatible uuid
3 *
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35 /*
36 * Force inclusion of SVID stuff since we need it if we're compiling in
37 * gcc-wall wall mode
38 */
39 #define _SVID_SOURCEcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:39:9`!
40 #define _DEFAULT_SOURCEcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:40:9`! /* since glibc 2.20 _SVID_SOURCE is deprecated */
41
42 #include "config.h"cxx . . $ `<file>/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/config.h`/
43
44 #ifdef _WIN32
45 #define _WIN32_WINNT 0x0500
46 #include <windows.h>
47 #define UUID MYUUID
48 #endif
49 #include <stdio.h>
50 #ifdef HAVE_UNISTD_Hcxx . . $ `<built-in>:412:9`!
51 #include <unistd.h>
52 #endif
53 #ifdef HAVE_STDLIB_Hcxx . . $ `<built-in>:411:9`!
54 #include <stdlib.h>
55 #endif
56 #include <string.h>
57 #include <fcntl.h>
58 #include <errno.h>
59 #include <sys/types.h>
60 #ifdef HAVE_SYS_TIME_H
61 #include <sys/time.h>
62 #endif
63 #ifdef HAVE_SYS_WAIT_H
64 #include <sys/wait.h>
65 #endif
66 #include <sys/stat.h>
67 #ifdef HAVE_SYS_FILE_Hcxx . . $ `<built-in>:415:9`!
68 #include <sys/file.h>
69 #endif
70 #ifdef HAVE_SYS_IOCTL_Hcxx . . $ `<built-in>:414:9`!
71 #include <sys/ioctl.h>
72 #endif
73 #ifdef HAVE_SYS_RANDOM_H
74 #include <sys/random.h>
75 #endif
76 #ifdef HAVE_SYS_SOCKET_Hcxx . . $ `<built-in>:416:9`!
77 #include <sys/socket.h>
78 #endif
79 #ifdef HAVE_SYS_UN_H
80 #include <sys/un.h>
81 #endif
82 #ifdef HAVE_SYS_SOCKIO_H
83 #include <sys/sockio.h>
84 #endif
85 #ifdef HAVE_NET_IF_Hcxx . . $ `<built-in>:417:9`!
86 #include <net/if.h>
87 #endif
88 #ifdef HAVE_NETINET_IN_Hcxx . . $ `<built-in>:418:9`!
89 #include <netinet/in.h>
90 #endif
91 #ifdef HAVE_NET_IF_DL_H
92 #include <net/if_dl.h>
93 #endif
94 #if defined(__linux__cxx . . $ `<built-in>:398:9`!) && defined(HAVE_SYS_SYSCALL_H)
95 #include <sys/syscall.h>
96 #endif
97 #ifdef HAVE_SYS_RESOURCE_H
98 #include <sys/resource.h>
99 #endif
100
101 #include "uuidP.h"cxx . . $ `<file>/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuidP.h`/
102 #include "uuidd.h"cxx . . $ `<file>/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuidd.h`/
103
104 #ifdef HAVE_SRANDOM
105 #define srand(x) srandom(x)
106 #define rand() random()
107 #endif
108
109 #ifdef TLS
110 #define THREAD_LOCAL static TLS
111 #else
112 #define THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! static
113 #endif
114
115 #if defined(__linux__cxx . . $ `<built-in>:398:9`!) && defined(__NR_gettid) && defined(HAVE_JRAND48)
116 #define DO_JRAND_MIX
117 THREAD_LOCAL unsigned short jrand_seed[3];
118 #endif
119
120 #ifdef _WIN32
121 #ifndef USE_MINGW
122 static void gettimeofday (struct timeval *tv, void *dummy)
123 {
124 FILETIME ftime;
125 uint64_t n;
126
127 GetSystemTimeAsFileTime (&ftime);
128 n = (((uint64_t) ftime.dwHighDateTime << 32)
129 + (uint64_t) ftime.dwLowDateTime);
130 if (n) {
131 n /= 10;
132 n -= ((369 * 365 + 89) * (uint64_t) 86400) * 1000000;
133 }
134
135 tv->tv_sec = n / 1000000;
136 tv->tv_usec = n % 1000000;
137 }
138 #endif
139 #endif
140
141 static int get_random_fdcxx . . $ get_random_fd(b126dc7c1de90089).(void)
142 {
143 struct timevalcxx . . $ timeval# tv;
144 static int fd = -2;
145 int i;
146
147 if (fd == -2) {
148 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, 0);
149 #ifndef _WIN32
150 fd = opencxx . . $ open(e156e3aa1e5c4ad1).("/dev/urandom", O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!);
151 if (fd == -1)
152 fd = opencxx . . $ open(e156e3aa1e5c4ad1).("/dev/random", O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`! | O_NONBLOCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:62:10`!);
153 if (fd >= 0) {
154 i = fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(fd, F_GETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:166:9`!);
155 if (i >= 0)
156 fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(fd, F_SETFDcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:167:9`!, i | FD_CLOEXECcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:219:9`!);
157 }
158 #endif
159 srandcxx . . $ srand(cb2f890c2fdad230).(((unsigned)getpidcxx . . $ getpid(b126dc7c1de90089).() << 16) ^ getuidcxx . . $ getuid(db52760e79bf9054).() ^ tv.tv_seccxx . . $ timeval#tv_sec. ^ tv.tv_useccxx . . $ timeval#tv_usec.);
160 #ifdef DO_JRAND_MIX
161 jrand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
162 jrand_seed[1] = getppid() ^ (tv.tv_usec & 0xFFFF);
163 jrand_seed[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
164 #endif
165 }
166 /* Crank the random number generator a few times */
167 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, 0);
168 for (i = (tv.tv_seccxx . . $ timeval#tv_sec. ^ tv.tv_useccxx . . $ timeval#tv_usec.) & 0x1F; i > 0; i--)
169 randcxx . . $ rand(b126dc7c1de90089).();
170 return fd;
171 }
172
173
174 /*
175 * Generate a series of random bytes. Use /dev/urandom if possible,
176 * and if not, use srandom/random.
177 */
178 static void get_random_bytescxx . . $ get_random_bytes(d0320739773070f2).(void *buf, int nbytes)
179 {
180 int i, n = nbytes, fd;
181 int lose_counter = 0;
182 unsigned char *cp = buf;
183
184 #ifdef HAVE_GETRANDOM
185 i = getrandom(buf, nbytes, 0);
186 if (i == nbytes)
187 return;
188 #endif
189 #ifdef HAVE_GETENTROPY
190 if (getentropy(buf, nbytes) == 0)
191 return;
192 #endif
193
194 fd = get_random_fdcxx . . $ get_random_fd(b126dc7c1de90089).();
195 if (fd >= 0) {
196 while (n > 0) {
197 i = readcxx . . $ read(7e34d0a36f355389).(fd, cp, n);
198 if (i <= 0) {
199 if (lose_counter++ > 16)
200 break;
201 continue;
202 }
203 n -= i;
204 cp += i;
205 lose_counter = 0;
206 }
207 }
208
209 /*
210 * We do this all the time, but this is the only source of
211 * randomness if /dev/random/urandom is out to lunch.
212 */
213 for (cp = buf, i = 0; i < nbytes; i++)
214 *cp++ ^= (randcxx . . $ rand(b126dc7c1de90089).() >> 7) & 0xFF;
215 #ifdef DO_JRAND_MIX
216 {
217 unsigned short tmp_seed[3];
218
219 memcpy(tmp_seed, jrand_seed, sizeof(tmp_seed));
220 jrand_seed[2] = jrand_seed[2] ^ syscall(__NR_gettid);
221 for (cp = buf, i = 0; i < nbytes; i++)
222 *cp++ ^= (jrand48(tmp_seed) >> 7) & 0xFF;
223 memcpy(jrand_seed, tmp_seed,
224 sizeof(jrand_seed) - sizeof(unsigned short));
225 }
226 #endif
227
228 return;
229 }
230
231 /*
232 * Get the ethernet hardware address, if we can find it...
233 *
234 * XXX for a windows version, probably should use GetAdaptersInfo:
235 * http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451
236 * commenting out get_node_id just to get gen_uuid to compile under windows
237 * is not the right way to go!
238 */
239 static int get_node_idcxx . . $ get_node_id(b2e27cdb9bef2230).(unsigned char *node_id)
240 {
241 #ifdef HAVE_NET_IF_Hcxx . . $ `<built-in>:417:9`!
242 int sd;
243 struct ifreqcxx . . $ ifreq# ifr, *ifrp;
244 struct ifconfcxx . . $ ifconf# ifc;
245 char buf[1024];
246 int n, i;
247 unsigned char *a;
248 #ifdef HAVE_NET_IF_DL_H
249 struct sockaddr_dl *sdlp;
250 #endif
251
252 /*
253 * BSD 4.4 defines the size of an ifreq to be
254 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
255 * However, under earlier systems, sa_len isn't present, so the size is
256 * just sizeof(struct ifreq)
257 */
258 #ifdef HAVE_SA_LEN
259 #ifndef max
260 #define max(a,b) ((a) > (b) ? (a) : (b))
261 #endif
262 #define ifreq_size(i) max(sizeof(struct ifreq),\
263 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
264 #else
265 #define ifreq_sizecxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:265:9`!(i) sizeof(struct ifreq)
266 #endif /* HAVE_SA_LEN*/
267
268 sd = socketcxx . . $ socket(2326284602b0bb2d).(AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, SOCK_DGRAMcxx . . $ `<external>/5956532346368192945/socket_type.h:31:9`!, IPPROTO_IPcxx . . $ `<external>/9581057695539925927/in.h:43:9`!);
269 if (sd < 0) {
270 return -1;
271 }
272 memsetcxx . . $ memset(d1de1e4fb215f1c6).(buf, 0, sizeof(buf));
273 ifc.ifc_lencxx . . $ ifconf#ifc_len. = sizeof(buf);
274 ifc.ifc_bufcxx . . $ `<external>/2638090476884991293/if.h:185:10`! = buf;
275 if (ioctlcxx . . $ ioctl(67fa1c773b05068c). (sd, SIOCGIFCONFcxx . . $ `<external>/13324550044150506404/sockios.h:59:9`!, (char *)&ifc) < 0) {
276 closecxx . . $ close(7864480464b09eea).(sd);
277 return -1;
278 }
279 n = ifc.ifc_lencxx . . $ ifconf#ifc_len.;
280 for (i = 0; i < n; i+= ifreq_sizecxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:265:9`!(*ifrp) ) {
281 ifrp = (struct ifreqcxx . . $ ifreq# *)((char *) ifc.ifc_bufcxx . . $ `<external>/2638090476884991293/if.h:185:10`!+i);
282 strncpycxx . . $ strncpy(255158a415b673b0).(ifr.ifr_namecxx . . $ `<external>/2638090476884991293/if.h:151:10`!, ifrp->ifr_namecxx . . $ `<external>/2638090476884991293/if.h:151:10`!, IFNAMSIZcxx . . $ `<external>/2638090476884991293/if.h:129:10`!);
283 #ifdef SIOCGIFHWADDRcxx . . $ `<external>/13324550044150506404/sockios.h:80:9`!
284 if (ioctlcxx . . $ ioctl(67fa1c773b05068c).(sd, SIOCGIFHWADDRcxx . . $ `<external>/13324550044150506404/sockios.h:80:9`!, &ifr) < 0)
285 continue;
286 a = (unsigned char *) &ifr.ifr_hwaddrcxx . . $ `<external>/2638090476884991293/if.h:152:10`!.sa_datacxx . . $ sockaddr#sa_data.;
287 #else
288 #ifdef SIOCGENADDR
289 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
290 continue;
291 a = (unsigned char *) ifr.ifr_enaddr;
292 #else
293 #ifdef HAVE_NET_IF_DL_H
294 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
295 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
296 continue;
297 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
298 #else
299 /*
300 * XXX we don't have a way of getting the hardware
301 * address
302 */
303 close(sd);
304 return 0;
305 #endif /* HAVE_NET_IF_DL_H */
306 #endif /* SIOCGENADDR */
307 #endif /* SIOCGIFHWADDR */
308 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
309 continue;
310 if (node_id) {
311 memcpycxx . . $ memcpy(cc817cb175f282d).(node_id, a, 6);
312 closecxx . . $ close(7864480464b09eea).(sd);
313 return 1;
314 }
315 }
316 closecxx . . $ close(7864480464b09eea).(sd);
317 #endif
318 return 0;
319 }
320
321 /* Assume that the gettimeofday() has microsecond granularity */
322 #define MAX_ADJUSTMENTcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:322:9`! 10
323
324 static int get_clockcxx . . $ get_clock(dbb8f44a7bc7b5b1).(uint32_tcxx . . $ uint32_t# *clock_high, uint32_tcxx . . $ uint32_t# *clock_low,
325 uint16_tcxx . . $ uint16_t# *ret_clock_seq, int *num)
326 {
327 THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! int adjustment = 0;
328 THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! struct timevalcxx . . $ timeval# last = {0, 0};
329 THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! int state_fd = -2;
330 THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! FILEcxx . . $ FILE# *state_f;
331 THREAD_LOCALcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:112:9`! uint16_tcxx . . $ uint16_t# clock_seq;
332 struct timevalcxx . . $ timeval# tv;
333 #ifndef _WIN32
334 struct flockcxx . . $ flock# fl;
335 #endif
336 uint64_tcxx . . $ uint64_t# clock_reg;
337 mode_tcxx . . $ mode_t# save_umask;
338 int len;
339
340 if (state_fd == -2) {
341 save_umask = umaskcxx . . $ umask(2141b656ce2cc063).(0);
342 state_fd = opencxx . . $ open(e156e3aa1e5c4ad1).("/var/lib/libuuid/clock.txt",
343 O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!|O_CREATcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:47:10`!, 0660);
344 (void) umaskcxx . . $ umask(2141b656ce2cc063).(save_umask);
345 if (state_fd >= 0) {
346 state_f = fdopencxx . . $ fdopen(8f4e2d7350ebd077).(state_fd, "r+");
347 if (!state_f) {
348 closecxx . . $ close(7864480464b09eea).(state_fd);
349 state_fd = -1;
350 }
351 }
352 }
353 #ifndef _WIN32
354 fl.l_typecxx . . $ flock#l_type. = F_WRLCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:224:10`!;
355 fl.l_whencecxx . . $ flock#l_whence. = SEEK_SETcxx . . $ `<external>/14710646456994434043/fcntl.h:137:10`!;
356 fl.l_startcxx . . $ flock#l_start. = 0;
357 fl.l_lencxx . . $ flock#l_len. = 0;
358 fl.l_pidcxx . . $ flock#l_pid. = 0;
359 if (state_fd >= 0) {
360 rewindcxx . . $ rewind(8d4262d45fc644ab).(state_f);
361 while (fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(state_fd, F_SETLKWcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:109:11`!, &fl) < 0) {
362 if ((errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`!) || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!))
363 continue;
364 fclosecxx . . $ fclose(89bc1efcdac0ca22).(state_f);
365 state_fd = -1;
366 break;
367 }
368 }
369 #endif
370 if (state_fd >= 0) {
371 unsigned int cl;
372 unsigned long tv1, tv2;
373 int a;
374
375 if (fscanfcxx . . $ fscanf(bc216f8e5b1ebeac).(state_f, "clock: %04x tv: %lu %lu adj: %d\n",
376 &cl, &tv1, &tv2, &a) == 4) {
377 clock_seq = cl & 0x3FFF;
378 last.tv_seccxx . . $ timeval#tv_sec. = tv1;
379 last.tv_useccxx . . $ timeval#tv_usec. = tv2;
380 adjustment = a;
381 }
382 }
383
384 if ((last.tv_seccxx . . $ timeval#tv_sec. == 0) && (last.tv_useccxx . . $ timeval#tv_usec. == 0)) {
385 get_random_bytescxx . . $ get_random_bytes(d0320739773070f2).(&clock_seq, sizeof(clock_seq));
386 clock_seq &= 0x3FFF;
387 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&last, 0);
388 last.tv_seccxx . . $ timeval#tv_sec.--;
389 }
390
391 try_again:
392 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, 0);
393 if ((tv.tv_seccxx . . $ timeval#tv_sec. < last.tv_seccxx . . $ timeval#tv_sec.) ||
394 ((tv.tv_seccxx . . $ timeval#tv_sec. == last.tv_seccxx . . $ timeval#tv_sec.) &&
395 (tv.tv_useccxx . . $ timeval#tv_usec. < last.tv_useccxx . . $ timeval#tv_usec.))) {
396 clock_seq = (clock_seq+1) & 0x3FFF;
397 adjustment = 0;
398 last = tv;
399 } else if ((tv.tv_seccxx . . $ timeval#tv_sec. == last.tv_seccxx . . $ timeval#tv_sec.) &&
400 (tv.tv_useccxx . . $ timeval#tv_usec. == last.tv_useccxx . . $ timeval#tv_usec.)) {
401 if (adjustment >= MAX_ADJUSTMENTcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/gen_uuid.c:322:9`!)
402 goto try_again;
403 adjustment++;
404 } else {
405 adjustment = 0;
406 last = tv;
407 }
408
409 clock_reg = tv.tv_useccxx . . $ timeval#tv_usec.*10 + adjustment;
410 clock_reg += ((uint64_tcxx . . $ uint64_t#) tv.tv_seccxx . . $ timeval#tv_sec.)*10000000;
411 clock_reg += (((uint64_tcxx . . $ uint64_t#) 0x01B21DD2) << 32) + 0x13814000;
412
413 if (num && (*num > 1)) {
414 adjustment += *num - 1;
415 last.tv_useccxx . . $ timeval#tv_usec. += adjustment / 10;
416 adjustment = adjustment % 10;
417 last.tv_seccxx . . $ timeval#tv_sec. += last.tv_useccxx . . $ timeval#tv_usec. / 1000000;
418 last.tv_useccxx . . $ timeval#tv_usec. = last.tv_useccxx . . $ timeval#tv_usec. % 1000000;
419 }
420
421 if (state_fd > 0) {
422 rewindcxx . . $ rewind(8d4262d45fc644ab).(state_f);
423 len = fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(state_f,
424 "clock: %04x tv: %016lu %08lu adj: %08d\n",
425 clock_seq, (unsigned long)last.tv_seccxx . . $ timeval#tv_sec.,
426 (unsigned long)last.tv_useccxx . . $ timeval#tv_usec., adjustment);
427 fflushcxx . . $ fflush(89bc1efcdac0ca22).(state_f);
428 if (ftruncatecxx . . $ ftruncate(bba9d6e9fb34fad7).(state_fd, len) < 0) {
429 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(state_f, " \n");
430 fflushcxx . . $ fflush(89bc1efcdac0ca22).(state_f);
431 }
432 rewindcxx . . $ rewind(8d4262d45fc644ab).(state_f);
433 #ifndef _WIN32
434 fl.l_typecxx . . $ flock#l_type. = F_UNLCKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:225:10`!;
435 if (fcntlcxx . . $ fcntl(cac4efb70f20a0fb).(state_fd, F_SETLKcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:108:11`!, &fl) < 0) {
436 fclosecxx . . $ fclose(89bc1efcdac0ca22).(state_f);
437 state_fd = -1;
438 }
439 #endif
440 }
441
442 *clock_high = clock_reg >> 32;
443 *clock_low = clock_reg;
444 *ret_clock_seq = clock_seq;
445 return 0;
446 }
447
448 #if defined(USE_UUIDD) && defined(HAVE_SYS_UN_H)
449 static ssize_t read_all(int fd, char *buf, size_t count)
450 {
451 ssize_t ret;
452 ssize_t c = 0;
453 int tries = 0;
454
455 memset(buf, 0, count);
456 while (count > 0) {
457 ret = read(fd, buf, count);
458 if (ret <= 0) {
459 if ((errno == EAGAIN || errno == EINTR || ret == 0) &&
460 (tries++ < 5))
461 continue;
462 return c ? c : -1;
463 }
464 if (ret > 0)
465 tries = 0;
466 count -= ret;
467 buf += ret;
468 c += ret;
469 }
470 return c;
471 }
472
473 /*
474 * Close all file descriptors
475 */
476 static void close_all_fds(void)
477 {
478 int i, max;
479
480 #if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX)
481 max = sysconf(_SC_OPEN_MAX);
482 #elif defined(HAVE_GETDTABLESIZE)
483 max = getdtablesize();
484 #elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
485 struct rlimit rl;
486
487 getrlimit(RLIMIT_NOFILE, &rl);
488 max = rl.rlim_cur;
489 #else
490 max = OPEN_MAX;
491 #endif
492
493 for (i=0; i < max; i++) {
494 close(i);
495 if (i <= 2)
496 open("/dev/null", O_RDWR);
497 }
498 }
499 #endif /* defined(USE_UUIDD) && defined(HAVE_SYS_UN_H) */
500
501 #if __GNUC_PREREQcxx . . $ `<external>/18206667465210621810/features.h:173:10`! (4, 6)
502 #pragma GCC diagnostic push
503 #if !defined(USE_UUIDD) || !defined(HAVE_SYS_UN_H)
504 #pragma GCC diagnostic ignored "-Wunused-parameter"
505 #endif
506 #endif
507 /*
508 * Try using the uuidd daemon to generate the UUID
509 *
510 * Returns 0 on success, non-zero on failure.
511 */
512 static int get_uuid_via_daemoncxx . . $ get_uuid_via_daemon(3530b5bd96ff502f).(int op, uuid_tcxx . . $ uuid_t# out, int *num)
513 {
514 #if defined(USE_UUIDD) && defined(HAVE_SYS_UN_H)
515 char op_buf[64];
516 int op_len;
517 int s;
518 ssize_t ret;
519 int32_t reply_len = 0, expected = 16;
520 struct sockaddr_un srv_addr;
521 struct stat st;
522 pid_t pid;
523 static const char *uuidd_path = UUIDD_PATH;
524 static int access_ret = -2;
525 static int start_attempts = 0;
526
527 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
528 return -1;
529
530 srv_addr.sun_family = AF_UNIX;
531 strcpy(srv_addr.sun_path, UUIDD_SOCKET_PATH);
532
533 if (connect(s, (const struct sockaddr *) &srv_addr,
534 sizeof(struct sockaddr_un)) < 0) {
535 if (access_ret == -2)
536 access_ret = access(uuidd_path, X_OK);
537 if (access_ret == 0)
538 access_ret = stat(uuidd_path, &st);
539 if (access_ret == 0 && (st.st_mode & (S_ISUID | S_ISGID)) == 0)
540 access_ret = access(UUIDD_DIR, W_OK);
541 if (access_ret == 0 && start_attempts++ < 5) {
542 if ((pid = fork()) == 0) {
543 close_all_fds();
544 execl(uuidd_path, "uuidd", "-qT", "300",
545 (char *) NULL);
546 exit(1);
547 }
548 (void) waitpid(pid, 0, 0);
549 if (connect(s, (const struct sockaddr *) &srv_addr,
550 sizeof(struct sockaddr_un)) < 0)
551 goto fail;
552 } else
553 goto fail;
554 }
555 op_buf[0] = op;
556 op_len = 1;
557 if (op == UUIDD_OP_BULK_TIME_UUID) {
558 memcpy(op_buf+1, num, sizeof(*num));
559 op_len += sizeof(*num);
560 expected += sizeof(*num);
561 }
562
563 ret = write(s, op_buf, op_len);
564 if (ret < 1)
565 goto fail;
566
567 ret = read_all(s, (char *) &reply_len, sizeof(reply_len));
568 if (ret < 0)
569 goto fail;
570
571 if (reply_len != expected)
572 goto fail;
573
574 ret = read_all(s, op_buf, reply_len);
575
576 if (op == UUIDD_OP_BULK_TIME_UUID)
577 memcpy(op_buf+16, num, sizeof(int));
578
579 memcpy(out, op_buf, 16);
580
581 close(s);
582 return ((ret == expected) ? 0 : -1);
583
584 fail:
585 close(s);
586 #endif
587 return -1;
588 }
589 #if __GNUC_PREREQcxx . . $ `<external>/18206667465210621810/features.h:173:10`! (4, 6)
590 #pragma GCC diagnostic pop
591 #endif
592
593 void uuid__generate_timecxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:20:9`!(uuid_tcxx . . $ uuid_t# out, int *num)
594 {
595 static unsigned char node_id[6];
596 static int has_init = 0;
597 struct uuidcxx . . $ uuid# uu;
598 uint32_tcxx . . $ uint32_t# clock_mid;
599
600 if (!has_init) {
601 if (get_node_idcxx . . $ get_node_id(b2e27cdb9bef2230).(node_id) <= 0) {
602 get_random_bytescxx . . $ get_random_bytes(d0320739773070f2).(node_id, 6);
603 /*
604 * Set multicast bit, to prevent conflicts
605 * with IEEE 802 addresses obtained from
606 * network cards
607 */
608 node_id[0] |= 0x01;
609 }
610 has_init = 1;
611 }
612 get_clockcxx . . $ get_clock(dbb8f44a7bc7b5b1).(&clock_mid, &uu.time_lowcxx . . $ uuid#time_low., &uu.clock_seqcxx . . $ uuid#clock_seq., num);
613 uu.clock_seqcxx . . $ uuid#clock_seq. |= 0x8000;
614 uu.time_midcxx . . $ uuid#time_mid. = (uint16_tcxx . . $ uint16_t#) clock_mid;
615 uu.time_hi_and_versioncxx . . $ uuid#time_hi_and_version. = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
616 memcpycxx . . $ memcpy(cc817cb175f282d).(uu.nodecxx . . $ uuid#node., node_id, 6);
617 uuid_packcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:26:9`!(&uu, out);
618 }
619
620 void uuid_generate_timecxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:23:9`!(uuid_tcxx . . $ uuid_t# out)
621 {
622 #ifdef TLS
623 THREAD_LOCAL int num = 0;
624 THREAD_LOCAL struct uuid uu;
625 THREAD_LOCAL time_t last_time = 0;
626 time_t now;
627
628 if (num > 0) {
629 now = time(0);
630 if (now > last_time+1)
631 num = 0;
632 }
633 if (num <= 0) {
634 num = 1000;
635 if (get_uuid_via_daemon(UUIDD_OP_BULK_TIME_UUID,
636 out, &num) == 0) {
637 last_time = time(0);
638 uuid_unpack(out, &uu);
639 num--;
640 return;
641 }
642 num = 0;
643 }
644 if (num > 0) {
645 uu.time_low++;
646 if (uu.time_low == 0) {
647 uu.time_mid++;
648 if (uu.time_mid == 0)
649 uu.time_hi_and_version++;
650 }
651 num--;
652 uuid_pack(&uu, out);
653 return;
654 }
655 #else
656 if (get_uuid_via_daemoncxx . . $ get_uuid_via_daemon(3530b5bd96ff502f).(UUIDD_OP_TIME_UUIDcxx . . $ `Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuidd.h:45:9`!, out, 0) == 0)
657 return;
658 #endif
659
660 uuid__generate_timecxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:20:9`!(out, 0);
661 }
662
663
664 void uuid__generate_randomcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:19:9`!(uuid_tcxx . . $ uuid_t# out, int *num)
665 {
666 uuid_tcxx . . $ uuid_t# buf;
667 struct uuidcxx . . $ uuid# uu;
668 int i, n;
669
670 if (!num || !*num)
671 n = 1;
672 else
673 n = *num;
674
675 for (i = 0; i < n; i++) {
676 get_random_bytescxx . . $ get_random_bytes(d0320739773070f2).(buf, sizeof(buf));
677 uuid_unpackcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:30:9`!(buf, &uu);
678
679 uu.clock_seqcxx . . $ uuid#clock_seq. = (uu.clock_seqcxx . . $ uuid#clock_seq. & 0x3FFF) | 0x8000;
680 uu.time_hi_and_versioncxx . . $ uuid#time_hi_and_version. = (uu.time_hi_and_versioncxx . . $ uuid#time_hi_and_version. & 0x0FFF)
681 | 0x4000;
682 uuid_packcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:26:9`!(&uu, out);
683 out += sizeof(uuid_tcxx . . $ uuid_t#);
684 }
685 }
686
687 void uuid_generate_randomcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:22:9`!(uuid_tcxx . . $ uuid_t# out)
688 {
689 int num = 1;
690 /* No real reason to use the daemon for random uuid's -- yet */
691
692 uuid__generate_randomcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:19:9`!(out, &num);
693 }
694
695
696 /*
697 * This is the generic front-end to uuid_generate_random and
698 * uuid_generate_time. It uses uuid_generate_random only if
699 * /dev/urandom is available, since otherwise we won't have
700 * high-quality randomness.
701 */
702 void uuid_generatecxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:21:9`!(uuid_tcxx . . $ uuid_t# out)
703 {
704 if (get_random_fdcxx . . $ get_random_fd(b126dc7c1de90089).() >= 0)
705 uuid_generate_randomcxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:22:9`!(out);
706 else
707 uuid_generate_timecxx . . $ `build/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmuuid/uuid_mangle.h:23:9`!(out);
708 }