io.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /*
  2. * Socket and pipe I/O utilities used in rsync.
  3. *
  4. * Copyright (C) 1996-2001 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2003-2009 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. /* Rsync provides its own multiplexing system, which is used to send
  23. * stderr and stdout over a single socket.
  24. *
  25. * For historical reasons this is off during the start of the
  26. * connection, but it's switched on quite early using
  27. * io_start_multiplex_out() and io_start_multiplex_in(). */
  28. #include "rsync.h"
  29. #include "ifuncs.h"
  30. /** If no timeout is specified then use a 60 second select timeout */
  31. #define SELECT_TIMEOUT 60
  32. extern int bwlimit;
  33. extern size_t bwlimit_writemax;
  34. extern int io_timeout;
  35. extern int am_server;
  36. extern int am_daemon;
  37. extern int am_sender;
  38. extern int am_generator;
  39. extern int inc_recurse;
  40. extern int io_error;
  41. extern int eol_nulls;
  42. extern int flist_eof;
  43. extern int list_only;
  44. extern int read_batch;
  45. extern int compat_flags;
  46. extern int protect_args;
  47. extern int checksum_seed;
  48. extern int protocol_version;
  49. extern int remove_source_files;
  50. extern int preserve_hard_links;
  51. extern struct stats stats;
  52. extern struct file_list *cur_flist;
  53. #ifdef ICONV_OPTION
  54. extern int filesfrom_convert;
  55. extern iconv_t ic_send, ic_recv;
  56. #endif
  57. int csum_length = SHORT_SUM_LENGTH; /* initial value */
  58. int allowed_lull = 0;
  59. int ignore_timeout = 0;
  60. int batch_fd = -1;
  61. int msgdone_cnt = 0;
  62. /* Ignore an EOF error if non-zero. See whine_about_eof(). */
  63. int kluge_around_eof = 0;
  64. int msg_fd_in = -1;
  65. int msg_fd_out = -1;
  66. int sock_f_in = -1;
  67. int sock_f_out = -1;
  68. static int iobuf_f_in = -1;
  69. static char *iobuf_in;
  70. static size_t iobuf_in_siz;
  71. static size_t iobuf_in_ndx;
  72. static size_t iobuf_in_remaining;
  73. static int iobuf_f_out = -1;
  74. static char *iobuf_out;
  75. static int iobuf_out_cnt;
  76. int flist_forward_from = -1;
  77. static int io_multiplexing_out;
  78. static int io_multiplexing_in;
  79. static time_t last_io_in;
  80. static time_t last_io_out;
  81. static int no_flush;
  82. static int write_batch_monitor_in = -1;
  83. static int write_batch_monitor_out = -1;
  84. static int io_filesfrom_f_in = -1;
  85. static int io_filesfrom_f_out = -1;
  86. static xbuf ff_buf = EMPTY_XBUF;
  87. static char ff_lastchar;
  88. #ifdef ICONV_OPTION
  89. static xbuf iconv_buf = EMPTY_XBUF;
  90. #endif
  91. static int defer_forwarding_messages = 0, keep_defer_forwarding = 0;
  92. static int select_timeout = SELECT_TIMEOUT;
  93. static int active_filecnt = 0;
  94. static OFF_T active_bytecnt = 0;
  95. static int first_message = 1;
  96. static char int_byte_extra[64] = {
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (00 - 3F)/4 */
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (40 - 7F)/4 */
  99. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* (80 - BF)/4 */
  100. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */
  101. };
  102. #define REMOTE_OPTION_ERROR "rsync: on remote machine: -"
  103. #define REMOTE_OPTION_ERROR2 ": unknown option"
  104. enum festatus { FES_SUCCESS, FES_REDO, FES_NO_SEND };
  105. static void check_timeout(void)
  106. {
  107. time_t t, chk;
  108. if (!io_timeout || ignore_timeout)
  109. return;
  110. t = time(NULL);
  111. if (!last_io_in)
  112. last_io_in = t;
  113. chk = MAX(last_io_out, last_io_in);
  114. if (t - chk >= io_timeout) {
  115. if (am_server || am_daemon)
  116. exit_cleanup(RERR_TIMEOUT);
  117. rprintf(FERROR, "[%s] io timeout after %d seconds -- exiting\n",
  118. who_am_i(), (int)(t-chk));
  119. exit_cleanup(RERR_TIMEOUT);
  120. }
  121. }
  122. static void readfd(int fd, char *buffer, size_t N);
  123. static void writefd(int fd, const char *buf, size_t len);
  124. static void writefd_unbuffered(int fd, const char *buf, size_t len);
  125. static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert);
  126. static flist_ndx_list redo_list, hlink_list;
  127. struct msg_list_item {
  128. struct msg_list_item *next;
  129. char convert;
  130. char buf[1];
  131. };
  132. struct msg_list {
  133. struct msg_list_item *head, *tail;
  134. };
  135. static struct msg_list msg_queue;
  136. static void got_flist_entry_status(enum festatus status, const char *buf)
  137. {
  138. int ndx = IVAL(buf, 0);
  139. struct file_list *flist = flist_for_ndx(ndx, "got_flist_entry_status");
  140. if (remove_source_files) {
  141. active_filecnt--;
  142. active_bytecnt -= F_LENGTH(flist->files[ndx - flist->ndx_start]);
  143. }
  144. if (inc_recurse)
  145. flist->in_progress--;
  146. switch (status) {
  147. case FES_SUCCESS:
  148. if (remove_source_files)
  149. send_msg(MSG_SUCCESS, buf, 4, 0);
  150. if (preserve_hard_links) {
  151. struct file_struct *file = flist->files[ndx - flist->ndx_start];
  152. if (F_IS_HLINKED(file)) {
  153. flist_ndx_push(&hlink_list, ndx);
  154. flist->in_progress++;
  155. }
  156. }
  157. break;
  158. case FES_REDO:
  159. if (read_batch) {
  160. if (inc_recurse)
  161. flist->in_progress++;
  162. break;
  163. }
  164. if (inc_recurse)
  165. flist->to_redo++;
  166. flist_ndx_push(&redo_list, ndx);
  167. break;
  168. case FES_NO_SEND:
  169. break;
  170. }
  171. }
  172. /* Note the fds used for the main socket (which might really be a pipe
  173. * for a local transfer, but we can ignore that). */
  174. void io_set_sock_fds(int f_in, int f_out)
  175. {
  176. sock_f_in = f_in;
  177. sock_f_out = f_out;
  178. }
  179. void set_io_timeout(int secs)
  180. {
  181. io_timeout = secs;
  182. allowed_lull = (io_timeout + 1) / 2;
  183. if (!io_timeout || allowed_lull > SELECT_TIMEOUT)
  184. select_timeout = SELECT_TIMEOUT;
  185. else
  186. select_timeout = allowed_lull;
  187. if (read_batch)
  188. allowed_lull = 0;
  189. }
  190. /* Setup the fd used to receive MSG_* messages. Only needed during the
  191. * early stages of being a local sender (up through the sending of the
  192. * file list) or when we're the generator (to fetch the messages from
  193. * the receiver). */
  194. void set_msg_fd_in(int fd)
  195. {
  196. msg_fd_in = fd;
  197. }
  198. /* Setup the fd used to send our MSG_* messages. Only needed when
  199. * we're the receiver (to send our messages to the generator). */
  200. void set_msg_fd_out(int fd)
  201. {
  202. msg_fd_out = fd;
  203. set_nonblocking(msg_fd_out);
  204. }
  205. /* Add a message to the pending MSG_* list. */
  206. static void msg_list_add(struct msg_list *lst, int code, const char *buf, int len, int convert)
  207. {
  208. struct msg_list_item *m;
  209. int sz = len + 4 + sizeof m[0] - 1;
  210. if (!(m = (struct msg_list_item *)new_array(char, sz)))
  211. out_of_memory("msg_list_add");
  212. m->next = NULL;
  213. m->convert = convert;
  214. SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len);
  215. memcpy(m->buf + 4, buf, len);
  216. if (lst->tail)
  217. lst->tail->next = m;
  218. else
  219. lst->head = m;
  220. lst->tail = m;
  221. }
  222. static inline int flush_a_msg(int fd)
  223. {
  224. struct msg_list_item *m = msg_queue.head;
  225. int len = IVAL(m->buf, 0) & 0xFFFFFF;
  226. int tag = *((uchar*)m->buf+3) - MPLEX_BASE;
  227. if (!(msg_queue.head = m->next))
  228. msg_queue.tail = NULL;
  229. defer_forwarding_messages++;
  230. mplex_write(fd, tag, m->buf + 4, len, m->convert);
  231. defer_forwarding_messages--;
  232. free(m);
  233. return len;
  234. }
  235. static void msg_flush(void)
  236. {
  237. if (am_generator) {
  238. while (msg_queue.head && io_multiplexing_out)
  239. stats.total_written += flush_a_msg(sock_f_out) + 4;
  240. } else {
  241. while (msg_queue.head)
  242. (void)flush_a_msg(msg_fd_out);
  243. }
  244. }
  245. static void check_for_d_option_error(const char *msg)
  246. {
  247. static char rsync263_opts[] = "BCDHIKLPRSTWabceghlnopqrtuvxz";
  248. char *colon;
  249. int saw_d = 0;
  250. if (*msg != 'r'
  251. || strncmp(msg, REMOTE_OPTION_ERROR, sizeof REMOTE_OPTION_ERROR - 1) != 0)
  252. return;
  253. msg += sizeof REMOTE_OPTION_ERROR - 1;
  254. if (*msg == '-' || (colon = strchr(msg, ':')) == NULL
  255. || strncmp(colon, REMOTE_OPTION_ERROR2, sizeof REMOTE_OPTION_ERROR2 - 1) != 0)
  256. return;
  257. for ( ; *msg != ':'; msg++) {
  258. if (*msg == 'd')
  259. saw_d = 1;
  260. else if (*msg == 'e')
  261. break;
  262. else if (strchr(rsync263_opts, *msg) == NULL)
  263. return;
  264. }
  265. if (saw_d) {
  266. rprintf(FWARNING,
  267. "*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n");
  268. }
  269. }
  270. /* Read a message from the MSG_* fd and handle it. This is called either
  271. * during the early stages of being a local sender (up through the sending
  272. * of the file list) or when we're the generator (to fetch the messages
  273. * from the receiver). */
  274. static void read_msg_fd(void)
  275. {
  276. char buf[2048];
  277. size_t n;
  278. struct file_list *flist;
  279. int fd = msg_fd_in;
  280. int tag, len;
  281. /* Temporarily disable msg_fd_in. This is needed to avoid looping back
  282. * to this routine from writefd_unbuffered(). */
  283. no_flush++;
  284. msg_fd_in = -1;
  285. defer_forwarding_messages++;
  286. readfd(fd, buf, 4);
  287. tag = IVAL(buf, 0);
  288. len = tag & 0xFFFFFF;
  289. tag = (tag >> 24) - MPLEX_BASE;
  290. switch (tag) {
  291. case MSG_DONE:
  292. if (len < 0 || len > 1 || !am_generator) {
  293. invalid_msg:
  294. rprintf(FERROR, "invalid message %d:%d [%s%s]\n",
  295. tag, len, who_am_i(),
  296. inc_recurse ? "/inc" : "");
  297. exit_cleanup(RERR_STREAMIO);
  298. }
  299. if (len) {
  300. readfd(fd, buf, len);
  301. stats.total_read = read_varlong(fd, 3);
  302. }
  303. msgdone_cnt++;
  304. break;
  305. case MSG_REDO:
  306. if (len != 4 || !am_generator)
  307. goto invalid_msg;
  308. readfd(fd, buf, 4);
  309. got_flist_entry_status(FES_REDO, buf);
  310. break;
  311. case MSG_FLIST:
  312. if (len != 4 || !am_generator || !inc_recurse)
  313. goto invalid_msg;
  314. readfd(fd, buf, 4);
  315. /* Read extra file list from receiver. */
  316. assert(iobuf_in != NULL);
  317. assert(iobuf_f_in == fd);
  318. if (verbose > 3) {
  319. rprintf(FINFO, "[%s] receiving flist for dir %d\n",
  320. who_am_i(), IVAL(buf,0));
  321. }
  322. flist = recv_file_list(fd);
  323. flist->parent_ndx = IVAL(buf,0);
  324. #ifdef SUPPORT_HARD_LINKS
  325. if (preserve_hard_links)
  326. match_hard_links(flist);
  327. #endif
  328. break;
  329. case MSG_FLIST_EOF:
  330. if (len != 0 || !am_generator || !inc_recurse)
  331. goto invalid_msg;
  332. flist_eof = 1;
  333. break;
  334. case MSG_IO_ERROR:
  335. if (len != 4)
  336. goto invalid_msg;
  337. readfd(fd, buf, len);
  338. io_error |= IVAL(buf, 0);
  339. break;
  340. case MSG_DELETED:
  341. if (len >= (int)sizeof buf || !am_generator)
  342. goto invalid_msg;
  343. readfd(fd, buf, len);
  344. send_msg(MSG_DELETED, buf, len, 1);
  345. break;
  346. case MSG_SUCCESS:
  347. if (len != 4 || !am_generator)
  348. goto invalid_msg;
  349. readfd(fd, buf, 4);
  350. got_flist_entry_status(FES_SUCCESS, buf);
  351. break;
  352. case MSG_NO_SEND:
  353. if (len != 4 || !am_generator)
  354. goto invalid_msg;
  355. readfd(fd, buf, 4);
  356. got_flist_entry_status(FES_NO_SEND, buf);
  357. break;
  358. case MSG_ERROR_SOCKET:
  359. case MSG_ERROR_UTF8:
  360. case MSG_CLIENT:
  361. if (!am_generator)
  362. goto invalid_msg;
  363. if (tag == MSG_ERROR_SOCKET)
  364. io_end_multiplex_out();
  365. /* FALL THROUGH */
  366. case MSG_INFO:
  367. case MSG_ERROR:
  368. case MSG_ERROR_XFER:
  369. case MSG_WARNING:
  370. case MSG_LOG:
  371. while (len) {
  372. n = len;
  373. if (n >= sizeof buf)
  374. n = sizeof buf - 1;
  375. readfd(fd, buf, n);
  376. rwrite((enum logcode)tag, buf, n, !am_generator);
  377. len -= n;
  378. }
  379. break;
  380. default:
  381. rprintf(FERROR, "unknown message %d:%d [%s]\n",
  382. tag, len, who_am_i());
  383. exit_cleanup(RERR_STREAMIO);
  384. }
  385. no_flush--;
  386. msg_fd_in = fd;
  387. if (!--defer_forwarding_messages && !no_flush)
  388. msg_flush();
  389. }
  390. /* This is used by the generator to limit how many file transfers can
  391. * be active at once when --remove-source-files is specified. Without
  392. * this, sender-side deletions were mostly happening at the end. */
  393. void increment_active_files(int ndx, int itemizing, enum logcode code)
  394. {
  395. while (1) {
  396. /* TODO: tune these limits? */
  397. int limit = active_bytecnt >= 128*1024 ? 10 : 50;
  398. if (active_filecnt < limit)
  399. break;
  400. check_for_finished_files(itemizing, code, 0);
  401. if (active_filecnt < limit)
  402. break;
  403. if (iobuf_out_cnt)
  404. io_flush(NORMAL_FLUSH);
  405. else
  406. read_msg_fd();
  407. }
  408. active_filecnt++;
  409. active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]);
  410. }
  411. /* Write an message to a multiplexed stream. If this fails, rsync exits. */
  412. static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert)
  413. {
  414. char buffer[BIGPATHBUFLEN]; /* Oversized for use by iconv code. */
  415. size_t n = len;
  416. #ifdef ICONV_OPTION
  417. /* We need to convert buf before doing anything else so that we
  418. * can include the (converted) byte length in the message header. */
  419. if (convert && ic_send != (iconv_t)-1) {
  420. xbuf outbuf, inbuf;
  421. INIT_XBUF(outbuf, buffer + 4, 0, sizeof buffer - 4);
  422. INIT_XBUF(inbuf, (char*)buf, len, -1);
  423. iconvbufs(ic_send, &inbuf, &outbuf,
  424. ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
  425. if (inbuf.len > 0) {
  426. rprintf(FERROR, "overflowed conversion buffer in mplex_write");
  427. exit_cleanup(RERR_UNSUPPORTED);
  428. }
  429. n = len = outbuf.len;
  430. } else
  431. #endif
  432. if (n > 1024 - 4) /* BIGPATHBUFLEN can handle 1024 bytes */
  433. n = 0; /* We'd rather do 2 writes than too much memcpy(). */
  434. else
  435. memcpy(buffer + 4, buf, n);
  436. SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
  437. keep_defer_forwarding++; /* defer_forwarding_messages++ on return */
  438. writefd_unbuffered(fd, buffer, n+4);
  439. keep_defer_forwarding--;
  440. if (len > n)
  441. writefd_unbuffered(fd, buf+n, len-n);
  442. if (!--defer_forwarding_messages && !no_flush)
  443. msg_flush();
  444. }
  445. int send_msg(enum msgcode code, const char *buf, int len, int convert)
  446. {
  447. if (msg_fd_out < 0) {
  448. if (!defer_forwarding_messages)
  449. return io_multiplex_write(code, buf, len, convert);
  450. if (!io_multiplexing_out)
  451. return 0;
  452. msg_list_add(&msg_queue, code, buf, len, convert);
  453. return 1;
  454. }
  455. if (flist_forward_from >= 0)
  456. msg_list_add(&msg_queue, code, buf, len, convert);
  457. else
  458. mplex_write(msg_fd_out, code, buf, len, convert);
  459. return 1;
  460. }
  461. void send_msg_int(enum msgcode code, int num)
  462. {
  463. char numbuf[4];
  464. SIVAL(numbuf, 0, num);
  465. send_msg(code, numbuf, 4, 0);
  466. }
  467. void wait_for_receiver(void)
  468. {
  469. if (io_flush(NORMAL_FLUSH))
  470. return;
  471. read_msg_fd();
  472. }
  473. int get_redo_num(void)
  474. {
  475. return flist_ndx_pop(&redo_list);
  476. }
  477. int get_hlink_num(void)
  478. {
  479. return flist_ndx_pop(&hlink_list);
  480. }
  481. /**
  482. * When we're the receiver and we have a local --files-from list of names
  483. * that needs to be sent over the socket to the sender, we have to do two
  484. * things at the same time: send the sender a list of what files we're
  485. * processing and read the incoming file+info list from the sender. We do
  486. * this by augmenting the read_timeout() function to copy this data. It
  487. * uses ff_buf to read a block of data from f_in (when it is ready, since
  488. * it might be a pipe) and then blast it out f_out (when it is ready to
  489. * receive more data).
  490. */
  491. void io_set_filesfrom_fds(int f_in, int f_out)
  492. {
  493. io_filesfrom_f_in = f_in;
  494. io_filesfrom_f_out = f_out;
  495. alloc_xbuf(&ff_buf, 2048);
  496. #ifdef ICONV_OPTION
  497. if (protect_args)
  498. alloc_xbuf(&iconv_buf, 1024);
  499. #endif
  500. }
  501. /* It's almost always an error to get an EOF when we're trying to read from the
  502. * network, because the protocol is (for the most part) self-terminating.
  503. *
  504. * There is one case for the receiver when it is at the end of the transfer
  505. * (hanging around reading any keep-alive packets that might come its way): if
  506. * the sender dies before the generator's kill-signal comes through, we can end
  507. * up here needing to loop until the kill-signal arrives. In this situation,
  508. * kluge_around_eof will be < 0.
  509. *
  510. * There is another case for older protocol versions (< 24) where the module
  511. * listing was not terminated, so we must ignore an EOF error in that case and
  512. * exit. In this situation, kluge_around_eof will be > 0. */
  513. static void whine_about_eof(int fd)
  514. {
  515. if (kluge_around_eof && fd == sock_f_in) {
  516. int i;
  517. if (kluge_around_eof > 0)
  518. exit_cleanup(0);
  519. /* If we're still here after 10 seconds, exit with an error. */
  520. for (i = 10*1000/20; i--; )
  521. msleep(20);
  522. }
  523. rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
  524. "(%.0f bytes received so far) [%s]\n",
  525. (double)stats.total_read, who_am_i());
  526. exit_cleanup(RERR_STREAMIO);
  527. }
  528. /**
  529. * Read from a socket with I/O timeout. return the number of bytes
  530. * read. If no bytes can be read then exit, never return a number <= 0.
  531. *
  532. * TODO: If the remote shell connection fails, then current versions
  533. * actually report an "unexpected EOF" error here. Since it's a
  534. * fairly common mistake to try to use rsh when ssh is required, we
  535. * should trap that: if we fail to read any data at all, we should
  536. * give a better explanation. We can tell whether the connection has
  537. * started by looking e.g. at whether the remote version is known yet.
  538. */
  539. static int read_timeout(int fd, char *buf, size_t len)
  540. {
  541. int n, cnt = 0;
  542. io_flush(FULL_FLUSH);
  543. while (cnt == 0) {
  544. /* until we manage to read *something* */
  545. fd_set r_fds, w_fds;
  546. struct timeval tv;
  547. int maxfd = fd;
  548. int count;
  549. FD_ZERO(&r_fds);
  550. FD_ZERO(&w_fds);
  551. FD_SET(fd, &r_fds);
  552. if (io_filesfrom_f_out >= 0) {
  553. int new_fd;
  554. if (ff_buf.len == 0) {
  555. if (io_filesfrom_f_in >= 0) {
  556. FD_SET(io_filesfrom_f_in, &r_fds);
  557. new_fd = io_filesfrom_f_in;
  558. } else {
  559. io_filesfrom_f_out = -1;
  560. new_fd = -1;
  561. }
  562. } else {
  563. FD_SET(io_filesfrom_f_out, &w_fds);
  564. new_fd = io_filesfrom_f_out;
  565. }
  566. if (new_fd > maxfd)
  567. maxfd = new_fd;
  568. }
  569. tv.tv_sec = select_timeout;
  570. tv.tv_usec = 0;
  571. errno = 0;
  572. count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
  573. if (count <= 0) {
  574. if (errno == EBADF) {
  575. defer_forwarding_messages = 0;
  576. exit_cleanup(RERR_SOCKETIO);
  577. }
  578. check_timeout();
  579. continue;
  580. }
  581. if (io_filesfrom_f_out >= 0) {
  582. if (ff_buf.len) {
  583. if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
  584. int l = write(io_filesfrom_f_out,
  585. ff_buf.buf + ff_buf.pos,
  586. ff_buf.len);
  587. if (l > 0) {
  588. if (!(ff_buf.len -= l))
  589. ff_buf.pos = 0;
  590. else
  591. ff_buf.pos += l;
  592. } else if (errno != EINTR) {
  593. /* XXX should we complain? */
  594. io_filesfrom_f_out = -1;
  595. }
  596. }
  597. } else if (io_filesfrom_f_in >= 0) {
  598. if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
  599. #ifdef ICONV_OPTION
  600. xbuf *ibuf = filesfrom_convert ? &iconv_buf : &ff_buf;
  601. #else
  602. xbuf *ibuf = &ff_buf;
  603. #endif
  604. int l = read(io_filesfrom_f_in, ibuf->buf, ibuf->size);
  605. if (l <= 0) {
  606. if (l == 0 || errno != EINTR) {
  607. /* Send end-of-file marker */
  608. memcpy(ff_buf.buf, "\0\0", 2);
  609. ff_buf.len = ff_lastchar? 2 : 1;
  610. ff_buf.pos = 0;
  611. io_filesfrom_f_in = -1;
  612. }
  613. } else {
  614. #ifdef ICONV_OPTION
  615. if (filesfrom_convert) {
  616. iconv_buf.pos = 0;
  617. iconv_buf.len = l;
  618. iconvbufs(ic_send, &iconv_buf, &ff_buf,
  619. ICB_EXPAND_OUT|ICB_INCLUDE_BAD|ICB_INCLUDE_INCOMPLETE);
  620. l = ff_buf.len;
  621. }
  622. #endif
  623. if (!eol_nulls) {
  624. char *s = ff_buf.buf + l;
  625. /* Transform CR and/or LF into '\0' */
  626. while (s-- > ff_buf.buf) {
  627. if (*s == '\n' || *s == '\r')
  628. *s = '\0';
  629. }
  630. }
  631. if (!ff_lastchar) {
  632. /* Last buf ended with a '\0', so don't
  633. * let this buf start with one. */
  634. while (l && ff_buf.buf[ff_buf.pos] == '\0')
  635. ff_buf.pos++, l--;
  636. }
  637. if (!l)
  638. ff_buf.pos = 0;
  639. else {
  640. char *f = ff_buf.buf + ff_buf.pos;
  641. char *t = f;
  642. char *eob = f + l;
  643. /* Eliminate any multi-'\0' runs. */
  644. while (f != eob) {
  645. if (!(*t++ = *f++)) {
  646. while (f != eob && !*f)
  647. f++, l--;
  648. }
  649. }
  650. ff_lastchar = f[-1];
  651. }
  652. ff_buf.len = l;
  653. }
  654. }
  655. }
  656. }
  657. if (!FD_ISSET(fd, &r_fds))
  658. continue;
  659. n = read(fd, buf, len);
  660. if (n <= 0) {
  661. if (n == 0)
  662. whine_about_eof(fd); /* Doesn't return. */
  663. if (errno == EINTR || errno == EWOULDBLOCK
  664. || errno == EAGAIN)
  665. continue;
  666. /* Don't write errors on a dead socket. */
  667. if (fd == sock_f_in) {
  668. io_end_multiplex_out();
  669. rsyserr(FERROR_SOCKET, errno, "read error");
  670. } else
  671. rsyserr(FERROR, errno, "read error");
  672. exit_cleanup(RERR_STREAMIO);
  673. }
  674. buf += n;
  675. len -= n;
  676. cnt += n;
  677. if (fd == sock_f_in && io_timeout)
  678. last_io_in = time(NULL);
  679. }
  680. return cnt;
  681. }
  682. /* Read a line into the "buf" buffer. */
  683. int read_line(int fd, char *buf, size_t bufsiz, int flags)
  684. {
  685. char ch, *s, *eob;
  686. int cnt;
  687. #ifdef ICONV_OPTION
  688. if (flags & RL_CONVERT && iconv_buf.size < bufsiz)
  689. realloc_xbuf(&iconv_buf, bufsiz + 1024);
  690. #endif
  691. start:
  692. #ifdef ICONV_OPTION
  693. s = flags & RL_CONVERT ? iconv_buf.buf : buf;
  694. #else
  695. s = buf;
  696. #endif
  697. eob = s + bufsiz - 1;
  698. while (1) {
  699. cnt = read(fd, &ch, 1);
  700. if (cnt < 0 && (errno == EWOULDBLOCK
  701. || errno == EINTR || errno == EAGAIN)) {
  702. struct timeval tv;
  703. fd_set r_fds, e_fds;
  704. FD_ZERO(&r_fds);
  705. FD_SET(fd, &r_fds);
  706. FD_ZERO(&e_fds);
  707. FD_SET(fd, &e_fds);
  708. tv.tv_sec = select_timeout;
  709. tv.tv_usec = 0;
  710. if (!select(fd+1, &r_fds, NULL, &e_fds, &tv))
  711. check_timeout();
  712. /*if (FD_ISSET(fd, &e_fds))
  713. rprintf(FINFO, "select exception on fd %d\n", fd); */
  714. continue;
  715. }
  716. if (cnt != 1)
  717. break;
  718. if (flags & RL_EOL_NULLS ? ch == '\0' : (ch == '\r' || ch == '\n')) {
  719. /* Skip empty lines if dumping comments. */
  720. if (flags & RL_DUMP_COMMENTS && s == buf)
  721. continue;
  722. break;
  723. }
  724. if (s < eob)
  725. *s++ = ch;
  726. }
  727. *s = '\0';
  728. if (flags & RL_DUMP_COMMENTS && (*buf == '#' || *buf == ';'))
  729. goto start;
  730. #ifdef ICONV_OPTION
  731. if (flags & RL_CONVERT) {
  732. xbuf outbuf;
  733. INIT_XBUF(outbuf, buf, 0, bufsiz);
  734. iconv_buf.pos = 0;
  735. iconv_buf.len = s - iconv_buf.buf;
  736. iconvbufs(ic_recv, &iconv_buf, &outbuf,
  737. ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
  738. outbuf.buf[outbuf.len] = '\0';
  739. return outbuf.len;
  740. }
  741. #endif
  742. return s - buf;
  743. }
  744. void read_args(int f_in, char *mod_name, char *buf, size_t bufsiz, int rl_nulls,
  745. char ***argv_p, int *argc_p, char **request_p)
  746. {
  747. int maxargs = MAX_ARGS;
  748. int dot_pos = 0;
  749. int argc = 0;
  750. char **argv, *p;
  751. int rl_flags = (rl_nulls ? RL_EOL_NULLS : 0);
  752. #ifdef ICONV_OPTION
  753. rl_flags |= (protect_args && ic_recv != (iconv_t)-1 ? RL_CONVERT : 0);
  754. #endif
  755. if (!(argv = new_array(char *, maxargs)))
  756. out_of_memory("read_args");
  757. if (mod_name && !protect_args)
  758. argv[argc++] = "rsyncd";
  759. while (1) {
  760. if (read_line(f_in, buf, bufsiz, rl_flags) == 0)
  761. break;
  762. if (argc == maxargs-1) {
  763. maxargs += MAX_ARGS;
  764. if (!(argv = realloc_array(argv, char *, maxargs)))
  765. out_of_memory("read_args");
  766. }
  767. if (dot_pos) {
  768. if (request_p) {
  769. *request_p = strdup(buf);
  770. request_p = NULL;
  771. }
  772. if (mod_name)
  773. glob_expand_module(mod_name, buf, &argv, &argc, &maxargs);
  774. else
  775. glob_expand(buf, &argv, &argc, &maxargs);
  776. } else {
  777. if (!(p = strdup(buf)))
  778. out_of_memory("read_args");
  779. argv[argc++] = p;
  780. if (*p == '.' && p[1] == '\0')
  781. dot_pos = argc;
  782. }
  783. }
  784. argv[argc] = NULL;
  785. glob_expand(NULL, NULL, NULL, NULL);
  786. *argc_p = argc;
  787. *argv_p = argv;
  788. }
  789. int io_start_buffering_out(int f_out)
  790. {
  791. if (iobuf_out) {
  792. assert(f_out == iobuf_f_out);
  793. return 0;
  794. }
  795. if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
  796. out_of_memory("io_start_buffering_out");
  797. iobuf_out_cnt = 0;
  798. iobuf_f_out = f_out;
  799. return 1;
  800. }
  801. int io_start_buffering_in(int f_in)
  802. {
  803. if (iobuf_in) {
  804. assert(f_in == iobuf_f_in);
  805. return 0;
  806. }
  807. iobuf_in_siz = 2 * IO_BUFFER_SIZE;
  808. if (!(iobuf_in = new_array(char, iobuf_in_siz)))
  809. out_of_memory("io_start_buffering_in");
  810. iobuf_f_in = f_in;
  811. return 1;
  812. }
  813. void io_end_buffering_in(void)
  814. {
  815. if (!iobuf_in)
  816. return;
  817. free(iobuf_in);
  818. iobuf_in = NULL;
  819. iobuf_in_ndx = 0;
  820. iobuf_in_remaining = 0;
  821. iobuf_f_in = -1;
  822. }
  823. void io_end_buffering_out(void)
  824. {
  825. if (!iobuf_out)
  826. return;
  827. io_flush(FULL_FLUSH);
  828. free(iobuf_out);
  829. iobuf_out = NULL;
  830. iobuf_f_out = -1;
  831. }
  832. void maybe_flush_socket(int important)
  833. {
  834. if (iobuf_out && iobuf_out_cnt
  835. && (important || time(NULL) - last_io_out >= 5))
  836. io_flush(NORMAL_FLUSH);
  837. }
  838. void maybe_send_keepalive(void)
  839. {
  840. if (time(NULL) - last_io_out >= allowed_lull) {
  841. if (!iobuf_out || !iobuf_out_cnt) {
  842. if (protocol_version < 29)
  843. send_msg(MSG_DATA, "", 0, 0);
  844. else if (protocol_version >= 30)
  845. send_msg(MSG_NOOP, "", 0, 0);
  846. else {
  847. write_int(sock_f_out, cur_flist->used);
  848. write_shortint(sock_f_out, ITEM_IS_NEW);
  849. }
  850. }
  851. if (iobuf_out)
  852. io_flush(NORMAL_FLUSH);
  853. }
  854. }
  855. void start_flist_forward(int f_in)
  856. {
  857. assert(iobuf_out != NULL);
  858. assert(iobuf_f_out == msg_fd_out);
  859. flist_forward_from = f_in;
  860. defer_forwarding_messages++;
  861. }
  862. void stop_flist_forward(void)
  863. {
  864. flist_forward_from = -1;
  865. defer_forwarding_messages--;
  866. io_flush(FULL_FLUSH);
  867. }
  868. /**
  869. * Continue trying to read len bytes - don't return until len has been
  870. * read.
  871. **/
  872. static void read_loop(int fd, char *buf, size_t len)
  873. {
  874. while (len) {
  875. int n = read_timeout(fd, buf, len);
  876. buf += n;
  877. len -= n;
  878. }
  879. }
  880. /**
  881. * Read from the file descriptor handling multiplexing - return number
  882. * of bytes read.
  883. *
  884. * Never returns <= 0.
  885. */
  886. static int readfd_unbuffered(int fd, char *buf, size_t len)
  887. {
  888. size_t msg_bytes;
  889. int tag, cnt = 0;
  890. char line[BIGPATHBUFLEN];
  891. if (!iobuf_in || fd != iobuf_f_in)
  892. return read_timeout(fd, buf, len);
  893. if (!io_multiplexing_in && iobuf_in_remaining == 0) {
  894. iobuf_in_remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
  895. iobuf_in_ndx = 0;
  896. }
  897. while (cnt == 0) {
  898. if (iobuf_in_remaining) {
  899. len = MIN(len, iobuf_in_remaining);
  900. memcpy(buf, iobuf_in + iobuf_in_ndx, len);
  901. iobuf_in_ndx += len;
  902. iobuf_in_remaining -= len;
  903. cnt = len;
  904. break;
  905. }
  906. read_loop(fd, line, 4);
  907. tag = IVAL(line, 0);
  908. msg_bytes = tag & 0xFFFFFF;
  909. tag = (tag >> 24) - MPLEX_BASE;
  910. switch (tag) {
  911. case MSG_DATA:
  912. if (msg_bytes > iobuf_in_siz) {
  913. if (!(iobuf_in = realloc_array(iobuf_in, char,
  914. msg_bytes)))
  915. out_of_memory("readfd_unbuffered");
  916. iobuf_in_siz = msg_bytes;
  917. }
  918. read_loop(fd, iobuf_in, msg_bytes);
  919. iobuf_in_remaining = msg_bytes;
  920. iobuf_in_ndx = 0;
  921. break;
  922. case MSG_NOOP:
  923. if (msg_bytes != 0)
  924. goto invalid_msg;
  925. if (am_sender)
  926. maybe_send_keepalive();
  927. break;
  928. case MSG_IO_ERROR:
  929. if (msg_bytes != 4)
  930. goto invalid_msg;
  931. read_loop(fd, line, msg_bytes);
  932. send_msg_int(MSG_IO_ERROR, IVAL(line, 0));
  933. io_error |= IVAL(line, 0);
  934. break;
  935. case MSG_DELETED:
  936. if (msg_bytes >= sizeof line)
  937. goto overflow;
  938. #ifdef ICONV_OPTION
  939. if (ic_recv != (iconv_t)-1) {
  940. xbuf outbuf, inbuf;
  941. char ibuf[512];
  942. int add_null = 0;
  943. int pos = 0;
  944. INIT_CONST_XBUF(outbuf, line);
  945. INIT_XBUF(inbuf, ibuf, 0, -1);
  946. while (msg_bytes) {
  947. inbuf.len = msg_bytes > sizeof ibuf
  948. ? sizeof ibuf : msg_bytes;
  949. read_loop(fd, inbuf.buf, inbuf.len);
  950. if (!(msg_bytes -= inbuf.len)
  951. && !ibuf[inbuf.len-1])
  952. inbuf.len--, add_null = 1;
  953. if (iconvbufs(ic_send, &inbuf, &outbuf,
  954. ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE) < 0)
  955. goto overflow;
  956. pos = -1;
  957. }
  958. if (add_null) {
  959. if (outbuf.len == outbuf.size)
  960. goto overflow;
  961. outbuf.buf[outbuf.len++] = '\0';
  962. }
  963. msg_bytes = outbuf.len;
  964. } else
  965. #endif
  966. read_loop(fd, line, msg_bytes);
  967. /* A directory name was sent with the trailing null */
  968. if (msg_bytes > 0 && !line[msg_bytes-1])
  969. log_delete(line, S_IFDIR);
  970. else {
  971. line[msg_bytes] = '\0';
  972. log_delete(line, S_IFREG);
  973. }
  974. break;
  975. case MSG_SUCCESS:
  976. if (msg_bytes != 4) {
  977. invalid_msg:
  978. rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n",
  979. tag, (long)msg_bytes, who_am_i());
  980. exit_cleanup(RERR_STREAMIO);
  981. }
  982. read_loop(fd, line, msg_bytes);
  983. successful_send(IVAL(line, 0));
  984. break;
  985. case MSG_NO_SEND:
  986. if (msg_bytes != 4)
  987. goto invalid_msg;
  988. read_loop(fd, line, msg_bytes);
  989. send_msg_int(MSG_NO_SEND, IVAL(line, 0));
  990. break;
  991. case MSG_INFO:
  992. case MSG_ERROR:
  993. case MSG_ERROR_XFER:
  994. case MSG_WARNING:
  995. if (msg_bytes >= sizeof line) {
  996. overflow:
  997. rprintf(FERROR,
  998. "multiplexing overflow %d:%ld [%s]\n",
  999. tag, (long)msg_bytes, who_am_i());
  1000. exit_cleanup(RERR_STREAMIO);
  1001. }
  1002. read_loop(fd, line, msg_bytes);
  1003. rwrite((enum logcode)tag, line, msg_bytes, 1);
  1004. if (first_message) {
  1005. if (list_only && !am_sender && tag == 1) {
  1006. line[msg_bytes] = '\0';
  1007. check_for_d_option_error(line);
  1008. }
  1009. first_message = 0;
  1010. }
  1011. break;
  1012. default:
  1013. rprintf(FERROR, "unexpected tag %d [%s]\n",
  1014. tag, who_am_i());
  1015. exit_cleanup(RERR_STREAMIO);
  1016. }
  1017. }
  1018. if (iobuf_in_remaining == 0)
  1019. io_flush(NORMAL_FLUSH);
  1020. return cnt;
  1021. }
  1022. /* Do a buffered read from fd. Don't return until all N bytes have
  1023. * been read. If all N can't be read then exit with an error. */
  1024. static void readfd(int fd, char *buffer, size_t N)
  1025. {
  1026. int cnt;
  1027. size_t total = 0;
  1028. while (total < N) {
  1029. cnt = readfd_unbuffered(fd, buffer + total, N-total);
  1030. total += cnt;
  1031. }
  1032. if (fd == write_batch_monitor_in) {
  1033. if ((size_t)write(batch_fd, buffer, total) != total)
  1034. exit_cleanup(RERR_FILEIO);
  1035. }
  1036. if (fd == flist_forward_from)
  1037. writefd(iobuf_f_out, buffer, total);
  1038. if (fd == sock_f_in)
  1039. stats.total_read += total;
  1040. }
  1041. unsigned short read_shortint(int f)
  1042. {
  1043. char b[2];
  1044. readfd(f, b, 2);
  1045. return (UVAL(b, 1) << 8) + UVAL(b, 0);
  1046. }
  1047. int32 read_int(int f)
  1048. {
  1049. char b[4];
  1050. int32 num;
  1051. readfd(f, b, 4);
  1052. num = IVAL(b, 0);
  1053. #if SIZEOF_INT32 > 4
  1054. if (num & (int32)0x80000000)
  1055. num |= ~(int32)0xffffffff;
  1056. #endif
  1057. return num;
  1058. }
  1059. int32 read_varint(int f)
  1060. {
  1061. union {
  1062. char b[5];
  1063. int32 x;
  1064. } u;
  1065. uchar ch;
  1066. int extra;
  1067. u.x = 0;
  1068. readfd(f, (char*)&ch, 1);
  1069. extra = int_byte_extra[ch / 4];
  1070. if (extra) {
  1071. uchar bit = ((uchar)1<<(8-extra));
  1072. if (extra >= (int)sizeof u.b) {
  1073. rprintf(FERROR, "Overflow in read_varint()\n");
  1074. exit_cleanup(RERR_STREAMIO);
  1075. }
  1076. readfd(f, u.b, extra);
  1077. u.b[extra] = ch & (bit-1);
  1078. } else
  1079. u.b[0] = ch;
  1080. #if CAREFUL_ALIGNMENT
  1081. u.x = IVAL(u.b,0);
  1082. #endif
  1083. #if SIZEOF_INT32 > 4
  1084. if (u.x & (int32)0x80000000)
  1085. u.x |= ~(int32)0xffffffff;
  1086. #endif
  1087. return u.x;
  1088. }
  1089. int64 read_varlong(int f, uchar min_bytes)
  1090. {
  1091. union {
  1092. char b[9];
  1093. int64 x;
  1094. } u;
  1095. char b2[8];
  1096. int extra;
  1097. #if SIZEOF_INT64 < 8
  1098. memset(u.b, 0, 8);
  1099. #else
  1100. u.x = 0;
  1101. #endif
  1102. readfd(f, b2, min_bytes);
  1103. memcpy(u.b, b2+1, min_bytes-1);
  1104. extra = int_byte_extra[CVAL(b2, 0) / 4];
  1105. if (extra) {
  1106. uchar bit = ((uchar)1<<(8-extra));
  1107. if (min_bytes + extra > (int)sizeof u.b) {
  1108. rprintf(FERROR, "Overflow in read_varlong()\n");
  1109. exit_cleanup(RERR_STREAMIO);
  1110. }
  1111. readfd(f, u.b + min_bytes - 1, extra);
  1112. u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1);
  1113. #if SIZEOF_INT64 < 8
  1114. if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) {
  1115. rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
  1116. exit_cleanup(RERR_UNSUPPORTED);
  1117. }
  1118. #endif
  1119. } else
  1120. u.b[min_bytes + extra - 1] = CVAL(b2, 0);
  1121. #if SIZEOF_INT64 < 8
  1122. u.x = IVAL(u.b,0);
  1123. #elif CAREFUL_ALIGNMENT
  1124. u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32);
  1125. #endif
  1126. return u.x;
  1127. }
  1128. int64 read_longint(int f)
  1129. {
  1130. #if SIZEOF_INT64 >= 8
  1131. char b[9];
  1132. #endif
  1133. int32 num = read_int(f);
  1134. if (num != (int32)0xffffffff)
  1135. return num;
  1136. #if SIZEOF_INT64 < 8
  1137. rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
  1138. exit_cleanup(RERR_UNSUPPORTED);
  1139. #else
  1140. readfd(f, b, 8);
  1141. return IVAL(b,0) | (((int64)IVAL(b,4))<<32);
  1142. #endif
  1143. }
  1144. void read_buf(int f, char *buf, size_t len)
  1145. {
  1146. readfd(f,buf,len);
  1147. }
  1148. void read_sbuf(int f, char *buf, size_t len)
  1149. {
  1150. readfd(f, buf, len);
  1151. buf[len] = '\0';
  1152. }
  1153. uchar read_byte(int f)
  1154. {
  1155. uchar c;
  1156. readfd(f, (char *)&c, 1);
  1157. return c;
  1158. }
  1159. int read_vstring(int f, char *buf, int bufsize)
  1160. {
  1161. int len = read_byte(f);
  1162. if (len & 0x80)
  1163. len = (len & ~0x80) * 0x100 + read_byte(f);
  1164. if (len >= bufsize) {
  1165. rprintf(FERROR, "over-long vstring received (%d > %d)\n",
  1166. len, bufsize - 1);
  1167. return -1;
  1168. }
  1169. if (len)
  1170. readfd(f, buf, len);
  1171. buf[len] = '\0';
  1172. return len;
  1173. }
  1174. /* Populate a sum_struct with values from the socket. This is
  1175. * called by both the sender and the receiver. */
  1176. void read_sum_head(int f, struct sum_struct *sum)
  1177. {
  1178. int32 max_blength = protocol_version < 30 ? OLD_MAX_BLOCK_SIZE : MAX_BLOCK_SIZE;
  1179. sum->count = read_int(f);
  1180. if (sum->count < 0) {
  1181. rprintf(FERROR, "Invalid checksum count %ld [%s]\n",
  1182. (long)sum->count, who_am_i());
  1183. exit_cleanup(RERR_PROTOCOL);
  1184. }
  1185. sum->blength = read_int(f);
  1186. if (sum->blength < 0 || sum->blength > max_blength) {
  1187. rprintf(FERROR, "Invalid block length %ld [%s]\n",
  1188. (long)sum->blength, who_am_i());
  1189. exit_cleanup(RERR_PROTOCOL);
  1190. }
  1191. sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
  1192. if (sum->s2length < 0 || sum->s2length > MAX_DIGEST_LEN) {
  1193. rprintf(FERROR, "Invalid checksum length %d [%s]\n",
  1194. sum->s2length, who_am_i());
  1195. exit_cleanup(RERR_PROTOCOL);
  1196. }
  1197. sum->remainder = read_int(f);
  1198. if (sum->remainder < 0 || sum->remainder > sum->blength) {
  1199. rprintf(FERROR, "Invalid remainder length %ld [%s]\n",
  1200. (long)sum->remainder, who_am_i());
  1201. exit_cleanup(RERR_PROTOCOL);
  1202. }
  1203. }
  1204. /* Send the values from a sum_struct over the socket. Set sum to
  1205. * NULL if there are no checksums to send. This is called by both
  1206. * the generator and the sender. */
  1207. void write_sum_head(int f, struct sum_struct *sum)
  1208. {
  1209. static struct sum_struct null_sum;
  1210. if (sum == NULL)
  1211. sum = &null_sum;
  1212. write_int(f, sum->count);
  1213. write_int(f, sum->blength);
  1214. if (protocol_version >= 27)
  1215. write_int(f, sum->s2length);
  1216. write_int(f, sum->remainder);
  1217. }
  1218. /**
  1219. * Sleep after writing to limit I/O bandwidth usage.
  1220. *
  1221. * @todo Rather than sleeping after each write, it might be better to
  1222. * use some kind of averaging. The current algorithm seems to always
  1223. * use a bit less bandwidth than specified, because it doesn't make up
  1224. * for slow periods. But arguably this is a feature. In addition, we
  1225. * ought to take the time used to write the data into account.
  1226. *
  1227. * During some phases of big transfers (file FOO is uptodate) this is
  1228. * called with a small bytes_written every time. As the kernel has to
  1229. * round small waits up to guarantee that we actually wait at least the
  1230. * requested number of microseconds, this can become grossly inaccurate.
  1231. * We therefore keep track of the bytes we've written over time and only
  1232. * sleep when the accumulated delay is at least 1 tenth of a second.
  1233. **/
  1234. static void sleep_for_bwlimit(int bytes_written)
  1235. {
  1236. static struct timeval prior_tv;
  1237. static long total_written = 0;
  1238. struct timeval tv, start_tv;
  1239. long elapsed_usec, sleep_usec;
  1240. #define ONE_SEC 1000000L /* # of microseconds in a second */
  1241. if (!bwlimit_writemax)
  1242. return;
  1243. total_written += bytes_written;
  1244. gettimeofday(&start_tv, NULL);
  1245. if (prior_tv.tv_sec) {
  1246. elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
  1247. + (start_tv.tv_usec - prior_tv.tv_usec);
  1248. total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
  1249. if (total_written < 0)
  1250. total_written = 0;
  1251. }
  1252. sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
  1253. if (sleep_usec < ONE_SEC / 10) {
  1254. prior_tv = start_tv;
  1255. return;
  1256. }
  1257. tv.tv_sec = sleep_usec / ONE_SEC;
  1258. tv.tv_usec = sleep_usec % ONE_SEC;
  1259. select(0, NULL, NULL, NULL, &tv);
  1260. gettimeofday(&prior_tv, NULL);
  1261. elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
  1262. + (prior_tv.tv_usec - start_tv.tv_usec);
  1263. total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
  1264. }
  1265. static const char *what_fd_is(int fd)
  1266. {
  1267. static char buf[20];
  1268. if (fd == sock_f_out)
  1269. return "socket";
  1270. else if (fd == msg_fd_out)
  1271. return "message fd";
  1272. else if (fd == batch_fd)
  1273. return "batch file";
  1274. else {
  1275. snprintf(buf, sizeof buf, "fd %d", fd);
  1276. return buf;
  1277. }
  1278. }
  1279. /* Write len bytes to the file descriptor fd, looping as necessary to get
  1280. * the job done and also (in certain circumstances) reading any data on
  1281. * msg_fd_in to avoid deadlock.
  1282. *
  1283. * This function underlies the multiplexing system. The body of the
  1284. * application never calls this function directly. */
  1285. static void writefd_unbuffered(int fd, const char *buf, size_t len)
  1286. {
  1287. size_t n, total = 0;
  1288. fd_set w_fds, r_fds, e_fds;
  1289. int maxfd, count, cnt, using_r_fds;
  1290. int defer_inc = 0;
  1291. struct timeval tv;
  1292. if (no_flush++)
  1293. defer_forwarding_messages++, defer_inc++;
  1294. while (total < len) {
  1295. FD_ZERO(&w_fds);
  1296. FD_SET(fd, &w_fds);
  1297. FD_ZERO(&e_fds);
  1298. FD_SET(fd, &e_fds);
  1299. maxfd = fd;
  1300. if (msg_fd_in >= 0) {
  1301. FD_ZERO(&r_fds);
  1302. FD_SET(msg_fd_in, &r_fds);
  1303. if (msg_fd_in > maxfd)
  1304. maxfd = msg_fd_in;
  1305. using_r_fds = 1;
  1306. } else
  1307. using_r_fds = 0;
  1308. tv.tv_sec = select_timeout;
  1309. tv.tv_usec = 0;
  1310. errno = 0;
  1311. count = select(maxfd + 1, using_r_fds ? &r_fds : NULL,
  1312. &w_fds, &e_fds, &tv);
  1313. if (count <= 0) {
  1314. if (count < 0 && errno == EBADF)
  1315. exit_cleanup(RERR_SOCKETIO);
  1316. check_timeout();
  1317. continue;
  1318. }
  1319. /*if (FD_ISSET(fd, &e_fds))
  1320. rprintf(FINFO, "select exception on fd %d\n", fd); */
  1321. if (using_r_fds && FD_ISSET(msg_fd_in, &r_fds))
  1322. read_msg_fd();
  1323. if (!FD_ISSET(fd, &w_fds))
  1324. continue;
  1325. n = len - total;
  1326. if (bwlimit_writemax && n > bwlimit_writemax)
  1327. n = bwlimit_writemax;
  1328. cnt = write(fd, buf + total, n);
  1329. if (cnt <= 0) {
  1330. if (cnt < 0) {
  1331. if (errno == EINTR)
  1332. continue;
  1333. if (errno == EWOULDBLOCK || errno == EAGAIN) {
  1334. msleep(1);
  1335. continue;
  1336. }
  1337. }
  1338. /* Don't try to write errors back across the stream. */
  1339. if (fd == sock_f_out)
  1340. io_end_multiplex_out();
  1341. /* Don't try to write errors down a failing msg pipe. */
  1342. if (am_server && fd == msg_fd_out)
  1343. exit_cleanup(RERR_STREAMIO);
  1344. rsyserr(FERROR, errno,
  1345. "writefd_unbuffered failed to write %ld bytes to %s [%s]",
  1346. (long)len, what_fd_is(fd), who_am_i());
  1347. /* If the other side is sending us error messages, try
  1348. * to grab any messages they sent before they died. */
  1349. while (!am_server && fd == sock_f_out && io_multiplexing_in) {
  1350. char buf[1024];
  1351. set_io_timeout(30);
  1352. ignore_timeout = 0;
  1353. readfd_unbuffered(sock_f_in, buf, sizeof buf);
  1354. }
  1355. exit_cleanup(RERR_STREAMIO);
  1356. }
  1357. total += cnt;
  1358. defer_forwarding_messages++, defer_inc++;
  1359. if (fd == sock_f_out) {
  1360. if (io_timeout || am_generator)
  1361. last_io_out = time(NULL);
  1362. sleep_for_bwlimit(cnt);
  1363. }
  1364. }
  1365. no_flush--;
  1366. if (keep_defer_forwarding)
  1367. defer_inc--;
  1368. if (!(defer_forwarding_messages -= defer_inc) && !no_flush)
  1369. msg_flush();
  1370. }
  1371. int io_flush(int flush_it_all)
  1372. {
  1373. int flushed_something = 0;
  1374. if (no_flush)
  1375. return 0;
  1376. if (iobuf_out_cnt) {
  1377. if (io_multiplexing_out)
  1378. mplex_write(sock_f_out, MSG_DATA, iobuf_out, iobuf_out_cnt, 0);
  1379. else
  1380. writefd_unbuffered(iobuf_f_out, iobuf_out, iobuf_out_cnt);
  1381. iobuf_out_cnt = 0;
  1382. flushed_something = 1;
  1383. }
  1384. if (flush_it_all && !defer_forwarding_messages && msg_queue.head) {
  1385. msg_flush();
  1386. flushed_something = 1;
  1387. }
  1388. return flushed_something;
  1389. }
  1390. static void writefd(int fd, const char *buf, size_t len)
  1391. {
  1392. if (fd == sock_f_out)
  1393. stats.total_written += len;
  1394. if (fd == write_batch_monitor_out)
  1395. writefd_unbuffered(batch_fd, buf, len);
  1396. if (!iobuf_out || fd != iobuf_f_out) {
  1397. writefd_unbuffered(fd, buf, len);
  1398. return;
  1399. }
  1400. while (len) {
  1401. int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
  1402. if (n > 0) {
  1403. memcpy(iobuf_out+iobuf_out_cnt, buf, n);
  1404. buf += n;
  1405. len -= n;
  1406. iobuf_out_cnt += n;
  1407. }
  1408. if (iobuf_out_cnt == IO_BUFFER_SIZE)
  1409. io_flush(NORMAL_FLUSH);
  1410. }
  1411. }
  1412. void write_shortint(int f, unsigned short x)
  1413. {
  1414. char b[2];
  1415. b[0] = (char)x;
  1416. b[1] = (char)(x >> 8);
  1417. writefd(f, b, 2);
  1418. }
  1419. void write_int(int f, int32 x)
  1420. {
  1421. char b[4];
  1422. SIVAL(b, 0, x);
  1423. writefd(f, b, 4);
  1424. }
  1425. void write_varint(int f, int32 x)
  1426. {
  1427. char b[5];
  1428. uchar bit;
  1429. int cnt = 4;
  1430. SIVAL(b, 1, x);
  1431. while (cnt > 1 && b[cnt] == 0)
  1432. cnt--;
  1433. bit = ((uchar)1<<(7-cnt+1));
  1434. if (CVAL(b, cnt) >= bit) {
  1435. cnt++;
  1436. *b = ~(bit-1);
  1437. } else if (cnt > 1)
  1438. *b = b[cnt] | ~(bit*2-1);
  1439. else
  1440. *b = b[cnt];
  1441. writefd(f, b, cnt);
  1442. }
  1443. void write_varlong(int f, int64 x, uchar min_bytes)
  1444. {
  1445. char b[9];
  1446. uchar bit;
  1447. int cnt = 8;
  1448. SIVAL(b, 1, x);
  1449. #if SIZEOF_INT64 >= 8
  1450. SIVAL(b, 5, x >> 32);
  1451. #else
  1452. if (x <= 0x7FFFFFFF && x >= 0)
  1453. memset(b + 5, 0, 4);
  1454. else {
  1455. rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
  1456. exit_cleanup(RERR_UNSUPPORTED);
  1457. }
  1458. #endif
  1459. while (cnt > min_bytes && b[cnt] == 0)
  1460. cnt--;
  1461. bit = ((uchar)1<<(7-cnt+min_bytes));
  1462. if (CVAL(b, cnt) >= bit) {
  1463. cnt++;
  1464. *b = ~(bit-1);
  1465. } else if (cnt > min_bytes)
  1466. *b = b[cnt] | ~(bit*2-1);
  1467. else
  1468. *b = b[cnt];
  1469. writefd(f, b, cnt);
  1470. }
  1471. /*
  1472. * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
  1473. * 64-bit types on this platform.
  1474. */
  1475. void write_longint(int f, int64 x)
  1476. {
  1477. char b[12], * const s = b+4;
  1478. SIVAL(s, 0, x);
  1479. if (x <= 0x7FFFFFFF && x >= 0) {
  1480. writefd(f, s, 4);
  1481. return;
  1482. }
  1483. #if SIZEOF_INT64 < 8
  1484. rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
  1485. exit_cleanup(RERR_UNSUPPORTED);
  1486. #else
  1487. memset(b, 0xFF, 4);
  1488. SIVAL(s, 4, x >> 32);
  1489. writefd(f, b, 12);
  1490. #endif
  1491. }
  1492. void write_buf(int f, const char *buf, size_t len)
  1493. {
  1494. writefd(f,buf,len);
  1495. }
  1496. /** Write a string to the connection */
  1497. void write_sbuf(int f, const char *buf)
  1498. {
  1499. writefd(f, buf, strlen(buf));
  1500. }
  1501. void write_byte(int f, uchar c)
  1502. {
  1503. writefd(f, (char *)&c, 1);
  1504. }
  1505. void write_vstring(int f, const char *str, int len)
  1506. {
  1507. uchar lenbuf[3], *lb = lenbuf;
  1508. if (len > 0x7F) {
  1509. if (len > 0x7FFF) {
  1510. rprintf(FERROR,
  1511. "attempting to send over-long vstring (%d > %d)\n",
  1512. len, 0x7FFF);
  1513. exit_cleanup(RERR_PROTOCOL);
  1514. }
  1515. *lb++ = len / 0x100 + 0x80;
  1516. }
  1517. *lb = len;
  1518. writefd(f, (char*)lenbuf, lb - lenbuf + 1);
  1519. if (len)
  1520. writefd(f, str, len);
  1521. }
  1522. /* Send a file-list index using a byte-reduction method. */
  1523. void write_ndx(int f, int32 ndx)
  1524. {
  1525. static int32 prev_positive = -1, prev_negative = 1;
  1526. int32 diff, cnt = 0;
  1527. char b[6];
  1528. if (protocol_version < 30 || read_batch) {
  1529. write_int(f, ndx);
  1530. return;
  1531. }
  1532. /* Send NDX_DONE as a single-byte 0 with no side effects. Send
  1533. * negative nums as a positive after sending a leading 0xFF. */
  1534. if (ndx >= 0) {
  1535. diff = ndx - prev_positive;
  1536. prev_positive = ndx;
  1537. } else if (ndx == NDX_DONE) {
  1538. *b = 0;
  1539. writefd(f, b, 1);
  1540. return;
  1541. } else {
  1542. b[cnt++] = (char)0xFF;
  1543. ndx = -ndx;
  1544. diff = ndx - prev_negative;
  1545. prev_negative = ndx;
  1546. }
  1547. /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767
  1548. * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE
  1549. * & all 4 bytes of the (non-negative) num with the high-bit set. */
  1550. if (diff < 0xFE && diff > 0)
  1551. b[cnt++] = (char)diff;
  1552. else if (diff < 0 || diff > 0x7FFF) {
  1553. b[cnt++] = (char)0xFE;
  1554. b[cnt++] = (char)((ndx >> 24) | 0x80);
  1555. b[cnt++] = (char)ndx;
  1556. b[cnt++] = (char)(ndx >> 8);
  1557. b[cnt++] = (char)(ndx >> 16);
  1558. } else {
  1559. b[cnt++] = (char)0xFE;
  1560. b[cnt++] = (char)(diff >> 8);
  1561. b[cnt++] = (char)diff;
  1562. }
  1563. writefd(f, b, cnt);
  1564. }
  1565. /* Receive a file-list index using a byte-reduction method. */
  1566. int32 read_ndx(int f)
  1567. {
  1568. static int32 prev_positive = -1, prev_negative = 1;
  1569. int32 *prev_ptr, num;
  1570. char b[4];
  1571. if (protocol_version < 30)
  1572. return read_int(f);
  1573. readfd(f, b, 1);
  1574. if (CVAL(b, 0) == 0xFF) {
  1575. readfd(f, b, 1);
  1576. prev_ptr = &prev_negative;
  1577. } else if (CVAL(b, 0) == 0)
  1578. return NDX_DONE;
  1579. else
  1580. prev_ptr = &prev_positive;
  1581. if (CVAL(b, 0) == 0xFE) {
  1582. readfd(f, b, 2);
  1583. if (CVAL(b, 0) & 0x80) {
  1584. b[3] = CVAL(b, 0) & ~0x80;
  1585. b[0] = b[1];
  1586. readfd(f, b+1, 2);
  1587. num = IVAL(b, 0);
  1588. } else
  1589. num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr;
  1590. } else
  1591. num = UVAL(b, 0) + *prev_ptr;
  1592. *prev_ptr = num;
  1593. if (prev_ptr == &prev_negative)
  1594. num = -num;
  1595. return num;
  1596. }
  1597. /* Read a line of up to bufsiz-1 characters into buf. Strips
  1598. * the (required) trailing newline and all carriage returns.
  1599. * Returns 1 for success; 0 for I/O error or truncation. */
  1600. int read_line_old(int f, char *buf, size_t bufsiz)
  1601. {
  1602. bufsiz--; /* leave room for the null */
  1603. while (bufsiz > 0) {
  1604. buf[0] = 0;
  1605. read_buf(f, buf, 1);
  1606. if (buf[0] == 0)
  1607. return 0;
  1608. if (buf[0] == '\n')
  1609. break;
  1610. if (buf[0] != '\r') {
  1611. buf++;
  1612. bufsiz--;
  1613. }
  1614. }
  1615. *buf = '\0';
  1616. return bufsiz > 0;
  1617. }
  1618. void io_printf(int fd, const char *format, ...)
  1619. {
  1620. va_list ap;
  1621. char buf[BIGPATHBUFLEN];
  1622. int len;
  1623. va_start(ap, format);
  1624. len = vsnprintf(buf, sizeof buf, format, ap);
  1625. va_end(ap);
  1626. if (len < 0)
  1627. exit_cleanup(RERR_STREAMIO);
  1628. if (len > (int)sizeof buf) {
  1629. rprintf(FERROR, "io_printf() was too long for the buffer.\n");
  1630. exit_cleanup(RERR_STREAMIO);
  1631. }
  1632. write_sbuf(fd, buf);
  1633. }
  1634. /** Setup for multiplexing a MSG_* stream with the data stream. */
  1635. void io_start_multiplex_out(void)
  1636. {
  1637. io_flush(NORMAL_FLUSH);
  1638. io_start_buffering_out(sock_f_out);
  1639. io_multiplexing_out = 1;
  1640. }
  1641. /** Setup for multiplexing a MSG_* stream with the data stream. */
  1642. void io_start_multiplex_in(void)
  1643. {
  1644. io_flush(NORMAL_FLUSH);
  1645. io_start_buffering_in(sock_f_in);
  1646. io_multiplexing_in = 1;
  1647. }
  1648. /** Write an message to the multiplexed data stream. */
  1649. int io_multiplex_write(enum msgcode code, const char *buf, size_t len, int convert)
  1650. {
  1651. if (!io_multiplexing_out)
  1652. return 0;
  1653. io_flush(NORMAL_FLUSH);
  1654. stats.total_written += (len+4);
  1655. mplex_write(sock_f_out, code, buf, len, convert);
  1656. return 1;
  1657. }
  1658. void io_end_multiplex_in(void)
  1659. {
  1660. io_multiplexing_in = 0;
  1661. io_end_buffering_in();
  1662. }
  1663. /** Stop output multiplexing. */
  1664. void io_end_multiplex_out(void)
  1665. {
  1666. io_multiplexing_out = 0;
  1667. io_end_buffering_out();
  1668. }
  1669. void start_write_batch(int fd)
  1670. {
  1671. /* Some communication has already taken place, but we don't
  1672. * enable batch writing until here so that we can write a
  1673. * canonical record of the communication even though the
  1674. * actual communication so far depends on whether a daemon
  1675. * is involved. */
  1676. write_int(batch_fd, protocol_version);
  1677. if (protocol_version >= 30)
  1678. write_byte(batch_fd, compat_flags);
  1679. write_int(batch_fd, checksum_seed);
  1680. if (am_sender)
  1681. write_batch_monitor_out = fd;
  1682. else
  1683. write_batch_monitor_in = fd;
  1684. }
  1685. void stop_write_batch(void)
  1686. {
  1687. write_batch_monitor_out = -1;
  1688. write_batch_monitor_in = -1;
  1689. }