1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Utilities used in rsync
31 /****************************************************************************
32 Set a fd into nonblocking mode
33 ****************************************************************************/
34 void set_nonblocking(int fd)
38 if((val = fcntl(fd, F_GETFL, 0)) == -1)
40 if (!(val & NONBLOCK_FLAG)) {
42 fcntl(fd, F_SETFL, val);
46 /****************************************************************************
47 Set a fd into blocking mode
48 ****************************************************************************/
49 void set_blocking(int fd)
53 if((val = fcntl(fd, F_GETFL, 0)) == -1)
55 if (val & NONBLOCK_FLAG) {
56 val &= ~NONBLOCK_FLAG;
57 fcntl(fd, F_SETFL, val);
62 /* create a file descriptor pair - like pipe() but use socketpair if
63 possible (because of blocking issues on pipes)
65 always set non-blocking
67 int fd_pair(int fd[2])
72 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
78 set_nonblocking(fd[0]);
79 set_nonblocking(fd[1]);
86 /* this is derived from CVS code
88 note that in the child STDIN is set to blocking and STDOUT
89 is set to non-blocking. This is necessary as rsh relies on stdin being blocking
90 and ssh relies on stdout being non-blocking
92 if blocking_io is set then use blocking io on both fds. That can be
93 used to cope with badly broken rsh implementations like the one on
96 int piped_child(char **command,int *f_in,int *f_out)
100 int from_child_pipe[2];
101 extern int blocking_io;
103 if (fd_pair(to_child_pipe) < 0 ||
104 fd_pair(from_child_pipe) < 0) {
105 rprintf(FERROR,"pipe: %s\n",strerror(errno));
106 exit_cleanup(RERR_IPC);
112 rprintf(FERROR,"fork: %s\n",strerror(errno));
113 exit_cleanup(RERR_IPC);
118 extern int orig_umask;
119 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
120 close(to_child_pipe[1]) < 0 ||
121 close(from_child_pipe[0]) < 0 ||
122 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
123 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
124 exit_cleanup(RERR_IPC);
126 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
127 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
129 set_blocking(STDIN_FILENO);
131 set_blocking(STDOUT_FILENO);
133 execvp(command[0], command);
134 rprintf(FERROR,"Failed to exec %s : %s\n",
135 command[0],strerror(errno));
136 exit_cleanup(RERR_IPC);
139 if (close(from_child_pipe[1]) < 0 ||
140 close(to_child_pipe[0]) < 0) {
141 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
142 exit_cleanup(RERR_IPC);
145 *f_in = from_child_pipe[0];
146 *f_out = to_child_pipe[1];
151 int local_child(int argc, char **argv,int *f_in,int *f_out)
154 int to_child_pipe[2];
155 int from_child_pipe[2];
157 if (fd_pair(to_child_pipe) < 0 ||
158 fd_pair(from_child_pipe) < 0) {
159 rprintf(FERROR,"pipe: %s\n",strerror(errno));
160 exit_cleanup(RERR_IPC);
166 rprintf(FERROR,"fork: %s\n",strerror(errno));
167 exit_cleanup(RERR_IPC);
171 extern int am_sender;
172 extern int am_server;
174 am_sender = !am_sender;
177 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
178 close(to_child_pipe[1]) < 0 ||
179 close(from_child_pipe[0]) < 0 ||
180 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
181 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
182 exit_cleanup(RERR_IPC);
184 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
185 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
186 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
189 if (close(from_child_pipe[1]) < 0 ||
190 close(to_child_pipe[0]) < 0) {
191 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
192 exit_cleanup(RERR_IPC);
195 *f_in = from_child_pipe[0];
196 *f_out = to_child_pipe[1];
203 void out_of_memory(char *str)
205 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
206 exit_cleanup(RERR_MALLOC);
209 void overflow(char *str)
211 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
212 exit_cleanup(RERR_MALLOC);
217 int set_modtime(char *fname,time_t modtime)
220 if (dry_run) return 0;
224 tbuf.actime = time(NULL);
225 tbuf.modtime = modtime;
226 return utime(fname,&tbuf);
227 #elif defined(HAVE_UTIME)
231 return utime(fname,t);
234 t[0].tv_sec = time(NULL);
236 t[1].tv_sec = modtime;
238 return utimes(fname,t);
244 /****************************************************************************
245 create any necessary directories in fname. Unfortunately we don't know
246 what perms to give the directory when this is called so we need to rely
248 ****************************************************************************/
249 int create_directory_path(char *fname)
251 extern int orig_umask;
254 while (*fname == '/') fname++;
255 while (strncmp(fname,"./",2)==0) fname += 2;
258 while ((p=strchr(p,'/'))) {
260 do_mkdir(fname,0777 & ~orig_umask);
268 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
269 Return LEN upon success, write's (negative) error code otherwise.
271 derived from GNU C's cccp.c.
273 static int full_write(int desc, char *ptr, int len)
279 int written = write (desc, ptr, len);
287 total_written += written;
291 return total_written;
294 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
295 Return the actual number of bytes read, zero for EOF, or negative
298 derived from GNU C's cccp.c. */
299 static int safe_read(int desc, char *ptr, int len)
308 n_chars = read(desc, ptr, len);
309 } while (n_chars < 0 && errno == EINTR);
311 n_chars = read(desc, ptr, len);
318 /* copy a file - this is used in conjunction with the --temp-dir option */
319 int copy_file(char *source, char *dest, mode_t mode)
324 int len; /* Number of bytes read into `buf'. */
326 ifd = do_open(source, O_RDONLY, 0);
328 rprintf(FERROR,"open %s: %s\n",
329 source,strerror(errno));
333 if (robust_unlink(dest) && errno != ENOENT) {
334 rprintf(FERROR,"unlink %s: %s\n",
335 dest,strerror(errno));
339 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
341 rprintf(FERROR,"open %s: %s\n",
342 dest,strerror(errno));
347 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
348 if (full_write(ofd, buf, len) < 0) {
349 rprintf(FERROR,"write %s: %s\n",
350 dest,strerror(errno));
361 rprintf(FERROR,"read %s: %s\n",
362 source,strerror(errno));
370 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
371 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
372 will shuffle the filenames around a bit as long as the file is still
373 busy; this is because this function does not know if the unlink call
374 is due to a new file coming in, or --delete trying to remove old
375 .rsyncNNN files, hence it renames it each time.
377 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
378 #define MAX_RENAMES_DIGITS 3
379 #define MAX_RENAMES 1000
381 int robust_unlink(char *fname)
384 return do_unlink(fname);
386 static int counter = 1;
388 char path[MAXPATHLEN];
390 rc = do_unlink(fname);
391 if ((rc == 0) || (errno != ETXTBSY))
394 strlcpy(path, fname, MAXPATHLEN);
397 while((path[--pos] != '/') && (pos >= 0))
400 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
401 pos += sizeof(".rsync")-1;
403 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
408 /* start where the last one left off to reduce chance of clashes */
411 sprintf(&path[pos], "%03d", counter);
412 if (++counter >= MAX_RENAMES)
414 } while (((rc = access(path, 0)) == 0) && (counter != start));
417 rprintf(FINFO,"renaming %s to %s because of text busy\n",
420 /* maybe we should return rename()'s exit status? Nah. */
421 if (do_rename(fname, path) != 0) {
429 int robust_rename(char *from, char *to)
432 return do_rename(from, to);
434 int rc = do_rename(from, to);
435 if ((rc == 0) || (errno != ETXTBSY))
437 if (robust_unlink(to) != 0)
439 return do_rename(from, to);
444 static pid_t all_pids[10];
447 /* fork and record the pid of the child */
450 pid_t newpid = fork();
453 all_pids[num_pids++] = newpid;
458 /* kill all children */
459 void kill_all(int sig)
462 for (i=0;i<num_pids;i++) {
463 if (all_pids[i] != getpid())
464 kill(all_pids[i], sig);
468 /* turn a user name into a uid */
469 int name_to_uid(char *name, uid_t *uid)
472 if (!name || !*name) return 0;
473 pass = getpwnam(name);
481 /* turn a group name into a gid */
482 int name_to_gid(char *name, gid_t *gid)
485 if (!name || !*name) return 0;
486 grp = getgrnam(name);
495 /* lock a byte range in a open file */
496 int lock_range(int fd, int offset, int len)
500 lock.l_type = F_WRLCK;
501 lock.l_whence = SEEK_SET;
502 lock.l_start = offset;
506 return fcntl(fd,F_SETLK,&lock) == 0;
510 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
512 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
514 argv[*argc] = strdup(s);
518 extern int sanitize_paths;
524 argv[*argc] = strdup(s);
525 if (sanitize_paths) {
526 sanitize_path(argv[*argc], NULL);
529 memset(&globbuf, 0, sizeof(globbuf));
530 glob(argv[*argc], 0, NULL, &globbuf);
531 if (globbuf.gl_pathc == 0) {
536 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
537 if (i == 0) free(argv[*argc]);
538 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
539 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
546 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
548 char *s = argv[*argc];
552 if (!s || !*s) return;
554 if (strncmp(s, base, strlen(base)) == 0) {
559 if (!s) out_of_memory("glob_expand");
561 base = (char *)malloc(strlen(base1)+3);
562 if (!base) out_of_memory("glob_expand");
564 sprintf(base," %s/", base1);
567 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
568 /* split it at this point */
570 glob_expand_one(q, argv, argc, maxargs);
574 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
580 /*******************************************************************
581 convert a string to lower case
582 ********************************************************************/
583 void strlower(char *s)
586 if (isupper(*s)) *s = tolower(*s);
591 /* this is like vsnprintf but it always null terminates, so you
592 can fit at most n-1 chars in */
593 int vslprintf(char *str, int n, const char *format, va_list ap)
595 int ret = vsnprintf(str, n, format, ap);
596 if (ret >= n || ret < 0) {
605 /* like snprintf but always null terminates */
606 int slprintf(char *str, int n, char *format, ...)
611 va_start(ap, format);
612 ret = vslprintf(str,n,format,ap);
618 void *Realloc(void *p, int size)
620 if (!p) return (void *)malloc(size);
621 return (void *)realloc(p, size);
625 void clean_fname(char *name)
636 if ((p=strstr(name,"/./"))) {
644 if ((p=strstr(name,"//"))) {
652 if (strncmp(p=name,"./",2) == 0) {
660 if (l > 1 && p[l-1] == '/') {
668 * Make path appear as if a chroot had occurred:
669 * 1. remove leading "/" (or replace with "." if at end)
670 * 2. remove leading ".." components (except those allowed by "reldir")
671 * 3. delete any other "<dir>/.." (recursively)
672 * Can only shrink paths, so sanitizes in place.
673 * While we're at it, remove double slashes and "." components like
674 * clean_fname does(), but DON'T remove a trailing slash because that
675 * is sometimes significant on command line arguments.
676 * If "reldir" is non-null, it is a sanitized directory that the path will be
677 * relative to, so allow as many ".." at the beginning of the path as
678 * there are components in reldir. This is used for symbolic link targets.
679 * If reldir is non-null and the path began with "/", to be completely like
680 * a chroot we should add in depth levels of ".." at the beginning of the
681 * path, but that would blow the assumption that the path doesn't grow and
682 * it is not likely to end up being a valid symlink anyway, so just do
683 * the normal removal of the leading "/" instead.
684 * Contributed by Dave Dykstra <dwd@bell-labs.com>
687 void sanitize_path(char *p, char *reldir)
696 if (*reldir++ == '/') {
704 /* remove leading slashes */
708 /* this loop iterates once per filename component in p.
709 * both p (and sanp if the original had a slash) should
710 * always be left pointing after a slash
712 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
713 /* skip "." component */
714 while (*++p == '/') {
715 /* skip following slashes */
721 if ((*p == '.') && (*(p+1) == '.') &&
722 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
723 /* ".." component followed by slash or end */
724 if ((depth > 0) && (sanp == start)) {
725 /* allow depth levels of .. at the beginning */
733 /* back up sanp one level */
734 --sanp; /* now pointing at slash */
735 while ((sanp > start) && (*(sanp - 1) != '/')) {
736 /* skip back up to slash */
744 /* copy one component through next slash */
746 if ((*p == '\0') || (*(p-1) == '/')) {
748 /* skip multiple slashes */
755 /* move the virtual beginning to leave the .. alone */
759 if ((sanp == start) && !allowdotdot) {
760 /* ended up with nothing, so put in "." component */
762 * note that the !allowdotdot doesn't prevent this from
763 * happening in all allowed ".." situations, but I didn't
764 * think it was worth putting in an extra variable to ensure
765 * it since an extra "." won't hurt in those situations.
773 static char curr_dir[MAXPATHLEN];
775 /* like chdir() but can be reversed with pop_dir() if save is set. It
776 is also much faster as it remembers where we have been */
777 char *push_dir(char *dir, int save)
779 char *ret = curr_dir;
780 static int initialised;
784 getcwd(curr_dir, sizeof(curr_dir)-1);
787 if (!dir) return NULL; /* this call was probably just to initialize */
789 if (chdir(dir)) return NULL;
792 ret = strdup(curr_dir);
796 strlcpy(curr_dir, dir, sizeof(curr_dir));
798 strlcat(curr_dir,"/", sizeof(curr_dir));
799 strlcat(curr_dir,dir, sizeof(curr_dir));
802 clean_fname(curr_dir);
807 /* reverse a push_dir call */
808 int pop_dir(char *dir)
818 strlcpy(curr_dir, dir, sizeof(curr_dir));
825 /* we need to supply our own strcmp function for file list comparisons
826 to ensure that signed/unsigned usage is consistent between machines. */
827 int u_strcmp(const char *cs1, const char *cs2)
829 const uchar *s1 = (const uchar *)cs1;
830 const uchar *s2 = (const uchar *)cs2;
832 while (*s1 && *s2 && (*s1 == *s2)) {
836 return (int)*s1 - (int)*s2;
839 static OFF_T last_ofs;
841 void end_progress(OFF_T size)
843 extern int do_progress, am_server;
845 if (do_progress && !am_server) {
846 rprintf(FINFO,"%.0f (100%%)\n", (double)size);
851 void show_progress(OFF_T ofs, OFF_T size)
853 extern int do_progress, am_server;
855 if (do_progress && !am_server) {
856 if (ofs > last_ofs + 1000) {
857 int pct = (int)((100.0*ofs)/size);
858 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
864 /* determine if a symlink points outside the current directory tree */
865 int unsafe_symlink(char *dest, char *src)
870 /* all absolute and null symlinks are unsafe */
871 if (!dest || !(*dest) || (*dest == '/')) return 1;
874 if (!src) out_of_memory("unsafe_symlink");
876 /* find out what our safety margin is */
877 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
878 if (strcmp(tok,"..") == 0) {
880 } else if (strcmp(tok,".") == 0) {
888 /* drop by one to account for the filename portion */
892 if (!dest) out_of_memory("unsafe_symlink");
894 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
895 if (strcmp(tok,"..") == 0) {
897 } else if (strcmp(tok,".") == 0) {
902 /* if at any point we go outside the current directory then
903 stop - it is unsafe */
904 if (depth < 0) break;
912 /****************************************************************************
913 return the date and time as a string
914 ****************************************************************************/
915 char *timestring(time_t t)
917 static char TimeBuf[200];
918 struct tm *tm = localtime(&t);
921 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
923 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
926 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
927 TimeBuf[strlen(TimeBuf)-1] = 0;
934 /*******************************************************************
935 sleep for a specified number of milliseconds
936 ********************************************************************/
940 struct timeval tval,t1,t2;
942 gettimeofday(&t1, NULL);
943 gettimeofday(&t2, NULL);
946 tval.tv_sec = (t-tdiff)/1000;
947 tval.tv_usec = 1000*((t-tdiff)%1000);
950 select(0,NULL,NULL, NULL, &tval);
952 gettimeofday(&t2, NULL);
953 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
954 (t2.tv_usec - t1.tv_usec)/1000;
959 /*******************************************************************
960 Determine if two file modification times are equivalent (either exact
961 or in the modification timestamp window established by --modify-window)
962 Returns 0 if the times should be treated as the same, 1 if the
963 first is later and -1 if the 2nd is later
964 *******************************************************************/
965 int cmp_modtime(time_t file1, time_t file2)
967 extern int modify_window;
970 if (file2 - file1 <= modify_window) return 0;
973 if (file1 - file2 <= modify_window) return 0;
981 /*******************************************************************
982 This routine is a trick to immediately catch errors when debugging
983 with insure. A xterm with a gdb is popped up when insure catches
984 a error. It is Linux specific.
985 ********************************************************************/
986 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
992 sprintf(cmd, "/usr/X11R6/bin/xterm -display :0 -T Panic -n Panic -e /bin/sh -c 'cat /tmp/ierrs.*.%d ; gdb /proc/%d/exe %d'",
993 getpid(), getpid(), getpid());
997 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
998 fn = dlsym(h, "_Insure_trap_error");
1001 ret = fn(a1, a2, a3, a4, a5, a6);