syscall.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * Syscall wrappers to ensure that nothing gets done in dry_run mode
  3. * and to handle system peculiarities.
  4. *
  5. * Copyright (C) 1998 Andrew Tridgell
  6. * Copyright (C) 2002 Martin Pool
  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. #include "rsync.h"
  23. #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
  24. #include <sys/un.h>
  25. #endif
  26. #ifdef HAVE_SYS_ATTR_H
  27. #include <sys/attr.h>
  28. #endif
  29. extern int dry_run;
  30. extern int am_root;
  31. extern int read_only;
  32. extern int list_only;
  33. extern int preserve_perms;
  34. extern int preserve_executability;
  35. #define RETURN_ERROR_IF(x,e) \
  36. do { \
  37. if (x) { \
  38. errno = (e); \
  39. return -1; \
  40. } \
  41. } while (0)
  42. #define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
  43. int do_unlink(const char *fname)
  44. {
  45. if (dry_run) return 0;
  46. RETURN_ERROR_IF_RO_OR_LO;
  47. return unlink(fname);
  48. }
  49. int do_symlink(const char *fname1, const char *fname2)
  50. {
  51. if (dry_run) return 0;
  52. RETURN_ERROR_IF_RO_OR_LO;
  53. return symlink(fname1, fname2);
  54. }
  55. #ifdef HAVE_LINK
  56. int do_link(const char *fname1, const char *fname2)
  57. {
  58. if (dry_run) return 0;
  59. RETURN_ERROR_IF_RO_OR_LO;
  60. return link(fname1, fname2);
  61. }
  62. #endif
  63. int do_lchown(const char *path, uid_t owner, gid_t group)
  64. {
  65. if (dry_run) return 0;
  66. RETURN_ERROR_IF_RO_OR_LO;
  67. #ifndef HAVE_LCHOWN
  68. #define lchown chown
  69. #endif
  70. return lchown(path, owner, group);
  71. }
  72. int do_mknod(const char *pathname, mode_t mode, dev_t dev)
  73. {
  74. if (dry_run) return 0;
  75. RETURN_ERROR_IF_RO_OR_LO;
  76. /* For --fake-super, we create a normal file with mode 0600. */
  77. if (am_root < 0) {
  78. int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
  79. if (fd < 0 || close(fd) < 0)
  80. return -1;
  81. return 0;
  82. }
  83. #if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
  84. if (S_ISFIFO(mode))
  85. return mkfifo(pathname, mode);
  86. #endif
  87. #if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
  88. if (S_ISSOCK(mode)) {
  89. int sock;
  90. struct sockaddr_un saddr;
  91. #ifdef HAVE_SOCKADDR_UN_LEN
  92. unsigned int len =
  93. #endif
  94. strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
  95. #ifdef HAVE_SOCKADDR_UN_LEN
  96. saddr.sun_len = len >= sizeof saddr.sun_path
  97. ? sizeof saddr.sun_path : len + 1;
  98. #endif
  99. saddr.sun_family = AF_UNIX;
  100. if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
  101. || (unlink(pathname) < 0 && errno != ENOENT)
  102. || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
  103. return -1;
  104. close(sock);
  105. #ifdef HAVE_CHMOD
  106. return do_chmod(pathname, mode);
  107. #else
  108. return 0;
  109. #endif
  110. }
  111. #endif
  112. #ifdef HAVE_MKNOD
  113. return mknod(pathname, mode, dev);
  114. #else
  115. return -1;
  116. #endif
  117. }
  118. int do_rmdir(const char *pathname)
  119. {
  120. if (dry_run) return 0;
  121. RETURN_ERROR_IF_RO_OR_LO;
  122. return rmdir(pathname);
  123. }
  124. int do_open(const char *pathname, int flags, mode_t mode)
  125. {
  126. if (flags != O_RDONLY) {
  127. RETURN_ERROR_IF(dry_run, 0);
  128. RETURN_ERROR_IF_RO_OR_LO;
  129. }
  130. return open(pathname, flags | O_BINARY, mode);
  131. }
  132. #ifdef HAVE_CHMOD
  133. int do_chmod(const char *path, mode_t mode)
  134. {
  135. int code;
  136. if (dry_run) return 0;
  137. RETURN_ERROR_IF_RO_OR_LO;
  138. #ifdef HAVE_LCHMOD
  139. code = lchmod(path, mode & CHMOD_BITS);
  140. #else
  141. if (S_ISLNK(mode)) {
  142. # if defined HAVE_SETATTRLIST
  143. struct attrlist attrList;
  144. uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
  145. memset(&attrList, 0, sizeof attrList);
  146. attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
  147. attrList.commonattr = ATTR_CMN_ACCESSMASK;
  148. code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
  149. # else
  150. code = 1;
  151. # endif
  152. } else
  153. code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
  154. #endif /* !HAVE_LCHMOD */
  155. if (code != 0 && (preserve_perms || preserve_executability))
  156. return code;
  157. return 0;
  158. }
  159. #endif
  160. int do_rename(const char *fname1, const char *fname2)
  161. {
  162. if (dry_run) return 0;
  163. RETURN_ERROR_IF_RO_OR_LO;
  164. return rename(fname1, fname2);
  165. }
  166. #ifdef HAVE_FTRUNCATE
  167. int do_ftruncate(int fd, OFF_T size)
  168. {
  169. int ret;
  170. if (dry_run) return 0;
  171. RETURN_ERROR_IF_RO_OR_LO;
  172. do {
  173. ret = ftruncate(fd, size);
  174. } while (ret < 0 && errno == EINTR);
  175. return ret;
  176. }
  177. #endif
  178. void trim_trailing_slashes(char *name)
  179. {
  180. int l;
  181. /* Some BSD systems cannot make a directory if the name
  182. * contains a trailing slash.
  183. * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
  184. /* Don't change empty string; and also we can't improve on
  185. * "/" */
  186. l = strlen(name);
  187. while (l > 1) {
  188. if (name[--l] != '/')
  189. break;
  190. name[l] = '\0';
  191. }
  192. }
  193. int do_mkdir(char *fname, mode_t mode)
  194. {
  195. if (dry_run) return 0;
  196. RETURN_ERROR_IF_RO_OR_LO;
  197. trim_trailing_slashes(fname);
  198. return mkdir(fname, mode);
  199. }
  200. /* like mkstemp but forces permissions */
  201. int do_mkstemp(char *template, mode_t perms)
  202. {
  203. RETURN_ERROR_IF(dry_run, 0);
  204. RETURN_ERROR_IF(read_only, EROFS);
  205. perms |= S_IWUSR;
  206. #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
  207. {
  208. int fd = mkstemp(template);
  209. if (fd == -1)
  210. return -1;
  211. if (fchmod(fd, perms) != 0 && preserve_perms) {
  212. int errno_save = errno;
  213. close(fd);
  214. unlink(template);
  215. errno = errno_save;
  216. return -1;
  217. }
  218. #if defined HAVE_SETMODE && O_BINARY
  219. setmode(fd, O_BINARY);
  220. #endif
  221. return fd;
  222. }
  223. #else
  224. if (!mktemp(template))
  225. return -1;
  226. return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
  227. #endif
  228. }
  229. int do_stat(const char *fname, STRUCT_STAT *st)
  230. {
  231. #ifdef USE_STAT64_FUNCS
  232. return stat64(fname, st);
  233. #else
  234. return stat(fname, st);
  235. #endif
  236. }
  237. int do_lstat(const char *fname, STRUCT_STAT *st)
  238. {
  239. #ifdef SUPPORT_LINKS
  240. # ifdef USE_STAT64_FUNCS
  241. return lstat64(fname, st);
  242. # else
  243. return lstat(fname, st);
  244. # endif
  245. #else
  246. return do_stat(fname, st);
  247. #endif
  248. }
  249. int do_fstat(int fd, STRUCT_STAT *st)
  250. {
  251. #ifdef USE_STAT64_FUNCS
  252. return fstat64(fd, st);
  253. #else
  254. return fstat(fd, st);
  255. #endif
  256. }
  257. OFF_T do_lseek(int fd, OFF_T offset, int whence)
  258. {
  259. #ifdef HAVE_LSEEK64
  260. #if !SIZEOF_OFF64_T
  261. OFF_T lseek64();
  262. #else
  263. off64_t lseek64();
  264. #endif
  265. return lseek64(fd, offset, whence);
  266. #else
  267. return lseek(fd, offset, whence);
  268. #endif
  269. }
  270. #ifdef HAVE_UTIMENSAT
  271. int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
  272. {
  273. struct timespec t[2];
  274. if (dry_run) return 0;
  275. RETURN_ERROR_IF_RO_OR_LO;
  276. t[0].tv_sec = 0;
  277. t[0].tv_nsec = UTIME_NOW;
  278. t[1].tv_sec = modtime;
  279. t[1].tv_nsec = mod_nsec;
  280. return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
  281. }
  282. #endif
  283. #ifdef HAVE_LUTIMES
  284. int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
  285. {
  286. struct timeval t[2];
  287. if (dry_run) return 0;
  288. RETURN_ERROR_IF_RO_OR_LO;
  289. t[0].tv_sec = time(NULL);
  290. t[0].tv_usec = 0;
  291. t[1].tv_sec = modtime;
  292. t[1].tv_usec = mod_nsec / 1000;
  293. return lutimes(fname, t);
  294. }
  295. #endif
  296. #ifdef HAVE_UTIMES
  297. int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
  298. {
  299. struct timeval t[2];
  300. if (dry_run) return 0;
  301. RETURN_ERROR_IF_RO_OR_LO;
  302. t[0].tv_sec = time(NULL);
  303. t[0].tv_usec = 0;
  304. t[1].tv_sec = modtime;
  305. t[1].tv_usec = mod_nsec / 1000;
  306. return utimes(fname, t);
  307. }
  308. #elif defined HAVE_UTIME
  309. int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
  310. {
  311. #ifdef HAVE_STRUCT_UTIMBUF
  312. struct utimbuf tbuf;
  313. #else
  314. time_t t[2];
  315. #endif
  316. if (dry_run) return 0;
  317. RETURN_ERROR_IF_RO_OR_LO;
  318. # ifdef HAVE_STRUCT_UTIMBUF
  319. tbuf.actime = time(NULL);
  320. tbuf.modtime = modtime;
  321. return utime(fname, &tbuf);
  322. # else
  323. t[0] = time(NULL);
  324. t[1] = modtime;
  325. return utime(fname, t);
  326. # endif
  327. }
  328. #else
  329. #error Need utimes or utime function.
  330. #endif