Skip to content

Commit

Permalink
Pass in vardict as a filename rather than an open file descriptor.
Browse files Browse the repository at this point in the history
  • Loading branch information
gittup committed Dec 17, 2015
1 parent 7eb2192 commit fdae134
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 63 deletions.
26 changes: 1 addition & 25 deletions src/dllinject/dllinject.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ static const wchar_t *wcscasestr(const wchar_t *arg1, const wchar_t *arg2);
static char s_depfilename[PATH_MAX];
static char s_vardict_file[PATH_MAX];
static HANDLE deph = INVALID_HANDLE_VALUE;
static HANDLE vardicth = INVALID_HANDLE_VALUE;

static int writef(const char *data, unsigned int len)
{
Expand Down Expand Up @@ -1776,19 +1775,6 @@ static int open_file(const char *depfilename)
return 0;
}

static int open_vardict_file(const char *vardict_file)
{
vardicth = CreateFileA(vardict_file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
if(vardicth == INVALID_HANDLE_VALUE) {
/* Not an error if the file doesn't exist - we may not have a vardict. */
if(GetLastError() != ERROR_FILE_NOT_FOUND) {
fprintf(stderr, "tup error: Unable to open vardict file '%s' in dllinject. Windows error code: 0x%08lx\n", vardict_file, GetLastError());
return -1;
}
}
return 0;
}

/* -------------------------------------------------------------------------- */

BOOL WINAPI DllMain(HANDLE HDllHandle, DWORD Reason, LPVOID Reserved)
Expand All @@ -1806,8 +1792,6 @@ DWORD tup_inject_init(remote_thread_t* r)
{
static int initialised = 0;
char filename[MAX_PATH];
char vardict_env[64];
int vardict_fd = -1;
OSVERSIONINFO osinfo;

if (initialised)
Expand All @@ -1832,15 +1816,7 @@ DWORD tup_inject_init(remote_thread_t* r)

if (open_file(r->depfilename))
return 1;
if (open_vardict_file(r->vardict_file))
return 1;

if(vardicth != INVALID_HANDLE_VALUE) {
vardict_fd = _open_osfhandle((intptr_t)vardicth, 0);
}
snprintf(vardict_env, sizeof(vardict_env), TUP_VARDICT_NAME "=%i", vardict_fd);
vardict_env[sizeof(vardict_env)-1] = 0;
putenv(vardict_env);
_putenv_s(TUP_VARDICT_NAME, r->vardict_file);

strcpy(s_depfilename, r->depfilename);
strcpy(s_vardict_file, r->vardict_file);
Expand Down
25 changes: 6 additions & 19 deletions src/tup/server/master_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ static int master_fork_loop(void)
struct execmsg em;
pthread_attr_t attr;
int null_fd;
int vardict_fd = -2;
char job[PATH_MAX];
char dir[PATH_MAX];
char vardict_file[PATH_MAX];
Expand Down Expand Up @@ -591,14 +590,6 @@ static int master_fork_loop(void)
return -1;
if(read_all(msd[0], vardict_file, em.vardictlen) < 0)
return -1;
vardict_fd = open(vardict_file, O_RDONLY);
if(vardict_fd < 0) {
if(errno != ENOENT) {
perror(vardict_file);
fprintf(stderr, "tup error: Unable to open the vardict file in master_fork\n");
return -1;
}
}

waiter = malloc(sizeof *waiter);
if(!waiter) {
Expand All @@ -617,15 +608,17 @@ static int master_fork_loop(void)
char **envp;
char **curp;
char *curenv;
char fd_name[64];
char full_vardict_file[PATH_MAX];

if(close(msd[0]) < 0) {
perror("close(msd[0])");
exit(1);
}

snprintf(fd_name, sizeof(fd_name), TUP_VARDICT_NAME "=%i", vardict_fd);
fd_name[63] = 0;
if(snprintf(full_vardict_file, sizeof(full_vardict_file), TUP_VARDICT_NAME "=%s/%s", get_tup_top(), vardict_file) >= (signed)sizeof(full_vardict_file)) {
fprintf(stderr, "tup error: full_vardict_file is sized incorrectly.\n");
exit(1);
}

/* +1 for the vardict variable, and +1 for the terminating
* NULL pointer.
Expand All @@ -645,7 +638,7 @@ static int master_fork_loop(void)
curp++;
curenv += strlen(curenv) + 1;
}
*curp = fd_name;
*curp = full_vardict_file;
curp++;
*curp = NULL;

Expand All @@ -655,12 +648,6 @@ static int master_fork_loop(void)
perror("execl");
exit(1);
}
if(vardict_fd >= 0) {
if(close(vardict_fd) < 0) {
perror("close(vardict_fd)");
return -1;
}
}
waiter->pid = pid;
waiter->sid = em.sid;
pthread_create(&pt, &attr, child_waiter, waiter);
Expand Down
28 changes: 17 additions & 11 deletions src/tup/vardict.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>

Expand All @@ -50,17 +51,22 @@ int tup_vardict_init(void)
TUP_VARDICT_NAME);
return -1;
}
fd = strtol(path, NULL, 0);
if(fd <= 0) {
/* If we don't have a vardict file, it's because there are
* no variables.
*/
tup_vars.len = 0;
tup_vars.num_entries = 0;
tup_vars.offsets = NULL;
tup_vars.entries = NULL;
tup_vars.map = NULL;
return 0;
fd = open(path, O_RDONLY);
if(fd < 0) {
if(errno == ENOENT) {
/* If we don't have a vardict file, it's because there are
* no variables.
*/
tup_vars.len = 0;
tup_vars.num_entries = 0;
tup_vars.offsets = NULL;
tup_vars.entries = NULL;
tup_vars.map = NULL;
return 0;
}
perror(path);
fprintf(stderr, "tup client error: Couldn't open vardict file '%s'\n", path);
return -1;
}

if(fstat(fd, &buf) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/t4047-open-fds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ update

# On Gentoo, stdout points to output-0, while on Ubuntu, it points to the
# redirected file (fds.txt). This might be a bash vs dash thing.
text=`cat fds.txt | grep -v ' 0 .*/dev/null' | grep -v ' 1 .*output-' | grep -v ' 1 .*fds.txt' | grep -v ' 2 .*errors' | grep -v '/\.tup/vardict'`
text=`cat fds.txt | grep -v ' 0 .*/dev/null' | grep -v ' 1 .*output-' | grep -v ' 1 .*fds.txt' | grep -v ' 2 .*errors'`
if [ "$text" != "total 0" ]; then
echo "Error: These fds shouldn't be open: $text" 1>&2
exit 1
Expand Down
15 changes: 8 additions & 7 deletions tup_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,17 @@ def config_var(key):
pass

if tup_vardict_num is None:
fd = os.getenv('tup_vardict');
if fd is None:
path = os.getenv('tup_vardict');
if path is None:
raise Exception('tup error: This python module can only be used as a sub-process from tup')

if int(fd) < 0 or os.fstat(int(fd)).st_size == 0:
tup_vardict_num = 0
if os.path.exists(path):
with open(path) as f:
tup_vardict_map = mmap.mmap(f.fileno(), 0, mmap.MAP_PRIVATE, mmap.PROT_READ);
tup_vardict_num = struct.unpack("@i", tup_vardict_map.read(4))[0];
tup_entry_size = (tup_vardict_num + 1) * 4
else:
tup_vardict_map = mmap.mmap(int(fd), 0, mmap.MAP_PRIVATE, mmap.PROT_READ);
tup_vardict_num = struct.unpack("@i", tup_vardict_map.read(4))[0];
tup_entry_size = (tup_vardict_num + 1) * 4
tup_vardict_num = 0

left = -1
right = tup_vardict_num
Expand Down

0 comments on commit fdae134

Please sign in to comment.