lmdb: iterate_range implementation
[sfrench/samba-autobuild/.git] / nsswitch / stress-nss-libwbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Stress test for parallel NSS & libwbclient calls.
5
6    Copyright (C) Ralph Wuerthner 2018
7
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.
12
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.
17
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/>.
20 */
21
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>
33
34 #define RUNTIME 10
35
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;
43 };
44
45 static void *query_nss_thread(void *ptr)
46 {
47         struct thread_state *state = ptr;
48         char buf[1024];
49         int rc;
50         struct passwd pwd, *result;
51
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;
67                 }
68                 state->nss_loop_count++;
69                 pthread_mutex_lock(&state->lock);
70                 if (state->fail) {
71                         pthread_mutex_unlock(&state->lock);
72                         break;
73                 }
74                 pthread_mutex_unlock(&state->lock);
75         }
76         return NULL;
77 }
78
79 static void *query_wbc_thread(void *ptr)
80 {
81         struct thread_state *state = ptr;
82         struct passwd *ppwd;
83         wbcErr wbc_status;
84
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;
95                 }
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;
102                 }
103                 pthread_mutex_unlock(&state->lock);
104         }
105         return NULL;
106 }
107
108 int main(int argc, char *argv[])
109 {
110         int rc, n;
111         struct thread_state state;
112         pthread_t threads[2];
113
114         if (argc < 2 ) {
115                 fprintf(stderr,"%s: missing domain user\n", argv[0]);
116                 return 1;
117         }
118
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);
127         }
128         state.fail = false;
129         state.nss_loop_count = 0;
130         state.wbc_loop_count = 0;
131
132         printf("query domain user '%s'\n", state.username);
133
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);
141         }
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);
148         }
149
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",
156                                 n,
157                                 strerror(rc));
158                         exit(1);
159                 }
160         }
161
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);
167
168         return state.fail;
169 }