From 146fddfdb1876d311653042a5545a729a9692725 Mon Sep 17 00:00:00 2001 From: piaip Date: Sat, 27 Jun 2009 17:37:42 +0000 Subject: * make Rename() return real status result when SIGCHLD was set to SIG_IGN. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4706 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- common/sys/file.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'common') diff --git a/common/sys/file.c b/common/sys/file.c index 9e993905..b86899dd 100644 --- a/common/sys/file.c +++ b/common/sys/file.c @@ -198,26 +198,35 @@ int copy_file(const char *src, const char *dst) return copy_file_to_file(src, dst); } +#include int Rename(const char *src, const char *dst) { + pid_t pid; + sig_t s; + int ret = -1; + if (rename(src, dst) == 0) return 0; - if (!strchr(src, ';') && !strchr(dst, ';')) + + // prevent malicious shell escapes + if (strchr(src, ';') || !strchr(dst, ';')) + return -1; + + // because we need the return value, override the signal handler + s = signal(SIGCHLD, NULL); + pid = fork(); + + if (pid == 0) + execl("/bin/mv", "mv", "-f", src, dst, (char *)NULL); + else if (pid > 0) { - pid_t pid = fork(); - if (pid == 0) - execl("/bin/mv", "mv", "-f", src, dst, (char *)NULL); - else if (pid > 0) - { - int status = -1; - waitpid(pid, &status, 0); - return WEXITSTATUS(status) == 0 ? 0 : -1; - } - else - return -1; + waitpid(pid, &ret, 0); + ret = WEXITSTATUS(ret) == 0 ? 0 : -1; } - return -1; + + signal(SIGCHLD, s); + return ret; } int -- cgit v1.2.3