rsync.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * Copyright (C) 1996, 2000 Andrew Tridgell
  3. * Copyright (C) 1996 Paul Mackerras
  4. * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
  5. * Copyright (C) 2003-2008 Wayne Davison
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, visit the http://fsf.org website.
  19. */
  20. #define False 0
  21. #define True 1
  22. #define BLOCK_SIZE 700
  23. #define RSYNC_RSH_ENV "RSYNC_RSH"
  24. #define RSYNC_RSH_IO_ENV "RSYNC_RSH_IO"
  25. #define RSYNC_NAME "rsync"
  26. /* RSYNCD_SYSCONF is now set in config.h */
  27. #define RSYNCD_USERCONF "rsyncd.conf"
  28. #define DEFAULT_LOCK_FILE "/var/run/rsyncd.lock"
  29. #define URL_PREFIX "rsync://"
  30. #define SYMLINK_PREFIX "/rsyncd-munged/" /* This MUST have a trailing slash! */
  31. #define SYMLINK_PREFIX_LEN ((int)sizeof SYMLINK_PREFIX - 1)
  32. #define BACKUP_SUFFIX "~"
  33. /* a non-zero CHAR_OFFSET makes the rolling sum stronger, but is
  34. incompatible with older versions :-( */
  35. #define CHAR_OFFSET 0
  36. /* These flags are only used during the flist transfer. */
  37. #define XMIT_TOP_DIR (1<<0)
  38. #define XMIT_SAME_MODE (1<<1)
  39. #define XMIT_SAME_RDEV_pre28 (1<<2) /* protocols 20 - 27 */
  40. #define XMIT_EXTENDED_FLAGS (1<<2) /* protocols 28 - now */
  41. #define XMIT_SAME_UID (1<<3)
  42. #define XMIT_SAME_GID (1<<4)
  43. #define XMIT_SAME_NAME (1<<5)
  44. #define XMIT_LONG_NAME (1<<6)
  45. #define XMIT_SAME_TIME (1<<7)
  46. #define XMIT_SAME_RDEV_MAJOR (1<<8) /* protocols 28 - now (devices only) */
  47. #define XMIT_NO_CONTENT_DIR (1<<8) /* protocols 30 - now (dirs only) */
  48. #define XMIT_HLINKED (1<<9) /* protocols 28 - now */
  49. #define XMIT_SAME_DEV_pre30 (1<<10) /* protocols 28 - 29 */
  50. #define XMIT_USER_NAME_FOLLOWS (1<<10) /* protocols 30 - now */
  51. #define XMIT_RDEV_MINOR_8_pre30 (1<<11) /* protocols 28 - 29 */
  52. #define XMIT_GROUP_NAME_FOLLOWS (1<<11) /* protocols 30 - now */
  53. #define XMIT_HLINK_FIRST (1<<12) /* protocols 30 - now (HLINKED files only) */
  54. #define XMIT_IO_ERROR_ENDLIST (1<<12) /* protocols 31*- now (w/XMIT_EXTENDED_FLAGS) (also protocol 30 w/'f' compat flag) */
  55. /* These flags are used in the live flist data. */
  56. #define FLAG_TOP_DIR (1<<0) /* sender/receiver/generator */
  57. #define FLAG_OWNED_BY_US (1<<0) /* generator: set by make_file() for aux flists only */
  58. #define FLAG_FILE_SENT (1<<1) /* sender/receiver/generator */
  59. #define FLAG_DIR_CREATED (1<<1) /* generator */
  60. #define FLAG_CONTENT_DIR (1<<2) /* sender/receiver/generator */
  61. #define FLAG_MOUNT_DIR (1<<3) /* sender/generator (dirs only) */
  62. #define FLAG_SKIP_HLINK (1<<3) /* receiver/generator (w/FLAG_HLINKED) */
  63. #define FLAG_DUPLICATE (1<<4) /* sender */
  64. #define FLAG_MISSING_DIR (1<<4) /* generator */
  65. #define FLAG_HLINKED (1<<5) /* receiver/generator (checked on all types) */
  66. #define FLAG_HLINK_FIRST (1<<6) /* receiver/generator (w/FLAG_HLINKED) */
  67. #define FLAG_IMPLIED_DIR (1<<6) /* sender/receiver/generator (dirs only) */
  68. #define FLAG_HLINK_LAST (1<<7) /* receiver/generator */
  69. #define FLAG_HLINK_DONE (1<<8) /* receiver/generator (checked on all types) */
  70. #define FLAG_LENGTH64 (1<<9) /* sender/receiver/generator */
  71. #define FLAG_SKIP_GROUP (1<<10) /* receiver/generator */
  72. #define FLAG_TIME_FAILED (1<<11)/* generator */
  73. /* These flags are passed to functions but not stored. */
  74. #define FLAG_DIVERT_DIRS (1<<16) /* sender, but must be unique */
  75. /* These flags are for get_dirlist(). */
  76. #define GDL_IGNORE_FILTER_RULES (1<<0)
  77. /* Some helper macros for matching bits. */
  78. #define BITS_SET(val,bits) (((val) & (bits)) == (bits))
  79. #define BITS_SETnUNSET(val,onbits,offbits) (((val) & ((onbits)|(offbits))) == (onbits))
  80. #define BITS_EQUAL(b1,b2,mask) (((unsigned)(b1) & (unsigned)(mask)) \
  81. == ((unsigned)(b2) & (unsigned)(mask)))
  82. /* update this if you make incompatible changes */
  83. #define PROTOCOL_VERSION 30
  84. /* This is used when working on a new protocol version in CVS, and should
  85. * be a new non-zero value for each CVS change that affects the protocol.
  86. * It must ALWAYS be 0 when the protocol goes final (and NEVER before)! */
  87. #define SUBPROTOCOL_VERSION 0
  88. /* We refuse to interoperate with versions that are not in this range.
  89. * Note that we assume we'll work with later versions: the onus is on
  90. * people writing them to make sure that they don't send us anything
  91. * we won't understand.
  92. *
  93. * Interoperation with old but supported protocol versions
  94. * should cause a warning to be printed. At a future date
  95. * the old protocol will become the minimum and
  96. * compatibility code removed.
  97. *
  98. * There are two possible explanations for the limit at
  99. * MAX_PROTOCOL_VERSION: either to allow new major-rev versions that
  100. * do not interoperate with us, and (more likely) so that we can
  101. * detect an attempt to connect rsync to a non-rsync server, which is
  102. * unlikely to begin by sending a byte between MIN_PROTOCL_VERSION and
  103. * MAX_PROTOCOL_VERSION. */
  104. #define MIN_PROTOCOL_VERSION 20
  105. #define OLD_PROTOCOL_VERSION 25
  106. #define MAX_PROTOCOL_VERSION 40
  107. #define FILECNT_LOOKAHEAD 1000
  108. #define RSYNC_PORT 873
  109. #define SPARSE_WRITE_SIZE (1024)
  110. #define WRITE_SIZE (32*1024)
  111. #define CHUNK_SIZE (32*1024)
  112. #define MAX_MAP_SIZE (256*1024)
  113. #define IO_BUFFER_SIZE (4092)
  114. #define MAX_BLOCK_SIZE ((int32)1 << 17)
  115. /* For compatibility with older rsyncs */
  116. #define OLD_MAX_BLOCK_SIZE ((int32)1 << 29)
  117. #define IOERR_GENERAL (1<<0) /* For backward compatibility, this must == 1 */
  118. #define IOERR_VANISHED (1<<1)
  119. #define IOERR_DEL_LIMIT (1<<2)
  120. #define MAX_ARGS 1000
  121. #define MAX_BASIS_DIRS 20
  122. #define MAX_SERVER_ARGS (MAX_BASIS_DIRS*2 + 100)
  123. #define MPLEX_BASE 7
  124. #define NO_FILTERS 0
  125. #define SERVER_FILTERS 1
  126. #define ALL_FILTERS 2
  127. #define XFLG_FATAL_ERRORS (1<<0)
  128. #define XFLG_OLD_PREFIXES (1<<1)
  129. #define XFLG_ANCHORED2ABS (1<<2) /* leading slash indicates absolute */
  130. #define XFLG_ABS_IF_SLASH (1<<3) /* leading or interior slash is absolute */
  131. #define XFLG_DIR2WILD3 (1<<4) /* dir/ match gets trailing *** added */
  132. #define ATTRS_REPORT (1<<0)
  133. #define ATTRS_SKIP_MTIME (1<<1)
  134. #define FULL_FLUSH 1
  135. #define NORMAL_FLUSH 0
  136. #define PDIR_CREATE 1
  137. #define PDIR_DELETE 0
  138. /* Note: 0x00 - 0x7F are used for basis_dir[] indexes! */
  139. #define FNAMECMP_BASIS_DIR_LOW 0x00 /* Must remain 0! */
  140. #define FNAMECMP_BASIS_DIR_HIGH 0x7F
  141. #define FNAMECMP_FNAME 0x80
  142. #define FNAMECMP_PARTIAL_DIR 0x81
  143. #define FNAMECMP_BACKUP 0x82
  144. #define FNAMECMP_FUZZY 0x83
  145. /* For use by the itemize_changes code */
  146. #define ITEM_REPORT_ATIME (1<<0)
  147. #define ITEM_REPORT_CHANGE (1<<1)
  148. #define ITEM_REPORT_SIZE (1<<2) /* regular files only */
  149. #define ITEM_REPORT_TIMEFAIL (1<<2) /* symlinks only */
  150. #define ITEM_REPORT_TIME (1<<3)
  151. #define ITEM_REPORT_PERMS (1<<4)
  152. #define ITEM_REPORT_OWNER (1<<5)
  153. #define ITEM_REPORT_GROUP (1<<6)
  154. #define ITEM_REPORT_ACL (1<<7)
  155. #define ITEM_REPORT_XATTR (1<<8)
  156. #define ITEM_BASIS_TYPE_FOLLOWS (1<<11)
  157. #define ITEM_XNAME_FOLLOWS (1<<12)
  158. #define ITEM_IS_NEW (1<<13)
  159. #define ITEM_LOCAL_CHANGE (1<<14)
  160. #define ITEM_TRANSFER (1<<15)
  161. /* These are outside the range of the transmitted flags. */
  162. #define ITEM_MISSING_DATA (1<<16) /* used by log_formatted() */
  163. #define ITEM_DELETED (1<<17) /* used by log_formatted() */
  164. #define ITEM_MATCHED (1<<18) /* used by itemize() */
  165. #define SIGNIFICANT_ITEM_FLAGS (~(\
  166. ITEM_BASIS_TYPE_FOLLOWS | ITEM_XNAME_FOLLOWS | ITEM_LOCAL_CHANGE))
  167. #define CFN_KEEP_DOT_DIRS (1<<0)
  168. #define CFN_KEEP_TRAILING_SLASH (1<<1)
  169. #define CFN_DROP_TRAILING_DOT_DIR (1<<2)
  170. #define CFN_COLLAPSE_DOT_DOT_DIRS (1<<3)
  171. #define SP_DEFAULT 0
  172. #define SP_KEEP_DOT_DIRS (1<<0)
  173. #define CD_NORMAL 0
  174. #define CD_SKIP_CHDIR 1
  175. /* Log-message categories. FLOG only goes to the log file, not the client;
  176. * FCLIENT is the opposite. */
  177. enum logcode {
  178. FNONE=0, /* never sent */
  179. FERROR_XFER=1, FINFO=2, /* sent over socket for any protocol */
  180. FERROR=3, FWARNING=4, /* sent over socket for protocols >= 30 */
  181. FERROR_SOCKET=5, FLOG=6, /* only sent via receiver -> generator pipe */
  182. FERROR_UTF8=8, /* only sent via receiver -> generator pipe */
  183. FCLIENT=7 /* never transmitted (e.g. server converts to FINFO) */
  184. };
  185. /* Messages types that are sent over the message channel. The logcode
  186. * values must all be present here with identical numbers. */
  187. enum msgcode {
  188. MSG_DATA=0, /* raw data on the multiplexed stream */
  189. MSG_ERROR_XFER=FERROR_XFER, MSG_INFO=FINFO, /* remote logging */
  190. MSG_ERROR=FERROR, MSG_WARNING=FWARNING, /* protocol-30 remote logging */
  191. MSG_ERROR_SOCKET=FERROR_SOCKET, /* sibling logging */
  192. MSG_ERROR_UTF8=FERROR_UTF8, /* sibling logging */
  193. MSG_LOG=FLOG, MSG_CLIENT=FCLIENT, /* sibling logging */
  194. MSG_REDO=9, /* reprocess indicated flist index */
  195. MSG_FLIST=20, /* extra file list over sibling socket */
  196. MSG_FLIST_EOF=21,/* we've transmitted all the file lists */
  197. MSG_IO_ERROR=22,/* the sending side had an I/O error */
  198. MSG_NOOP=42, /* a do-nothing message */
  199. MSG_SUCCESS=100,/* successfully updated indicated flist index */
  200. MSG_DELETED=101,/* successfully deleted a file on receiving side */
  201. MSG_NO_SEND=102,/* sender failed to open a file we wanted */
  202. MSG_DONE=86 /* current phase is done */
  203. };
  204. #define NDX_DONE -1
  205. #define NDX_FLIST_EOF -2
  206. #define NDX_FLIST_OFFSET -101
  207. #include "errcode.h"
  208. #include "config.h"
  209. /* The default RSYNC_RSH is always set in config.h. */
  210. #include <stdio.h>
  211. #ifdef HAVE_SYS_TYPES_H
  212. # include <sys/types.h>
  213. #endif
  214. #ifdef HAVE_SYS_STAT_H
  215. # include <sys/stat.h>
  216. #endif
  217. #ifdef STDC_HEADERS
  218. # include <stdlib.h>
  219. # include <stddef.h>
  220. #else
  221. # ifdef HAVE_STDLIB_H
  222. # include <stdlib.h>
  223. # endif
  224. #endif
  225. #ifdef HAVE_STRING_H
  226. # if !defined STDC_HEADERS && defined HAVE_MEMORY_H
  227. # include <memory.h>
  228. # endif
  229. # include <string.h>
  230. #endif
  231. #ifdef HAVE_STRINGS_H
  232. # include <strings.h>
  233. #endif
  234. #ifdef HAVE_INTTYPES_H
  235. # include <inttypes.h>
  236. #endif
  237. #ifdef HAVE_STDINT_H
  238. # include <stdint.h>
  239. #endif
  240. #ifdef HAVE_UNISTD_H
  241. # include <unistd.h>
  242. #endif
  243. #ifdef HAVE_SYS_PARAM_H
  244. #include <sys/param.h>
  245. #endif
  246. #if defined HAVE_MALLOC_H && (defined HAVE_MALLINFO || !defined HAVE_STDLIB_H)
  247. #include <malloc.h>
  248. #endif
  249. #ifdef HAVE_SYS_SOCKET_H
  250. #include <sys/socket.h>
  251. #endif
  252. #ifdef TIME_WITH_SYS_TIME
  253. #include <sys/time.h>
  254. #include <time.h>
  255. #else
  256. #ifdef HAVE_SYS_TIME_H
  257. #include <sys/time.h>
  258. #else
  259. #include <time.h>
  260. #endif
  261. #endif
  262. #ifdef HAVE_FCNTL_H
  263. #include <fcntl.h>
  264. #else
  265. #ifdef HAVE_SYS_FCNTL_H
  266. #include <sys/fcntl.h>
  267. #endif
  268. #endif
  269. #ifdef HAVE_SYS_IOCTL_H
  270. #include <sys/ioctl.h>
  271. #endif
  272. #ifdef HAVE_SYS_FILIO_H
  273. #include <sys/filio.h>
  274. #endif
  275. #include <signal.h>
  276. #ifdef HAVE_SYS_WAIT_H
  277. #include <sys/wait.h>
  278. #endif
  279. #ifdef HAVE_CTYPE_H
  280. #include <ctype.h>
  281. #endif
  282. #ifdef HAVE_GRP_H
  283. #include <grp.h>
  284. #endif
  285. #include <errno.h>
  286. #ifdef HAVE_UTIME_H
  287. #include <utime.h>
  288. #endif
  289. #if defined HAVE_LUTIMES || defined HAVE_UTIMENSAT
  290. #define CAN_SET_SYMLINK_TIMES 1
  291. #endif
  292. #if defined HAVE_LCHOWN || defined CHOWN_MODIFIES_SYMLINK
  293. #define CAN_CHOWN_SYMLINK 1
  294. #endif
  295. #if defined HAVE_LCHMOD || defined HAVE_SETATTRLIST
  296. #define CAN_CHMOD_SYMLINK 1
  297. #endif
  298. #ifdef HAVE_SYS_SELECT_H
  299. #include <sys/select.h>
  300. #endif
  301. #ifdef HAVE_SYS_MODE_H
  302. /* apparently AIX needs this for S_ISLNK */
  303. #ifndef S_ISLNK
  304. #include <sys/mode.h>
  305. #endif
  306. #endif
  307. /* these are needed for the uid/gid mapping code */
  308. #include <pwd.h>
  309. #include <grp.h>
  310. #include <stdarg.h>
  311. #include <netinet/in.h>
  312. #include <arpa/inet.h>
  313. #ifdef HAVE_NETDB_H
  314. #include <netdb.h>
  315. #endif
  316. #include <syslog.h>
  317. #include <sys/file.h>
  318. #ifdef HAVE_DIRENT_H
  319. # include <dirent.h>
  320. #else
  321. # define dirent direct
  322. # ifdef HAVE_SYS_NDIR_H
  323. # include <sys/ndir.h>
  324. # endif
  325. # ifdef HAVE_SYS_DIR_H
  326. # include <sys/dir.h>
  327. # endif
  328. # ifdef HAVE_NDIR_H
  329. # include <ndir.h>
  330. # endif
  331. #endif
  332. #ifdef MAJOR_IN_MKDEV
  333. #include <sys/mkdev.h>
  334. # if !defined makedev && (defined mkdev || defined _WIN32 || defined __WIN32__)
  335. # define makedev mkdev
  336. # endif
  337. #elif defined MAJOR_IN_SYSMACROS
  338. #include <sys/sysmacros.h>
  339. #endif
  340. #ifdef MAKEDEV_TAKES_3_ARGS
  341. #define MAKEDEV(devmajor,devminor) makedev(0,devmajor,devminor)
  342. #else
  343. #define MAKEDEV(devmajor,devminor) makedev(devmajor,devminor)
  344. #endif
  345. #ifdef HAVE_COMPAT_H
  346. #include <compat.h>
  347. #endif
  348. #ifdef HAVE_LIMITS_H
  349. # include <limits.h>
  350. #endif
  351. #if defined USE_ICONV_OPEN && defined HAVE_ICONV_H
  352. #include <iconv.h>
  353. #ifndef ICONV_CONST
  354. #define ICONV_CONST
  355. #endif
  356. #else
  357. #ifdef ICONV_CONST
  358. #undef ICONV_CONST
  359. #endif
  360. #ifdef ICONV_OPTION
  361. #undef ICONV_OPTION
  362. #endif
  363. #ifdef iconv_t
  364. #undef iconv_t
  365. #endif
  366. #define iconv_t int
  367. #endif
  368. #include <assert.h>
  369. #include "lib/pool_alloc.h"
  370. #ifndef HAVE_ID_T
  371. typedef unsigned int id_t;
  372. #endif
  373. #ifndef HAVE_PID_T
  374. typedef int pid_t;
  375. #endif
  376. #ifndef HAVE_MODE_T
  377. typedef unsigned int mode_t;
  378. #endif
  379. #ifndef HAVE_OFF_T
  380. typedef long off_t;
  381. #undef SIZEOF_OFF_T
  382. #define SIZEOF_OFF_T SIZEOF_LONG
  383. #endif
  384. #ifndef HAVE_SIZE_T
  385. typedef unsigned int size_t;
  386. #endif
  387. #define BOOL int
  388. #ifndef uchar
  389. #define uchar unsigned char
  390. #endif
  391. #ifdef SIGNED_CHAR_OK
  392. #define schar signed char
  393. #else
  394. #define schar char
  395. #endif
  396. #ifndef int16
  397. #if SIZEOF_INT16_T == 2
  398. # define int16 int16_t
  399. #else
  400. # define int16 short
  401. #endif
  402. #endif
  403. #ifndef uint16
  404. #if SIZEOF_UINT16_T == 2
  405. # define uint16 uint16_t
  406. #else
  407. # define uint16 unsigned int16
  408. #endif
  409. #endif
  410. /* Find a variable that is either exactly 32-bits or longer.
  411. * If some code depends on 32-bit truncation, it will need to
  412. * take special action in a "#if SIZEOF_INT32 > 4" section. */
  413. #ifndef int32
  414. #if SIZEOF_INT32_T == 4
  415. # define int32 int32_t
  416. # define SIZEOF_INT32 4
  417. #elif SIZEOF_INT == 4
  418. # define int32 int
  419. # define SIZEOF_INT32 4
  420. #elif SIZEOF_LONG == 4
  421. # define int32 long
  422. # define SIZEOF_INT32 4
  423. #elif SIZEOF_SHORT == 4
  424. # define int32 short
  425. # define SIZEOF_INT32 4
  426. #elif SIZEOF_INT > 4
  427. # define int32 int
  428. # define SIZEOF_INT32 SIZEOF_INT
  429. #elif SIZEOF_LONG > 4
  430. # define int32 long
  431. # define SIZEOF_INT32 SIZEOF_LONG
  432. #else
  433. # error Could not find a 32-bit integer variable
  434. #endif
  435. #else
  436. # define SIZEOF_INT32 4
  437. #endif
  438. #ifndef uint32
  439. #if SIZEOF_UINT32_T == 4
  440. # define uint32 uint32_t
  441. #else
  442. # define uint32 unsigned int32
  443. #endif
  444. #endif
  445. #if SIZEOF_OFF_T == 8 || !SIZEOF_OFF64_T || !defined HAVE_STRUCT_STAT64
  446. #define OFF_T off_t
  447. #define STRUCT_STAT struct stat
  448. #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF_T
  449. #else
  450. #define OFF_T off64_t
  451. #define STRUCT_STAT struct stat64
  452. #define USE_STAT64_FUNCS 1
  453. #define SIZEOF_CAPITAL_OFF_T SIZEOF_OFF64_T
  454. #endif
  455. /* CAVEAT: on some systems, int64 will really be a 32-bit integer IFF
  456. * that's the maximum size the file system can handle and there is no
  457. * 64-bit type available. The rsync source must therefore take steps
  458. * to ensure that any code that really requires a 64-bit integer has
  459. * it (e.g. the checksum code uses two 32-bit integers for its 64-bit
  460. * counter). */
  461. #if SIZEOF_INT64_T == 8
  462. # define int64 int64_t
  463. # define SIZEOF_INT64 8
  464. #elif SIZEOF_LONG == 8
  465. # define int64 long
  466. # define SIZEOF_INT64 8
  467. #elif SIZEOF_INT == 8
  468. # define int64 int
  469. # define SIZEOF_INT64 8
  470. #elif SIZEOF_LONG_LONG == 8
  471. # define int64 long long
  472. # define SIZEOF_INT64 8
  473. #elif SIZEOF_OFF64_T == 8
  474. # define int64 off64_t
  475. # define SIZEOF_INT64 8
  476. #elif SIZEOF_OFF_T == 8
  477. # define int64 off_t
  478. # define SIZEOF_INT64 8
  479. #elif SIZEOF_INT > 8
  480. # define int64 int
  481. # define SIZEOF_INT64 SIZEOF_INT
  482. #elif SIZEOF_LONG > 8
  483. # define int64 long
  484. # define SIZEOF_INT64 SIZEOF_LONG
  485. #elif SIZEOF_LONG_LONG > 8
  486. # define int64 long long
  487. # define SIZEOF_INT64 SIZEOF_LONG_LONG
  488. #else
  489. /* As long as it gets... */
  490. # define int64 off_t
  491. # define SIZEOF_INT64 SIZEOF_OFF_T
  492. #endif
  493. struct hashtable {
  494. void *nodes;
  495. int32 size, entries;
  496. uint32 node_size;
  497. int key64;
  498. };
  499. struct ht_int32_node {
  500. void *data;
  501. int32 key;
  502. };
  503. struct ht_int64_node {
  504. void *data;
  505. int64 key;
  506. };
  507. #define HT_NODE(tbl, bkts, i) ((void*)((char*)(bkts) + (i)*(tbl)->node_size))
  508. #define HT_KEY(node, k64) ((k64)? ((struct ht_int64_node*)(node))->key \
  509. : (int64)((struct ht_int32_node*)(node))->key)
  510. #ifndef MIN
  511. #define MIN(a,b) ((a)<(b)?(a):(b))
  512. #endif
  513. #ifndef MAX
  514. #define MAX(a,b) ((a)>(b)?(a):(b))
  515. #endif
  516. #ifndef MAXHOSTNAMELEN
  517. #define MAXHOSTNAMELEN 256
  518. #endif
  519. #define SUM_LENGTH 16
  520. #define SHORT_SUM_LENGTH 2
  521. #define BLOCKSUM_BIAS 10
  522. #ifndef MAXPATHLEN
  523. #define MAXPATHLEN 1024
  524. #endif
  525. /* We want a roomy line buffer that can hold more than MAXPATHLEN,
  526. * and significantly more than an overly short MAXPATHLEN. */
  527. #if MAXPATHLEN < 4096
  528. #define BIGPATHBUFLEN (4096+1024)
  529. #else
  530. #define BIGPATHBUFLEN (MAXPATHLEN+1024)
  531. #endif
  532. #ifndef NAME_MAX
  533. #define NAME_MAX 255
  534. #endif
  535. #ifndef INADDR_NONE
  536. #define INADDR_NONE 0xffffffff
  537. #endif
  538. #ifndef IN_LOOPBACKNET
  539. #define IN_LOOPBACKNET 127
  540. #endif
  541. #if HAVE_UNIXWARE_ACLS|HAVE_SOLARIS_ACLS|HAVE_HPUX_ACLS
  542. #define ACLS_NEED_MASK 1
  543. #endif
  544. union file_extras {
  545. int32 num;
  546. uint32 unum;
  547. };
  548. struct file_struct {
  549. const char *dirname; /* The dir info inside the transfer */
  550. time_t modtime; /* When the item was last modified */
  551. uint32 len32; /* Lowest 32 bits of the file's length */
  552. uint16 mode; /* The item's type and permissions */
  553. uint16 flags; /* The FLAG_* bits for this item */
  554. const char basename[1]; /* The basename (AKA filename) follows */
  555. };
  556. extern int file_extra_cnt;
  557. extern int inc_recurse;
  558. extern int uid_ndx;
  559. extern int gid_ndx;
  560. extern int acls_ndx;
  561. extern int xattrs_ndx;
  562. #define FILE_STRUCT_LEN (offsetof(struct file_struct, basename))
  563. #define EXTRA_LEN (sizeof (union file_extras))
  564. #define PTR_EXTRA_CNT ((sizeof (char *) + EXTRA_LEN - 1) / EXTRA_LEN)
  565. #define DEV_EXTRA_CNT 2
  566. #define DIRNODE_EXTRA_CNT 3
  567. #define SUM_EXTRA_CNT ((MAX_DIGEST_LEN + EXTRA_LEN - 1) / EXTRA_LEN)
  568. #define REQ_EXTRA(f,ndx) ((union file_extras*)(f) - (ndx))
  569. #define OPT_EXTRA(f,bump) ((union file_extras*)(f) - file_extra_cnt - 1 - (bump))
  570. #define LEN64_BUMP(f) ((f)->flags & FLAG_LENGTH64 ? 1 : 0)
  571. #define HLINK_BUMP(f) ((f)->flags & (FLAG_HLINKED|FLAG_HLINK_DONE) ? inc_recurse+1 : 0)
  572. #define ACL_BUMP(f) (acls_ndx ? 1 : 0)
  573. /* The length applies to all items. */
  574. #if SIZEOF_INT64 < 8
  575. #define F_LENGTH(f) ((int64)(f)->len32)
  576. #else
  577. #define F_LENGTH(f) ((int64)(f)->len32 + ((f)->flags & FLAG_LENGTH64 \
  578. ? (int64)OPT_EXTRA(f, 0)->unum << 32 : 0))
  579. #endif
  580. /* If there is a symlink string, it is always right after the basename */
  581. #define F_SYMLINK(f) ((f)->basename + strlen((f)->basename) + 1)
  582. /* The sending side always has this available: */
  583. #define F_PATHNAME(f) (*(const char**)REQ_EXTRA(f, PTR_EXTRA_CNT))
  584. /* The receiving side always has this available: */
  585. #define F_DEPTH(f) REQ_EXTRA(f, 1)->num
  586. /* When the associated option is on, all entries will have these present: */
  587. #define F_OWNER(f) REQ_EXTRA(f, uid_ndx)->unum
  588. #define F_GROUP(f) REQ_EXTRA(f, gid_ndx)->unum
  589. #define F_ACL(f) REQ_EXTRA(f, acls_ndx)->num
  590. #define F_XATTR(f) REQ_EXTRA(f, xattrs_ndx)->num
  591. #define F_NDX(f) REQ_EXTRA(f, unsort_ndx)->num
  592. /* These items are per-entry optional: */
  593. #define F_HL_GNUM(f) OPT_EXTRA(f, LEN64_BUMP(f))->num /* non-dirs */
  594. #define F_HL_PREV(f) OPT_EXTRA(f, LEN64_BUMP(f)+inc_recurse)->num /* non-dirs */
  595. #define F_DIR_NODE_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) \
  596. + DIRNODE_EXTRA_CNT - 1)->num) /* sender dirs */
  597. #define F_DIR_RELNAMES_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + DIRNODE_EXTRA_CNT \
  598. + PTR_EXTRA_CNT - 1)->num) /* sender dirs */
  599. #define F_DIR_DEFACL(f) OPT_EXTRA(f, LEN64_BUMP(f))->unum /* receiver dirs */
  600. #define F_DIR_DEV_P(f) (&OPT_EXTRA(f, LEN64_BUMP(f) + ACL_BUMP(f) \
  601. + DEV_EXTRA_CNT - 1)->unum) /* receiver dirs */
  602. /* This optional item might follow an F_HL_*() item.
  603. * (Note: a device doesn't need to check LEN64_BUMP(f).) */
  604. #define F_RDEV_P(f) (&OPT_EXTRA(f, HLINK_BUMP(f) + DEV_EXTRA_CNT - 1)->unum)
  605. /* The sum is only present on regular files. */
  606. #define F_SUM(f) ((char*)OPT_EXTRA(f, LEN64_BUMP(f) + HLINK_BUMP(f) \
  607. + SUM_EXTRA_CNT - 1))
  608. /* Some utility defines: */
  609. #define F_IS_ACTIVE(f) (f)->basename[0]
  610. #define F_IS_HLINKED(f) ((f)->flags & FLAG_HLINKED)
  611. #define F_HLINK_NOT_FIRST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_FIRST)
  612. #define F_HLINK_NOT_LAST(f) BITS_SETnUNSET((f)->flags, FLAG_HLINKED, FLAG_HLINK_LAST)
  613. /* These access the F_DIR_DEV_P() and F_RDEV_P() values: */
  614. #define DEV_MAJOR(a) (a)[0]
  615. #define DEV_MINOR(a) (a)[1]
  616. /* These access the F_DIRS_NODE_P() values: */
  617. #define DIR_PARENT(a) (a)[0]
  618. #define DIR_FIRST_CHILD(a) (a)[1]
  619. #define DIR_NEXT_SIBLING(a) (a)[2]
  620. /*
  621. * Start the flist array at FLIST_START entries and grow it
  622. * by doubling until FLIST_LINEAR then grow by FLIST_LINEAR
  623. */
  624. #define FLIST_START (32 * 1024)
  625. #define FLIST_LINEAR (FLIST_START * 512)
  626. /*
  627. * Extent size for allocation pools: A minimum size of 128KB
  628. * is needed to mmap them so that freeing will release the
  629. * space to the OS.
  630. *
  631. * Larger sizes reduce leftover fragments and speed free calls
  632. * (when they happen). Smaller sizes increase the chance of
  633. * freed allocations freeing whole extents.
  634. */
  635. #define NORMAL_EXTENT (256 * 1024)
  636. #define SMALL_EXTENT (128 * 1024)
  637. #define FLIST_TEMP (1<<1)
  638. struct file_list {
  639. struct file_list *next, *prev;
  640. struct file_struct **files, **sorted;
  641. alloc_pool_t file_pool;
  642. void *pool_boundary;
  643. int used, malloced;
  644. int low, high; /* 0-relative index values excluding empties */
  645. int ndx_start; /* the start offset for inc_recurse mode */
  646. int flist_num; /* 1-relative file_list number or 0 */
  647. int parent_ndx; /* dir_flist index of parent directory */
  648. int in_progress, to_redo;
  649. };
  650. #define SUMFLG_SAME_OFFSET (1<<0)
  651. struct sum_buf {
  652. OFF_T offset; /**< offset in file of this chunk */
  653. int32 len; /**< length of chunk of file */
  654. uint32 sum1; /**< simple checksum */
  655. int32 chain; /**< next hash-table collision */
  656. short flags; /**< flag bits */
  657. char sum2[SUM_LENGTH]; /**< checksum */
  658. };
  659. struct sum_struct {
  660. OFF_T flength; /**< total file length */
  661. struct sum_buf *sums; /**< points to info for each chunk */
  662. int32 count; /**< how many chunks */
  663. int32 blength; /**< block_length */
  664. int32 remainder; /**< flength % block_length */
  665. int s2length; /**< sum2_length */
  666. };
  667. struct map_struct {
  668. OFF_T file_size; /* File size (from stat) */
  669. OFF_T p_offset; /* Window start */
  670. OFF_T p_fd_offset; /* offset of cursor in fd ala lseek */
  671. char *p; /* Window pointer */
  672. int32 p_size; /* Largest window size we allocated */
  673. int32 p_len; /* Latest (rounded) window size */
  674. int32 def_window_size; /* Default window size */
  675. int fd; /* File Descriptor */
  676. int status; /* first errno from read errors */
  677. };
  678. #define MATCHFLG_WILD (1<<0) /* pattern has '*', '[', and/or '?' */
  679. #define MATCHFLG_WILD2 (1<<1) /* pattern has '**' */
  680. #define MATCHFLG_WILD2_PREFIX (1<<2) /* pattern starts with "**" */
  681. #define MATCHFLG_WILD3_SUFFIX (1<<3) /* pattern ends with "***" */
  682. #define MATCHFLG_ABS_PATH (1<<4) /* path-match on absolute path */
  683. #define MATCHFLG_INCLUDE (1<<5) /* this is an include, not an exclude */
  684. #define MATCHFLG_DIRECTORY (1<<6) /* this matches only directories */
  685. #define MATCHFLG_WORD_SPLIT (1<<7) /* split rules on whitespace */
  686. #define MATCHFLG_NO_INHERIT (1<<8) /* don't inherit these rules */
  687. #define MATCHFLG_NO_PREFIXES (1<<9) /* parse no prefixes from patterns */
  688. #define MATCHFLG_MERGE_FILE (1<<10)/* specifies a file to merge */
  689. #define MATCHFLG_PERDIR_MERGE (1<<11)/* merge-file is searched per-dir */
  690. #define MATCHFLG_EXCLUDE_SELF (1<<12)/* merge-file name should be excluded */
  691. #define MATCHFLG_FINISH_SETUP (1<<13)/* per-dir merge file needs setup */
  692. #define MATCHFLG_NEGATE (1<<14)/* rule matches when pattern does not */
  693. #define MATCHFLG_CVS_IGNORE (1<<15)/* rule was -C or :C */
  694. #define MATCHFLG_SENDER_SIDE (1<<16)/* rule applies to the sending side */
  695. #define MATCHFLG_RECEIVER_SIDE (1<<17)/* rule applies to the receiving side */
  696. #define MATCHFLG_CLEAR_LIST (1<<18)/* this item is the "!" token */
  697. #define MATCHFLG_PERISHABLE (1<<19)/* perishable if parent dir goes away */
  698. #define MATCHFLGS_FROM_CONTAINER (MATCHFLG_ABS_PATH | MATCHFLG_INCLUDE \
  699. | MATCHFLG_DIRECTORY | MATCHFLG_SENDER_SIDE \
  700. | MATCHFLG_NEGATE | MATCHFLG_RECEIVER_SIDE \
  701. | MATCHFLG_PERISHABLE)
  702. struct filter_struct {
  703. struct filter_struct *next;
  704. char *pattern;
  705. uint32 match_flags;
  706. union {
  707. int slash_cnt;
  708. struct filter_list_struct *mergelist;
  709. } u;
  710. };
  711. struct filter_list_struct {
  712. struct filter_struct *head;
  713. struct filter_struct *tail;
  714. char *debug_type;
  715. };
  716. struct stats {
  717. int64 total_size;
  718. int64 total_transferred_size;
  719. int64 total_written;
  720. int64 total_read;
  721. int64 literal_data;
  722. int64 matched_data;
  723. int64 flist_buildtime;
  724. int64 flist_xfertime;
  725. int64 flist_size;
  726. int num_files;
  727. int num_transferred_files;
  728. };
  729. struct chmod_mode_struct;
  730. struct flist_ndx_item {
  731. struct flist_ndx_item *next;
  732. int ndx;
  733. };
  734. typedef struct {
  735. struct flist_ndx_item *head, *tail;
  736. } flist_ndx_list;
  737. #define EMPTY_ITEM_LIST {NULL, 0, 0}
  738. typedef struct {
  739. void *items;
  740. size_t count;
  741. size_t malloced;
  742. } item_list;
  743. #define EXPAND_ITEM_LIST(lp, type, incr) \
  744. (type*)expand_item_list(lp, sizeof (type), #type, incr)
  745. #define EMPTY_XBUF {NULL, 0, 0, 0}
  746. typedef struct {
  747. char *buf;
  748. size_t pos; /* pos = read pos in the buf */
  749. size_t len; /* len = chars following pos */
  750. size_t size; /* size = total space in buf */
  751. } xbuf;
  752. #define INIT_XBUF(xb, str, ln, sz) (xb).buf = (str), (xb).len = (ln), (xb).size = (sz), (xb).pos = 0
  753. #define INIT_XBUF_STRLEN(xb, str) (xb).buf = (str), (xb).len = strlen((xb).buf), (xb).size = (-1), (xb).pos = 0
  754. /* This one is used to make an output xbuf based on a char[] buffer: */
  755. #define INIT_CONST_XBUF(xb, bf) (xb).buf = (bf), (xb).size = sizeof (bf), (xb).len = (xb).pos = 0
  756. #define ICB_EXPAND_OUT (1<<0)
  757. #define ICB_INCLUDE_BAD (1<<1)
  758. #define ICB_INCLUDE_INCOMPLETE (1<<2)
  759. #define RL_EOL_NULLS (1<<0)
  760. #define RL_DUMP_COMMENTS (1<<1)
  761. #define RL_CONVERT (1<<2)
  762. typedef struct {
  763. char name_type;
  764. char fname[1]; /* has variable size */
  765. } relnamecache;
  766. #include "byteorder.h"
  767. #include "lib/mdigest.h"
  768. #include "lib/wildmatch.h"
  769. #include "lib/permstring.h"
  770. #include "lib/addrinfo.h"
  771. #ifndef __GNUC__
  772. #define __attribute__(x)
  773. #else
  774. # if __GNUC__ <= 2
  775. # define NORETURN
  776. # endif
  777. #endif
  778. #define UNUSED(x) x __attribute__((__unused__))
  779. #ifndef NORETURN
  780. #define NORETURN __attribute__((__noreturn__))
  781. #endif
  782. typedef struct {
  783. STRUCT_STAT st;
  784. #ifdef SUPPORT_ACLS
  785. struct rsync_acl *acc_acl; /* access ACL */
  786. struct rsync_acl *def_acl; /* default ACL */
  787. #endif
  788. #ifdef SUPPORT_XATTRS
  789. item_list *xattr;
  790. #endif
  791. } stat_x;
  792. #define ACL_READY(sx) ((sx).acc_acl != NULL)
  793. #define XATTR_READY(sx) ((sx).xattr != NULL)
  794. #include "proto.h"
  795. #ifndef SUPPORT_XATTRS
  796. #define x_stat(fn,fst,xst) do_stat(fn,fst)
  797. #define x_lstat(fn,fst,xst) do_lstat(fn,fst)
  798. #define x_fstat(fd,fst,xst) do_fstat(fd,fst)
  799. #endif
  800. /* We have replacement versions of these if they're missing. */
  801. #ifndef HAVE_ASPRINTF
  802. int asprintf(char **ptr, const char *format, ...);
  803. #endif
  804. #ifndef HAVE_VASPRINTF
  805. int vasprintf(char **ptr, const char *format, va_list ap);
  806. #endif
  807. #if !defined HAVE_VSNPRINTF || !defined HAVE_C99_VSNPRINTF
  808. #define vsnprintf rsync_vsnprintf
  809. int vsnprintf(char *str, size_t count, const char *fmt, va_list args);
  810. #endif
  811. #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF
  812. #define snprintf rsync_snprintf
  813. int snprintf(char *str, size_t count, const char *fmt,...);
  814. #endif
  815. #ifndef HAVE_STRERROR
  816. extern char *sys_errlist[];
  817. #define strerror(i) sys_errlist[i]
  818. #endif
  819. #ifndef HAVE_STRCHR
  820. # define strchr index
  821. # define strrchr rindex
  822. #endif
  823. #ifndef HAVE_ERRNO_DECL
  824. extern int errno;
  825. #endif
  826. #ifdef HAVE_READLINK
  827. #define SUPPORT_LINKS 1
  828. #endif
  829. #ifdef HAVE_LINK
  830. #define SUPPORT_HARD_LINKS 1
  831. #endif
  832. #ifdef HAVE_SIGACTION
  833. #define SIGACTION(n,h) sigact.sa_handler=(h), sigaction((n),&sigact,NULL)
  834. #define signal(n,h) we_need_to_call_SIGACTION_not_signal(n,h)
  835. #else
  836. #define SIGACTION(n,h) signal(n,h)
  837. #endif
  838. #ifndef EWOULDBLOCK
  839. #define EWOULDBLOCK EAGAIN
  840. #endif
  841. #ifndef STDIN_FILENO
  842. #define STDIN_FILENO 0
  843. #endif
  844. #ifndef STDOUT_FILENO
  845. #define STDOUT_FILENO 1
  846. #endif
  847. #ifndef STDERR_FILENO
  848. #define STDERR_FILENO 2
  849. #endif
  850. #ifndef S_IRUSR
  851. #define S_IRUSR 0400
  852. #endif
  853. #ifndef S_IWUSR
  854. #define S_IWUSR 0200
  855. #endif
  856. #ifndef ACCESSPERMS
  857. #define ACCESSPERMS 0777
  858. #endif
  859. #ifndef S_ISVTX
  860. #define S_ISVTX 0
  861. #endif
  862. #define CHMOD_BITS (S_ISUID | S_ISGID | S_ISVTX | ACCESSPERMS)
  863. #ifndef _S_IFMT
  864. #define _S_IFMT 0170000
  865. #endif
  866. #ifndef _S_IFLNK
  867. #define _S_IFLNK 0120000
  868. #endif
  869. #ifndef S_ISLNK
  870. #define S_ISLNK(mode) (((mode) & (_S_IFMT)) == (_S_IFLNK))
  871. #endif
  872. #ifndef S_ISBLK
  873. #define S_ISBLK(mode) (((mode) & (_S_IFMT)) == (_S_IFBLK))
  874. #endif
  875. #ifndef S_ISCHR
  876. #define S_ISCHR(mode) (((mode) & (_S_IFMT)) == (_S_IFCHR))
  877. #endif
  878. #ifndef S_ISSOCK
  879. #ifdef _S_IFSOCK
  880. #define S_ISSOCK(mode) (((mode) & (_S_IFMT)) == (_S_IFSOCK))
  881. #else
  882. #define S_ISSOCK(mode) (0)
  883. #endif
  884. #endif
  885. #ifndef S_ISFIFO
  886. #ifdef _S_IFIFO
  887. #define S_ISFIFO(mode) (((mode) & (_S_IFMT)) == (_S_IFIFO))
  888. #else
  889. #define S_ISFIFO(mode) (0)
  890. #endif
  891. #endif
  892. #ifndef S_ISDIR
  893. #define S_ISDIR(mode) (((mode) & (_S_IFMT)) == (_S_IFDIR))
  894. #endif
  895. #ifndef S_ISREG
  896. #define S_ISREG(mode) (((mode) & (_S_IFMT)) == (_S_IFREG))
  897. #endif
  898. /* work out what fcntl flag to use for non-blocking */
  899. #ifdef O_NONBLOCK
  900. # define NONBLOCK_FLAG O_NONBLOCK
  901. #elif defined SYSV
  902. # define NONBLOCK_FLAG O_NDELAY
  903. #else
  904. # define NONBLOCK_FLAG FNDELAY
  905. #endif
  906. #ifndef INADDR_LOOPBACK
  907. #define INADDR_LOOPBACK 0x7f000001
  908. #endif
  909. #ifndef INADDR_NONE
  910. #define INADDR_NONE 0xffffffff
  911. #endif
  912. #define IS_SPECIAL(mode) (S_ISSOCK(mode) || S_ISFIFO(mode))
  913. #define IS_DEVICE(mode) (S_ISCHR(mode) || S_ISBLK(mode))
  914. #define PRESERVE_FILE_TIMES (1<<0)
  915. #define PRESERVE_DIR_TIMES (1<<1)
  916. #define PRESERVE_LINK_TIMES (1<<2)
  917. /* Initial mask on permissions given to temporary files. Mask off setuid
  918. bits and group access because of potential race-condition security
  919. holes, and mask other access because mode 707 is bizarre */
  920. #define INITACCESSPERMS 0700
  921. /* handler for null strings in printf format */
  922. #define NS(s) ((s)?(s):"<NULL>")
  923. /* Convenient wrappers for malloc and realloc. Use them. */
  924. #define new(type) ((type*)malloc(sizeof (type)))
  925. #define new0(type) ((type*)calloc(1, sizeof (type)))
  926. #define new_array(type, num) ((type*)_new_array((num), sizeof (type), 0))
  927. #define new_array0(type, num) ((type*)_new_array((num), sizeof (type), 1))
  928. #define realloc_array(ptr, type, num) ((type*)_realloc_array((ptr), sizeof(type), (num)))
  929. /* use magic gcc attributes to catch format errors */
  930. void rprintf(enum logcode , const char *, ...)
  931. __attribute__((format (printf, 2, 3)))
  932. ;
  933. /* This is just like rprintf, but it also tries to print some
  934. * representation of the error code. Normally errcode = errno. */
  935. void rsyserr(enum logcode, int, const char *, ...)
  936. __attribute__((format (printf, 3, 4)))
  937. ;
  938. /* Make sure that the O_BINARY flag is defined. */
  939. #ifndef O_BINARY
  940. #define O_BINARY 0
  941. #endif
  942. #ifndef HAVE_STRLCPY
  943. size_t strlcpy(char *d, const char *s, size_t bufsize);
  944. #endif
  945. #ifndef HAVE_STRLCAT
  946. size_t strlcat(char *d, const char *s, size_t bufsize);
  947. #endif
  948. #ifndef WEXITSTATUS
  949. #define WEXITSTATUS(stat) ((int)(((stat)>>8)&0xFF))
  950. #endif
  951. #ifndef WIFEXITED
  952. #define WIFEXITED(stat) ((int)((stat)&0xFF) == 0)
  953. #endif
  954. #define exit_cleanup(code) _exit_cleanup(code, __FILE__, __LINE__)
  955. #ifdef HAVE_GETEUID
  956. #define MY_UID() geteuid()
  957. #else
  958. #define MY_UID() getuid()
  959. #endif
  960. #ifdef HAVE_GETEGID
  961. #define MY_GID() getegid()
  962. #else
  963. #define MY_GID() getgid()
  964. #endif
  965. #ifdef FORCE_FD_ZERO_MEMSET
  966. #undef FD_ZERO
  967. #define FD_ZERO(fdsetp) memset(fdsetp, 0, sizeof (fd_set))
  968. #endif
  969. extern int verbose;
  970. #ifndef HAVE_INET_NTOP
  971. const char *inet_ntop(int af, const void *src, char *dst, size_t size);
  972. #endif
  973. #ifndef HAVE_INET_PTON
  974. int inet_pton(int af, const char *src, void *dst);
  975. #endif
  976. #ifdef MAINTAINER_MODE
  977. const char *get_panic_action(void);
  978. #endif