Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assert aborts in lua parsing #504 #506

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Tuprules.tup
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ LDFLAGS_tup-ldpreload.so += $(SHARED)
LDFLAGS_tup-ldpreload.so += -ldl
LDFLAGS_tup-ldpreload.so += -pthread
endif

ifeq (@(LOCK),dotlock)
CFLAGS += -DUSE_DOTLOCK
endif

19 changes: 19 additions & 0 deletions bootstrap-wsl1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /bin/sh -e
# This is similar to bootstrap.sh, except it uses 'tup generate' to build a
# temporary shell script in case tup needs to be built in an environment that
# doesn't support FUSE. The resulting tup binary will still require FUSE to
# operate (on those platforms where it is used).

# Dependencies:
# libc6-dev

CFLAGS="-g" ./build.sh

if [ ! -d .tup ]; then
./build/tup init
fi
./build/tup generate --verbose --config local-build-configs/wsl1.config build-wsl1.sh
./build-wsl1.sh
echo "Build complete. If ./tup works, you can remove the 'build' directory."


3 changes: 3 additions & 0 deletions local-build-configs/wsl1.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_TUP_SERVER=ldpreload
CONFIG_LOCK=dotlock

8 changes: 8 additions & 0 deletions src/ldpreload/ldpreload.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,14 @@ static int ignore_file(const char *file)
return 1;
if(strncmp(file, "/proc/", 6) == 0)
return 1;
if(strncmp(file, "/var/", 5) == 0)
return 1;
if(strncmp(file, "/run/", 5) == 0)
return 1;
if(strncmp(file, "/tmp/", 5) == 0)
return 1;
if(strncmp(file, "/usr/", 5) == 0)
return 1;
if(is_ccache_path(file))
return 1;
return 0;
Expand Down
10 changes: 9 additions & 1 deletion src/tup/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ static int db_open(void)

if(tup_db)
return 0;
#ifdef USE_DOTLOCK
if(sqlite3_open_v2(TUP_DB_FILE, &tup_db, SQLITE_OPEN_READWRITE, "unix-dotfile") != 0) {
#else
if(sqlite3_open_v2(TUP_DB_FILE, &tup_db, SQLITE_OPEN_READWRITE, NULL) != 0) {
#endif
fprintf(stderr, "Unable to open database: %s\n",
sqlite3_errmsg(tup_db));
return -1;
Expand Down Expand Up @@ -334,7 +338,11 @@ int tup_db_create(int db_sync, int memory_db)
} else {
dbname = TUP_DB_FILE;
}
rc = sqlite3_open(dbname, &tup_db);
#ifdef USE_DOTLOCK
rc = sqlite3_open_v2(dbname, &tup_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "unix-dotfile");
#else
rc = sqlite3_open_v2(dbname, &tup_db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
#endif
if(rc == 0) {
printf(".tup repository initialized: %s\n", dbname);
} else {
Expand Down
115 changes: 115 additions & 0 deletions src/tup/flock/fcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,87 @@
#include <unistd.h>
#include <errno.h>

#ifdef USE_DOTLOCK
#include "tup/config.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <dlfcn.h>

#define dotlock_path_len 256


static int get_dotlockname(int fd, char *name, size_t len)
{
struct stat file_stat;
int ret;
ret = fstat (fd, &file_stat);
if (ret < 0) return -1;

int inode = file_stat.st_ino; // inode now contains inode number of the file with descriptor fd

// todo: .tup from config.h?
//size_t sz = snprintf(name, len, "/run/lock/tup-%d.lock", inode);
size_t sz = snprintf(name, len, "/tmp/tup-%d.lock", inode);
if (sz >= len) return -1;

return 0;
}



static int make_dotlock(int fd, int wait_type)
{
int prev_errno = errno;
int ret = -1;
do {
char dotlockname[dotlock_path_len];
get_dotlockname(fd, dotlockname, dotlock_path_len);
//if ((fdl = open(dotlockname, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
//int fdl = open(dotlockname, O_WRONLY | O_CREAT | O_EXCL, 0666);
int fdl = mkdir(dotlockname, 0666);

if (fdl < 0)
{
if (errno == EEXIST)
{
ret = 1;
}
else
{
ret = -1;
break;
}
}
else
{
ret = 0;
break;
}
//close(fdl);
if (wait_type == 1) sleep(1);
else if (wait_type == 2) usleep(10000);
} while (wait_type > 0);
errno = prev_errno;
return ret;
}

static int remove_dotlock(int fd)
{
char dotlockname[dotlock_path_len];
get_dotlockname(fd, dotlockname, dotlock_path_len);
//if (unlink(dotlockname) < 0)
if (rmdir(dotlockname) < 0)
{
return -1;
}
return 0;
}



#endif

int tup_lock_open(int basefd, const char *lockname, tup_lock_t *lock)
{
int fd;
Expand All @@ -36,6 +117,7 @@ int tup_lock_open(int basefd, const char *lockname, tup_lock_t *lock)
return -1;
}
*lock = fd;

return 0;
}

Expand All @@ -48,6 +130,12 @@ void tup_lock_close(tup_lock_t lock)

int tup_flock(tup_lock_t fd)
{
#ifdef USE_DOTLOCK
//char dotlockname[dotlock_path_len];
//get_dotlockname(fd, dotlockname, dotlock_path_len);
//while (mkdir(dotlockname, 0666) < 0) {
if (make_dotlock(fd, 1) < 0) return -1;
#else
struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
Expand All @@ -59,12 +147,20 @@ int tup_flock(tup_lock_t fd)
perror("fcntl F_WRLCK");
return -1;
}
#endif
return 0;
}

/* Returns: -1 error, 0 got lock, 1 would block */
int tup_try_flock(tup_lock_t fd)
{
#ifdef USE_DOTLOCK
//char dotlockname[dotlock_path_len];
//get_dotlockname(fd, dotlockname, dotlock_path_len);
//if (mkdir(dotlockname, 0666) < 0) {
int mdl = make_dotlock(fd, 0);
if (mdl != 0) return mdl;
#else
struct flock fl = {
.l_type = F_WRLCK,
.l_whence = SEEK_SET,
Expand All @@ -78,11 +174,21 @@ int tup_try_flock(tup_lock_t fd)
perror("fcntl F_WRLCK");
return -1;
}
#endif
return 0;
}

int tup_unflock(tup_lock_t fd)
{
#ifdef USE_DOTLOCK
//char dotlockname[dotlock_path_len];
//get_dotlockname(fd, dotlockname, dotlock_path_len);
//if (rmdir(dotlockname) < 0) {
if (remove_dotlock(fd) < 0) {
perror("rm dotlock");
return -1;
}
#else
struct flock fl = {
.l_type = F_UNLCK,
.l_whence = SEEK_SET,
Expand All @@ -94,11 +200,18 @@ int tup_unflock(tup_lock_t fd)
perror("fcntl F_UNLCK");
return -1;
}
#endif
return 0;
}

int tup_wait_flock(tup_lock_t fd)
{
#ifdef USE_DOTLOCK
//char dotlockname[dotlock_path_len];
//get_dotlockname(fd, dotlockname, dotlock_path_len);
//while (mkdir(dotlockname, 0666) < 0) {
if (make_dotlock(fd, 2) < 0) return -1;
#else
struct flock fl;

while(1) {
Expand All @@ -116,5 +229,7 @@ int tup_wait_flock(tup_lock_t fd)
break;
usleep(10000);
}
#endif
return 0;
}

8 changes: 5 additions & 3 deletions src/tup/luaparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,10 @@ int tup_lua_parser_new_state(void)

int parse_lua_include_rules(struct tupfile *tf)
{
int top = lua_gettop(gls);
if(parser_include_rules(tf, "Tuprules.lua") < 0) {
if(tf->luaerror == TUPLUA_PENDINGERROR) {
assert(lua_gettop(gls) == 2);
assert(lua_gettop(gls) == top + 2);
fprintf(tf->f, "tup error %s\n", tuplua_tostring(gls, -1));
lua_pop(gls, 1);
tf->luaerror = TUPLUA_ERRORSHOWN;
Expand All @@ -830,13 +831,14 @@ int parse_lua_include_rules(struct tupfile *tf)
int parse_lua_tupfile(struct tupfile *tf, struct buf *b, const char *name)
{
struct tuplua_reader_data lrd = {b, 0};
int top = lua_gettop(gls);

lua_getfield(gls, LUA_REGISTRYINDEX, "tup_traceback");

if(lua_load(gls, &tuplua_reader, &lrd, name, 0) != LUA_OK) {
fprintf(tf->f, "tup error %s\n", tuplua_tostring(gls, -1));
tf->luaerror = TUPLUA_PENDINGERROR;
assert(lua_gettop(gls) == 2);
assert(lua_gettop(gls) == top + 2);
return -1;
}

Expand All @@ -849,7 +851,7 @@ int parse_lua_tupfile(struct tupfile *tf, struct buf *b, const char *name)
fprintf(tf->f, "tup error %s\n", tuplua_tostring(gls, -1));
tf->luaerror = TUPLUA_ERRORSHOWN;
lua_pop(gls, 2);
assert(lua_gettop(gls) == 0);
assert(lua_gettop(gls) == top);
return -1;
}

Expand Down