1 /*
2 * This file is part of strace test suite.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include <assert.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 unsigned long
20 inode_of_sockfd(const int fd)
21 {
22 assert(fd >= 0);
23
24 char linkpath[sizeof("/proc/self/fd/%u") + sizeof(int) * 3];
25 assert(snprintf(linkpath, sizeof(linkpath), "/proc/self/fd/%u", fd)
26 < (int) sizeof(linkpath));
27
28 char path[PATH_MAX + 1];
29 const ssize_t path_len = readlink(linkpath, path, sizeof(path) - 1);
30 if (path_len < 0)
31 perror_msg_and_fail("readlink: %s", linkpath);
32 path[path_len] = '\0';
33
34 static const char prefix[] = "socket:[";
35 const size_t prefix_len = sizeof(prefix) - 1;
36 assert(strncmp(path, prefix, prefix_len) == 0
37 && path[path_len - 1] == ']');
38
39 return strtoul(path + prefix_len, NULL, 10);
40 }