1

クラスで作成したシェルにキーロガーを実装する必要があるという問題があります。子プロセスが作成され、execlp() が実行された後、while ループ内でプログラムの流れを取得してループを継続するのに問題があります。

これは、私が問題を抱えている部分で作業するために作成した簡単なプログラムです..私のメインプログラム、pipe.cには、ユーザーからの入力を取得し続ける「べき」whileループを持つ親/子プロセスが含まれていますfgets()、子プロセスの作成、dup2() の使用、stdout への書き込み、次に子プロセスが receive.c 実行可能ファイルを呼び出し、stdin から入力を取得して表示します。

/* file: pipe.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
  int key_logger_on = 0;
  int p[2];
  pid_t pid;
  char str[256];
  char input[1024];
  int status;
  char * file = "test.txt";

  printf("Input :: ");
  while(fgets(input, sizeof(input), stdin)) {

    if (pipe(p)==-1) {
      perror("Pipe create error");
      exit(1);
    }

    if ((pid=fork())==-1) {
      perror("Fork create error");
      exit(1);
    }

    if (pid==0) {
      close(p[1]);  // Close write
      dup2(p[0],0);
      close(p[0]);
      execlp("receive",file,NULL);
   }

    else {
      close(p[0]);  // Close read
      fflush(stdout);
      dup2(p[1],1);
      close(p[1]);
      write(1, input, strlen(input)+1);
      waitpid(pid, NULL, 0);
    }
    printf("Input :: ");
  }
}

以下は、入力の stdin を取得して表示する単純な receive.c です。このファイルは、パラメーターを渡すテストにすぎません。

/* file: receive.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  char input[256];
  fgets(input, sizeof(input), stdin);
  printf("FILE: %s  RECEIVE: %s", argv[0],input);
  return 0;
}

現時点では、これが最初に実行されたとき、入力を取得し、それを標準出力に送信し、子が受信を呼び出し、入力を出力し、親プログラム全体が終了し、while ループが無視され、すべてが単に実行されるだけです。終了します。私はフォークとパイプに非常に慣れていないので、これに対処するのは非常にイライラします! 初めてここに質問を投稿させられました!事前にどうもありがとうございました。

4

1 に答える 1

1

今日は繰り返しの課題としてやりました。このコードを確認してください。私もあなたの受信でそれをテストしました:

#define PREAD 0
#define PWRITE 1

/*
 * 
 */

    int main(int argc, char** argv) {

        int key_logger_on = 0;
        int pIn[2];
        int pOut[2]; 
        pid_t pid;
        char str[256];
        char input[1024] = "";
        int status;

        char file[] = "test.txt";
        char buf;
        printf("Input :: ");
        while (fgets(input,sizeof(input),stdin)) {

            char nChar;
            int nResult;

            if (pipe(pIn) < 0) {
                perror("allocating pipe for child input redirect");
                return -1;
            }
            if (pipe(pOut) < 0) {
                close(pIn[PREAD]);
                close(pIn[PWRITE]);
                perror("allocating pipe for child output redirect");
                return -1;
            }

            pid = fork();
            if ( pid==0) {
                // child continues here

                // redirect stdin
                if (dup2(pIn[PREAD], 0) == -1) {
                    perror("stdin");
                    return -1;
                }

                // redirect stdout
                if (dup2(pOut[PWRITE], 1) == -1) {
                    perror("stdout");
                    return -1;
                }

                // redirect stderr
                if (dup2(pOut[PWRITE], 2) == -1) {
                    perror("stderr");
                    return -1;
                }

                // all these are for use by parent only
                close(pIn[PREAD]);
                close(pIn[PWRITE]);
                close(pOut[PREAD]);
                close(pOut[PWRITE]);

                // run child process image
                nResult = execl("receive",file,NULL);

                exit(nResult);
            } else if (pid > 0) {
                // parent continues here

                // close unused file descriptors, these are for child only
                close(pIn[PREAD]);
                close(pOut[PWRITE]);

                write(pIn[PWRITE], input, strlen(input));

                // char by char reading
                while (read(pOut[PREAD], &nChar, 1) == 1) {
                    write(STDOUT_FILENO, &nChar, 1);
                }

                // close we done
                close(pIn[PWRITE]);
                close(pOut[PREAD]);
            }
            printf("Input :: ");
        }
    }
于 2013-05-11T12:36:51.093 に答える