|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * test_thread_funcs.c |
| 4 | + * libc thread test program |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 8 | + * |
| 9 | + * $Header: /cvsroot/pgsql/src/tools/Attic/test_thread_funcs.c,v 1.1 2003/09/03 19:30:31 momjian Exp $ |
| 10 | + * |
| 11 | + * This program tests to see if your standard libc functions use |
| 12 | + * pthread_setspecific()/pthread_getspecific() to be thread-safe. |
| 13 | + * See src/port/thread.c for more details. |
| 14 | + * |
| 15 | + * This program first tests to see if each function returns a constant |
| 16 | + * memory pointer within the same thread, then, assuming it does, tests |
| 17 | + * to see if the pointers are different for different threads. If they |
| 18 | + * are, the function is thread-safe. |
| 19 | + * |
| 20 | + * This program must be compiled with the thread flags required by your |
| 21 | + * operating system. See src/template for the appropriate flags, if any. |
| 22 | + * |
| 23 | + *------------------------------------------------------------------------- |
| 24 | + */ |
| 25 | + |
| 26 | + |
| 27 | +#include <pthread.h> |
| 28 | +#include <unistd.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <stdlib.h> |
| 31 | +#include <netdb.h> |
| 32 | +#include <sys/types.h> |
| 33 | +#include <pwd.h> |
| 34 | +#include <string.h> |
| 35 | +#include <errno.h> |
| 36 | + |
| 37 | +void func_call_1(void); |
| 38 | +void func_call_2(void); |
| 39 | + |
| 40 | +struct hostent *hostent_p1; |
| 41 | +struct hostent *hostent_p2; |
| 42 | + |
| 43 | +struct passwd *passwd_p1; |
| 44 | +struct passwd *passwd_p2; |
| 45 | + |
| 46 | +char *strerror_p1; |
| 47 | +char *strerror_p2; |
| 48 | + |
| 49 | +int main(int argc, char *argv[]) |
| 50 | +{ |
| 51 | + pthread_t thread1, |
| 52 | + thread2; |
| 53 | + |
| 54 | + if (argc > 1) |
| 55 | + { |
| 56 | + fprintf(stderr, "Usage: %s\n", argv[0]); |
| 57 | + return 1; |
| 58 | + } |
| 59 | + |
| 60 | + pthread_create(&thread1, NULL, (void *) func_call_1, NULL); |
| 61 | + pthread_create(&thread2, NULL, (void *) func_call_2, NULL); |
| 62 | + pthread_join(thread1, NULL); |
| 63 | + pthread_join(thread2, NULL); |
| 64 | + |
| 65 | + if (hostent_p1 != hostent_p2 && |
| 66 | + passwd_p1 != passwd_p2 && |
| 67 | + strerror_p1 != strerror_p2) |
| 68 | + printf("Your functions are all thread-safe\n"); |
| 69 | + else |
| 70 | + printf("Your functions are _not_ all thread-safe\n"); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +void func_call_1(void) { |
| 76 | + void *p; |
| 77 | + |
| 78 | + hostent_p1 = gethostbyname("yahoo.com"); |
| 79 | + p = gethostbyname("slashdot.org"); |
| 80 | + if (hostent_p1 != p) |
| 81 | + { |
| 82 | + printf("Your gethostbyname() changes the static memory area between calls\n"); |
| 83 | + hostent_p1 = NULL; /* force thread-safe failure report */ |
| 84 | + } |
| 85 | + |
| 86 | + passwd_p1 = getpwuid(0); |
| 87 | + p = getpwuid(1); |
| 88 | + if (passwd_p1 != p) |
| 89 | + { |
| 90 | + printf("Your getpwuid() changes the static memory area between calls\n"); |
| 91 | + passwd_p1 = NULL; /* force thread-safe failure report */ |
| 92 | + } |
| 93 | + |
| 94 | + strerror_p1 = strerror(EACCES); |
| 95 | + /* |
| 96 | + * If strerror() uses sys_errlist, the pointer might change for different |
| 97 | + * errno values, so we don't check to see if it varies within the thread. |
| 98 | + */ |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void func_call_2(void) { |
| 103 | + void *p; |
| 104 | + |
| 105 | + hostent_p2 = gethostbyname("google.com"); |
| 106 | + p = gethostbyname("postgresql.org"); |
| 107 | + if (hostent_p2 != p) |
| 108 | + { |
| 109 | + printf("Your gethostbyname() changes the static memory area between calls\n"); |
| 110 | + hostent_p2 = NULL; /* force thread-safe failure report */ |
| 111 | + } |
| 112 | + |
| 113 | + passwd_p2 = getpwuid(2); |
| 114 | + p = getpwuid(3); |
| 115 | + if (passwd_p2 != p) |
| 116 | + { |
| 117 | + printf("Your getpwuid() changes the static memory area between calls\n"); |
| 118 | + passwd_p2 = NULL; /* force thread-safe failure report */ |
| 119 | + } |
| 120 | + |
| 121 | + strerror_p2 = strerror(EINVAL); |
| 122 | + /* |
| 123 | + * If strerror() uses sys_errlist, the pointer might change for different |
| 124 | + * errno values, so we don't check to see if it varies within the thread. |
| 125 | + */ |
| 126 | +} |
0 commit comments