Annotation of src/usr.bin/rsync/io.c, Revision 1.12
1.12 ! benno 1: /* $Id: io.c,v 1.11 2019/02/18 21:55:27 benno Exp $ */
1.1 benno 2: /*
3: * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/stat.h>
18:
19: #include <assert.h>
20: #include <endian.h>
21: #include <errno.h>
22: #include <poll.h>
23: #include <stdint.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #include "extern.h"
1.7 florian 30:
31: /*
32: * A non-blocking check to see whether there's POLLIN data in fd.
33: * Returns <0 on failure, 0 if there's no data, >0 if there is.
34: */
1.1 benno 35: int
36: io_read_check(struct sess *sess, int fd)
37: {
38: struct pollfd pfd;
39:
40: pfd.fd = fd;
41: pfd.events = POLLIN;
42:
43: if (poll(&pfd, 1, 0) < 0) {
44: ERR(sess, "poll");
45: return -1;
46: }
1.7 florian 47: return (pfd.revents & POLLIN);
1.1 benno 48: }
49:
50: /*
51: * Write buffer to non-blocking descriptor.
52: * Returns zero on failure, non-zero on success (zero or more bytes).
1.7 florian 53: * On success, fills in "sz" with the amount written.
1.1 benno 54: */
55: static int
1.5 deraadt 56: io_write_nonblocking(struct sess *sess, int fd, const void *buf, size_t bsz,
57: size_t *sz)
1.1 benno 58: {
59: struct pollfd pfd;
60: ssize_t wsz;
1.6 florian 61: int c;
1.1 benno 62:
63: *sz = 0;
64:
1.4 deraadt 65: if (bsz == 0)
1.1 benno 66: return 1;
67:
68: pfd.fd = fd;
69: pfd.events = POLLOUT;
70:
1.7 florian 71: /* Poll and check for all possible errors. */
72:
1.6 florian 73: if ((c = poll(&pfd, 1, POLL_TIMEOUT)) == -1) {
1.1 benno 74: ERR(sess, "poll");
75: return 0;
1.6 florian 76: } else if (c == 0) {
77: ERRX(sess, "poll: timeout");
78: return 0;
1.7 florian 79: } else if ((pfd.revents & (POLLERR|POLLNVAL))) {
1.1 benno 80: ERRX(sess, "poll: bad fd");
81: return 0;
82: } else if ((pfd.revents & POLLHUP)) {
83: ERRX(sess, "poll: hangup");
84: return 0;
1.3 deraadt 85: } else if (!(pfd.revents & POLLOUT)) {
1.1 benno 86: ERRX(sess, "poll: unknown event");
87: return 0;
88: }
89:
1.7 florian 90: /* Now the non-blocking write. */
91:
1.1 benno 92: if ((wsz = write(fd, buf, bsz)) < 0) {
93: ERR(sess, "write");
94: return 0;
95: }
96:
97: *sz = wsz;
98: return 1;
99: }
100:
101: /*
102: * Blocking write of the full size of the buffer.
103: * Returns 0 on failure, non-zero on success (all bytes written).
104: */
105: static int
1.5 deraadt 106: io_write_blocking(struct sess *sess, int fd, const void *buf, size_t sz)
1.1 benno 107: {
108: size_t wsz;
109: int c;
110:
111: while (sz > 0) {
112: c = io_write_nonblocking(sess, fd, buf, sz, &wsz);
1.3 deraadt 113: if (!c) {
1.1 benno 114: ERRX1(sess, "io_write_nonblocking");
115: return 0;
1.4 deraadt 116: } else if (wsz == 0) {
1.1 benno 117: ERRX(sess, "io_write_nonblocking: short write");
118: return 0;
119: }
120: buf += wsz;
121: sz -= wsz;
122: }
123:
124: return 1;
125: }
126:
127: /*
128: * Write "buf" of size "sz" to non-blocking descriptor.
129: * Returns zero on failure, non-zero on success (all bytes written to
130: * the descriptor).
131: */
132: int
133: io_write_buf(struct sess *sess, int fd, const void *buf, size_t sz)
134: {
135: int32_t tag, tagbuf;
136: size_t wsz;
137: int c;
138:
1.3 deraadt 139: if (!sess->mplex_writes) {
1.1 benno 140: c = io_write_blocking(sess, fd, buf, sz);
141: sess->total_write += sz;
142: return c;
143: }
144:
145: while (sz > 0) {
146: wsz = sz & 0xFFFFFF;
147: tag = (7 << 24) + wsz;
148: tagbuf = htole32(tag);
1.3 deraadt 149: if (!io_write_blocking(sess, fd, &tagbuf, sizeof(tagbuf))) {
1.1 benno 150: ERRX1(sess, "io_write_blocking");
151: return 0;
152: }
1.3 deraadt 153: if (!io_write_blocking(sess, fd, buf, wsz)) {
1.1 benno 154: ERRX1(sess, "io_write_blocking");
155: return 0;
156: }
157: sess->total_write += wsz;
158: sz -= wsz;
159: buf += wsz;
160: }
161:
162: return 1;
163: }
164:
165: /*
166: * Write "line" (NUL-terminated) followed by a newline.
167: * Returns zero on failure, non-zero on succcess.
168: */
169: int
170: io_write_line(struct sess *sess, int fd, const char *line)
171: {
172:
1.3 deraadt 173: if (!io_write_buf(sess, fd, line, strlen(line)))
1.1 benno 174: ERRX1(sess, "io_write_buf");
1.3 deraadt 175: else if (!io_write_byte(sess, fd, '\n'))
1.1 benno 176: ERRX1(sess, "io_write_byte");
177: else
178: return 1;
179:
180: return 0;
181: }
182:
183: /*
184: * Read buffer from non-blocking descriptor.
185: * Returns zero on failure, non-zero on success (zero or more bytes).
186: */
187: static int
188: io_read_nonblocking(struct sess *sess,
189: int fd, void *buf, size_t bsz, size_t *sz)
190: {
191: struct pollfd pfd;
192: ssize_t rsz;
1.6 florian 193: int c;
1.1 benno 194:
195: *sz = 0;
196:
1.4 deraadt 197: if (bsz == 0)
1.1 benno 198: return 1;
199:
200: pfd.fd = fd;
201: pfd.events = POLLIN;
202:
1.7 florian 203: /* Poll and check for all possible errors. */
204:
1.6 florian 205: if ((c = poll(&pfd, 1, POLL_TIMEOUT)) == -1) {
1.1 benno 206: ERR(sess, "poll");
207: return 0;
1.6 florian 208: } else if (c == 0) {
209: ERRX(sess, "poll: timeout");
210: return 0;
1.7 florian 211: } else if ((pfd.revents & (POLLERR|POLLNVAL))) {
1.1 benno 212: ERRX(sess, "poll: bad fd");
213: return 0;
1.3 deraadt 214: } else if (!(pfd.revents & (POLLIN|POLLHUP))) {
1.1 benno 215: ERRX(sess, "poll: unknown event");
216: return 0;
217: }
218:
1.7 florian 219: /* Now the non-blocking read, checking for EOF. */
220:
1.1 benno 221: if ((rsz = read(fd, buf, bsz)) < 0) {
222: ERR(sess, "read");
223: return 0;
1.4 deraadt 224: } else if (rsz == 0) {
1.1 benno 225: ERRX(sess, "unexpected end of file");
226: return 0;
227: }
228:
229: *sz = rsz;
230: return 1;
231: }
232:
233: /*
234: * Blocking read of the full size of the buffer.
235: * This can be called from either the error type message or a regular
236: * message---or for that matter, multiplexed or not.
237: * Returns 0 on failure, non-zero on success (all bytes read).
238: */
239: static int
240: io_read_blocking(struct sess *sess,
241: int fd, void *buf, size_t sz)
242: {
243: size_t rsz;
244: int c;
245:
246: while (sz > 0) {
247: c = io_read_nonblocking(sess, fd, buf, sz, &rsz);
1.3 deraadt 248: if (!c) {
1.1 benno 249: ERRX1(sess, "io_read_nonblocking");
250: return 0;
1.4 deraadt 251: } else if (rsz == 0) {
1.1 benno 252: ERRX(sess, "io_read_nonblocking: short read");
253: return 0;
254: }
255: buf += rsz;
256: sz -= rsz;
257: }
258:
259: return 1;
260: }
261:
262: /*
263: * When we do a lot of writes in a row (such as when the sender emits
264: * the file list), the server might be sending us multiplexed log
265: * messages.
266: * If it sends too many, it clogs the socket.
267: * This function looks into the read buffer and clears out any log
268: * messages pending.
269: * If called when there are valid data reads available, this function
270: * does nothing.
271: * Returns zero on failure, non-zero on success.
272: */
273: int
274: io_read_flush(struct sess *sess, int fd)
275: {
276: int32_t tagbuf, tag;
277: char mpbuf[1024];
278:
279: if (sess->mplex_read_remain)
280: return 1;
281:
282: /*
283: * First, read the 4-byte multiplex tag.
284: * The first byte is the tag identifier (7 for normal
285: * data, !7 for out-of-band data), the last three are
286: * for the remaining data size.
287: */
288:
1.3 deraadt 289: if (!io_read_blocking(sess, fd, &tagbuf, sizeof(tagbuf))) {
1.1 benno 290: ERRX1(sess, "io_read_blocking");
291: return 0;
292: }
293: tag = le32toh(tagbuf);
294: sess->mplex_read_remain = tag & 0xFFFFFF;
295: tag >>= 24;
1.4 deraadt 296: if (tag == 7)
1.1 benno 297: return 1;
298:
299: tag -= 7;
300:
301: if (sess->mplex_read_remain > sizeof(mpbuf)) {
302: ERRX(sess, "multiplex buffer overflow");
303: return 0;
1.4 deraadt 304: } else if (sess->mplex_read_remain == 0)
1.1 benno 305: return 1;
306:
1.12 ! benno 307: if (!io_read_blocking(sess, fd, mpbuf, sess->mplex_read_remain)) {
1.1 benno 308: ERRX1(sess, "io_read_blocking");
309: return 0;
310: }
1.4 deraadt 311: if (mpbuf[sess->mplex_read_remain - 1] == '\n')
1.1 benno 312: mpbuf[--sess->mplex_read_remain] = '\0';
313:
314: /*
315: * Always print the server's messages, as the server
316: * will control its own log levelling.
317: */
318:
319: LOG0(sess, "%.*s", (int)sess->mplex_read_remain, mpbuf);
320: sess->mplex_read_remain = 0;
321:
1.2 benno 322: /*
1.1 benno 323: * I only know that a tag of one means an error.
324: * This means that we should exit.
325: */
326:
1.4 deraadt 327: if (tag == 1) {
1.1 benno 328: ERRX1(sess, "error from remote host");
329: return 0;
330: }
331: return 1;
332: }
333:
334: /*
335: * Read buffer from non-blocking descriptor, possibly in multiplex read
336: * mode.
337: * Returns zero on failure, non-zero on success (all bytes read from
338: * the descriptor).
339: */
340: int
341: io_read_buf(struct sess *sess, int fd, void *buf, size_t sz)
342: {
343: size_t rsz;
344: int c;
345:
346: /* If we're not multiplexing, read directly. */
347:
1.3 deraadt 348: if (!sess->mplex_reads) {
1.4 deraadt 349: assert(sess->mplex_read_remain == 0);
1.1 benno 350: c = io_read_blocking(sess, fd, buf, sz);
351: sess->total_read += sz;
352: return c;
353: }
354:
355: while (sz > 0) {
356: /*
357: * First, check to see if we have any regular data
358: * hanging around waiting to be read.
359: * If so, read the lesser of that data and whatever
360: * amount we currently want.
361: */
362:
363: if (sess->mplex_read_remain) {
364: rsz = sess->mplex_read_remain < sz ?
365: sess->mplex_read_remain : sz;
1.3 deraadt 366: if (!io_read_blocking(sess, fd, buf, rsz)) {
1.1 benno 367: ERRX1(sess, "io_read_blocking");
368: return 0;
369: }
370: sz -= rsz;
371: sess->mplex_read_remain -= rsz;
372: buf += rsz;
373: sess->total_read += rsz;
374: continue;
375: }
376:
1.4 deraadt 377: assert(sess->mplex_read_remain == 0);
1.3 deraadt 378: if (!io_read_flush(sess, fd)) {
1.1 benno 379: ERRX1(sess, "io_read_flush");
380: return 0;
381: }
382: }
383:
384: return 1;
385: }
386:
1.7 florian 387: /*
388: * Like io_write_buf(), but for a long (which is a composite type).
389: * Returns zero on failure, non-zero on success.
390: */
1.1 benno 391: int
392: io_write_long(struct sess *sess, int fd, int64_t val)
393: {
394: int64_t nv;
395:
396: /* Short-circuit: send as an integer if possible. */
397:
1.7 florian 398: if (val <= INT32_MAX && val >= 0) {
399: if (!io_write_int(sess, fd, (int32_t)val)) {
400: ERRX1(sess, "io_write_int");
401: return 0;
402: }
403: return 1;
404: }
1.1 benno 405:
406: /* Otherwise, pad with max integer, then send 64-bit. */
407:
408: nv = htole64(val);
409:
1.3 deraadt 410: if (!io_write_int(sess, fd, INT32_MAX))
1.7 florian 411: ERRX1(sess, "io_write_int");
1.3 deraadt 412: else if (!io_write_buf(sess, fd, &nv, sizeof(int64_t)))
1.7 florian 413: ERRX1(sess, "io_write_buf");
1.1 benno 414: else
415: return 1;
416:
417: return 0;
418: }
419:
1.7 florian 420: /*
421: * Like io_write_buf(), but for an integer.
422: * Returns zero on failure, non-zero on success.
423: */
1.1 benno 424: int
425: io_write_int(struct sess *sess, int fd, int32_t val)
426: {
427: int32_t nv;
428:
429: nv = htole32(val);
430:
1.3 deraadt 431: if (!io_write_buf(sess, fd, &nv, sizeof(int32_t))) {
1.7 florian 432: ERRX1(sess, "io_write_buf");
1.1 benno 433: return 0;
434: }
435: return 1;
436: }
437:
438: /*
439: * A simple assertion-protected memory copy from th einput "val" or size
440: * "valsz" into our buffer "buf", full size "buflen", position "bufpos".
441: * Increases our "bufpos" appropriately.
442: * This has no return value, but will assert() if the size of the buffer
443: * is insufficient for the new data.
444: */
445: void
1.2 benno 446: io_buffer_buf(struct sess *sess, void *buf,
1.1 benno 447: size_t *bufpos, size_t buflen, const void *val, size_t valsz)
448: {
449:
450: assert(*bufpos + valsz <= buflen);
451: memcpy(buf + *bufpos, val, valsz);
452: *bufpos += valsz;
453: }
454:
455: /*
1.7 florian 456: * Like io_buffer_buf(), but also accomodating for multiplexing codes.
457: * This should NEVER be passed to io_write_buf(), but instead passed
458: * directly to a write operation.
459: */
460: void
461: io_lowbuffer_buf(struct sess *sess, void *buf,
462: size_t *bufpos, size_t buflen, const void *val, size_t valsz)
463: {
464: int32_t tagbuf;
465:
1.9 deraadt 466: if (valsz == 0)
1.7 florian 467: return;
468:
469: if (!sess->mplex_writes) {
470: io_buffer_buf(sess, buf, bufpos, buflen, val, valsz);
471: return;
472: }
473:
474: assert(*bufpos + valsz + sizeof(int32_t) <= buflen);
475: assert(valsz == (valsz & 0xFFFFFF));
476: tagbuf = htole32((7 << 24) + valsz);
477:
478: io_buffer_int(sess, buf, bufpos, buflen, tagbuf);
479: io_buffer_buf(sess, buf, bufpos, buflen, val, valsz);
480: }
481:
482: /*
483: * Allocate the space needed for io_lowbuffer_buf() and friends.
484: * This should be called for *each* lowbuffer operation, so:
485: * io_lowbuffer_alloc(... sizeof(int32_t));
486: * io_lowbuffer_int(...);
487: * io_lowbuffer_alloc(... sizeof(int32_t));
488: * io_lowbuffer_int(...);
489: * And not sizeof(int32_t) * 2 or whatnot.
490: * Returns zero on failure, non-zero on succes.
491: */
492: int
493: io_lowbuffer_alloc(struct sess *sess, void **buf,
494: size_t *bufsz, size_t *bufmax, size_t sz)
495: {
496: void *pp;
497: size_t extra;
498:
499: extra = sess->mplex_writes ? sizeof(int32_t) : 0;
500:
501: if (*bufsz + sz + extra > *bufmax) {
502: pp = realloc(*buf, *bufsz + sz + extra);
503: if (pp == NULL) {
504: ERR(sess, "realloc");
505: return 0;
506: }
507: *buf = pp;
508: *bufmax = *bufsz + sz + extra;
509: }
510: *bufsz += sz + extra;
511: return 1;
512: }
513:
514: /*
515: * Like io_lowbuffer_buf(), but for a single integer.
516: */
517: void
518: io_lowbuffer_int(struct sess *sess, void *buf,
519: size_t *bufpos, size_t buflen, int32_t val)
520: {
521: int32_t nv = htole32(val);
522:
523: io_lowbuffer_buf(sess, buf, bufpos, buflen, &nv, sizeof(int32_t));
524: }
525:
526: /*
527: * Like io_buffer_buf(), but for a single integer.
1.1 benno 528: */
529: void
1.2 benno 530: io_buffer_int(struct sess *sess, void *buf,
1.1 benno 531: size_t *bufpos, size_t buflen, int32_t val)
532: {
533: int32_t nv = htole32(val);
534:
1.4 deraadt 535: io_buffer_buf(sess, buf, bufpos, buflen, &nv, sizeof(int32_t));
1.1 benno 536: }
537:
1.7 florian 538: /*
539: * Like io_read_buf(), but for a long >=0.
540: * Returns zero on failure, non-zero on success.
541: */
1.1 benno 542: int
543: io_read_ulong(struct sess *sess, int fd, uint64_t *val)
544: {
545: int64_t oval;
546:
1.3 deraadt 547: if (!io_read_long(sess, fd, &oval)) {
1.7 florian 548: ERRX1(sess, "io_read_long");
1.1 benno 549: return 0;
550: } else if (oval < 0) {
551: ERRX(sess, "io_read_size: negative value");
552: return 1;
553: }
554:
555: *val = oval;
556: return 1;
557: }
558:
1.7 florian 559: /*
560: * Like io_read_buf(), but for a long.
561: * Returns zero on failure, non-zero on success.
562: */
1.1 benno 563: int
564: io_read_long(struct sess *sess, int fd, int64_t *val)
565: {
566: int64_t oval;
567: int32_t sval;
568:
569: /* Start with the short-circuit: read as an int. */
570:
1.3 deraadt 571: if (!io_read_int(sess, fd, &sval)) {
1.7 florian 572: ERRX1(sess, "io_read_int");
1.1 benno 573: return 0;
1.4 deraadt 574: } else if (sval != INT32_MAX) {
1.1 benno 575: *val = sval;
576: return 1;
577: }
578:
579: /* If the int is maximal, read as 64 bits. */
580:
1.3 deraadt 581: if (!io_read_buf(sess, fd, &oval, sizeof(int64_t))) {
1.7 florian 582: ERRX1(sess, "io_read_buf");
1.1 benno 583: return 0;
584: }
585:
586: *val = le64toh(oval);
587: return 1;
588: }
589:
590: /*
591: * One thing we often need to do is read a size_t.
592: * These are transmitted as int32_t, so make sure that the value
593: * transmitted is not out of range.
594: * FIXME: I assume that size_t can handle int32_t's max.
1.7 florian 595: * Returns zero on failure, non-zero on success.
1.1 benno 596: */
597: int
598: io_read_size(struct sess *sess, int fd, size_t *val)
599: {
600: int32_t oval;
601:
1.3 deraadt 602: if (!io_read_int(sess, fd, &oval)) {
1.7 florian 603: ERRX1(sess, "io_read_int");
1.1 benno 604: return 0;
605: } else if (oval < 0) {
606: ERRX(sess, "io_read_size: negative value");
607: return 0;
608: }
609:
610: *val = oval;
611: return 1;
612: }
613:
1.7 florian 614: /*
615: * Like io_read_buf(), but for an integer.
616: * Returns zero on failure, non-zero on success.
617: */
1.1 benno 618: int
619: io_read_int(struct sess *sess, int fd, int32_t *val)
620: {
621: int32_t oval;
622:
1.3 deraadt 623: if (!io_read_buf(sess, fd, &oval, sizeof(int32_t))) {
1.7 florian 624: ERRX1(sess, "io_read_buf");
1.1 benno 625: return 0;
626: }
627:
628: *val = le32toh(oval);
629: return 1;
630: }
631:
632: /*
633: * Copies "valsz" from "buf", full size "bufsz" at position" bufpos",
634: * into "val".
635: * Calls assert() if the source doesn't have enough data.
636: * Increases "bufpos" to the new position.
637: */
638: void
1.2 benno 639: io_unbuffer_buf(struct sess *sess, const void *buf,
1.1 benno 640: size_t *bufpos, size_t bufsz, void *val, size_t valsz)
641: {
642:
643: assert(*bufpos + valsz <= bufsz);
644: memcpy(val, buf + *bufpos, valsz);
645: *bufpos += valsz;
646: }
647:
648: /*
1.7 florian 649: * Calls io_unbuffer_buf() and converts.
1.1 benno 650: */
651: void
1.2 benno 652: io_unbuffer_int(struct sess *sess, const void *buf,
1.1 benno 653: size_t *bufpos, size_t bufsz, int32_t *val)
654: {
655: int32_t oval;
656:
1.5 deraadt 657: io_unbuffer_buf(sess, buf, bufpos, bufsz, &oval, sizeof(int32_t));
1.1 benno 658: *val = le32toh(oval);
659: }
660:
1.7 florian 661: /*
662: * Calls io_unbuffer_buf() and converts.
663: */
1.1 benno 664: int
1.2 benno 665: io_unbuffer_size(struct sess *sess, const void *buf,
1.1 benno 666: size_t *bufpos, size_t bufsz, size_t *val)
667: {
668: int32_t oval;
669:
670: io_unbuffer_int(sess, buf, bufpos, bufsz, &oval);
671: if (oval < 0) {
672: ERRX(sess, "io_unbuffer_size: negative value");
673: return 0;
674: }
675: *val = oval;
676: return 1;
677: }
678:
1.7 florian 679: /*
680: * Like io_read_buf(), but for a single byte >=0.
681: * Returns zero on failure, non-zero on success.
682: */
1.1 benno 683: int
684: io_read_byte(struct sess *sess, int fd, uint8_t *val)
685: {
686:
1.3 deraadt 687: if (!io_read_buf(sess, fd, val, sizeof(uint8_t))) {
1.7 florian 688: ERRX1(sess, "io_read_buf");
1.1 benno 689: return 0;
690: }
691: return 1;
692: }
693:
1.7 florian 694: /*
695: * Like io_write_buf(), but for a single byte.
696: * Returns zero on failure, non-zero on success.
697: */
1.1 benno 698: int
699: io_write_byte(struct sess *sess, int fd, uint8_t val)
700: {
701:
1.3 deraadt 702: if (!io_write_buf(sess, fd, &val, sizeof(uint8_t))) {
1.7 florian 703: ERRX1(sess, "io_write_buf");
1.1 benno 704: return 0;
705: }
706: return 1;
707: }