Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #855 from tom-wa/fix_847
Browse files Browse the repository at this point in the history
intercept/open: fix example and warning

fix #847
  • Loading branch information
markus2330 authored Jul 23, 2016
2 parents 44dc7b8 + 7a51684 commit a0dfa23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/bindings/intercept/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ When the an application tries to call `open` or `open64` it canonicalizes the pa
% echo testfile > ~/testfile
% echo testreplacement > ~/testreplacement
% kdb set /preload/open
% kdb set /preload/open/\~\\/testfile "~/testreplacement"
% LD_PRELOAD=/path/to/libelektraintercept.so cat ~/testfile
Expand Down
25 changes: 15 additions & 10 deletions src/bindings/intercept/intercept.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct _Node
typedef struct _Node Node;
static Node * head = NULL;


static void canonicalizePath (char * buffer, char * toAppend)
{
char * destPtr = buffer + strlen (buffer);
Expand Down Expand Up @@ -196,8 +195,15 @@ static Node * resolvePathname (const char * pathname)
return node;
}


typedef int (*orig_open_f_type) (const char * pathname, int flags, ...);

typedef union {
void * d;
orig_open_f_type f;
} Symbol;


int open (const char * pathname, int flags, ...)
{
Node * node = resolvePathname (pathname);
Expand All @@ -216,9 +222,8 @@ int open (const char * pathname, int flags, ...)
{
flags = (flags & (~(0 | O_WRONLY | O_APPEND)));
}

orig_open_f_type orig_open;
orig_open = (orig_open_f_type)dlsym (RTLD_NEXT, "open");
Symbol orig_open;
orig_open.d = dlsym (RTLD_NEXT, "open");

int fd;
if (flags & O_CREAT)
Expand All @@ -228,11 +233,11 @@ int open (const char * pathname, int flags, ...)
va_start (argptr, flags);
mode = va_arg (argptr, int);
va_end (argptr);
fd = orig_open (newPath, flags, mode);
fd = orig_open.f (newPath, flags, mode);
}
else
{
fd = orig_open (newPath, flags);
fd = orig_open.f (newPath, flags);
}
return fd;
}
Expand All @@ -255,8 +260,8 @@ int open64 (const char * pathname, int flags, ...)
flags = (flags & (~(0 | O_WRONLY | O_APPEND)));
}

orig_open_f_type orig_open64;
orig_open64 = (orig_open_f_type)dlsym (RTLD_NEXT, "open64");
Symbol orig_open64;
orig_open64.d = dlsym (RTLD_NEXT, "open64");

int fd;
if (flags & O_CREAT)
Expand All @@ -266,11 +271,11 @@ int open64 (const char * pathname, int flags, ...)
va_start (argptr, flags);
mode = va_arg (argptr, int);
va_end (argptr);
fd = orig_open64 (newPath, flags, mode);
fd = orig_open64.f (newPath, flags, mode);
}
else
{
fd = orig_open64 (newPath, flags);
fd = orig_open64.f (newPath, flags);
}
return fd;
}

0 comments on commit a0dfa23

Please sign in to comment.