Debian Codegraph @

unix/tclUnixCompat.c

raw
1 /*
2 * tclUnixCompat.c
3 *
4 * Written by: Zoran Vasiljevic ([email protected]).
5 *
6 * See the file "license.terms" for information on usage and redistribution of
7 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 */
9
10 #include "tclInt.h"
11 #include <pwd.h>
12 #include <grp.h>
13 #include <errno.h>
14 #include <string.h>
15
16 /*
17 * See also: SC_BLOCKING_STYLE in unix/tcl.m4
18 */
19
20 #ifdef USE_FIONBIO
21 # ifdef HAVE_SYS_FILIO_H
22 # include <sys/filio.h> /* For FIONBIO. */
23 # endif
24 # ifdef HAVE_SYS_IOCTL_H
25 # include <sys/ioctl.h>
26 # endif
27 #endif /* USE_FIONBIO */
28
29 /*
30 * Used to pad structures at size'd boundaries
31 *
32 * This macro assumes that the pointer 'buffer' was created from an aligned
33 * pointer by adding the 'length'. If this 'length' was not a multiple of the
34 * 'size' the result is unaligned and PadBuffer corrects both the pointer,
35 * _and_ the 'length'. The latter means that future increments of 'buffer' by
36 * 'length' stay aligned.
37 */
38
39 #define PadBuffer(buffer, length, size) \
40 if (((length) % (size))) { \
41 (buffer) += ((size) - ((length) % (size))); \
42 (length) += ((size) - ((length) % (size))); \
43 }
44
45 /*
46 * Per-thread private storage used to store values returned from MT-unsafe
47 * library calls.
48 */
49
50 #ifdef TCL_THREADS
51
52 typedef struct {
53 struct passwd pwd;
54 #if defined(HAVE_GETPWNAM_R_5) || defined(HAVE_GETPWUID_R_5)
55 #define NEED_PW_CLEANER 1
56 char *pbuf;
57 int pbuflen;
58 #else
59 char pbuf[2048];
60 #endif
61
62 struct group grp;
63 #if defined(HAVE_GETGRNAM_R_5) || defined(HAVE_GETGRGID_R_5)
64 #define NEED_GR_CLEANER 1
65 char *gbuf;
66 int gbuflen;
67 #else
68 char gbuf[2048];
69 #endif
70
71 #if !defined(HAVE_MTSAFE_GETHOSTBYNAME) || !defined(HAVE_MTSAFE_GETHOSTBYADDR)
72 struct hostent hent;
73 char hbuf[2048];
74 #endif
75 } ThreadSpecificData;
76 static Tcl_ThreadDataKey dataKey;
77
78 #if ((!defined(HAVE_GETHOSTBYNAME_R) || !defined(HAVE_GETHOSTBYADDR_R)) && \
79 (!defined(HAVE_MTSAFE_GETHOSTBYNAME) || \
80 !defined(HAVE_MTSAFE_GETHOSTBYADDR))) || \
81 !defined(HAVE_GETPWNAM_R) || !defined(HAVE_GETPWUID_R) || \
82 !defined(HAVE_GETGRNAM_R) || !defined(HAVE_GETGRGID_R)
83 /*
84 * Mutex to lock access to MT-unsafe calls. This is just to protect our own
85 * usage. It does not protect us from others calling the same functions
86 * without (or using some different) lock.
87 */
88
89 static Tcl_Mutex compatLock;
90
91 /*
92 * Helper function declarations. Note that these are only used if needed and
93 * only defined if used (via the NEED_* macros).
94 */
95
96 #undef NEED_COPYARRAY
97 #undef NEED_COPYGRP
98 #undef NEED_COPYHOSTENT
99 #undef NEED_COPYPWD
100 #undef NEED_COPYSTRING
101
102 #if !defined(HAVE_GETGRNAM_R_5) && !defined(HAVE_GETGRNAM_R_4)
103 #define NEED_COPYGRP 1
104 static int CopyGrp(struct group *tgtPtr, char *buf, int buflen);
105 #endif
106
107 #if !defined(HAVE_GETPWNAM_R_5) && !defined(HAVE_GETPWNAM_R_4)
108 #define NEED_COPYPWD 1
109 static int CopyPwd(struct passwd *tgtPtr, char *buf, int buflen);
110 #endif
111
112 static int CopyArray(char **src, int elsize, char *buf,
113 int buflen);
114 static int CopyHostent(struct hostent *tgtPtr, char *buf,
115 int buflen);
116 static int CopyString(const char *src, char *buf, int buflen);
117
118 #endif
119
120 #ifdef NEED_PW_CLEANER
121 static void FreePwBuf(ClientData dummy);
122 #endif
123 #ifdef NEED_GR_CLEANER
124 static void FreeGrBuf(ClientData dummy);
125 #endif
126 #endif /* TCL_THREADS */
127
128 /*
129 *---------------------------------------------------------------------------
130 *
131 * TclUnixSetBlockingMode --
132 *
133 * Set the blocking mode of a file descriptor.
134 *
135 * Results:
136 *
137 * 0 on success, -1 (with errno set) on error.
138 *
139 *---------------------------------------------------------------------------
140 */
141
142 int
143 TclUnixSetBlockingMode(
144 int fd, /* File descriptor */
145 int mode) /* Either TCL_MODE_BLOCKING or
146 * TCL_MODE_NONBLOCKING. */
147 {
148 #ifndef USE_FIONBIO
149 int flags = fcntl(fd, F_GETFL);
150
151 if (mode == TCL_MODE_BLOCKING) {
152 flags &= ~O_NONBLOCK;
153 } else {
154 flags |= O_NONBLOCK;
155 }
156 return fcntl(fd, F_SETFL, flags);
157 #else /* USE_FIONBIO */
158 int state = (mode == TCL_MODE_NONBLOCKING);
159
160 return ioctl(fd, FIONBIO, &state);
161 #endif /* !USE_FIONBIO */
162 }
163
164 /*
165 *---------------------------------------------------------------------------
166 *
167 * TclpGetPwNam --
168 *
169 * Thread-safe wrappers for getpwnam(). See "man getpwnam" for more
170 * details.
171 *
172 * Results:
173 * Pointer to struct passwd on success or NULL on error.
174 *
175 * Side effects:
176 * None.
177 *
178 *---------------------------------------------------------------------------
179 */
180
181 struct passwd *
182 TclpGetPwNam(
183 const char *name)
184 {
185 #if !defined(TCL_THREADS)
186 return getpwnam(name);
187 #else
188 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
189
190 #if defined(HAVE_GETPWNAM_R_5)
191 struct passwd *pwPtr = NULL;
192
193 /*
194 * How to allocate a buffer of the right initial size. If you want the
195 * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
196 * and weep.
197 */
198
199 if (tsdPtr->pbuf == NULL) {
200 tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
201 if (tsdPtr->pbuflen < 1) {
202 tsdPtr->pbuflen = 1024;
203 }
204 tsdPtr->pbuf = (char *)ckalloc(tsdPtr->pbuflen);
205 Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
206 }
207 while (1) {
208 int e = getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, tsdPtr->pbuflen,
209 &pwPtr);
210
211 if (e == 0) {
212 break;
213 } else if (e != ERANGE) {
214 return NULL;
215 }
216 tsdPtr->pbuflen *= 2;
217 tsdPtr->pbuf = (char *)ckrealloc(tsdPtr->pbuf, tsdPtr->pbuflen);
218 }
219 return (pwPtr != NULL ? &tsdPtr->pwd : NULL);
220
221 #elif defined(HAVE_GETPWNAM_R_4)
222 return getpwnam_r(name, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));
223
224 #else
225 struct passwd *pwPtr;
226
227 Tcl_MutexLock(&compatLock);
228 pwPtr = getpwnam(name);
229 if (pwPtr != NULL) {
230 tsdPtr->pwd = *pwPtr;
231 pwPtr = &tsdPtr->pwd;
232 if (CopyPwd(&tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf)) == -1) {
233 pwPtr = NULL;
234 }
235 }
236 Tcl_MutexUnlock(&compatLock);
237 return pwPtr;
238 #endif
239
240 return NULL; /* Not reached. */
241 #endif /* TCL_THREADS */
242 }
243
244 /*
245 *---------------------------------------------------------------------------
246 *
247 * TclpGetPwUid --
248 *
249 * Thread-safe wrappers for getpwuid(). See "man getpwuid" for more
250 * details.
251 *
252 * Results:
253 * Pointer to struct passwd on success or NULL on error.
254 *
255 * Side effects:
256 * None.
257 *
258 *---------------------------------------------------------------------------
259 */
260
261 struct passwd *
262 TclpGetPwUid(
263 uid_t uid)
264 {
265 #if !defined(TCL_THREADS)
266 return getpwuid(uid);
267 #else
268 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
269
270 #if defined(HAVE_GETPWUID_R_5)
271 struct passwd *pwPtr = NULL;
272
273 /*
274 * How to allocate a buffer of the right initial size. If you want the
275 * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
276 * and weep.
277 */
278
279 if (tsdPtr->pbuf == NULL) {
280 tsdPtr->pbuflen = (int) sysconf(_SC_GETPW_R_SIZE_MAX);
281 if (tsdPtr->pbuflen < 1) {
282 tsdPtr->pbuflen = 1024;
283 }
284 tsdPtr->pbuf = (char *)ckalloc(tsdPtr->pbuflen);
285 Tcl_CreateThreadExitHandler(FreePwBuf, NULL);
286 }
287 while (1) {
288 int e = getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, tsdPtr->pbuflen,
289 &pwPtr);
290
291 if (e == 0) {
292 break;
293 } else if (e != ERANGE) {
294 return NULL;
295 }
296 tsdPtr->pbuflen *= 2;
297 tsdPtr->pbuf = (char *)ckrealloc(tsdPtr->pbuf, tsdPtr->pbuflen);
298 }
299 return (pwPtr != NULL ? &tsdPtr->pwd : NULL);
300
301 #elif defined(HAVE_GETPWUID_R_4)
302 return getpwuid_r(uid, &tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf));
303
304 #else
305 struct passwd *pwPtr;
306
307 Tcl_MutexLock(&compatLock);
308 pwPtr = getpwuid(uid);
309 if (pwPtr != NULL) {
310 tsdPtr->pwd = *pwPtr;
311 pwPtr = &tsdPtr->pwd;
312 if (CopyPwd(&tsdPtr->pwd, tsdPtr->pbuf, sizeof(tsdPtr->pbuf)) == -1) {
313 pwPtr = NULL;
314 }
315 }
316 Tcl_MutexUnlock(&compatLock);
317 return pwPtr;
318 #endif
319
320 return NULL; /* Not reached. */
321 #endif /* TCL_THREADS */
322 }
323
324 /*
325 *---------------------------------------------------------------------------
326 *
327 * FreePwBuf --
328 *
329 * Helper that is used to dispose of space allocated and referenced from
330 * the ThreadSpecificData for user entries. (Darn that baroque POSIX
331 * reentrant interface.)
332 *
333 *---------------------------------------------------------------------------
334 */
335
336 #ifdef NEED_PW_CLEANER
337 static void
338 FreePwBuf(
339 ClientData dummy)
340 {
341 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
342 (void)dummy;
343
344 ckfree(tsdPtr->pbuf);
345 }
346 #endif /* NEED_PW_CLEANER */
347
348 /*
349 *---------------------------------------------------------------------------
350 *
351 * TclpGetGrNam --
352 *
353 * Thread-safe wrappers for getgrnam(). See "man getgrnam" for more
354 * details.
355 *
356 * Results:
357 * Pointer to struct group on success or NULL on error.
358 *
359 * Side effects:
360 * None.
361 *
362 *---------------------------------------------------------------------------
363 */
364
365 struct group *
366 TclpGetGrNam(
367 const char *name)
368 {
369 #if !defined(TCL_THREADS)
370 return getgrnam(name);
371 #else
372 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
373
374 #if defined(HAVE_GETGRNAM_R_5)
375 struct group *grPtr = NULL;
376
377 /*
378 * How to allocate a buffer of the right initial size. If you want the
379 * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
380 * and weep.
381 */
382
383 if (tsdPtr->gbuf == NULL) {
384 tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
385 if (tsdPtr->gbuflen < 1) {
386 tsdPtr->gbuflen = 1024;
387 }
388 tsdPtr->gbuf = (char *)ckalloc(tsdPtr->gbuflen);
389 Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
390 }
391 while (1) {
392 int e = getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, tsdPtr->gbuflen,
393 &grPtr);
394
395 if (e == 0) {
396 break;
397 } else if (e != ERANGE) {
398 return NULL;
399 }
400 tsdPtr->gbuflen *= 2;
401 tsdPtr->gbuf = (char *)ckrealloc(tsdPtr->gbuf, tsdPtr->gbuflen);
402 }
403 return (grPtr != NULL ? &tsdPtr->grp : NULL);
404
405 #elif defined(HAVE_GETGRNAM_R_4)
406 return getgrnam_r(name, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));
407
408 #else
409 struct group *grPtr;
410
411 Tcl_MutexLock(&compatLock);
412 grPtr = getgrnam(name);
413 if (grPtr != NULL) {
414 tsdPtr->grp = *grPtr;
415 grPtr = &tsdPtr->grp;
416 if (CopyGrp(&tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf)) == -1) {
417 grPtr = NULL;
418 }
419 }
420 Tcl_MutexUnlock(&compatLock);
421 return grPtr;
422 #endif
423
424 return NULL; /* Not reached. */
425 #endif /* TCL_THREADS */
426 }
427
428 /*
429 *---------------------------------------------------------------------------
430 *
431 * TclpGetGrGid --
432 *
433 * Thread-safe wrappers for getgrgid(). See "man getgrgid" for more
434 * details.
435 *
436 * Results:
437 * Pointer to struct group on success or NULL on error.
438 *
439 * Side effects:
440 * None.
441 *
442 *---------------------------------------------------------------------------
443 */
444
445 struct group *
446 TclpGetGrGid(
447 gid_t gid)
448 {
449 #if !defined(TCL_THREADS)
450 return getgrgid(gid);
451 #else
452 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
453
454 #if defined(HAVE_GETGRGID_R_5)
455 struct group *grPtr = NULL;
456
457 /*
458 * How to allocate a buffer of the right initial size. If you want the
459 * gory detail, see http://www.opengroup.org/austin/docs/austin_328.txt
460 * and weep.
461 */
462
463 if (tsdPtr->gbuf == NULL) {
464 tsdPtr->gbuflen = (int) sysconf(_SC_GETGR_R_SIZE_MAX);
465 if (tsdPtr->gbuflen < 1) {
466 tsdPtr->gbuflen = 1024;
467 }
468 tsdPtr->gbuf = (char *)ckalloc(tsdPtr->gbuflen);
469 Tcl_CreateThreadExitHandler(FreeGrBuf, NULL);
470 }
471 while (1) {
472 int e = getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, tsdPtr->gbuflen,
473 &grPtr);
474
475 if (e == 0) {
476 break;
477 } else if (e != ERANGE) {
478 return NULL;
479 }
480 tsdPtr->gbuflen *= 2;
481 tsdPtr->gbuf = (char *)ckrealloc(tsdPtr->gbuf, tsdPtr->gbuflen);
482 }
483 return (grPtr != NULL ? &tsdPtr->grp : NULL);
484
485 #elif defined(HAVE_GETGRGID_R_4)
486 return getgrgid_r(gid, &tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf));
487
488 #else
489 struct group *grPtr;
490
491 Tcl_MutexLock(&compatLock);
492 grPtr = getgrgid(gid);
493 if (grPtr != NULL) {
494 tsdPtr->grp = *grPtr;
495 grPtr = &tsdPtr->grp;
496 if (CopyGrp(&tsdPtr->grp, tsdPtr->gbuf, sizeof(tsdPtr->gbuf)) == -1) {
497 grPtr = NULL;
498 }
499 }
500 Tcl_MutexUnlock(&compatLock);
501 return grPtr;
502 #endif
503
504 return NULL; /* Not reached. */
505 #endif /* TCL_THREADS */
506 }
507
508 /*
509 *---------------------------------------------------------------------------
510 *
511 * FreeGrBuf --
512 *
513 * Helper that is used to dispose of space allocated and referenced from
514 * the ThreadSpecificData for group entries. (Darn that baroque POSIX
515 * reentrant interface.)
516 *
517 *---------------------------------------------------------------------------
518 */
519
520 #ifdef NEED_GR_CLEANER
521 static void
522 FreeGrBuf(
523 ClientData dummy)
524 {
525 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
526 (void)dummy;
527
528 ckfree(tsdPtr->gbuf);
529 }
530 #endif /* NEED_GR_CLEANER */
531
532 /*
533 *---------------------------------------------------------------------------
534 *
535 * TclpGetHostByName --
536 *
537 * Thread-safe wrappers for gethostbyname(). See "man gethostbyname" for
538 * more details.
539 *
540 * Results:
541 * Pointer to struct hostent on success or NULL on error.
542 *
543 * Side effects:
544 * None.
545 *
546 *---------------------------------------------------------------------------
547 */
548
549 struct hostent *
550 TclpGetHostByName(
551 const char *name)
552 {
553 #if !defined(TCL_THREADS) || defined(HAVE_MTSAFE_GETHOSTBYNAME)
554 return gethostbyname(name);
555 #else
556 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
557
558 #if defined(HAVE_GETHOSTBYNAME_R_5)
559 int local_errno;
560
561 return gethostbyname_r(name, &tsdPtr->hent, tsdPtr->hbuf,
562 sizeof(tsdPtr->hbuf), &local_errno);
563
564 #elif defined(HAVE_GETHOSTBYNAME_R_6)
565 struct hostent *hePtr = NULL;
566 int local_errno, result;
567
568 result = gethostbyname_r(name, &tsdPtr->hent, tsdPtr->hbuf,
569 sizeof(tsdPtr->hbuf), &hePtr, &local_errno);
570 return (result == 0) ? hePtr : NULL;
571
572 #elif defined(HAVE_GETHOSTBYNAME_R_3)
573 struct hostent_data data;
574
575 return (gethostbyname_r(name, &tsdPtr->hent, &data) == 0)
576 ? &tsdPtr->hent : NULL;
577
578 #else
579 #define NEED_COPYHOSTENT 1
580 struct hostent *hePtr;
581
582 Tcl_MutexLock(&compatLock);
583 hePtr = gethostbyname(name);
584 if (hePtr != NULL) {
585 tsdPtr->hent = *hePtr;
586 hePtr = &tsdPtr->hent;
587 if (CopyHostent(&tsdPtr->hent, tsdPtr->hbuf,
588 sizeof(tsdPtr->hbuf)) == -1) {
589 hePtr = NULL;
590 }
591 }
592 Tcl_MutexUnlock(&compatLock);
593 return hePtr;
594 #endif
595
596 return NULL; /* Not reached. */
597 #endif /* TCL_THREADS */
598 }
599
600 /*
601 *---------------------------------------------------------------------------
602 *
603 * TclpGetHostByAddr --
604 *
605 * Thread-safe wrappers for gethostbyaddr(). See "man gethostbyaddr" for
606 * more details.
607 *
608 * Results:
609 * Pointer to struct hostent on success or NULL on error.
610 *
611 * Side effects:
612 * None.
613 *
614 *---------------------------------------------------------------------------
615 */
616
617 struct hostent *
618 TclpGetHostByAddr(
619 const char *addr,
620 int length,
621 int type)
622 {
623 #if !defined(TCL_THREADS) || defined(HAVE_MTSAFE_GETHOSTBYADDR)
624 return gethostbyaddr(addr, length, type);
625 #else
626 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
627
628 #if defined(HAVE_GETHOSTBYADDR_R_7)
629 int local_errno;
630
631 return gethostbyaddr_r(addr, length, type, &tsdPtr->hent, tsdPtr->hbuf,
632 sizeof(tsdPtr->hbuf), &local_errno);
633
634 #elif defined(HAVE_GETHOSTBYADDR_R_8)
635 struct hostent *hePtr;
636 int local_errno;
637
638 return (gethostbyaddr_r(addr, length, type, &tsdPtr->hent, tsdPtr->hbuf,
639 sizeof(tsdPtr->hbuf), &hePtr, &local_errno) == 0)
640 ? &tsdPtr->hent : NULL;
641 #else
642 #define NEED_COPYHOSTENT 1
643 struct hostent *hePtr;
644
645 Tcl_MutexLock(&compatLock);
646 hePtr = gethostbyaddr(addr, length, type);
647 if (hePtr != NULL) {
648 tsdPtr->hent = *hePtr;
649 hePtr = &tsdPtr->hent;
650 if (CopyHostent(&tsdPtr->hent, tsdPtr->hbuf,
651 sizeof(tsdPtr->hbuf)) == -1) {
652 hePtr = NULL;
653 }
654 }
655 Tcl_MutexUnlock(&compatLock);
656 return hePtr;
657 #endif
658
659 return NULL; /* Not reached. */
660 #endif /* TCL_THREADS */
661 }
662
663 /*
664 *---------------------------------------------------------------------------
665 *
666 * CopyGrp --
667 *
668 * Copies string fields of the group structure to the private buffer,
669 * honouring the size of the buffer.
670 *
671 * Results:
672 * 0 on success or -1 on error (errno = ERANGE).
673 *
674 * Side effects:
675 * None.
676 *
677 *---------------------------------------------------------------------------
678 */
679
680 #ifdef NEED_COPYGRP
681 #define NEED_COPYARRAY 1
682 #define NEED_COPYSTRING 1
683
684 static int
685 CopyGrp(
686 struct group *tgtPtr,
687 char *buf,
688 int buflen)
689 {
690 char *p = buf;
691 int copied, len = 0;
692
693 /*
694 * Copy username.
695 */
696
697 copied = CopyString(tgtPtr->gr_name, p, buflen - len);
698 if (copied == -1) {
699 goto range;
700 }
701 tgtPtr->gr_name = (copied > 0) ? p : NULL;
702 len += copied;
703 p = buf + len;
704
705 /*
706 * Copy password.
707 */
708
709 copied = CopyString(tgtPtr->gr_passwd, p, buflen - len);
710 if (copied == -1) {
711 goto range;
712 }
713 tgtPtr->gr_passwd = (copied > 0) ? p : NULL;
714 len += copied;
715 p = buf + len;
716
717 /*
718 * Copy group members.
719 */
720
721 PadBuffer(p, len, sizeof(char *));
722 copied = CopyArray((char **)tgtPtr->gr_mem, -1, p, buflen - len);
723 if (copied == -1) {
724 goto range;
725 }
726 tgtPtr->gr_mem = (copied > 0) ? (char **)p : NULL;
727
728 return 0;
729
730 range:
731 errno = ERANGE;
732 return -1;
733 }
734 #endif /* NEED_COPYGRP */
735
736 /*
737 *---------------------------------------------------------------------------
738 *
739 * CopyHostent --
740 *
741 * Copies string fields of the hostent structure to the private buffer,
742 * honouring the size of the buffer.
743 *
744 * Results:
745 * Number of bytes copied on success or -1 on error (errno = ERANGE)
746 *
747 * Side effects:
748 * None
749 *
750 *---------------------------------------------------------------------------
751 */
752
753 #ifdef NEED_COPYHOSTENT
754 #define NEED_COPYSTRING 1
755 #define NEED_COPYARRAY 1
756
757 static int
758 CopyHostent(
759 struct hostent *tgtPtr,
760 char *buf,
761 int buflen)
762 {
763 char *p = buf;
764 int copied, len = 0;
765
766 copied = CopyString(tgtPtr->h_name, p, buflen - len);
767 if (copied == -1) {
768 goto range;
769 }
770 tgtPtr->h_name = (copied > 0) ? p : NULL;
771 len += copied;
772 p = buf + len;
773
774 PadBuffer(p, len, sizeof(char *));
775 copied = CopyArray(tgtPtr->h_aliases, -1, p, buflen - len);
776 if (copied == -1) {
777 goto range;
778 }
779 tgtPtr->h_aliases = (copied > 0) ? (char **)p : NULL;
780 len += copied;
781 p += len;
782
783 PadBuffer(p, len, sizeof(char *));
784 copied = CopyArray(tgtPtr->h_addr_list, tgtPtr->h_length, p, buflen-len);
785 if (copied == -1) {
786 goto range;
787 }
788 tgtPtr->h_addr_list = (copied > 0) ? (char **)p : NULL;
789
790 return 0;
791
792 range:
793 errno = ERANGE;
794 return -1;
795 }
796 #endif /* NEED_COPYHOSTENT */
797
798 /*
799 *---------------------------------------------------------------------------
800 *
801 * CopyPwd --
802 *
803 * Copies string fields of the passwd structure to the private buffer,
804 * honouring the size of the buffer.
805 *
806 * Results:
807 * 0 on success or -1 on error (errno = ERANGE).
808 *
809 * Side effects:
810 * We are not copying the gecos field as it may not be supported on all
811 * platforms.
812 *
813 *---------------------------------------------------------------------------
814 */
815
816 #ifdef NEED_COPYPWD
817 #define NEED_COPYSTRING 1
818
819 static int
820 CopyPwd(
821 struct passwd *tgtPtr,
822 char *buf,
823 int buflen)
824 {
825 char *p = buf;
826 int copied, len = 0;
827
828 copied = CopyString(tgtPtr->pw_name, p, buflen - len);
829 if (copied == -1) {
830 range:
831 errno = ERANGE;
832 return -1;
833 }
834 tgtPtr->pw_name = (copied > 0) ? p : NULL;
835 len += copied;
836 p = buf + len;
837
838 copied = CopyString(tgtPtr->pw_passwd, p, buflen - len);
839 if (copied == -1) {
840 goto range;
841 }
842 tgtPtr->pw_passwd = (copied > 0) ? p : NULL;
843 len += copied;
844 p = buf + len;
845
846 copied = CopyString(tgtPtr->pw_dir, p, buflen - len);
847 if (copied == -1) {
848 goto range;
849 }
850 tgtPtr->pw_dir = (copied > 0) ? p : NULL;
851 len += copied;
852 p = buf + len;
853
854 copied = CopyString(tgtPtr->pw_shell, p, buflen - len);
855 if (copied == -1) {
856 goto range;
857 }
858 tgtPtr->pw_shell = (copied > 0) ? p : NULL;
859
860 return 0;
861 }
862 #endif /* NEED_COPYPWD */
863
864 /*
865 *---------------------------------------------------------------------------
866 *
867 * CopyArray --
868 *
869 * Copies array of NULL-terminated or fixed-length strings to the private
870 * buffer, honouring the size of the buffer.
871 *
872 * Results:
873 * Number of bytes copied on success or -1 on error (errno = ERANGE)
874 *
875 * Side effects:
876 * None.
877 *
878 *---------------------------------------------------------------------------
879 */
880
881 #ifdef NEED_COPYARRAY
882 static int
883 CopyArray(
884 char **src, /* Array of elements to copy. */
885 int elsize, /* Size of each element, or -1 to indicate
886 * that they are C strings of dynamic
887 * length. */
888 char *buf, /* Buffer to copy into. */
889 int buflen) /* Size of buffer. */
890 {
891 int i, j, len = 0;
892 char *p, **newBuffer;
893
894 if (src == NULL) {
895 return 0;
896 }
897
898 for (i = 0; src[i] != NULL; i++) {
899 /*
900 * Empty loop to count how many.
901 */
902 }
903 len = sizeof(char *) * (i + 1); /* Leave place for the array. */
904 if (len > buflen) {
905 return -1;
906 }
907
908 newBuffer = (char **)buf;
909 p = buf + len;
910
911 for (j = 0; j < i; j++) {
912 int sz = (elsize<0 ? (int) strlen(src[j]) + 1 : elsize);
913
914 len += sz;
915 if (len > buflen) {
916 return -1;
917 }
918 memcpy(p, src[j], sz);
919 newBuffer[j] = p;
920 p = buf + len;
921 }
922 newBuffer[j] = NULL;
923
924 return len;
925 }
926 #endif /* NEED_COPYARRAY */
927
928 /*
929 *---------------------------------------------------------------------------
930 *
931 * CopyString --
932 *
933 * Copies a NULL-terminated string to the private buffer, honouring the
934 * size of the buffer
935 *
936 * Results:
937 * 0 success or -1 on error (errno = ERANGE)
938 *
939 * Side effects:
940 * None
941 *
942 *---------------------------------------------------------------------------
943 */
944
945 #ifdef NEED_COPYSTRING
946 static int
947 CopyString(
948 const char *src, /* String to copy. */
949 char *buf, /* Buffer to copy into. */
950 int buflen) /* Size of buffer. */
951 {
952 int len = 0;
953
954 if (src != NULL) {
955 len = strlen(src) + 1;
956 if (len > buflen) {
957 return -1;
958 }
959 memcpy(buf, src, len);
960 }
961
962 return len;
963 }
964 #endif /* NEED_COPYSTRING */
965
966 /*
967 * Local Variables:
968 * mode: c
969 * c-basic-offset: 4
970 * fill-column: 78
971 * End:
972 */
973
974 /*
975 *------------------------------------------------------------------------
976 *
977 * TclWinCPUID --
978 *
979 * Get CPU ID information on an Intel box under UNIX (either Linux or Cygwin)
980 *
981 * Results:
982 * Returns TCL_OK if successful, TCL_ERROR if CPUID is not supported.
983 *
984 * Side effects:
985 * If successful, stores EAX, EBX, ECX and EDX registers after the CPUID
986 * instruction in the four integers designated by 'regsPtr'
987 *
988 *----------------------------------------------------------------------
989 */
990
991 int
992 TclWinCPUID(
993 unsigned int index, /* Which CPUID value to retrieve. */
994 unsigned int *regsPtr) /* Registers after the CPUID. */
995 {
996 int status = TCL_ERROR;
997
998 /* See: <http://en.wikipedia.org/wiki/CPUID> */
999 #if defined(HAVE_CPUID)
1000 #if defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
1001 __asm__ __volatile__("movq %%rbx, %%rsi \n\t" /* save %rbx */
1002 "cpuid \n\t"
1003 "xchgq %%rsi, %%rbx \n\t" /* restore the old %rbx */
1004 : "=a"(regsPtr[0]), "=S"(regsPtr[1]), "=c"(regsPtr[2]), "=d"(regsPtr[3])
1005 : "a"(index));
1006 status = TCL_OK;
1007 #elif defined(__i386__) || defined(_M_IX86)
1008 __asm__ __volatile__("mov %%ebx, %%esi \n\t" /* save %ebx */
1009 "cpuid \n\t"
1010 "xchg %%esi, %%ebx \n\t" /* restore the old %ebx */
1011 : "=a"(regsPtr[0]), "=S"(regsPtr[1]), "=c"(regsPtr[2]), "=d"(regsPtr[3])
1012 : "a"(index));
1013 status = TCL_OK;
1014 #endif
1015 #endif
1016 return status;
1017 }
1018
1019 /*
1020 * Local Variables:
1021 * mode: c
1022 * c-basic-offset: 4
1023 * fill-column: 78
1024 * End:
1025 */