io.c 45 KB

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