cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob 25c1b3e5411736cd8bba3ef55a623d9ce3409929 3841 bytes (raw)
$ git show HEAD:test/trywrite-1.c	# shows this blob on the CLI

  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
 
/*
 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 */
#include "check.h"
#include "iov_str.h"

static void writev_simple(void)
{
	struct iovec iov[1];
	int fds[2];

	pipe_or_die(fds);

	IOV_STR(iov, "HELLO");
	{
		void *x = mog_trywritev(fds[1], iov, 1);

		assert(x == NULL && "couldn't write 5 bytes to pipe");
	}

	close_pipe(fds);
}

static void write_wrong(void)
{
	struct iovec iov[1];
	int fds[2];

	pipe_or_die(fds);

	IOV_STR(iov, "HELLO");
	{
		int null = open("/dev/null", O_RDONLY);
		void *x;

		assert(null >= 0 && "invalid FD opening /dev/nulL");
		x = mog_trywritev(null, iov, 1);
		assert(x == MOG_WR_ERROR && "did not return error");
		close(null);
	}

	if (0) { /* FIXME: this makes valgrind unhappy */
		void *x = mog_trysend(fds[0], (void *)1, 3, 0);

		assert(x == MOG_WR_ERROR && "bad addr did not return error");
	}

	close_pipe(fds);

	{
		void *x = mog_trywritev(fds[1], iov, 1);

		assert(x == MOG_WR_ERROR && "did not return error");
	}

	{
		char buf[3] = { 0 };
		void *x = mog_trysend(fds[1], buf, 3, 0);

		assert(x == MOG_WR_ERROR && "bad FD did not return error");
	}
}

static void writev_buffer(void)
{
	struct iovec iov[1];
	int fds[2];
	ssize_t total = 0;
	void *x;
	struct mog_wbuf *wbuf = NULL;
	int nread;
	enum mog_write_state state;

	pipe_or_die(fds);

	IOV_STR(iov, "HELLO");

	mog_set_nonblocking(fds[1], true);

	for (;;) {
		x = mog_trywritev(fds[1], iov, 1);

		if (x == NULL) {
			total += iov[0].iov_len = 5;
		} else if (x == MOG_WR_ERROR) {
			assert(0 && "unexpected MOG_WR_ERROR");
		} else {
			assert(x && "wtf");
			wbuf = x;
			break;
		}
	}

	assert(wbuf && "wbuf not initialized, how did we break from loop?");

	/* pipe is blocked */
	{
		ioctl(fds[0], FIONREAD, &nread);
		assert(nread == total && "nread != total");
	}

	/* pipe should be busy */
	{
		state = mog_tryflush(fds[1], &wbuf);
		assert(MOG_WRSTATE_BUSY == state && "not busy?");
		assert(wbuf && "wbuf got nulled");
	}

	/* drain some */
	{
		char * tmp = xmalloc(nread);
		ssize_t r = read(fds[0], tmp, nread);

		assert(r == nread && "couldn't drain all");

		free(tmp);
	}

	/* flush (succeed) */
	{
		state = mog_tryflush(fds[1], &wbuf);
		assert(MOG_WRSTATE_DONE == state && "didn't finish");
		assert(wbuf == NULL && "didn't null");
	}

	close_pipe(fds);
}

static void trysend_buffer(void)
{
	int fds[2];
	ssize_t total = 0;
	void *x;
	struct mog_wbuf *wbuf = NULL;
	int nread;
	char buf[5] = { 0 };
	size_t len = sizeof(buf);

	socketpair_or_die(fds);
	mog_set_nonblocking(fds[1], true);

	for (;;) {
		x = mog_trysend(fds[1], buf, len, 0);

		if (x == NULL) {
			total += len;
		} else if (x == MOG_WR_ERROR) {
			assert(0 && "unexpected MOG_WR_ERROR");
		} else {
			assert(x && "wtf");
			wbuf = x;
			break;
		}
	}

	assert(wbuf && "wbuf not initialized, how did we break from loop?");
	free(wbuf); /* for valgrind, we should never actually do this */

	/* socket is/was blocked, it should be readable */
	{
		ioctl(fds[0], FIONREAD, &nread);
		assert(nread > 0 && "nread non-zero");
	}

	/* ignore errors when closing sockets, BSDs can return weird values */
	close(fds[1]);
	close(fds[0]);
}

static void writev_big(size_t len)
{
	struct iovec iov[3];
	void *ptrv[3];
	int fds[2];
	void *x;
	int i;

	pipe_or_die(fds);

	for (i = 0; i < 3; i++) {
		ptrv[i] = iov[i].iov_base = xmalloc(len);
		iov[i].iov_len = len;
		memset(ptrv[i], 'a' + i, len);
	}

	mog_set_nonblocking(fds[1], true);
	x = mog_trywritev(fds[1], iov, 3);
	assert(x != MOG_WR_ERROR && x != NULL && "did not buffer on busy");
	free(x);

	close_pipe(fds);

	for (i = 0; i < 3; i++)
		free(ptrv[i]);
}

int main(void)
{
	writev_simple();
	write_wrong();
	writev_buffer();
	writev_big(166331);
	writev_big(65536);
	trysend_buffer();

	return 0;
}

git clone https://yhbt.net/cmogstored.git