Skip to content

Commit

Permalink
watcher_unix: support symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Dust1404 committed Aug 10, 2023
1 parent e2a7bc7 commit 9bf8ced
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions unix/watcher_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstdlib>
#include <cstring>
#include <linux/elf.h>
#include <linux/limits.h>
#include <string>
#include <sys/fcntl.h>
#include <sys/resource.h>
Expand Down Expand Up @@ -96,3 +97,6 @@ ssize_t calculateStaticMemoryUsage(const std::string &fileName) {
ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * 1024; }

size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss * 1024; }

char fileNameBuf[PATH_MAX];
size_t maxPathLength = PATH_MAX;
5 changes: 5 additions & 0 deletions unix/watcher_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include <limits.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <mach-o/utils.h>
#include <string>
#include <sys/fcntl.h>
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/syslimits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -147,3 +149,6 @@ ssize_t calculateStaticMemoryUsage(const std::string &fileName) {
ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * (isAppleSilicon ? 4 : 1); }

size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss / (isAppleSilicon ? 4 : 1); }

char fileNameBuf[MAX_INPUT];
size_t maxPathLength = MAX_INPUT;
18 changes: 17 additions & 1 deletion unix/watcher_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern void initWatcher();
extern ssize_t calculateStaticMemoryUsage(const std::string &);
extern ssize_t getMemoryRLimit(ssize_t memoryLimitInMB);
extern size_t getMaxRSSInByte(long ru_maxrss);
extern char fileNameBuf[];
extern size_t maxPathLength;

/**
* argv[1]: QString("\"%1\" %2").arg(executableFile, arguments) in `judgingthread.cpp` 执行命令
Expand All @@ -53,7 +55,21 @@ auto main(int /*argc*/, char *argv[]) -> int {
std::string fileName(argv[1]);
fileName = fileName.substr(1);
fileName = fileName.substr(0, fileName.find("\""));
// TODO: 处理符号链接

int rc = readlink(fileName.c_str(), fileNameBuf, maxPathLength);
if (rc > 0) {
fileNameBuf[rc] = '\0';
if (fileNameBuf[0] == '/') { // absolute path
fileName = fileNameBuf;
} else { // relative path
size_t dir_end = fileName.find_last_of('/');
if (dir_end == std::string::npos) {
fileName = std::string("./") + fileNameBuf;
} else {
fileName = fileName.substr(0, dir_end + 1) + fileNameBuf;
}
}
}

if (memoryLimit > 0) {
ssize_t staticMemoryUsage = calculateStaticMemoryUsage(fileName);
Expand Down

0 comments on commit 9bf8ced

Please sign in to comment.