(root)/
strace-6.5/
tests-mx32/
dirfd.c
       1  /*
       2   * Copyright (c) 2021 The strace developers.
       3   * All rights reserved.
       4   *
       5   * SPDX-License-Identifier: GPL-2.0-or-later
       6   */
       7  
       8  #include "tests.h"
       9  
      10  #include <dirent.h>
      11  #include <limits.h>
      12  #include <stdlib.h>
      13  #include <unistd.h>
      14  
      15  #include "xmalloc.h"
      16  
      17  int
      18  get_dir_fd(const char *dir_path)
      19  {
      20  	DIR *dir = opendir(dir_path);
      21  	if (dir == NULL)
      22  		perror_msg_and_fail("opendir: %s", dir_path);
      23  	int dfd = dirfd(dir);
      24  	if (dfd == -1)
      25  		perror_msg_and_fail("dirfd");
      26  	return dfd;
      27  }
      28  
      29  char *
      30  get_fd_path(int fd)
      31  {
      32  	char *proc = xasprintf("/proc/self/fd/%u", fd);
      33  	char *buf = xmalloc(PATH_MAX);
      34  	ssize_t n = readlink(proc, buf, PATH_MAX);
      35  	if (n < 0)
      36  		perror_msg_and_skip("readlink: %s", proc);
      37  	if (n >= PATH_MAX)
      38  		error_msg_and_fail("readlink: %s: %s", proc,
      39  				   "symlink value is too long");
      40  	buf[n] = '\0';
      41  	free(proc);
      42  	return buf;
      43  }