2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Utilities used in rsync
30 /****************************************************************************
31 Set a fd into nonblocking mode
32 ****************************************************************************/
33 void set_nonblocking(int fd)
37 if((val = fcntl(fd, F_GETFL, 0)) == -1)
39 if (!(val & NONBLOCK_FLAG)) {
41 fcntl(fd, F_SETFL, val);
45 /****************************************************************************
46 Set a fd into blocking mode
47 ****************************************************************************/
48 void set_blocking(int fd)
52 if((val = fcntl(fd, F_GETFL, 0)) == -1)
54 if (val & NONBLOCK_FLAG) {
55 val &= ~NONBLOCK_FLAG;
56 fcntl(fd, F_SETFL, val);
61 /* create a file descriptor pair - like pipe() but use socketpair if
62 possible (because of blocking issues on pipes)
64 always set non-blocking
66 int fd_pair(int fd[2])
71 ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
77 set_nonblocking(fd[0]);
78 set_nonblocking(fd[1]);
85 /* this is derived from CVS code
87 note that in the child STDIN is set to blocking and STDOUT
88 is set to non-blocking. This is necessary as rsh relies on stdin being blocking
89 and ssh relies on stdout being non-blocking
91 if blocking_io is set then use blocking io on both fds. That can be
92 used to cope with badly broken rsh implementations like the one on
95 int piped_child(char **command,int *f_in,int *f_out)
99 int from_child_pipe[2];
100 extern int blocking_io;
102 if (fd_pair(to_child_pipe) < 0 ||
103 fd_pair(from_child_pipe) < 0) {
104 rprintf(FERROR,"pipe: %s\n",strerror(errno));
105 exit_cleanup(RERR_IPC);
111 rprintf(FERROR,"fork: %s\n",strerror(errno));
112 exit_cleanup(RERR_IPC);
117 extern int orig_umask;
118 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
119 close(to_child_pipe[1]) < 0 ||
120 close(from_child_pipe[0]) < 0 ||
121 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
122 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
123 exit_cleanup(RERR_IPC);
125 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
126 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
128 set_blocking(STDIN_FILENO);
130 set_blocking(STDOUT_FILENO);
132 execvp(command[0], command);
133 rprintf(FERROR,"Failed to exec %s : %s\n",
134 command[0],strerror(errno));
135 exit_cleanup(RERR_IPC);
138 if (close(from_child_pipe[1]) < 0 ||
139 close(to_child_pipe[0]) < 0) {
140 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
141 exit_cleanup(RERR_IPC);
144 *f_in = from_child_pipe[0];
145 *f_out = to_child_pipe[1];
150 int local_child(int argc, char **argv,int *f_in,int *f_out)
153 int to_child_pipe[2];
154 int from_child_pipe[2];
156 if (fd_pair(to_child_pipe) < 0 ||
157 fd_pair(from_child_pipe) < 0) {
158 rprintf(FERROR,"pipe: %s\n",strerror(errno));
159 exit_cleanup(RERR_IPC);
165 rprintf(FERROR,"fork: %s\n",strerror(errno));
166 exit_cleanup(RERR_IPC);
170 extern int am_sender;
171 extern int am_server;
173 am_sender = !am_sender;
176 if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 ||
177 close(to_child_pipe[1]) < 0 ||
178 close(from_child_pipe[0]) < 0 ||
179 dup2(from_child_pipe[1], STDOUT_FILENO) < 0) {
180 rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno));
181 exit_cleanup(RERR_IPC);
183 if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]);
184 if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]);
185 start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
188 if (close(from_child_pipe[1]) < 0 ||
189 close(to_child_pipe[0]) < 0) {
190 rprintf(FERROR,"Failed to close : %s\n",strerror(errno));
191 exit_cleanup(RERR_IPC);
194 *f_in = from_child_pipe[0];
195 *f_out = to_child_pipe[1];
202 void out_of_memory(char *str)
204 rprintf(FERROR,"ERROR: out of memory in %s\n",str);
205 exit_cleanup(RERR_MALLOC);
208 void overflow(char *str)
210 rprintf(FERROR,"ERROR: buffer overflow in %s\n",str);
211 exit_cleanup(RERR_MALLOC);
216 int set_modtime(char *fname,time_t modtime)
219 if (dry_run) return 0;
223 tbuf.actime = time(NULL);
224 tbuf.modtime = modtime;
225 return utime(fname,&tbuf);
226 #elif defined(HAVE_UTIME)
230 return utime(fname,t);
233 t[0].tv_sec = time(NULL);
235 t[1].tv_sec = modtime;
237 return utimes(fname,t);
243 /****************************************************************************
244 create any necessary directories in fname. Unfortunately we don't know
245 what perms to give the directory when this is called so we need to rely
247 ****************************************************************************/
248 int create_directory_path(char *fname)
250 extern int orig_umask;
253 while (*fname == '/') fname++;
254 while (strncmp(fname,"./",2)==0) fname += 2;
257 while ((p=strchr(p,'/'))) {
259 do_mkdir(fname,0777 & ~orig_umask);
267 /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted.
268 Return LEN upon success, write's (negative) error code otherwise.
270 derived from GNU C's cccp.c.
272 static int full_write(int desc, char *ptr, int len)
278 int written = write (desc, ptr, len);
286 total_written += written;
290 return total_written;
293 /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted.
294 Return the actual number of bytes read, zero for EOF, or negative
297 derived from GNU C's cccp.c. */
298 static int safe_read(int desc, char *ptr, int len)
307 n_chars = read(desc, ptr, len);
308 } while (n_chars < 0 && errno == EINTR);
310 n_chars = read(desc, ptr, len);
317 /* copy a file - this is used in conjunction with the --temp-dir option */
318 int copy_file(char *source, char *dest, mode_t mode)
323 int len; /* Number of bytes read into `buf'. */
325 ifd = do_open(source, O_RDONLY, 0);
327 rprintf(FERROR,"open %s: %s\n",
328 source,strerror(errno));
332 if (robust_unlink(dest) && errno != ENOENT) {
333 rprintf(FERROR,"unlink %s: %s\n",
334 dest,strerror(errno));
338 ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode);
340 rprintf(FERROR,"open %s: %s\n",
341 dest,strerror(errno));
346 while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) {
347 if (full_write(ofd, buf, len) < 0) {
348 rprintf(FERROR,"write %s: %s\n",
349 dest,strerror(errno));
360 rprintf(FERROR,"read %s: %s\n",
361 source,strerror(errno));
369 Robust unlink: some OS'es (HPUX) refuse to unlink busy files, so
370 rename to <path>/.rsyncNNN instead. Note that successive rsync runs
371 will shuffle the filenames around a bit as long as the file is still
372 busy; this is because this function does not know if the unlink call
373 is due to a new file coming in, or --delete trying to remove old
374 .rsyncNNN files, hence it renames it each time.
376 /* MAX_RENAMES should be 10**MAX_RENAMES_DIGITS */
377 #define MAX_RENAMES_DIGITS 3
378 #define MAX_RENAMES 1000
380 int robust_unlink(char *fname)
383 return do_unlink(fname);
385 static int counter = 1;
387 char path[MAXPATHLEN];
389 rc = do_unlink(fname);
390 if ((rc == 0) || (errno != ETXTBSY))
393 strlcpy(path, fname, MAXPATHLEN);
396 while((path[--pos] != '/') && (pos >= 0))
399 strlcpy(&path[pos], ".rsync", MAXPATHLEN-pos);
400 pos += sizeof(".rsync")-1;
402 if (pos > (MAXPATHLEN-MAX_RENAMES_DIGITS-1)) {
407 /* start where the last one left off to reduce chance of clashes */
410 sprintf(&path[pos], "%03d", counter);
411 if (++counter >= MAX_RENAMES)
413 } while (((rc = access(path, 0)) == 0) && (counter != start));
416 rprintf(FINFO,"renaming %s to %s because of text busy\n",
419 /* maybe we should return rename()'s exit status? Nah. */
420 if (do_rename(fname, path) != 0) {
428 int robust_rename(char *from, char *to)
431 return do_rename(from, to);
433 int rc = do_rename(from, to);
434 if ((rc == 0) || (errno != ETXTBSY))
436 if (robust_unlink(to) != 0)
438 return do_rename(from, to);
443 static pid_t all_pids[10];
446 /* fork and record the pid of the child */
449 pid_t newpid = fork();
452 all_pids[num_pids++] = newpid;
457 /* kill all children */
458 void kill_all(int sig)
461 for (i=0;i<num_pids;i++) {
462 if (all_pids[i] != getpid())
463 kill(all_pids[i], sig);
467 /* turn a user name into a uid */
468 int name_to_uid(char *name, uid_t *uid)
471 if (!name || !*name) return 0;
472 pass = getpwnam(name);
480 /* turn a group name into a gid */
481 int name_to_gid(char *name, gid_t *gid)
484 if (!name || !*name) return 0;
485 grp = getgrnam(name);
494 /* lock a byte range in a open file */
495 int lock_range(int fd, int offset, int len)
499 lock.l_type = F_WRLCK;
500 lock.l_whence = SEEK_SET;
501 lock.l_start = offset;
505 return fcntl(fd,F_SETLK,&lock) == 0;
509 static void glob_expand_one(char *s, char **argv, int *argc, int maxargs)
511 #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H))
513 argv[*argc] = strdup(s);
517 extern int sanitize_paths;
523 argv[*argc] = strdup(s);
524 if (sanitize_paths) {
525 sanitize_path(argv[*argc], NULL);
528 memset(&globbuf, 0, sizeof(globbuf));
529 glob(argv[*argc], 0, NULL, &globbuf);
530 if (globbuf.gl_pathc == 0) {
535 for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) {
536 if (i == 0) free(argv[*argc]);
537 argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]);
538 if (!argv[(*argc) + i]) out_of_memory("glob_expand");
545 void glob_expand(char *base1, char **argv, int *argc, int maxargs)
547 char *s = argv[*argc];
551 if (!s || !*s) return;
553 if (strncmp(s, base, strlen(base)) == 0) {
558 if (!s) out_of_memory("glob_expand");
560 base = (char *)malloc(strlen(base1)+3);
561 if (!base) out_of_memory("glob_expand");
563 sprintf(base," %s/", base1);
566 while ((p = strstr(q,base)) && ((*argc) < maxargs)) {
567 /* split it at this point */
569 glob_expand_one(q, argv, argc, maxargs);
573 if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs);
579 /*******************************************************************
580 convert a string to lower case
581 ********************************************************************/
582 void strlower(char *s)
585 if (isupper(*s)) *s = tolower(*s);
590 /* this is like vsnprintf but it always null terminates, so you
591 can fit at most n-1 chars in */
592 int vslprintf(char *str, int n, const char *format, va_list ap)
594 int ret = vsnprintf(str, n, format, ap);
595 if (ret >= n || ret < 0) {
604 /* like snprintf but always null terminates */
605 int slprintf(char *str, int n, char *format, ...)
610 va_start(ap, format);
611 ret = vslprintf(str,n,format,ap);
617 void *Realloc(void *p, int size)
619 if (!p) return (void *)malloc(size);
620 return (void *)realloc(p, size);
624 void clean_fname(char *name)
635 if ((p=strstr(name,"/./"))) {
643 if ((p=strstr(name,"//"))) {
651 if (strncmp(p=name,"./",2) == 0) {
659 if (l > 1 && p[l-1] == '/') {
667 * Make path appear as if a chroot had occurred:
668 * 1. remove leading "/" (or replace with "." if at end)
669 * 2. remove leading ".." components (except those allowed by "reldir")
670 * 3. delete any other "<dir>/.." (recursively)
671 * Can only shrink paths, so sanitizes in place.
672 * While we're at it, remove double slashes and "." components like
673 * clean_fname does(), but DON'T remove a trailing slash because that
674 * is sometimes significant on command line arguments.
675 * If "reldir" is non-null, it is a sanitized directory that the path will be
676 * relative to, so allow as many ".." at the beginning of the path as
677 * there are components in reldir. This is used for symbolic link targets.
678 * If reldir is non-null and the path began with "/", to be completely like
679 * a chroot we should add in depth levels of ".." at the beginning of the
680 * path, but that would blow the assumption that the path doesn't grow and
681 * it is not likely to end up being a valid symlink anyway, so just do
682 * the normal removal of the leading "/" instead.
683 * Contributed by Dave Dykstra <dwd@bell-labs.com>
686 void sanitize_path(char *p, char *reldir)
695 if (*reldir++ == '/') {
703 /* remove leading slashes */
707 /* this loop iterates once per filename component in p.
708 * both p (and sanp if the original had a slash) should
709 * always be left pointing after a slash
711 if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) {
712 /* skip "." component */
713 while (*++p == '/') {
714 /* skip following slashes */
720 if ((*p == '.') && (*(p+1) == '.') &&
721 ((*(p+2) == '/') || (*(p+2) == '\0'))) {
722 /* ".." component followed by slash or end */
723 if ((depth > 0) && (sanp == start)) {
724 /* allow depth levels of .. at the beginning */
732 /* back up sanp one level */
733 --sanp; /* now pointing at slash */
734 while ((sanp > start) && (*(sanp - 1) != '/')) {
735 /* skip back up to slash */
743 /* copy one component through next slash */
745 if ((*p == '\0') || (*(p-1) == '/')) {
747 /* skip multiple slashes */
754 /* move the virtual beginning to leave the .. alone */
758 if ((sanp == start) && !allowdotdot) {
759 /* ended up with nothing, so put in "." component */
761 * note that the !allowdotdot doesn't prevent this from
762 * happening in all allowed ".." situations, but I didn't
763 * think it was worth putting in an extra variable to ensure
764 * it since an extra "." won't hurt in those situations.
772 static char curr_dir[MAXPATHLEN];
774 /* like chdir() but can be reversed with pop_dir() if save is set. It
775 is also much faster as it remembers where we have been */
776 char *push_dir(char *dir, int save)
778 char *ret = curr_dir;
779 static int initialised;
783 getcwd(curr_dir, sizeof(curr_dir)-1);
786 if (!dir) return NULL; /* this call was probably just to initialize */
788 if (chdir(dir)) return NULL;
791 ret = strdup(curr_dir);
795 strlcpy(curr_dir, dir, sizeof(curr_dir));
797 strlcat(curr_dir,"/", sizeof(curr_dir));
798 strlcat(curr_dir,dir, sizeof(curr_dir));
801 clean_fname(curr_dir);
806 /* reverse a push_dir call */
807 int pop_dir(char *dir)
817 strlcpy(curr_dir, dir, sizeof(curr_dir));
824 /* we need to supply our own strcmp function for file list comparisons
825 to ensure that signed/unsigned usage is consistent between machines. */
826 int u_strcmp(const char *cs1, const char *cs2)
828 const uchar *s1 = (const uchar *)cs1;
829 const uchar *s2 = (const uchar *)cs2;
831 while (*s1 && *s2 && (*s1 == *s2)) {
835 return (int)*s1 - (int)*s2;
838 static OFF_T last_ofs;
840 void end_progress(OFF_T size)
842 extern int do_progress, am_server;
844 if (do_progress && !am_server) {
845 rprintf(FINFO,"%.0f (100%%)\n", (double)size);
850 void show_progress(OFF_T ofs, OFF_T size)
852 extern int do_progress, am_server;
854 if (do_progress && !am_server) {
855 if (ofs > last_ofs + 1000) {
856 int pct = (int)((100.0*ofs)/size);
857 rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct);
863 /* determine if a symlink points outside the current directory tree */
864 int unsafe_symlink(char *dest, char *src)
869 /* all absolute and null symlinks are unsafe */
870 if (!dest || !(*dest) || (*dest == '/')) return 1;
873 if (!src) out_of_memory("unsafe_symlink");
875 /* find out what our safety margin is */
876 for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) {
877 if (strcmp(tok,"..") == 0) {
879 } else if (strcmp(tok,".") == 0) {
887 /* drop by one to account for the filename portion */
891 if (!dest) out_of_memory("unsafe_symlink");
893 for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) {
894 if (strcmp(tok,"..") == 0) {
896 } else if (strcmp(tok,".") == 0) {
901 /* if at any point we go outside the current directory then
902 stop - it is unsafe */
903 if (depth < 0) break;
911 /****************************************************************************
912 return the date and time as a string
913 ****************************************************************************/
914 char *timestring(time_t t)
916 static char TimeBuf[200];
917 struct tm *tm = localtime(&t);
920 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
922 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf));
925 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
926 TimeBuf[strlen(TimeBuf)-1] = 0;
933 /*******************************************************************
934 sleep for a specified number of milliseconds
935 ********************************************************************/
939 struct timeval tval,t1,t2;
941 gettimeofday(&t1, NULL);
942 gettimeofday(&t2, NULL);
945 tval.tv_sec = (t-tdiff)/1000;
946 tval.tv_usec = 1000*((t-tdiff)%1000);
949 select(0,NULL,NULL, NULL, &tval);
951 gettimeofday(&t2, NULL);
952 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
953 (t2.tv_usec - t1.tv_usec)/1000;
961 /*******************************************************************
962 This routine is a trick to immediately catch errors when debugging
963 with insure. A xterm with a gdb is popped up when insure catches
964 a error. It is Linux specific.
965 ********************************************************************/
966 int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
972 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'",
973 getpid(), getpid(), getpid());
977 h = dlopen("/usr/local/parasoft/insure++lite/lib.linux2/libinsure.so", RTLD_LAZY);
978 fn = dlsym(h, "_Insure_trap_error");
981 ret = fn(a1, a2, a3, a4, a5, a6);