0

我正在学习 stat 和 readlink 之类的系统调用。我尝试以两种不同的方式统计根目录:

  1. 只需统计root的路径。简单的。
  2. 然后,一个迂回的方式,fopen“/”,从我得到的 fd 中创建一个文件描述符路径,并读取链接它以获得“/”。然后统计。

我不明白这是我期望的工作方式,并且 inode 编号是相同的,除了当我在之后引入更多代码时,即 getcwd,inode 奇怪地不一样并且测试 2 失败。如果您同时运行这两个版本,无论有没有它说要删除的部分,p2 的 printf 也会发生变化,您可以看到/. 我在这里做错了什么或这里发生了什么?这些初始语句之后的代码是如何导致代码更早发生变化的?

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    /* Stat root this way */
    struct stat st1;
    char *p1 = "/";
    if (!stat(p1, &st1))
        printf("PASS\n");
    else
        printf("FAIL - %m\n");

    /* Stat root another way */
    struct stat st2;
    char fdpath[100], p2[100];
    FILE *fp = fopen("/", "r");
    sprintf(fdpath, "/proc/self/fd/%d", fileno(fp));
    readlink(fdpath, p2, sizeof(p2));
    if (!stat(p2, &st2))
        printf("PASS\n");
    else
        printf("FAIL - %m\n");

    printf("p2 = %s\n", p2);

    /* Check inodes are the same */
    printf("    st1.st_ino = %ld\n", st1.st_ino);
    printf("    st2.st_ino = %ld\n", st2.st_ino);

    /* TRY WITHOUT THIS - Adding this makes the inodes different! Take it out and they're the same */
    char cwd_buf[100];
    char *cwd_ret = getcwd(cwd_buf, sizeof(cwd_buf));
    if (cwd_ret == NULL)
        printf("getcwd failed - %m\n");
    printf("cwd_ret = %s\n", cwd_ret);

    return 0;
}
4

2 回答 2

2

从手册页:

readlink() 将符号链接路径名的内容放在缓冲区 buf 中,缓冲区大小为 bufsiz。 readlink() 不会将空字节附加到 buf。 如果缓冲区太小而无法容纳所有内容,它将(默默地)截断内容(到 bufsiz 字符的长度)。

因此,如果要p2用作字符串,则需要手动执行:

int size = readlink(fdpath, p2, sizeof(p2) - 1);
if (size < 0)
{
    // error
}
p2[size] = 0;

注意 -1: 应该为空终止符保留一个字节,即使字符串被截断。

于 2020-07-22T00:03:53.657 回答
2

readlink不会以 null 终止它返回的字符串,因此p2可能在 . 后面包含任意垃圾/

你应该做

ssize_t len = readlink(fdpath, p2, sizeof(p2));
if (len == -1) { /* error */ }
if (len == sizeof(p2)) { /* truncated */ }
p2[len] = '\0';
if (!stat(p2, &st2)) // ...

您也应该在许多其他地方进行错误检查。

最后添加和删除不相关的代码可能会改变堆栈布局(因为额外的局部变量),这意味着p2包含和不包含它的不同垃圾。甚至在某些情况下,垃圾是空字节,因此字符串恰好被正确终止了。这种事情在 C 代码中经常发生,而且没有特别的意义。这是C 标准语言中未定义的行为。不要想太多为什么会发生;只是尝试找到并修复错误,不要再次编写相同的错误:-)

于 2020-07-22T00:04:40.127 回答