xref: /aosp_15_r20/external/strace/file_handle.c (revision cf84ac9a129d8ea9952db616b4e9b904c4bdde56)
1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker  * Copyright (c) 2015 Dmitry V. Levin <[email protected]>
3*cf84ac9aSAndroid Build Coastguard Worker  * Copyright (c) 2015-2017 The strace developers.
4*cf84ac9aSAndroid Build Coastguard Worker  * All rights reserved.
5*cf84ac9aSAndroid Build Coastguard Worker  *
6*cf84ac9aSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
7*cf84ac9aSAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
8*cf84ac9aSAndroid Build Coastguard Worker  * are met:
9*cf84ac9aSAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
10*cf84ac9aSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
11*cf84ac9aSAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
13*cf84ac9aSAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
14*cf84ac9aSAndroid Build Coastguard Worker  * 3. The name of the author may not be used to endorse or promote products
15*cf84ac9aSAndroid Build Coastguard Worker  *    derived from this software without specific prior written permission.
16*cf84ac9aSAndroid Build Coastguard Worker  *
17*cf84ac9aSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*cf84ac9aSAndroid Build Coastguard Worker  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*cf84ac9aSAndroid Build Coastguard Worker  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*cf84ac9aSAndroid Build Coastguard Worker  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*cf84ac9aSAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*cf84ac9aSAndroid Build Coastguard Worker  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*cf84ac9aSAndroid Build Coastguard Worker  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*cf84ac9aSAndroid Build Coastguard Worker  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*cf84ac9aSAndroid Build Coastguard Worker  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*cf84ac9aSAndroid Build Coastguard Worker  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*cf84ac9aSAndroid Build Coastguard Worker  */
28*cf84ac9aSAndroid Build Coastguard Worker 
29*cf84ac9aSAndroid Build Coastguard Worker #include "defs.h"
30*cf84ac9aSAndroid Build Coastguard Worker 
31*cf84ac9aSAndroid Build Coastguard Worker #include "xlat/name_to_handle_at_flags.h"
32*cf84ac9aSAndroid Build Coastguard Worker 
33*cf84ac9aSAndroid Build Coastguard Worker #ifndef MAX_HANDLE_SZ
34*cf84ac9aSAndroid Build Coastguard Worker # define MAX_HANDLE_SZ 128
35*cf84ac9aSAndroid Build Coastguard Worker #endif
36*cf84ac9aSAndroid Build Coastguard Worker 
37*cf84ac9aSAndroid Build Coastguard Worker typedef struct {
38*cf84ac9aSAndroid Build Coastguard Worker 	unsigned int handle_bytes;
39*cf84ac9aSAndroid Build Coastguard Worker 	int handle_type;
40*cf84ac9aSAndroid Build Coastguard Worker } file_handle_header;
41*cf84ac9aSAndroid Build Coastguard Worker 
SYS_FUNC(name_to_handle_at)42*cf84ac9aSAndroid Build Coastguard Worker SYS_FUNC(name_to_handle_at)
43*cf84ac9aSAndroid Build Coastguard Worker {
44*cf84ac9aSAndroid Build Coastguard Worker 	file_handle_header h;
45*cf84ac9aSAndroid Build Coastguard Worker 	const kernel_ulong_t addr = tcp->u_arg[2];
46*cf84ac9aSAndroid Build Coastguard Worker 
47*cf84ac9aSAndroid Build Coastguard Worker 	if (entering(tcp)) {
48*cf84ac9aSAndroid Build Coastguard Worker 		/* dirfd */
49*cf84ac9aSAndroid Build Coastguard Worker 		print_dirfd(tcp, tcp->u_arg[0]);
50*cf84ac9aSAndroid Build Coastguard Worker 
51*cf84ac9aSAndroid Build Coastguard Worker 		/* pathname */
52*cf84ac9aSAndroid Build Coastguard Worker 		printpath(tcp, tcp->u_arg[1]);
53*cf84ac9aSAndroid Build Coastguard Worker 		tprints(", ");
54*cf84ac9aSAndroid Build Coastguard Worker 
55*cf84ac9aSAndroid Build Coastguard Worker 		/* handle */
56*cf84ac9aSAndroid Build Coastguard Worker 		if (umove_or_printaddr(tcp, addr, &h)) {
57*cf84ac9aSAndroid Build Coastguard Worker 			tprints(", ");
58*cf84ac9aSAndroid Build Coastguard Worker 
59*cf84ac9aSAndroid Build Coastguard Worker 			/* mount_id */
60*cf84ac9aSAndroid Build Coastguard Worker 			printaddr(tcp->u_arg[3]);
61*cf84ac9aSAndroid Build Coastguard Worker 			tprints(", ");
62*cf84ac9aSAndroid Build Coastguard Worker 
63*cf84ac9aSAndroid Build Coastguard Worker 			/* flags */
64*cf84ac9aSAndroid Build Coastguard Worker 			printflags(name_to_handle_at_flags, tcp->u_arg[4],
65*cf84ac9aSAndroid Build Coastguard Worker 				   "AT_???");
66*cf84ac9aSAndroid Build Coastguard Worker 
67*cf84ac9aSAndroid Build Coastguard Worker 			return RVAL_DECODED;
68*cf84ac9aSAndroid Build Coastguard Worker 		}
69*cf84ac9aSAndroid Build Coastguard Worker 		tprintf("{handle_bytes=%u", h.handle_bytes);
70*cf84ac9aSAndroid Build Coastguard Worker 
71*cf84ac9aSAndroid Build Coastguard Worker 		set_tcb_priv_ulong(tcp, h.handle_bytes);
72*cf84ac9aSAndroid Build Coastguard Worker 
73*cf84ac9aSAndroid Build Coastguard Worker 		return 0;
74*cf84ac9aSAndroid Build Coastguard Worker 	} else {
75*cf84ac9aSAndroid Build Coastguard Worker 		unsigned int i = get_tcb_priv_ulong(tcp);
76*cf84ac9aSAndroid Build Coastguard Worker 
77*cf84ac9aSAndroid Build Coastguard Worker 		if ((!syserror(tcp) || EOVERFLOW == tcp->u_error)
78*cf84ac9aSAndroid Build Coastguard Worker 		    && !umove(tcp, addr, &h)) {
79*cf84ac9aSAndroid Build Coastguard Worker 			unsigned char f_handle[MAX_HANDLE_SZ];
80*cf84ac9aSAndroid Build Coastguard Worker 
81*cf84ac9aSAndroid Build Coastguard Worker 			if (i != h.handle_bytes)
82*cf84ac9aSAndroid Build Coastguard Worker 				tprintf(" => %u", h.handle_bytes);
83*cf84ac9aSAndroid Build Coastguard Worker 			if (!syserror(tcp)) {
84*cf84ac9aSAndroid Build Coastguard Worker 				tprintf(", handle_type=%d", h.handle_type);
85*cf84ac9aSAndroid Build Coastguard Worker 				if (h.handle_bytes > MAX_HANDLE_SZ)
86*cf84ac9aSAndroid Build Coastguard Worker 					h.handle_bytes = MAX_HANDLE_SZ;
87*cf84ac9aSAndroid Build Coastguard Worker 				if (!umoven(tcp, addr + sizeof(h), h.handle_bytes,
88*cf84ac9aSAndroid Build Coastguard Worker 					    f_handle)) {
89*cf84ac9aSAndroid Build Coastguard Worker 					tprints(", f_handle=0x");
90*cf84ac9aSAndroid Build Coastguard Worker 					for (i = 0; i < h.handle_bytes; ++i)
91*cf84ac9aSAndroid Build Coastguard Worker 						tprintf("%02x", f_handle[i]);
92*cf84ac9aSAndroid Build Coastguard Worker 				}
93*cf84ac9aSAndroid Build Coastguard Worker 			}
94*cf84ac9aSAndroid Build Coastguard Worker 		}
95*cf84ac9aSAndroid Build Coastguard Worker 		tprints("}, ");
96*cf84ac9aSAndroid Build Coastguard Worker 
97*cf84ac9aSAndroid Build Coastguard Worker 		/* mount_id */
98*cf84ac9aSAndroid Build Coastguard Worker 		printnum_int(tcp, tcp->u_arg[3], "%d");
99*cf84ac9aSAndroid Build Coastguard Worker 		tprints(", ");
100*cf84ac9aSAndroid Build Coastguard Worker 
101*cf84ac9aSAndroid Build Coastguard Worker 		/* flags */
102*cf84ac9aSAndroid Build Coastguard Worker 		printflags(name_to_handle_at_flags, tcp->u_arg[4], "AT_???");
103*cf84ac9aSAndroid Build Coastguard Worker 	}
104*cf84ac9aSAndroid Build Coastguard Worker 	return 0;
105*cf84ac9aSAndroid Build Coastguard Worker }
106*cf84ac9aSAndroid Build Coastguard Worker 
SYS_FUNC(open_by_handle_at)107*cf84ac9aSAndroid Build Coastguard Worker SYS_FUNC(open_by_handle_at)
108*cf84ac9aSAndroid Build Coastguard Worker {
109*cf84ac9aSAndroid Build Coastguard Worker 	file_handle_header h;
110*cf84ac9aSAndroid Build Coastguard Worker 	const kernel_ulong_t addr = tcp->u_arg[1];
111*cf84ac9aSAndroid Build Coastguard Worker 
112*cf84ac9aSAndroid Build Coastguard Worker 	/* mount_fd */
113*cf84ac9aSAndroid Build Coastguard Worker 	printfd(tcp, tcp->u_arg[0]);
114*cf84ac9aSAndroid Build Coastguard Worker 	tprints(", ");
115*cf84ac9aSAndroid Build Coastguard Worker 
116*cf84ac9aSAndroid Build Coastguard Worker 	/* handle */
117*cf84ac9aSAndroid Build Coastguard Worker 	if (!umove_or_printaddr(tcp, addr, &h)) {
118*cf84ac9aSAndroid Build Coastguard Worker 		unsigned char f_handle[MAX_HANDLE_SZ];
119*cf84ac9aSAndroid Build Coastguard Worker 
120*cf84ac9aSAndroid Build Coastguard Worker 		tprintf("{handle_bytes=%u, handle_type=%d",
121*cf84ac9aSAndroid Build Coastguard Worker 			h.handle_bytes, h.handle_type);
122*cf84ac9aSAndroid Build Coastguard Worker 		if (h.handle_bytes > MAX_HANDLE_SZ)
123*cf84ac9aSAndroid Build Coastguard Worker 			h.handle_bytes = MAX_HANDLE_SZ;
124*cf84ac9aSAndroid Build Coastguard Worker 		if (!umoven(tcp, addr + sizeof(h), h.handle_bytes, &f_handle)) {
125*cf84ac9aSAndroid Build Coastguard Worker 			unsigned int i;
126*cf84ac9aSAndroid Build Coastguard Worker 
127*cf84ac9aSAndroid Build Coastguard Worker 			tprints(", f_handle=0x");
128*cf84ac9aSAndroid Build Coastguard Worker 			for (i = 0; i < h.handle_bytes; ++i)
129*cf84ac9aSAndroid Build Coastguard Worker 				tprintf("%02x", f_handle[i]);
130*cf84ac9aSAndroid Build Coastguard Worker 		}
131*cf84ac9aSAndroid Build Coastguard Worker 		tprints("}");
132*cf84ac9aSAndroid Build Coastguard Worker 	}
133*cf84ac9aSAndroid Build Coastguard Worker 	tprints(", ");
134*cf84ac9aSAndroid Build Coastguard Worker 
135*cf84ac9aSAndroid Build Coastguard Worker 	/* flags */
136*cf84ac9aSAndroid Build Coastguard Worker 	tprint_open_modes(tcp->u_arg[2]);
137*cf84ac9aSAndroid Build Coastguard Worker 
138*cf84ac9aSAndroid Build Coastguard Worker 	return RVAL_DECODED;
139*cf84ac9aSAndroid Build Coastguard Worker }
140