-1
이 질문을하기 전에 많은 게시물을 읽으면서 불행히도 그들 중 누구도 내 문제에 대해 필요한 해결책을 제시하지 못했습니다. gnu c로 mpi 프로그램을 작성하고 이전 하위 프로세스에서 hping3을 실행하려고합니다. 실제 상태는 내가 아이를 생성 할 수있는, 어쩌면 hping3 명령을 실행하지만, 아이의 파이프로 연결된 출력은 나에게 hping3에서 할 수있는 다음과 같은 오류 메시지가 있습니다 : execv를 사용하여 sudo로 프로그램을 실행하는 방법은 무엇입니까?
"you must specify only one target host at a time"
내가했던 다음 : 처음에는 내 매개 변수를 채우기 위해 여러 개의 char 배열을 만들었습니다. 왜 배열입니까? 나는 PARAMS의
//Parameter list for hping3 fork, maxlength precalculated (tcp restrictions and given params for hping)
char sudo[] = "/usr/bin/sudo";
char path[] = "/usr/sbin/hping3";
char scan[] = "--scan";
char port[] = "65535";
char ip[] = "192.168.111.111";
char verbose[] = "-V";
char *params[7];
params[0]=sudo;
params[1]=path;
params[2]=scan;
params[3]=port;
params[4]=ip;
params[5]=verbose;
params[6]=NULL;
변경은 다음과 같습니다 런타임 (반복 루프) 동안 포트와 IP를 변경하려면 :
sprintf(*(params + 3), "%u", *(portarray+i));
sprintf(*(params + 4), "%u.%u.%u.%u", (iterator & 0xFF000000)>>24, (iterator & 0x00FF0000)>>16, (iterator & 0x0000FF00)>>8, (iterator & 0x000000FF));
그리고 마지막으로 포크.
if(pid == -1){
perror("Error forking");
} else if(pid > 0){
//Parent does not write
close(pipes[1]);
//Status Message
printf("Pipe Output ...");
fflush(stdout);
//Parent, wait for child
//waitpid(pid, &status, 0);
//Save stdout from pipe
while((nbytes = read(pipes[0], buffer+count, buffsize-count)) != -1) count+=nbytes;
//Print out pipe
printf("hping3: (%.*s)\n", nbytes, buffer);
fflush(stdout);
wait(NULL);
close(pipes[0]);
} else {
//Child does not read
close(pipes[0]);
//Map stdout and stderr to write pipe-end
dup2(pipes[1], STDOUT_FILENO);
//dup2(pipes[1], STDERR_FILENO);
//Status Message
printf("Try to execute hping3 ...");
fflush(stdout);
//Child, exec hping with params
execv("/usr/sbin/hping3",params);
puts("never happens");
close(pipes[1]);
exit(1);
}
저는이 문제에 골머리를 앓고 있습니다. 12 시간 동안 해결책을 찾았습니다.
대신'sudo'를'execv'와 함께 실행하지 말고'sudo'로 프로그램을 실행하십시오. –
동일한 문제가 있는데, 전체 mpi 환경에서 작동하는지 실제로 확신 할 수 없습니다. 문제는 hping3이 루트를 필요로한다는 것입니다. –
그래서 프로그램 내부에서'sudo'를 사용하려고하면 루트 사용자가 프로그램을 실행해야합니다. –