Skip to content

Commit

Permalink
Mount bind common library paths
Browse files Browse the repository at this point in the history
To get more accurate memory usage.
  • Loading branch information
quark-zju committed Nov 18, 2014
1 parent 2c386bc commit 2eaaad6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/fs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ bool fs::is_dir(const string& path) {
return S_ISDIR(buf.st_mode) ? 1 : 0;
}

bool fs::is_symlink(const string& path) {
struct stat buf;
if (lstat(path.c_str(), &buf) == -1) return 0;
return S_ISLNK(buf.st_mode) ? 1 : 0;
}

bool fs::is_disconnected(const string& path) {
struct stat buf;
if (stat(path.c_str(), &buf) == -1) {
Expand Down
1 change: 1 addition & 0 deletions src/fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace fs {
std::string extname(const std::string& path);
bool is_disconnected(const std::string& path);
bool is_dir(const std::string& path);
bool is_symlink(const std::string& path);
int mkdir_p(const std::string& dir, const mode_t mode = 0755);
int rm_rf(const std::string& path);
bool is_absolute(const std::string& path);
Expand Down
25 changes: 23 additions & 2 deletions src/ljudge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace j = picojson;
#define EXT_LRUN_ARGS ".lrun_args"
#define EXT_NAME ".name"
#define EXT_OPT_FAKE_PASSWD "fake_passwd"
#define EXT_OPT_BIND_COMMON_LIBS "bind_common_libs"
#define EXT_FS_OVERRIDE ".fs_override"
#define EXT_SRC_NAME ".src_name"

Expand Down Expand Up @@ -439,7 +440,7 @@ static string prepare_dummy_passwd(const string& cache_dir) {
return path;
}

static list<string> get_override_lrun_args(const string& etc_dir, const string& cache_dir, const string& code_path, const string& env, const string& chroot_path) {
static list<string> get_override_lrun_args(const string& etc_dir, const string& cache_dir, const string& code_path, const string& env, const string& chroot_path, const string& interpreter_name = "") {
list<string> result;
// Hide real /etc/passwd (required by Python) on demand
if (fs::exists(fs::join(chroot_path, ETC_PASSWD)) && get_config_content(etc_dir, code_path, format("%s%s", env, EXT_OPT_FAKE_PASSWD), OPTION_VALUE_TRUE) == OPTION_VALUE_TRUE) {
Expand All @@ -449,6 +450,26 @@ static list<string> get_override_lrun_args(const string& etc_dir, const string&
result.push_back(passwd_path);
}

// mount --bind common libraries so that memory counter will be more accurate
// if this step is skipped, cgroup memory counter will add fuse related memory
if (get_config_content(etc_dir, code_path, format("%s%s", env, EXT_OPT_BIND_COMMON_LIBS), OPTION_VALUE_TRUE) == OPTION_VALUE_TRUE) {
const string common_paths[] = {
"/usr/lib", "/etc/alternatives", "/etc/ld.so.cache", "/etc/ld.so.conf", "/etc/ld.so.conf.d", "/lib", "/lib64",
interpreter_name.empty() ? "" : fs::join("/usr/bin", interpreter_name),
interpreter_name.empty() ? "" : fs::join("/etc/alternatives", interpreter_name),
};
for (size_t i = 0; i < sizeof(common_paths) / sizeof(common_paths[0]); ++i) {
const string& path = common_paths[i];
if (path.empty()) continue;
const string chrooted_path = fs::join(chroot_path, path);
if (!fs::exists(chrooted_path) || fs::is_symlink(chrooted_path)) continue;
result.push_back("--bindfs-ro");
result.push_back(chrooted_path);
result.push_back(path);
}
}

// override_dir in config
string override_dir = get_config_path(etc_dir, code_path, format("%s%s", env, EXT_FS_OVERRIDE));
if (override_dir.empty()) return result;

Expand Down Expand Up @@ -1808,7 +1829,7 @@ static LrunResult run_code(
lrun_args.append_default();
lrun_args.append("--chroot", chroot_path);
lrun_args.append("--bindfs-ro", fs::join(chroot_path, "/tmp"), dest);
lrun_args.append(get_override_lrun_args(etc_dir, cache_dir, code_path, ENV_RUN, chroot_path));
lrun_args.append(get_override_lrun_args(etc_dir, cache_dir, code_path, ENV_RUN, chroot_path, run_cmd.size() >= 2 ? (*run_cmd.begin()) : "" ));
lrun_args.append(limit);
lrun_args.append(escape_list(extra_lrun_args, mappings));
lrun_args.append(filter_user_lrun_args(escape_list(get_config_list(etc_dir, code_path, format("%s%s", env, EXT_LRUN_ARGS)), mappings)));
Expand Down

0 comments on commit 2eaaad6

Please sign in to comment.