X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/c7c11a0d4c315a052dddf97cc6d3d63a54b4a831..6c65e14634d9d2a7b103fe53aa7585b196c38227:/util.c?ds=sidebyside diff --git a/util.c b/util.c index 86ee3f0c..df2af3e8 100644 --- a/util.c +++ b/util.c @@ -1,6 +1,7 @@ -/* - Copyright (C) Andrew Tridgell 1996 - Copyright (C) Paul Mackerras 1996 +/* -*- c-file-style: "linux" -*- + + Copyright (C) 1996-2000 by Andrew Tridgell + Copyright (C) Paul Mackerras 1996 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -26,49 +27,88 @@ extern int verbose; + +/**************************************************************************** +Set a fd into nonblocking mode +****************************************************************************/ +void set_nonblocking(int fd) +{ + int val; + + if((val = fcntl(fd, F_GETFL, 0)) == -1) + return; + if (!(val & NONBLOCK_FLAG)) { + val |= NONBLOCK_FLAG; + fcntl(fd, F_SETFL, val); + } +} + /**************************************************************************** -Set a fd into nonblocking mode. Uses POSIX O_NONBLOCK if available, -else -if SYSV use O_NDELAY -if BSD use FNDELAY +Set a fd into blocking mode ****************************************************************************/ -int set_nonblocking(int fd) +void set_blocking(int fd) { int val; -#ifdef O_NONBLOCK -#define FLAG_TO_SET O_NONBLOCK + + if((val = fcntl(fd, F_GETFL, 0)) == -1) + return; + if (val & NONBLOCK_FLAG) { + val &= ~NONBLOCK_FLAG; + fcntl(fd, F_SETFL, val); + } +} + + +/* create a file descriptor pair - like pipe() but use socketpair if + possible (because of blocking issues on pipes) + + always set non-blocking + */ +int fd_pair(int fd[2]) +{ + int ret; + +#if HAVE_SOCKETPAIR + ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd); #else -#ifdef SYSV -#define FLAG_TO_SET O_NDELAY -#else /* BSD */ -#define FLAG_TO_SET FNDELAY -#endif + ret = pipe(fd); #endif + + if (ret == 0) { + set_nonblocking(fd[0]); + set_nonblocking(fd[1]); + } - if((val = fcntl(fd, F_GETFL, 0)) == -1) - return -1; - val |= FLAG_TO_SET; - return fcntl( fd, F_SETFL, val); -#undef FLAG_TO_SET + return ret; } -/* this is taken from CVS */ -int piped_child(char **command,int *f_in,int *f_out) +/* this is derived from CVS code + + note that in the child STDIN is set to blocking and STDOUT + is set to non-blocking. This is necessary as rsh relies on stdin being blocking + and ssh relies on stdout being non-blocking + + if blocking_io is set then use blocking io on both fds. That can be + used to cope with badly broken rsh implementations like the one on + solaris. + */ +pid_t piped_child(char **command,int *f_in,int *f_out) { - int pid; + pid_t pid; int to_child_pipe[2]; int from_child_pipe[2]; + extern int blocking_io; - if (pipe(to_child_pipe) < 0 || - pipe(from_child_pipe) < 0) { + if (fd_pair(to_child_pipe) < 0 || + fd_pair(from_child_pipe) < 0) { rprintf(FERROR,"pipe: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } pid = do_fork(); - if (pid < 0) { + if (pid == -1) { rprintf(FERROR,"fork: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } @@ -86,6 +126,10 @@ int piped_child(char **command,int *f_in,int *f_out) if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); umask(orig_umask); + set_blocking(STDIN_FILENO); + if (blocking_io) { + set_blocking(STDOUT_FILENO); + } execvp(command[0], command); rprintf(FERROR,"Failed to exec %s : %s\n", command[0],strerror(errno)); @@ -101,27 +145,24 @@ int piped_child(char **command,int *f_in,int *f_out) *f_in = from_child_pipe[0]; *f_out = to_child_pipe[1]; - set_nonblocking(*f_in); - set_nonblocking(*f_out); - return pid; } -int local_child(int argc, char **argv,int *f_in,int *f_out) +pid_t local_child(int argc, char **argv,int *f_in,int *f_out) { - int pid; + pid_t pid; int to_child_pipe[2]; int from_child_pipe[2]; - if (pipe(to_child_pipe) < 0 || - pipe(from_child_pipe) < 0) { + if (fd_pair(to_child_pipe) < 0 || + fd_pair(from_child_pipe) < 0) { rprintf(FERROR,"pipe: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } pid = do_fork(); - if (pid < 0) { + if (pid == -1) { rprintf(FERROR,"fork: %s\n",strerror(errno)); exit_cleanup(RERR_IPC); } @@ -282,7 +323,7 @@ int copy_file(char *source, char *dest, mode_t mode) char buf[1024 * 8]; int len; /* Number of bytes read into `buf'. */ - ifd = open(source, O_RDONLY); + ifd = do_open(source, O_RDONLY, 0); if (ifd == -1) { rprintf(FERROR,"open %s: %s\n", source,strerror(errno)); @@ -397,17 +438,6 @@ int robust_rename(char *from, char *to) return -1; return do_rename(from, to); #endif - } - - -/* sleep for a while via select */ -void u_sleep(int usec) -{ - struct timeval tv; - - tv.tv_sec = 0; - tv.tv_usec = usec; - select(0, NULL, NULL, NULL, &tv); } @@ -477,7 +507,7 @@ int lock_range(int fd, int offset, int len) } -static void glob_expand_one(char *s, char **argv, int *argc, int maxargs, int sanitize_paths) +static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) { #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H)) if (!*s) s = "."; @@ -485,14 +515,16 @@ static void glob_expand_one(char *s, char **argv, int *argc, int maxargs, int sa (*argc)++; return; #else + extern int sanitize_paths; glob_t globbuf; int i; if (!*s) s = "."; - s = strdup(s); - sanitize_path(s); - argv[*argc] = s; + argv[*argc] = strdup(s); + if (sanitize_paths) { + sanitize_path(argv[*argc], NULL); + } memset(&globbuf, 0, sizeof(globbuf)); glob(argv[*argc], 0, NULL, &globbuf); @@ -511,7 +543,7 @@ static void glob_expand_one(char *s, char **argv, int *argc, int maxargs, int sa #endif } -void glob_expand(char *base1, char **argv, int *argc, int maxargs, int sanitize_paths) +void glob_expand(char *base1, char **argv, int *argc, int maxargs) { char *s = argv[*argc]; char *p, *q; @@ -526,20 +558,17 @@ void glob_expand(char *base1, char **argv, int *argc, int maxargs, int sanitize_ s = strdup(s); if (!s) out_of_memory("glob_expand"); - base = (char *)malloc(strlen(base1)+3); - if (!base) out_of_memory("glob_expand"); - - sprintf(base," %s/", base1); + if (asprintf(&base," %s/", base1) <= 0) out_of_memory("glob_expand"); q = s; while ((p = strstr(q,base)) && ((*argc) < maxargs)) { /* split it at this point */ *p = 0; - glob_expand_one(q, argv, argc, maxargs, sanitize_paths); + glob_expand_one(q, argv, argc, maxargs); q = p+strlen(base); } - if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs, sanitize_paths); + if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs); free(s); free(base); @@ -556,33 +585,6 @@ void strlower(char *s) } } -/* this is like vsnprintf but it always null terminates, so you - can fit at most n-1 chars in */ -int vslprintf(char *str, int n, const char *format, va_list ap) -{ - int ret = vsnprintf(str, n, format, ap); - if (ret >= n || ret < 0) { - str[n-1] = 0; - return -1; - } - str[ret] = 0; - return ret; -} - - -/* like snprintf but always null terminates */ -int slprintf(char *str, int n, char *format, ...) -{ - va_list ap; - int ret; - - va_start(ap, format); - ret = vslprintf(str,n,format,ap); - va_end(ap); - return ret; -} - - void *Realloc(void *p, int size) { if (!p) return (void *)malloc(size); @@ -635,19 +637,37 @@ void clean_fname(char *name) /* * Make path appear as if a chroot had occurred: * 1. remove leading "/" (or replace with "." if at end) - * 2. remove leading ".." components + * 2. remove leading ".." components (except those allowed by "reldir") * 3. delete any other "