Skip to content

Commit

Permalink
fix: build source with user permission
Browse files Browse the repository at this point in the history
  • Loading branch information
seo-rii committed Dec 29, 2021
1 parent 201bc2d commit 5461f91
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:16-alpine
EXPOSE 80
RUN apk add --update alpine-sdk runuser
RUN apk add --update alpine-sdk
RUN apk update
COPY dist/ /HANA/dist/
COPY res/ /HANA/res/
Expand Down
2 changes: 1 addition & 1 deletion res/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
<div class="form">
<label
class="mdc-text-field mdc-text-field--outlined mdc-text-field--textarea mdc-text-field--no-label"
style="width: calc(100vw - 20px); margin: 10px 0"
style="width: calc(100vw - 40px); margin: 10px 0"
>
<span class="mdc-notched-outline">
<span class="mdc-notched-outline__leading"></span>
Expand Down
114 changes: 49 additions & 65 deletions src/runner/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (
CommonDataSet
>
) {
return new Promise<JudgeResult>((resolve) => {
return new Promise<JudgeResult>(async (resolve) => {
let match = Array(data.dataSet.data.length).fill(false)
const tmpPath = '/tmp/' + data.uid
const exePath = tmpPath + '/main'
Expand All @@ -28,78 +28,62 @@ export default function (
reason: 'CP',
})

execSync(`adduser --disabled-password --no-create-home p-${data.uid}`)

fs.mkdirSync(tmpPath)
execSync(`chmod 777 ${tmpPath}`)

for (const i of data.source)
fs.writeFileSync(tmpPath + '/' + i.name, i.source)

const cp = spawn('g++', [
`${tmpPath}/main.cpp`,
'-o',
exePath,
'-O2',
'-Wall',
'-lm',
'--static',
'-pipe',
'-std=c++17',
])
let stderr = ''
const result = await execute(
`p-${data.uid}`,
`g++ ${tmpPath}/main.cpp -o ${exePath} -O2 -Wall -lm --static -pipe -std=c++17`,
''
)

cp.stderr.on('data', (data) => {
if (data) stderr += data.toString()
})
if (result.code !== 0) {
fs.rmSync(tmpPath, { recursive: true })
resolve({
uid: data.uid,
result: match,
reason: 'CE',
time: 0,
memory: 0,
message: result.stderr,
})
return
}

cp.on('exit', async (code) => {
if (code !== 0) {
fs.rmdirSync(tmpPath, { recursive: true })
resolve({
uid: data.uid,
result: match,
reason: 'CE',
time: 0,
memory: 0,
message: stderr,
})
return
} else {
execSync(
`adduser --disabled-password --no-create-home p-${data.uid}`
)
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress: 0,
reason: 'RUN',
})
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress: 0,
reason: 'RUN',
})

for (const i in data.dataSet.data) {
const result = await execute(
`p-${data.uid}`,
exePath,
data.dataSet.data[i].input
)
if (isSame(result.stdout, data.dataSet.data[i].output))
match[i] = true
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress:
(i as unknown as number) / data.dataSet.data.length,
reason: 'RUN',
})
}
fs.rmSync(tmpPath, { recursive: true })
resolve({
uid: data.uid,
result: match,
reason:
match.reduce((a, b) => a + b, 0) === match.length
? 'AC'
: 'WA',
time: 0,
memory: 0,
})
execSync(`deluser p-${data.uid}`)
}
for (const i in data.dataSet.data) {
const result = await execute(
`p-${data.uid}`,
exePath,
data.dataSet.data[i].input
)
if (isSame(result.stdout, data.dataSet.data[i].output))
match[i] = true
sendMessage(WebSocketResponseType.JUDGE_PROGRESS, {
uid: data.uid,
progress: (i as unknown as number) / data.dataSet.data.length,
reason: 'RUN',
})
}
fs.rmSync(tmpPath, { recursive: true })
execSync(`deluser p-${data.uid}`)
resolve({
uid: data.uid,
result: match,
reason:
match.reduce((a, b) => a + b, 0) === match.length ? 'AC' : 'WA',
time: 0,
memory: 0,
})
})
}
10 changes: 3 additions & 7 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,9 @@ import { spawn } from 'child_process'
export function execute(userName: string, exePath: string, input: string) {
return new Promise<{ code: number; stdout: string; stderr: string }>(
(resolve, reject) => {
const child = spawn(
`runuser`,
['-l', userName, '-c', `'${exePath}'`],
{
stdio: ['pipe', 'pipe', 'pipe'],
}
)
const child = spawn(`su`, [userName, '-c', exePath], {
stdio: ['pipe', 'pipe', 'pipe'],
})

child.stdin.write(input)
child.stdin.end()
Expand Down

0 comments on commit 5461f91

Please sign in to comment.