사용자가 소유 한 파일을 찾을 프로그램을 작성하려고합니다. 그런 다음 출력을 변수로 인쇄하여 다른 프로그램과 함께 comunicate하거나 조작하십시오. 문제는 디렉토리를 /
으로 지정하면 대부분의 초기 찾기에서 권한이 거부됩니다. 하지만 권한이 거부되었다고 표시 되더라도 변수에 할당 된 NULL/가비지 또는 무언가가있는 것으로 보입니다. 다음 코드는 다음과 같습니다권한이 거부 된 popen 함수를 사용하는 동안 잘못된/쓰레기 값을 처리하는 방법
static struct{
char file_owned[8192][1024];
}information;
void get_file_owned(char *username)
{
FILE *stream3;
extern FILE *popen();
char command[1024];
char buff[1024];
int i;
sprintf(command, "find/-user %s -ls",username);
if(!(stream3 = popen(command), "r"))){
exit(1);
}
i=0;
while(fgets(buff, sizeof(buff), stream3)!=NULL){
sprintf(information.file_owned[i],"%s",buff); //it print something into file_owned. But I do now know what.
i++;
}
pclose(stream3);
return;
}
int main(int argc, char **argv)
{
static char *username;
int i;
username = "fikrie";
get_file_owned(username);
for(i=0;i<10;i++){
printf("%s\n",information.process_owned[i]);
}
return 0;
}
이 출력입니다 : 내가 기대하고있어
find: `/proc/1657/task/1659/ns` : Permission denied
find: `/proc/1713/task/1713/ns` : Permission denied
...
...
//There's a lot of this kind of output
...
Segmentation fault(core dumped)
의 거부 권한에 의해 발생. 그러면 process_owned에 쓰레기가 인쇄됩니다. 거부 된 권한에 대한 출력을 어떻게 처리해야합니까? 나는 get_file_owned 함수 안에서 NULL을 처리하려고 시도했다. 그러나 그것은 그것을 해결하지 못합니다.
while(fgets(buff, sizeof(buff), stream3)!=NULL){
if(buff == NULL){
continue;
}
sprintf(information.file_owned[i],"%s",buff);
i++;
}
또한 세그먼트 결함에 대해 gdb를 사용하려고했습니다. 결과는 다음과 같습니다.
warning: Can't read pathname for load map: Input/output error
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0 _IO_getline info (fp=0x9ec50b8, buf=0x2 <Address 0x2 out of bounds>, n=254, delim=10, extract_delim=1, eof=0x0) at iogetline.c:91
iogetline.c: No such file or directory.
내 홈 디렉토리에서이 파일을 실행하려고하면주의하십시오. /home/fikrie/Documents
. 분할 오류 또는 오류가 없습니다.
편집 : 또한이 프로그램을 루트 권한으로 실행했습니다. 구조를 다음 유형으로 변경하십시오.
struct{
char file_owned[1024];
}information[1024]; //I tried changing this into 8094 also. Just in case the array for this struct is not enough.
** OMG dat 구조체 ** ** –
아하하 ... 그저 그 예일뿐입니다. 모든 것을 타이핑하는 것만으로 게으르다. 그러나 나는 그것이 내가하려고하는 것을 설명하는 데 도움이되기를 바랍니다. –
루트 사용자로 프로그램을 실행 해 보셨습니까 ?? – const