syscall.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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-2008 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. if (S_ISLNK(mode)) {
  139. #ifdef HAVE_LCHMOD
  140. code = lchmod(path, mode & CHMOD_BITS);
  141. #elif defined HAVE_SETATTRLIST
  142. struct attrlist attrList;
  143. uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
  144. memset(&attrList, 0, sizeof attrList);
  145. attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
  146. attrList.commonattr = ATTR_CMN_ACCESSMASK;
  147. code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
  148. #else
  149. code = 1;
  150. #endif
  151. } else
  152. code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
  153. if (code != 0 && (preserve_perms || preserve_executability))
  154. return code;
  155. return 0;
  156. }
  157. #endif
  158. int do_rename(const char *fname1, const char *fname2)
  159. {
  160. if (dry_run) return 0;
  161. RETURN_ERROR_IF_RO_OR_LO;
  162. return rename(fname1, fname2);
  163. }
  164. void trim_trailing_slashes(char *name)
  165. {
  166. int l;
  167. /* Some BSD systems cannot make a directory if the name
  168. * contains a trailing slash.
  169. * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
  170. /* Don't change empty string; and also we can't improve on
  171. * "/" */
  172. l = strlen(name);
  173. while (l > 1) {
  174. if (name[--l] != '/')
  175. break;
  176. name[l] = '\0';
  177. }
  178. }
  179. int do_mkdir(char *fname, mode_t mode)
  180. {
  181. if (dry_run) return 0;
  182. RETURN_ERROR_IF_RO_OR_LO;
  183. trim_trailing_slashes(fname);
  184. return mkdir(fname, mode);
  185. }
  186. /* like mkstemp but forces permissions */
  187. int do_mkstemp(char *template, mode_t perms)
  188. {
  189. RETURN_ERROR_IF(dry_run, 0);
  190. RETURN_ERROR_IF(read_only, EROFS);
  191. perms |= S_IWUSR;
  192. #if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
  193. {
  194. int fd = mkstemp(template);
  195. if (fd == -1)
  196. return -1;
  197. if (fchmod(fd, perms) != 0 && preserve_perms) {
  198. int errno_save = errno;
  199. close(fd);
  200. unlink(template);
  201. errno = errno_save;
  202. return -1;
  203. }
  204. #if defined HAVE_SETMODE && O_BINARY
  205. setmode(fd, O_BINARY);
  206. #endif
  207. return fd;
  208. }
  209. #else
  210. if (!mktemp(template))
  211. return -1;
  212. return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
  213. #endif
  214. }
  215. int do_stat(const char *fname, STRUCT_STAT *st)
  216. {
  217. #ifdef USE_STAT64_FUNCS
  218. return stat64(fname, st);
  219. #else
  220. return stat(fname, st);
  221. #endif
  222. }
  223. int do_lstat(const char *fname, STRUCT_STAT *st)
  224. {
  225. #ifdef SUPPORT_LINKS
  226. # ifdef USE_STAT64_FUNCS
  227. return lstat64(fname, st);
  228. # else
  229. return lstat(fname, st);
  230. # endif
  231. #else
  232. return do_stat(fname, st);
  233. #endif
  234. }
  235. int do_fstat(int fd, STRUCT_STAT *st)
  236. {
  237. #ifdef USE_STAT64_FUNCS
  238. return fstat64(fd, st);
  239. #else
  240. return fstat(fd, st);
  241. #endif
  242. }
  243. OFF_T do_lseek(int fd, OFF_T offset, int whence)
  244. {
  245. #ifdef HAVE_LSEEK64
  246. #if !SIZEOF_OFF64_T
  247. OFF_T lseek64();
  248. #else
  249. off64_t lseek64();
  250. #endif
  251. return lseek64(fd, offset, whence);
  252. #else
  253. return lseek(fd, offset, whence);
  254. #endif
  255. }