summaryrefslogtreecommitdiffstats
path: root/nsswitch/stress-nss-libwbclient.c
blob: d9dc3b53869ed2cfa8a8554093ea471c380cd005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
   Unix SMB/CIFS implementation.

   Stress test for parallel NSS & libwbclient calls.

   Copyright (C) Ralph Wuerthner 2018

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pwd.h>
#include <wbclient.h>
#include <sys/socket.h>
#include <errno.h>
#include <assert.h>

#define RUNTIME 10

struct thread_state {
	const char *username;
	time_t timeout;
	pthread_mutex_t lock;
	bool fail;
	int nss_loop_count;
	int wbc_loop_count;
};

static void *query_nss_thread(void *ptr)
{
	struct thread_state *state = ptr;
	char buf[1024];
	ssize_t nread, nwritten;
	int p[2];
	int rc;
	struct passwd pwd, *result;
	pid_t pid;

	while (time(NULL) < state->timeout) {
		rc = getpwnam_r(state->username,
				&pwd,
				buf,
				sizeof(buf),
				&result);
		if (rc != 0 || result == NULL) {
			pthread_mutex_lock(&state->lock);
			state->fail = true;
			pthread_mutex_unlock(&state->lock);
			fprintf(stderr,
				"getpwnam_r failed with rc='%s' result=%p\n",
				strerror(rc),
				result);
			break;
		}
		state->nss_loop_count++;
		pthread_mutex_lock(&state->lock);
		if (state->fail) {
			pthread_mutex_unlock(&state->lock);
			break;
		}
		pthread_mutex_unlock(&state->lock);
	}

	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, p);
	if (rc != 0) {
		state->fail = true;
		return NULL;
	}

	/*
	 * Check getpwnam_r() still works after a fork,
	 * both in parent and child.
	 */

	pid = fork();
	if (pid == -1) {
		return NULL;
	}
	if (pid == 0) {
		/* Child */
		rc = getpwnam_r(state->username,
				&pwd,
				buf,
				sizeof(buf),
				&result);
		if (rc != 0 || result == NULL) {
			fprintf(stderr,
				"getpwnam_r failed with rc='%s' result=%p\n",
				strerror(rc),
				result);
			rc = 1;
			nwritten = write(p[0], &rc, sizeof(int));
			assert(nwritten == sizeof(int));
			exit(1);
		}
		printf("child: getpwnam_r in child succeeded\n");
		rc = 0;
		nwritten = write(p[0], &rc, sizeof(int));
		assert(nwritten == sizeof(int));
		exit(1);
	}

	/* Parent */

	/* Check result from child */
	nread = read(p[1], &rc, sizeof(int));
	if (nread != sizeof(int)) {
		fprintf(stderr,
			"read from child failed with errno='%s' nread=%zd\n",
			strerror(errno),
			nread);
		state->fail = true;
		return NULL;
	}

	if (rc != 0) {
		fprintf(stderr,
			"getpwnam_r failed in the child\n");
		state->fail = true;
		return NULL;
	}
	printf("parent: getpwnam_r in child succeeded\n");

	/* Verify getpwnam_r() in parent after fork */
	rc = getpwnam_r(state->username,
			&pwd,
			buf,
			sizeof(buf),
			&result);
	if (rc != 0 || result == NULL) {
		fprintf(stderr,
			"getpwnam_r failed with rc='%s' result=%p\n",
			strerror(rc),
			result);
		state->fail = true;
		return NULL;
	}
	printf("parent: getpwnam_r in parent succeeded\n");
	return NULL;
}

static void *query_wbc_thread(void *ptr)
{
	struct thread_state *state = ptr;
	struct passwd *ppwd;
	wbcErr wbc_status;
	pid_t pid;
	ssize_t nread, nwritten;
	int p[2];
	int rc;

	while (time(NULL) < state->timeout) {
		wbc_status = wbcGetpwnam(state->username, &ppwd);
		if (!WBC_ERROR_IS_OK(wbc_status)) {
			pthread_mutex_lock(&state->lock);
			state->fail = true;
			pthread_mutex_unlock(&state->lock);
			fprintf(stderr,
				"wbcGetpwnam failed with %s\n",
				wbcErrorString(wbc_status));
			break;
		}
		wbcFreeMemory(ppwd);
		state->wbc_loop_count++;
		pthread_mutex_lock(&state->lock);
		if (state->fail) {
			pthread_mutex_unlock(&state->lock);
			break;
		}
		pthread_mutex_unlock(&state->lock);
	}

	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, p);
	if (rc != 0) {
		state->fail = true;
		return NULL;
	}

	/*
	 * Check wbcGetpwnam() still works after a fork,
	 * both in parent and child.
	 */

	pid = fork();
	if (pid == -1) {
		return NULL;
	}
	if (pid == 0) {
		/* Child */
		wbc_status = wbcGetpwnam(state->username, &ppwd);
		if (!WBC_ERROR_IS_OK(wbc_status)) {
			fprintf(stderr,
				"wbcGetpwnam failed with %s\n",
				wbcErrorString(wbc_status));
			rc = 1;
			nwritten = write(p[0], &rc, sizeof(int));
			assert(nwritten == sizeof(int));
			exit(1);
		}
		wbcFreeMemory(ppwd);
		printf("child: wbcGetpwnam in child succeeded\n");
		rc = 0;
		nwritten = write(p[0], &rc, sizeof(int));
		assert(nwritten == sizeof(int));
		exit(1);
	}

	/* Parent */

	/* Check result from child */
	nread = read(p[1], &rc, sizeof(int));
	if (nread != sizeof(int)) {
		fprintf(stderr,
			"read from child failed with errno='%s' nread=%zd\n",
			strerror(errno),
			nread);
		state->fail = true;
		return NULL;
	}

	if (rc != 0) {
		fprintf(stderr,
			"wbcGetpwnam failed in the child\n");
		state->fail = true;
		return NULL;
	}
	printf("parent: wbcGetpwnam in child succeeded\n");

	/* Verify wbcGetpwnam() in parent after fork */
	wbc_status = wbcGetpwnam(state->username, &ppwd);
	if (!WBC_ERROR_IS_OK(wbc_status)) {
		fprintf(stderr,
			"wbcGetpwnam failed with %s\n",
			wbcErrorString(wbc_status));
		state->fail = true;
		return NULL;
	}
	wbcFreeMemory(ppwd);
	printf("parent: wbcGetpwnam in parent succeeded\n");
	return NULL;
}

int main(int argc, char *argv[])
{
	int rc, n;
	struct thread_state state;
	pthread_t threads[2];

	if (argc < 2 ) {
		fprintf(stderr,"%s: missing domain user\n", argv[0]);
		return 1;
	}

	state.username = argv[1];
	state.timeout = time(NULL) + RUNTIME;
	rc = pthread_mutex_init(&state.lock, NULL);
	if (rc != 0) {
		fprintf(stderr,
			"pthread_mutex_init failed: %s\n",
			strerror(rc));
		exit(1);
	}
	state.fail = false;
	state.nss_loop_count = 0;
	state.wbc_loop_count = 0;

	printf("query domain user '%s'\n", state.username);

	/* create query threads */
	rc = pthread_create(&threads[0], NULL, query_nss_thread, &state);
	if (rc != 0) {
		fprintf(stderr,
			"creating NSS thread failed: %s\n",
			strerror(rc));
		exit(1);
	}
	rc = pthread_create(&threads[1], NULL, query_wbc_thread, &state);
	if (rc != 0) {
		fprintf(stderr,
			"creating libwbclient thread failed: %s\n",
			strerror(rc));
		exit(1);
	}

	/* wait for query threads to terminate */
	for (n = 0; n < 2; n++) {
		rc = pthread_join(threads[n], NULL);
		if (rc != 0) {
			fprintf(stderr,
				"joining query thread %i failed: %s\n",
				n,
				strerror(rc));
			exit(1);
		}
	}

	fprintf(state.fail ? stderr: stdout,
		"test %s with %i NSS and %i libwbclient calls\n",
		state.fail ? "failed" : "passed",
		state.nss_loop_count,
		state.wbc_loop_count);

	return state.fail;
}