1 /*
2  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2016-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "defs.h"
30 
31 #include DEF_MPERS_TYPE(struct_stat)
32 
33 #include "asm_stat.h"
34 
35 #if defined MPERS_IS_m32
36 # undef HAVE_STRUCT_STAT
37 # undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
38 # ifdef HAVE_M32_STRUCT_STAT
39 #  define HAVE_STRUCT_STAT 1
40 #  ifdef HAVE_M32_STRUCT_STAT_ST_MTIME_NSEC
41 #   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
42 #  endif /* HAVE_M32_STRUCT_STAT_ST_MTIME_NSEC */
43 # endif /* HAVE_M32_STRUCT_STAT */
44 #elif defined MPERS_IS_mx32
45 # undef HAVE_STRUCT_STAT
46 # undef HAVE_STRUCT_STAT_ST_MTIME_NSEC
47 # ifdef HAVE_MX32_STRUCT_STAT
48 #  define HAVE_STRUCT_STAT 1
49 #  ifdef HAVE_MX32_STRUCT_STAT_ST_MTIME_NSEC
50 #   define HAVE_STRUCT_STAT_ST_MTIME_NSEC 1
51 #  endif /* HAVE_MX32_STRUCT_STAT_ST_MTIME_NSEC */
52 # endif /* HAVE_MX32_STRUCT_STAT */
53 #else /* !MPERS_IS_m32 && !MPERS_IS_mx32 */
54 # define HAVE_STRUCT_STAT 1
55 #endif
56 
57 #ifndef HAVE_STRUCT_STAT
58 struct stat {};
59 #endif
60 
61 typedef struct stat struct_stat;
62 
63 #include MPERS_DEFS
64 
65 #include "stat.h"
66 
67 #ifdef HAVE_STRUCT_STAT_ST_MTIME_NSEC
68 # define TIME_NSEC(arg) zero_extend_signed_to_ull(arg)
69 # define HAVE_NSEC true
70 #else
71 # define TIME_NSEC(arg) 0
72 # define HAVE_NSEC false
73 #endif
74 
MPERS_PRINTER_DECL(bool,fetch_struct_stat,struct tcb * const tcp,const kernel_ulong_t addr,struct strace_stat * const dst)75 MPERS_PRINTER_DECL(bool, fetch_struct_stat,
76 		   struct tcb *const tcp, const kernel_ulong_t addr,
77 		   struct strace_stat *const dst)
78 {
79 #ifdef HAVE_STRUCT_STAT
80 	struct_stat buf;
81 	if (umove_or_printaddr(tcp, addr, &buf))
82 		return false;
83 
84 	dst->dev = zero_extend_signed_to_ull(buf.st_dev);
85 	dst->ino = zero_extend_signed_to_ull(buf.st_ino);
86 	dst->rdev = zero_extend_signed_to_ull(buf.st_rdev);
87 	dst->size = zero_extend_signed_to_ull(buf.st_size);
88 	dst->blocks = zero_extend_signed_to_ull(buf.st_blocks);
89 	dst->blksize = zero_extend_signed_to_ull(buf.st_blksize);
90 	dst->mode = zero_extend_signed_to_ull(buf.st_mode);
91 	dst->nlink = zero_extend_signed_to_ull(buf.st_nlink);
92 	dst->uid = zero_extend_signed_to_ull(buf.st_uid);
93 	dst->gid = zero_extend_signed_to_ull(buf.st_gid);
94 	dst->atime = sign_extend_unsigned_to_ll(buf.st_atime);
95 	dst->ctime = sign_extend_unsigned_to_ll(buf.st_ctime);
96 	dst->mtime = sign_extend_unsigned_to_ll(buf.st_mtime);
97 	dst->atime_nsec = TIME_NSEC(buf.st_atime_nsec);
98 	dst->ctime_nsec = TIME_NSEC(buf.st_ctime_nsec);
99 	dst->mtime_nsec = TIME_NSEC(buf.st_mtime_nsec);
100 	dst->has_nsec = HAVE_NSEC;
101 	return true;
102 #else /* !HAVE_STRUCT_STAT */
103 	printaddr(addr);
104 	return false;
105 #endif
106 }
107