ctdb-build: Separate test backtrace support into separate subsystem
[Samba.git] / nsswitch / stress-nss-libwbclient.c
blobdf1d85c9c40239369965206504637e1f68da2a4a
1 /*
2 Unix SMB/CIFS implementation.
4 Stress test for parallel NSS & libwbclient calls.
6 Copyright (C) Ralph Wuerthner 2018
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <stdint.h>
24 #include <stdbool.h>
25 #include <pthread.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <time.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <pwd.h>
32 #include <wbclient.h>
34 #define RUNTIME 10
36 struct thread_state {
37 const char *username;
38 time_t timeout;
39 pthread_mutex_t lock;
40 bool fail;
41 int nss_loop_count;
42 int wbc_loop_count;
45 static void *query_nss_thread(void *ptr)
47 struct thread_state *state = ptr;
48 char buf[1024];
49 int rc;
50 struct passwd pwd, *result;
52 while (time(NULL) < state->timeout) {
53 rc = getpwnam_r(state->username,
54 &pwd,
55 buf,
56 sizeof(buf),
57 &result);
58 if (rc != 0 || result == NULL) {
59 pthread_mutex_lock(&state->lock);
60 state->fail = true;
61 pthread_mutex_unlock(&state->lock);
62 fprintf(stderr,
63 "getpwnam_r failed with rc='%s' result=%p\n",
64 strerror(rc),
65 result);
66 break;
68 state->nss_loop_count++;
69 pthread_mutex_lock(&state->lock);
70 if (state->fail) {
71 pthread_mutex_unlock(&state->lock);
72 break;
74 pthread_mutex_unlock(&state->lock);
76 return NULL;
79 static void *query_wbc_thread(void *ptr)
81 struct thread_state *state = ptr;
82 struct passwd *ppwd;
83 wbcErr wbc_status;
85 while (time(NULL) < state->timeout) {
86 wbc_status = wbcGetpwnam(state->username, &ppwd);
87 if (!WBC_ERROR_IS_OK(wbc_status)) {
88 pthread_mutex_lock(&state->lock);
89 state->fail = true;
90 pthread_mutex_unlock(&state->lock);
91 fprintf(stderr,
92 "wbcGetpwnam failed with %s\n",
93 wbcErrorString(wbc_status));
94 break;
96 wbcFreeMemory(ppwd);
97 state->wbc_loop_count++;
98 pthread_mutex_lock(&state->lock);
99 if (state->fail) {
100 pthread_mutex_unlock(&state->lock);
101 break;
103 pthread_mutex_unlock(&state->lock);
105 return NULL;
108 int main(int argc, char *argv[])
110 int rc, n;
111 struct thread_state state;
112 pthread_t threads[2];
114 if (argc < 2 ) {
115 fprintf(stderr,"%s: missing domain user\n", argv[0]);
116 return 1;
119 state.username = argv[1];
120 state.timeout = time(NULL) + RUNTIME;
121 rc = pthread_mutex_init(&state.lock, NULL);
122 if (rc != 0) {
123 fprintf(stderr,
124 "pthread_mutex_init failed: %s\n",
125 strerror(rc));
126 exit(1);
128 state.fail = false;
129 state.nss_loop_count = 0;
130 state.wbc_loop_count = 0;
132 printf("query domain user '%s'\n", state.username);
134 /* create query threads */
135 rc = pthread_create(&threads[0], NULL, query_nss_thread, &state);
136 if (rc != 0) {
137 fprintf(stderr,
138 "creating NSS thread failed: %s\n",
139 strerror(rc));
140 exit(1);
142 rc = pthread_create(&threads[1], NULL, query_wbc_thread, &state);
143 if (rc != 0) {
144 fprintf(stderr,
145 "creating libwbclient thread failed: %s\n",
146 strerror(rc));
147 exit(1);
150 /* wait for query threads to terminate */
151 for (n = 0; n < 2; n++) {
152 rc = pthread_join(threads[n], NULL);
153 if (rc != 0) {
154 fprintf(stderr,
155 "joining query thread %i failed: %s\n",
157 strerror(rc));
158 exit(1);
162 fprintf(state.fail ? stderr: stdout,
163 "test %s with %i NSS and %i libwbclient calls\n",
164 state.fail ? "failed" : "passed",
165 state.nss_loop_count,
166 state.wbc_loop_count);
168 return state.fail;