简体   繁体   English

Linux getgrent() 导致“分段错误”

[英]Linux getgrent() results in “Segmentation fault”

I'm trying to print all of system's groups (Ubuntu 20.04):我正在尝试打印所有系统组(Ubuntu 20.04):

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();
   
   exit(EXIT_SUCCESS);
}

I run the program with sudo and I get:我用sudo运行程序,我得到:

$ sudo ./program
Here are all of this system's groups:

Segmentation fault

Same error happens when working with struct spwd .使用struct spwd时会发生同样的错误。

Update更新

I updated the source with the include lines and I left out the lib/*.c part.我用include行更新了源代码,并省略了lib/*.c部分。 When I compile this exact piece of code:当我编译这段代码时:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: [...]
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

$ gcc -std=c17 main.c -o program
main.c: In function ‘main’:
main.c:11:18: warning: implicit declaration of function ‘getgrent’ [-Wimplicit-function-declaration]
   11 |    while ((grp = getgrent()) != NULL)
      |                  ^~~~~~~~
main.c:11:16: warning: assignment to ‘struct group *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   11 |    while ((grp = getgrent()) != NULL)
      |                ^
main.c:14:4: warning: implicit declaration of function ‘endgrent’ [-Wimplicit-function-declaration]
   14 |    endgrent();
      |    ^~~~~~~~

And When I run it:当我运行它时:

$ ./program 
Here are all of this system's groups:

Segmentation fault (core dumped)

$ sudo ./program 
Here are all of this system's groups:

Segmentation fault

Now when I run VS Code debugger it works correctly.现在,当我运行 VS Code 调试器时,它可以正常工作。 VS Code is configured according to this article . VS Code 根据这篇文章进行配置。

Here's my launch.json :这是我的launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc-9 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc-9 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

Add the correct headers:添加正确的标题:


#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();

   exit(EXIT_SUCCESS);
}

Update: -std=c17 is strange, it fails to include <grp.h> .更新: -std=c17很奇怪,它没有包含<grp.h> You probably need std=gnu17 , which includes some gnu+posix stuff.您可能需要std=gnu17 ,其中包括一些 gnu+posix 的东西。

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

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