Annotation of src/usr.bin/rsync/extern.h, Revision 1.19
1.19 ! benno 1: /* $Id: extern.h,v 1.18 2019/02/17 18:11:50 deraadt 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: #ifndef EXTERN_H
18: #define EXTERN_H
19:
20: /*
21: * This is the rsync protocol version that we support.
22: */
23: #define RSYNC_PROTOCOL (27)
24:
25: /*
1.19 ! benno 26: * The default service (see services(5)) for rsync.
! 27: */
! 28: #define RSYNC_SERVICE "rsync"
! 29:
! 30: /*
1.1 benno 31: * Maximum amount of file data sent over the wire at once.
32: */
33: #define MAX_CHUNK (32 * 1024)
34:
35: /*
36: * This is the minimum size for a block of data not including those in
37: * the remainder block.
38: */
39: #define BLOCK_SIZE_MIN (700)
40:
41: /*
42: * The sender and receiver use a two-phase synchronisation process.
43: * The first uses two-byte hashes; the second, 16-byte.
44: * (The second must hold a full MD4 digest.)
45: */
46: #define CSUM_LENGTH_PHASE1 (2)
47: #define CSUM_LENGTH_PHASE2 (16)
48:
1.14 florian 49: /*
50: * Use this for debugging deadlocks.
51: * All poll events will use it and catch time-outs.
52: */
53: #define POLL_TIMEOUT (INFTIM)
1.13 florian 54:
1.1 benno 55: /*
56: * Operating mode for a client or a server.
57: * Sender means we synchronise local files with those from remote.
58: * Receiver is the opposite.
59: * This is relative to which host we're running on.
60: */
61: enum fmode {
62: FARGS_SENDER,
63: FARGS_RECEIVER
64: };
65:
66: /*
67: * File arguments given on the command line.
68: * See struct opts.
69: */
70: struct fargs {
71: char *host; /* hostname or NULL if local */
72: char **sources; /* transfer source */
73: size_t sourcesz; /* number of sources */
74: char *sink; /* transfer endpoint */
75: enum fmode mode; /* mode of operation */
76: int remote; /* uses rsync:// or :: for remote */
77: char *module; /* if rsync://, the module */
78: };
79:
80: /*
81: * The subset of stat(2) information that we need.
82: * (There are some parts we don't use yet.)
83: */
84: struct flstat {
1.12 florian 85: mode_t mode; /* mode */
86: uid_t uid; /* user */
87: gid_t gid; /* group */
88: dev_t rdev; /* device type */
89: off_t size; /* size */
90: time_t mtime; /* modification */
1.1 benno 91: unsigned int flags;
1.12 florian 92: #define FLSTAT_TOP_DIR 0x01 /* a top-level directory */
1.1 benno 93:
94: };
95:
96: /*
97: * A list of files with their statistics.
98: */
99: struct flist {
100: char *path; /* path relative to root */
101: const char *wpath; /* "working" path for receiver */
102: struct flstat st; /* file information */
103: char *link; /* symlink target or NULL */
104: };
105:
106: /*
107: * Options passed into the command line.
108: * See struct fargs.
109: */
110: struct opts {
1.3 deraadt 111: int sender; /* --sender */
112: int server; /* --server */
113: int recursive; /* -r */
114: int verbose; /* -v */
115: int dry_run; /* -n */
116: int preserve_times; /* -t */
117: int preserve_perms; /* -p */
118: int preserve_links; /* -l */
1.5 benno 119: int preserve_gids; /* -g */
1.9 florian 120: int preserve_uids; /* -u */
1.3 deraadt 121: int del; /* --delete */
1.12 florian 122: int devices; /* --devices */
123: int specials; /* --specials */
1.3 deraadt 124: char *rsync_path; /* --rsync-path */
1.4 deraadt 125: char *ssh_prog; /* --rsh or -e */
1.18 deraadt 126: char *port; /* --port */
1.1 benno 127: };
128:
129: /*
130: * An individual block description for a file.
131: * See struct blkset.
132: */
133: struct blk {
134: off_t offs; /* offset in file */
135: size_t idx; /* block index */
136: size_t len; /* bytes in block */
137: uint32_t chksum_short; /* fast checksum */
138: unsigned char chksum_long[CSUM_LENGTH_PHASE2]; /* slow checksum */
139: };
140:
1.14 florian 141: enum blkstatst {
142: BLKSTAT_NONE = 0,
1.19 ! benno 143: BLKSTAT_NEXT,
1.14 florian 144: BLKSTAT_DATA,
145: BLKSTAT_TOK,
146: BLKSTAT_HASH,
1.19 ! benno 147: BLKSTAT_DONE,
! 148: BLKSTAT_PHASE,
1.14 florian 149: };
150:
1.1 benno 151: /*
1.13 florian 152: * Information for the sender updating receiver blocks reentrantly.
153: */
154: struct blkstat {
1.14 florian 155: off_t offs; /* position in sender file */
156: off_t total; /* total amount processed */
157: off_t dirty; /* total amount sent */
158: size_t hint; /* optimisation: next probable match */
159: void *map; /* mapped file or MAP_FAILED otherwise */
160: size_t mapsz; /* size of file or zero */
161: int fd; /* descriptor girding the map */
162: enum blkstatst curst; /* FSM for sending file blocks */
163: off_t curpos; /* sending: position in file to send */
164: off_t curlen; /* sending: length of send */
165: int32_t curtok; /* sending: next matching token or zero */
1.13 florian 166: };
167:
168: /*
1.1 benno 169: * When transferring file contents, we break the file down into blocks
170: * and work with those.
171: */
172: struct blkset {
173: off_t size; /* file size */
174: size_t rem; /* terminal block length if non-zero */
175: size_t len; /* block length */
176: size_t csum; /* checksum length */
177: struct blk *blks; /* all blocks */
178: size_t blksz; /* number of blks */
179: };
180:
181: /*
182: * Values required during a communication session.
183: */
184: struct sess {
185: const struct opts *opts; /* system options */
186: int32_t seed; /* checksum seed */
187: int32_t lver; /* local version */
188: int32_t rver; /* remote version */
189: uint64_t total_read; /* non-logging wire/reads */
190: uint64_t total_size; /* total file size */
191: uint64_t total_write; /* non-logging wire/writes */
192: int mplex_reads; /* multiplexing reads? */
193: size_t mplex_read_remain; /* remaining bytes */
194: int mplex_writes; /* multiplexing writes? */
195: };
196:
1.6 benno 197: /*
198: * Combination of name and numeric id for groups and users.
199: */
200: struct ident {
201: int32_t id; /* the gid_t or uid_t */
202: int32_t mapped; /* if receiving, the mapped gid */
203: char *name; /* resolved name */
204: };
205:
1.1 benno 206: struct download;
207: struct upload;
208:
1.17 deraadt 209: #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
1.14 florian 210:
1.1 benno 211: #define LOG0(_sess, _fmt, ...) \
212: rsync_log((_sess), __FILE__, __LINE__, -1, (_fmt), ##__VA_ARGS__)
213: #define LOG1(_sess, _fmt, ...) \
214: rsync_log((_sess), __FILE__, __LINE__, 0, (_fmt), ##__VA_ARGS__)
215: #define LOG2(_sess, _fmt, ...) \
216: rsync_log((_sess), __FILE__, __LINE__, 1, (_fmt), ##__VA_ARGS__)
217: #define LOG3(_sess, _fmt, ...) \
218: rsync_log((_sess), __FILE__, __LINE__, 2, (_fmt), ##__VA_ARGS__)
219: #define LOG4(_sess, _fmt, ...) \
220: rsync_log((_sess), __FILE__, __LINE__, 3, (_fmt), ##__VA_ARGS__)
221: #define ERRX1(_sess, _fmt, ...) \
222: rsync_errx1((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
223: #define WARNX(_sess, _fmt, ...) \
224: rsync_warnx((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
225: #define WARN(_sess, _fmt, ...) \
226: rsync_warn((_sess), 0, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
227: #define WARN1(_sess, _fmt, ...) \
228: rsync_warn((_sess), 1, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
229: #define WARN2(_sess, _fmt, ...) \
230: rsync_warn((_sess), 2, __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
231: #define ERR(_sess, _fmt, ...) \
232: rsync_err((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
233: #define ERRX(_sess, _fmt, ...) \
234: rsync_errx((_sess), __FILE__, __LINE__, (_fmt), ##__VA_ARGS__)
235:
236: __BEGIN_DECLS
237:
238: void rsync_log(struct sess *,
239: const char *, size_t, int, const char *, ...)
240: __attribute__((format(printf, 5, 6)));
241: void rsync_warnx1(struct sess *,
242: const char *, size_t, const char *, ...)
243: __attribute__((format(printf, 4, 5)));
244: void rsync_warn(struct sess *, int,
245: const char *, size_t, const char *, ...)
246: __attribute__((format(printf, 5, 6)));
247: void rsync_warnx(struct sess *, const char *,
248: size_t, const char *, ...)
249: __attribute__((format(printf, 4, 5)));
250: void rsync_err(struct sess *, const char *,
251: size_t, const char *, ...)
252: __attribute__((format(printf, 4, 5)));
253: void rsync_errx(struct sess *, const char *,
254: size_t, const char *, ...)
255: __attribute__((format(printf, 4, 5)));
256: void rsync_errx1(struct sess *, const char *,
257: size_t, const char *, ...)
258: __attribute__((format(printf, 4, 5)));
259:
1.2 benno 260: int flist_del(struct sess *, int,
1.1 benno 261: const struct flist *, size_t);
262: int flist_gen(struct sess *, size_t, char **,
263: struct flist **, size_t *);
264: int flist_gen_local(struct sess *, const char *,
265: struct flist **, size_t *);
266: void flist_free(struct flist *, size_t);
267: int flist_recv(struct sess *, int,
268: struct flist **, size_t *);
269: int flist_send(struct sess *, int, int,
270: const struct flist *, size_t);
1.2 benno 271: int flist_gen_dels(struct sess *, const char *,
1.1 benno 272: struct flist **, size_t *,
273: const struct flist *, size_t);
274:
275: char **fargs_cmdline(struct sess *, const struct fargs *);
276:
277: int io_read_buf(struct sess *, int, void *, size_t);
278: int io_read_byte(struct sess *, int, uint8_t *);
279: int io_read_check(struct sess *, int);
280: int io_read_flush(struct sess *, int);
281: int io_read_int(struct sess *, int, int32_t *);
282: int io_read_long(struct sess *, int, int64_t *);
283: int io_read_size(struct sess *, int, size_t *);
284: int io_read_ulong(struct sess *, int, uint64_t *);
285: int io_write_buf(struct sess *, int, const void *, size_t);
286: int io_write_byte(struct sess *, int, uint8_t);
287: int io_write_int(struct sess *, int, int32_t);
288: int io_write_line(struct sess *, int, const char *);
289: int io_write_long(struct sess *, int, int64_t);
290:
1.14 florian 291: int io_lowbuffer_alloc(struct sess *, void **,
292: size_t *, size_t *, size_t);
293: void io_lowbuffer_int(struct sess *, void *,
294: size_t *, size_t, int32_t);
295: void io_lowbuffer_buf(struct sess *, void *,
296: size_t *, size_t, const void *, size_t);
297:
1.2 benno 298: void io_buffer_int(struct sess *, void *,
1.1 benno 299: size_t *, size_t, int32_t);
1.2 benno 300: void io_buffer_buf(struct sess *, void *,
1.1 benno 301: size_t *, size_t, const void *, size_t);
302:
1.2 benno 303: void io_unbuffer_int(struct sess *, const void *,
1.1 benno 304: size_t *, size_t, int32_t *);
1.2 benno 305: int io_unbuffer_size(struct sess *, const void *,
1.1 benno 306: size_t *, size_t, size_t *);
1.2 benno 307: void io_unbuffer_buf(struct sess *, const void *,
1.1 benno 308: size_t *, size_t, void *, size_t);
309:
310: void rsync_child(const struct opts *, int, const struct fargs *)
311: __attribute__((noreturn));
312: int rsync_receiver(struct sess *, int, int, const char *);
313: int rsync_sender(struct sess *, int, int, size_t, char **);
314: int rsync_client(const struct opts *, int, const struct fargs *);
315: int rsync_socket(const struct opts *, const struct fargs *);
316: int rsync_server(const struct opts *, size_t, char *[]);
317: int rsync_downloader(struct download *, struct sess *, int *);
1.15 florian 318: int rsync_set_metadata(struct sess *, int, int,
1.10 florian 319: const struct flist *, const char *);
1.16 deraadt 320: int rsync_set_metadata_at(struct sess *, int, int,
1.12 florian 321: const struct flist *, const char *);
1.2 benno 322: int rsync_uploader(struct upload *,
1.1 benno 323: int *, struct sess *, int *);
324: int rsync_uploader_tail(struct upload *, struct sess *);
325:
326: struct download *download_alloc(struct sess *, int,
327: const struct flist *, size_t, int);
328: void download_free(struct download *);
1.12 florian 329: struct upload *upload_alloc(struct sess *, const char *, int, int, size_t,
1.1 benno 330: const struct flist *, size_t, mode_t);
331: void upload_free(struct upload *);
332:
333: struct blkset *blk_recv(struct sess *, int, const char *);
1.19 ! benno 334: void blk_recv_ack(struct sess *,
! 335: char [20], const struct blkset *, int32_t);
1.14 florian 336: void blk_match(struct sess *, const struct blkset *,
1.13 florian 337: const char *, struct blkstat *);
1.1 benno 338: int blk_send(struct sess *, int, size_t,
339: const struct blkset *, const char *);
340: int blk_send_ack(struct sess *, int, struct blkset *);
341:
342: uint32_t hash_fast(const void *, size_t);
343: void hash_slow(const void *, size_t,
344: unsigned char *, const struct sess *);
345: void hash_file(const void *, size_t,
346: unsigned char *, const struct sess *);
347:
348: int mkpath(struct sess *, char *);
1.11 florian 349:
350: int mkstempat(int, char *);
351: char *mkstemplinkat(char*, int, char *);
1.12 florian 352: char *mkstempfifoat(int, char *);
353: char *mkstempnodat(int, char *, mode_t, dev_t);
354: char *mkstempsock(const char *, char *);
1.19 ! benno 355: int mktemplate(struct sess *, char **, const char *, int);
1.1 benno 356:
357: char *symlink_read(struct sess *, const char *);
358: char *symlinkat_read(struct sess *, int, const char *);
359:
360: int sess_stats_send(struct sess *, int);
361: int sess_stats_recv(struct sess *, int);
1.6 benno 362:
1.9 florian 363: int idents_add(struct sess *, int, struct ident **, size_t *,
364: int32_t);
365: void idents_assign_gid(struct sess *,
366: struct flist *, size_t, const struct ident *, size_t);
367: void idents_assign_uid(struct sess *,
368: struct flist *, size_t, const struct ident *, size_t);
1.6 benno 369: void idents_free(struct ident *, size_t);
1.7 benno 370: int idents_recv(struct sess *, int, struct ident **, size_t *);
1.9 florian 371: void idents_remap(struct sess *, int, struct ident *, size_t);
1.6 benno 372: int idents_send(struct sess *, int, const struct ident *, size_t);
1.1 benno 373:
374: __END_DECLS
375:
376: #endif /*!EXTERN_H*/