Skip to content

Instantly share code, notes, and snippets.

@imasusen
Last active August 29, 2015 14:10
Show Gist options
  • Save imasusen/3024cd77d183aa7d631a to your computer and use it in GitHub Desktop.
Save imasusen/3024cd77d183aa7d631a to your computer and use it in GitHub Desktop.

Revisions

  1. imasusen revised this gist Dec 1, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ int main() {
    while (read(pipefd[0], buf, bufsize) > 0) {
    output += buf;

    // 出力量が多かったら子プロセスを殺す
    // 出力量が多かったら子プロセスを終わらす
    if (strlen(output.c_str()) > output_limit) {
    kill(pid, SIGXFSZ);
    output += "\n出力過多";
  2. imasusen revised this gist Dec 1, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,8 @@
    using namespace std;

    int main() {
    // パイプ(・㉨・)
    int pipefd[2]; // 0:read, 1:write

    // パイプ
    pipe2(pipefd, O_CLOEXEC);

    const int pid = fork();
  3. imasusen revised this gist Dec 1, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -42,12 +42,15 @@ int main() {

    close(pipefd[1]);

    // 子プロセスが出力してるものを読んでく
    const int bufsize = 1024;
    const int output_limit = 128*1024;
    char buf[bufsize] = "";
    std::string output = "";
    while (read(pipefd[0], buf, bufsize) > 0) {
    output += buf;

    // 出力量が多かったら子プロセスを殺す
    if (strlen(output.c_str()) > output_limit) {
    kill(pid, SIGXFSZ);
    output += "\n出力過多";
  4. imasusen revised this gist Dec 1, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -31,6 +31,7 @@ int main() {
    old_stderr = dup(STDERR_FILENO);
    dup2(pipefd[1], STDOUT_FILENO);
    dup2(pipefd[1], STDERR_FILENO);

    while (1) {
    cout << "a";
    }
  5. imasusen revised this gist Dec 1, 2014. 1 changed file with 22 additions and 20 deletions.
    42 changes: 22 additions & 20 deletions output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -4,44 +4,47 @@
    #include <unistd.h> // pipe2, dup, dup2, fork, read, close
    #include <fcntl.h> // O_CLOEXEC flag
    #include <signal.h> // kill

    #include <iostream>
    #include <string>
    using namespace std;

    int main() {
    int pipefd[2]; // 0:read, 1:write
    int old_stdout, old_stderr;
    std::string output = "";

    // 標準出力、標準エラーをパイプにリダイレクトさせる

    // パイプ
    pipe2(pipefd, O_CLOEXEC);
    old_stdout = dup(STDOUT_FILENO);
    old_stderr = dup(STDERR_FILENO);
    dup2(pipefd[1], STDOUT_FILENO);
    dup2(pipefd[1], STDERR_FILENO);


    const int pid = fork();
    if (pid == -1) {
    perror("fork");
    return -1;
    }
    if (pid == 0) {
    // 子プロセス

    close(pipefd[0]);

    // 標準出力、標準エラーをパイプにリダイレクトさせる
    int old_stdout, old_stderr;
    old_stdout = dup(STDOUT_FILENO);
    old_stderr = dup(STDERR_FILENO);
    dup2(pipefd[1], STDOUT_FILENO);
    dup2(pipefd[1], STDERR_FILENO);
    while (1) {
    cout << "a";
    }
    cout << " (^-^)/" << endl;
    }

    // 親プロセス

    close(pipefd[1]);
    dup2(old_stdout, STDOUT_FILENO);
    dup2(old_stderr, STDERR_FILENO);


    const int bufsize = 1024;
    char buf[bufsize] = "";
    const int output_limit = 128*1024;
    char buf[bufsize] = "";
    std::string output = "";
    while (read(pipefd[0], buf, bufsize) > 0) {
    output += buf;
    if (strlen(output.c_str()) > output_limit) {
    @@ -51,10 +54,9 @@ int main() {
    }
    }
    close(pipefd[0]);
    close(old_stdout);
    close(old_stderr);


    // 子プロセスが出力した内容を表示
    cout << output << endl;

    return 0;
    }
  6. imasusen created this gist Nov 26, 2014.
    60 changes: 60 additions & 0 deletions output_limit.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #include <sys/types.h>
    #include <stdio.h> // perror
    #include <string.h> // strlen
    #include <unistd.h> // pipe2, dup, dup2, fork, read, close
    #include <fcntl.h> // O_CLOEXEC flag
    #include <signal.h> // kill

    #include <iostream>
    #include <string>
    using namespace std;

    int main() {
    int pipefd[2]; // 0:read, 1:write
    int old_stdout, old_stderr;
    std::string output = "";

    // 標準出力、標準エラーをパイプにリダイレクトさせる
    pipe2(pipefd, O_CLOEXEC);
    old_stdout = dup(STDOUT_FILENO);
    old_stderr = dup(STDERR_FILENO);
    dup2(pipefd[1], STDOUT_FILENO);
    dup2(pipefd[1], STDERR_FILENO);

    const int pid = fork();
    if (pid == -1) {
    perror("fork");
    return -1;
    }
    if (pid == 0) {
    // 子プロセス
    while (1) {
    cout << "a";
    }
    cout << " (^-^)/" << endl;
    }

    // 親プロセス
    close(pipefd[1]);
    dup2(old_stdout, STDOUT_FILENO);
    dup2(old_stderr, STDERR_FILENO);

    const int bufsize = 1024;
    char buf[bufsize] = "";
    const int output_limit = 128*1024;
    while (read(pipefd[0], buf, bufsize) > 0) {
    output += buf;
    if (strlen(output.c_str()) > output_limit) {
    kill(pid, SIGXFSZ);
    output += "\n出力過多";
    break;
    }
    }
    close(pipefd[0]);
    close(old_stdout);
    close(old_stderr);

    cout << output << endl;

    return 0;
    }