Skip to content

Commit

Permalink
Backported fix for CORE-4475: attempt to create lock files directory …
Browse files Browse the repository at this point in the history
…fails during cleanup
  • Loading branch information
AlexPeshkoff committed Jun 30, 2014
1 parent 0aa194a commit cb65a2a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 62 deletions.
3 changes: 2 additions & 1 deletion builds/posix/Makefile.in.client.gfix
Expand Up @@ -39,7 +39,8 @@ include $(ROOT)/gen/make.shared.variables
@SET_MAKE@


GFIX_Other_Sources = jrd/isc.cpp jrd/ThreadData.cpp jrd/path_utils.cpp common/UtilSvc.cpp
GFIX_Other_Sources = jrd/isc.cpp jrd/ThreadData.cpp jrd/path_utils.cpp common/UtilSvc.cpp \
$(OS_SPECIFIC_Sources)

GFIX_Files = alice_meta.epp alice.cpp exe.cpp tdr.cpp aliceMain.cpp
GFIX_Sources = $(addprefix alice/, $(GFIX_Files)) $(GFIX_Other_Sources)
Expand Down
2 changes: 1 addition & 1 deletion builds/posix/Makefile.in.client.util
Expand Up @@ -90,7 +90,7 @@ FBTRACEMGR_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBTRACEMG
$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) $(FBCOMMON_ClientObjects)


FBGUARD_Other_Sources = jrd/isc.cpp jrd/divorce.cpp jrd/path_utils.cpp
FBGUARD_Other_Sources = jrd/isc.cpp jrd/divorce.cpp jrd/path_utils.cpp $(OS_SPECIFIC_Sources)
FBGUARD_Files = guard.cpp util.cpp
FBGUARD_Sources = $(addprefix utilities/guard/, $(FBGUARD_Files)) $(FBGUARD_Other_Sources)
FBGUARD_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBGUARD_Sources)))) $(FBCLASSES_ClientObjects)
Expand Down
3 changes: 2 additions & 1 deletion builds/posix/Makefile.in.embed.gfix
Expand Up @@ -42,7 +42,8 @@ include $(ROOT)/gen/make.shared.variables
GFIX_Files = alice_meta.epp \
alice.cpp exe.cpp tdr.cpp aliceMain.cpp

GFIX_Other_Sources += jrd/isc.cpp jrd/ThreadData.cpp jrd/path_utils.cpp common/UtilSvc.cpp
GFIX_Other_Sources += jrd/isc.cpp jrd/ThreadData.cpp jrd/path_utils.cpp common/UtilSvc.cpp \
$(OS_SPECIFIC_Sources)

GFIX_Sources = $(addprefix alice/, $(GFIX_Files)) $(GFIX_Other_Sources)
GFIX_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(GFIX_Sources))))
Expand Down
2 changes: 1 addition & 1 deletion builds/posix/Makefile.in.embed.util
Expand Up @@ -103,7 +103,7 @@ FBTRACEMGR_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBTRACEMG
$(FBCLASSES_MsgObjects) $(CLUMPLETS_Objects) $(FBCOMMON_ClientObjects)


FBGUARD_Other_Sources = jrd/isc.cpp jrd/divorce.cpp jrd/path_utils.cpp
FBGUARD_Other_Sources = jrd/isc.cpp jrd/divorce.cpp jrd/path_utils.cpp $(OS_SPECIFIC_Sources)
FBGUARD_Files = guard.cpp util.cpp
FBGUARD_Sources = $(addprefix utilities/guard/, $(FBGUARD_Files)) $(FBGUARD_Other_Sources)
FBGUARD_Objects = $(addprefix $(OBJ)/, $(addsuffix .o, $(basename $(FBGUARD_Sources)))) $(FBCLASSES_ClientObjects)
Expand Down
2 changes: 1 addition & 1 deletion builds/posix/prefix.aix_powerpc_xlc
Expand Up @@ -118,7 +118,7 @@ INTL_Files2=common/utils.cpp \
common/classes/MsgPrint.cpp common/classes/SafeArg.cpp \
common/classes/BaseStream.cpp common/classes/fb_string.cpp \
common/classes/TempFile.cpp \
jrd/gds.cpp jrd/isc.cpp \
jrd/gds.cpp jrd/isc.cpp $(OS_SPECIFIC_Sources)\
jrd/os/posix/path_utils.cpp jrd/os/posix/os_utils.cpp \
jrd/os/posix/config_root.cpp jrd/os/posix/fbsyslog.cpp

Expand Down
50 changes: 3 additions & 47 deletions src/jrd/gds.cpp
Expand Up @@ -202,8 +202,6 @@ static int blr_print_word(gds_ctl*);

static void sanitize(Firebird::string& locale);

static void safe_concat_path(TEXT* destbuf, const TEXT* srcbuf);

// New functions that try to be safe.
static SLONG safe_interpret(char* const s, const size_t bufsize,
const ISC_STATUS** const vector, bool legacy = false);
Expand Down Expand Up @@ -1736,7 +1734,7 @@ void API_ROUTINE gds__prefix(TEXT* resultString, const TEXT* file)
GDS_init_prefix();

strcpy(resultString, fb_prefix); // safe - no BO
safe_concat_path(resultString, file);
iscSafeConcatPath(resultString, file);
}


Expand All @@ -1757,12 +1755,7 @@ void API_ROUTINE gds__prefix_lock(TEXT* string, const TEXT* root)
GDS_init_prefix();

strcpy(string, fb_prefix_lock); // safe - no BO

// if someone wants to know prefix for lock files,
// sooner of all he wants that directory to exist
os_utils::createLockDirectory(string);

safe_concat_path(string, root);
iscSafeConcatPath(string, root);
}


Expand All @@ -1786,7 +1779,7 @@ void API_ROUTINE gds__prefix_msg(TEXT* string, const TEXT* root)
GDS_init_prefix();

strcpy(string, fb_prefix_msg); // safe - no BO
safe_concat_path(string, root);
iscSafeConcatPath(string, root);
}


Expand Down Expand Up @@ -3555,43 +3548,6 @@ static void sanitize(Firebird::string& locale)
}
}

static void safe_concat_path(TEXT *resultString, const TEXT *appendString)
{
/**************************************
*
* s a f e _ c o n c a t _ p a t h
*
**************************************
*
* Functional description
* Safely appends appendString to resultString using paths rules.
* resultString must be at most MAXPATHLEN size.
* Thread/signal safe code.
*
**************************************/
size_t len = strlen(resultString);
fb_assert(len > 0);

if (resultString[len - 1] != PathUtils::dir_sep && len < MAXPATHLEN - 1)
{
resultString[len++] = PathUtils::dir_sep;
resultString[len] = 0;
}

size_t alen = strlen(appendString);
if (len + alen > MAXPATHLEN - 1)
{
alen = MAXPATHLEN - 1 - len;
}

fb_assert(len < MAXPATHLEN);
fb_assert(alen < MAXPATHLEN);
fb_assert(len + alen < MAXPATHLEN);

memcpy(&resultString[len], appendString, alen);
resultString[len + alen] = 0;
}


void FB_EXPORTED gds__default_printer(void* /*arg*/, SSHORT offset, const TEXT* line)
{
Expand Down
60 changes: 60 additions & 0 deletions src/jrd/isc.cpp
Expand Up @@ -48,6 +48,8 @@
#include "../jrd/gds_proto.h"
#include "../jrd/isc_proto.h"
#include "../jrd/jrd_proto.h"
#include "../jrd/os/os_utils.h"
#include "../jrd/os/path_utils.h"

#include "../common/classes/init.h"

Expand Down Expand Up @@ -632,3 +634,61 @@ void iscLogException(const char* text, const Firebird::Exception& e)
iscLogStatus(text, s);
}


void iscPrefixLock(TEXT* string, const TEXT* root, bool createLockDir)
{
/**************************************
*
* i s c P r e f i x L o c k
*
**************************************
*
* Functional description
* Find appropriate Firebird lock file prefix.
*
**************************************/
gds__prefix_lock(string, "");

if (createLockDir)
os_utils::createLockDirectory(string);

iscSafeConcatPath(string, root);
}


void iscSafeConcatPath(TEXT *resultString, const TEXT *appendString)
{
/**************************************
*
* i s c S a f e C o n c a t P a t h
*
**************************************
*
* Functional description
* Safely appends appendString to resultString using paths rules.
* resultString must be at most MAXPATHLEN size.
* Thread/signal safe code.
*
**************************************/
size_t len = strlen(resultString);
fb_assert(len > 0);

if (resultString[len - 1] != PathUtils::dir_sep && len < MAXPATHLEN - 1)
{
resultString[len++] = PathUtils::dir_sep;
resultString[len] = 0;
}

size_t alen = strlen(appendString);
if (len + alen > MAXPATHLEN - 1)
{
alen = MAXPATHLEN - 1 - len;
}

fb_assert(len < MAXPATHLEN);
fb_assert(alen < MAXPATHLEN);
fb_assert(len + alen < MAXPATHLEN);

memcpy(&resultString[len], appendString, alen);
resultString[len + alen] = 0;
}
3 changes: 3 additions & 0 deletions src/jrd/isc_proto.h
Expand Up @@ -44,4 +44,7 @@ bool ISC_is_WinNT();
struct _SECURITY_ATTRIBUTES* ISC_get_security_desc();
#endif

void iscPrefixLock(TEXT* string, const TEXT* root, bool createLockDir);
void iscSafeConcatPath(TEXT *resultString, const TEXT *appendString);

#endif // JRD_ISC_PROTO_H
12 changes: 6 additions & 6 deletions src/jrd/isc_sync.cpp
Expand Up @@ -2293,7 +2293,7 @@ void ISC_remove_map_file(const TEXT* filename)
{
#ifndef WIN_NT
TEXT expanded_filename[MAXPATHLEN];
gds__prefix_lock(expanded_filename, filename);
iscPrefixLock(expanded_filename, filename, false);

// We can't do much (specially in dtors) when it fails
// therefore do not check for errors - at least it's just /tmp.
Expand Down Expand Up @@ -2329,15 +2329,15 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
**************************************/

TEXT expanded_filename[MAXPATHLEN];
gds__prefix_lock(expanded_filename, filename);
iscPrefixLock(expanded_filename, filename, true);

/* make the complete filename for the init file this file is to be used as a
master lock to eliminate possible race conditions with just a single file
locking. The race condition is caused as the conversion of a EXCLUSIVE
lock to a LCK_SHARED lock is not atomic*/

TEXT init_filename[MAXPATHLEN];
gds__prefix_lock(init_filename, INIT_FILE);
iscPrefixLock(init_filename, INIT_FILE, true);

const bool trunc_flag = (length != 0);

Expand Down Expand Up @@ -2370,7 +2370,7 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
if (fdSem < 0)
{
TEXT sem_filename[MAXPATHLEN];
gds__prefix_lock(sem_filename, SEM_FILE);
iscPrefixLock(sem_filename, SEM_FILE, true);
const int f = openFile(sem_filename);
if (f == -1)
{
Expand Down Expand Up @@ -2551,7 +2551,7 @@ UCHAR* ISC_map_file(ISC_STATUS* status_vector,
int retry_count = 0;

TEXT expanded_filename[MAXPATHLEN];
gds__prefix_lock(expanded_filename, filename);
iscPrefixLock(expanded_filename, filename, true);

const bool trunc_flag = (length != 0);
bool init_flag = false;
Expand Down Expand Up @@ -4189,7 +4189,7 @@ void ISC_unmap_file(ISC_STATUS* status_vector, sh_mem* shmem_data)
CloseHandle(shmem_data->sh_mem_hdr_object);

TEXT expanded_filename[MAXPATHLEN];
gds__prefix_lock(expanded_filename, shmem_data->sh_mem_name);
iscPrefixLock(expanded_filename, shmem_data->sh_mem_name, false);

// Delete file only if it is not used by anyone else
HANDLE hFile = CreateFile(expanded_filename,
Expand Down
4 changes: 2 additions & 2 deletions src/jrd/svc.cpp
Expand Up @@ -1329,7 +1329,7 @@ ISC_STATUS Service::query2(thread_db* tdbb,
gds__prefix(auxBuf, "");
break;
case isc_info_svc_get_env_lock:
gds__prefix_lock(auxBuf, "");
iscPrefixLock(auxBuf, "", false);
break;
case isc_info_svc_get_env_msg:
gds__prefix_msg(auxBuf, "");
Expand Down Expand Up @@ -1813,7 +1813,7 @@ void Service::query(USHORT send_item_length,
gds__prefix(PathBuffer, "");
break;
case isc_info_svc_get_env_lock:
gds__prefix_lock(PathBuffer, "");
iscPrefixLock(PathBuffer, "", false);
break;
case isc_info_svc_get_env_msg:
gds__prefix_msg(PathBuffer, "");
Expand Down
2 changes: 1 addition & 1 deletion src/jrd/trace/TraceConfigStorage.cpp
Expand Up @@ -236,7 +236,7 @@ void ConfigStorage::checkFile()
fb_assert(m_base->cnt_uses == 0);

char dir[MAXPATHLEN];
gds__prefix_lock(dir, "");
iscPrefixLock(dir, "", true);

PathName filename = TempFile::create("fb_trace_", dir);
filename.copyTo(cfg_file_name, sizeof(m_base->cfg_file_name));
Expand Down
2 changes: 1 addition & 1 deletion src/jrd/trace/TraceLog.cpp
Expand Up @@ -70,7 +70,7 @@ TraceLog::TraceLog(MemoryPool& pool, const PathName& fileName, bool reader) :
}

char dir[MAXPATHLEN];
gds__prefix_lock(dir, "");
iscPrefixLock(dir, "", true);
PathUtils::concatPath(m_baseFileName, dir, fileName);

TraceLogGuard guard(this);
Expand Down

0 comments on commit cb65a2a

Please sign in to comment.