nsswitch: add test for parallel NSS & libwbclient calls
[samba.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         pthread_mutex_init(&state.lock, NULL);
122         state.fail = false;
123         state.nss_loop_count = 0;
124         state.wbc_loop_count = 0;
125
126         printf("query domain user '%s'\n", state.username);
127
128         /* create query threads */
129         rc = pthread_create(&threads[0], NULL, query_nss_thread, &state);
130         if (rc != 0) {
131                 fprintf(stderr,
132                         "creating NSS thread failed: %s\n",
133                         strerror(rc));
134                 exit(1);
135         }
136         rc = pthread_create(&threads[1], NULL, query_wbc_thread, &state);
137         if (rc != 0) {
138                 fprintf(stderr,
139                         "creating libwbclient thread failed: %s\n",
140                         strerror(rc));
141                 exit(1);
142         }
143
144         /* wait for query threads to terminate */
145         for (n = 0; n < 2; n++) {
146                 pthread_join(threads[n], NULL);
147         }
148
149         fprintf(state.fail ? stderr: stdout,
150                 "test %s with %i NSS and %i libwbclient calls\n",
151                 state.fail ? "failed" : "passed",
152                 state.nss_loop_count,
153                 state.wbc_loop_count);
154
155         return state.fail;
156 }