简体   繁体   English

尝试使用返回拒绝权限的popen函数时如何处理无效/垃圾值

[英]How to handle an invalid/garbage value while trying to use popen function that returns a permission denied

I'm trying to write a program that will look for files owned by user. 我正在尝试编写一个程序来查找用户拥有的文件。 Then print the output into a variable to manipulate it or comunicate it with some other program. 然后将输出打印到变量中以进行操作或与其他程序进行通讯。 The problem is when I put the directory as / , most of the early find will be permission denied. 问题是当我将目录放置为/ ,大多数早期查找将被拒绝权限。 But there seems to be a NULL/garbage or something assigned to the variable even though it states that permission denied. 但是,尽管它声明权限被拒绝,但似乎有一个NULL /垃圾或分配给该变量的东西。 Here are the codes: 以下是代码:

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;
 }

This is the output: 这是输出:

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)

I'm expecting its caused by the permission denied. 我期望它是由拒绝所引起的。 Which will then just print a garbage into process_owned. 然后将仅将垃圾打印到process_owned中。 How should I handle output for the permission denied? 我应该如何处理拒绝权限的输出? I've tried to handle a NULL inside the get_file_owned function. 我试图在get_file_owned函数内部处理NULL。 But it doesn't solve it. 但这并不能解决。

while(fgets(buff, sizeof(buff), stream3)!=NULL){
    if(buff == NULL){
    continue;
    }
    sprintf(information.file_owned[i],"%s",buff);
    i++;
}

I've also tried to use gdb to see about the segmentation fault. 我也尝试过使用gdb查看分段错误。 This is the result: 结果如下:

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.

Please take note that if I tried to run this in my home directory. 请注意,如果我尝试在我的主目录中运行它。 /home/fikrie/Documents . /home/fikrie/Documents There's no segmentation fault or error. 没有细分错误或错误。

EDIT: I have also run this program as root. 编辑:我也已经以根用户身份运行了该程序。 Change the structure way into this type: 将结构更改为这种类型:

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. 

find propably returns more then 8192 lines. find可能返回的收益大于8192行。 Therefore the while loop overflows the array file_owned . 因此,while循环使数组file_owned溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM