summaryrefslogtreecommitdiff
path: root/src/dirent.c
blob: 3c2c09e9bf6041445ab0526d7a6adabd4e6739c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
 * Copyright (c) 2005-2015 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2014-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "defs.h"
#include "kernel_dirent.h"

#include DEF_MPERS_TYPE(kernel_dirent_t)

#include MPERS_DEFS

#include "xgetdents.h"

#define D_NAME_LEN_MAX 256

/* The minimum size of a valid directory entry.  */
static const unsigned int header_size =
	offsetof(kernel_dirent_t, d_name);

static void
print_dentry_head(const kernel_dirent_t *const dent)
{
	tprint_struct_begin();
	PRINT_FIELD_U(*dent, d_ino);
	tprint_struct_next();
	PRINT_FIELD_U(*dent, d_off);
	tprint_struct_next();
	PRINT_FIELD_U(*dent, d_reclen);
}

static unsigned int
decode_dentry_head(struct tcb *const tcp, const void *const arg)
{
	const kernel_dirent_t *const dent = arg;

	if (!abbrev(tcp))
		print_dentry_head(dent);

	return dent->d_reclen;
}

static int
decode_dentry_tail(struct tcb *const tcp, kernel_ulong_t addr,
		   const void *const arg, const unsigned int d_name_type_len)
{
	int rc = 0;

	/* !abbrev(tcp) */

	if (d_name_type_len) {
		unsigned int d_name_len = d_name_type_len - 1;
		if (d_name_len) {
			if (d_name_len > D_NAME_LEN_MAX)
				d_name_len = D_NAME_LEN_MAX;
			tprint_struct_next();
			tprints_field_name("d_name");
			rc = printpathn(tcp, addr, d_name_len - 1);
		}
		tprint_struct_next();
		tprints_field_name("d_type");
		const kernel_ulong_t d_type_addr =
			addr + (d_name_type_len - 1);
		unsigned char d_type;
		if (umove_or_printaddr(tcp, d_type_addr, &d_type))
			rc = -1;
		else
			printxval(dirent_types, d_type, "DT_???");
	}
	tprint_struct_end();

	return rc;
}

SYS_FUNC(getdents)
{
	return xgetdents(tcp, header_size,
			 decode_dentry_head, decode_dentry_tail);
}

static void
print_old_dirent(struct tcb *const tcp, const kernel_ulong_t addr)
{
	kernel_dirent_t dent;

	if (umove_or_printaddr(tcp, addr, &dent))
		return;

	print_dentry_head(&dent);
	tprint_struct_next();
	tprints_field_name("d_name");
	printpathn(tcp, addr + header_size,
		   MIN(dent.d_reclen, D_NAME_LEN_MAX));
	tprint_struct_end();
}

SYS_FUNC(readdir)
{
	if (entering(tcp)) {
		/* fd */
		printfd(tcp, tcp->u_arg[0]);
		tprint_arg_next();
	} else {
		/* dirp */
		if (tcp->u_rval == 0)
			printaddr(tcp->u_arg[1]);
		else
			print_old_dirent(tcp, tcp->u_arg[1]);

		/* count */
		const unsigned int count = tcp->u_arg[2];
		/* Not much point in printing this out, it is always 1. */
		if (count != 1) {
			tprint_arg_next();
			PRINT_VAL_U(count);
		}
	}
	return 0;
}