1 /*
2  * Check decoding of PTP_* commands of ioctl syscall.
3  *
4  * Copyright (c) 2018 Harsha Sharma <harshasharmaiitr@gmail.com>
5  * Copyright (c) 2018 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 
33 #ifdef HAVE_STRUCT_PTP_SYS_OFFSET
34 
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <inttypes.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/ioctl.h>
42 #include <linux/ptp_clock.h>
43 
44 #include "xlat.h"
45 #include "xlat/ptp_flags_options.h"
46 
47 static void
test_no_device(void)48 test_no_device(void)
49 {
50 	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_clock_caps, caps);
51 	fill_memory(caps, sizeof(*caps));
52 
53 	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_sys_offset, sysoff);
54 	fill_memory(sysoff, sizeof(*sysoff));
55 
56 	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_extts_request, extts);
57 	fill_memory(extts, sizeof(*extts));
58 
59 	TAIL_ALLOC_OBJECT_CONST_PTR(struct ptp_perout_request, perout);
60 	fill_memory(perout, sizeof(*perout));
61 
62 	int saved_errno;
63 
64 	/* PTP_CLOCK_GETCAPS */
65 	ioctl(-1, PTP_CLOCK_GETCAPS, NULL);
66 	printf("ioctl(-1, PTP_CLOCK_GETCAPS, NULL) = -1 EBADF (%m)\n");
67 	ioctl(-1, PTP_CLOCK_GETCAPS, caps);
68 	printf("ioctl(-1, PTP_CLOCK_GETCAPS, %p) = -1 EBADF (%m)\n", caps);
69 
70 	/* PTP_SYS_OFFSET */
71 	ioctl(-1, PTP_SYS_OFFSET, NULL);
72 	printf("ioctl(-1, PTP_SYS_OFFSET, NULL) = -1 EBADF (%m)\n");
73 	ioctl(-1, PTP_SYS_OFFSET, sysoff);
74 	printf("ioctl(-1, PTP_SYS_OFFSET, {n_samples=%u}) = -1 EBADF (%m)\n",
75 	       sysoff->n_samples);
76 
77 	/* PTP_ENABLE_PPS */
78 	ioctl(-1, PTP_ENABLE_PPS, 0);
79 	printf("ioctl(-1, PTP_ENABLE_PPS, 0) = -1 EBADF (%m)\n");
80 	ioctl(-1, PTP_ENABLE_PPS, 1);
81 	printf("ioctl(-1, PTP_ENABLE_PPS, 1) = -1 EBADF (%m)\n");
82 
83 	/* PTP_EXTTS_REQUEST */
84 	ioctl(-1, PTP_EXTTS_REQUEST, NULL);
85 	printf("ioctl(-1, PTP_EXTTS_REQUEST, NULL) = -1 EBADF (%m)\n");
86 	ioctl(-1, PTP_EXTTS_REQUEST, extts);
87 	saved_errno = errno;
88 	printf("ioctl(-1, PTP_EXTTS_REQUEST, {index=%d, flags=", extts->index);
89 	printflags(ptp_flags_options, extts->flags, "PTP_???");
90 	errno = saved_errno;
91 	printf("}) = -1 EBADF (%m)\n");
92 
93 	/* PTP_PEROUT_REQUEST */
94 	ioctl(-1, PTP_PEROUT_REQUEST, NULL);
95 	printf("ioctl(-1, PTP_PEROUT_REQUEST, NULL) = -1 EBADF (%m)\n");
96 	ioctl(-1, PTP_PEROUT_REQUEST, perout);
97 	saved_errno = errno;
98 	printf("ioctl(-1, PTP_PEROUT_REQUEST, {start={sec=%" PRId64
99 	       ", nsec=%" PRIu32 "}, period={sec=%" PRId64 ", nsec=%" PRIu32 "}"
100 	       ", index=%d, flags=",
101 	       (int64_t) perout->start.sec, perout->start.nsec,
102 	       (int64_t)perout->period.sec, perout->period.nsec, perout->index);
103 	printflags(ptp_flags_options, perout->flags, "PTP_???");
104 	errno = saved_errno;
105 	printf("}) = -1 EBADF (%m)\n");
106 
107 	/* unrecognized */
108 	ioctl(-1, _IOC(_IOC_READ, PTP_CLK_MAGIC, 0xff, 0xfe), 0);
109 	printf("ioctl(-1, _IOC(_IOC_READ, %#x, 0xff, 0xfe), 0)"
110 	       " = -1 EBADF (%m)\n", PTP_CLK_MAGIC);
111 
112 	const unsigned long arg = (unsigned long) 0xfacefeeddeadbeefULL;
113 	ioctl(-1, _IOC(_IOC_WRITE, PTP_CLK_MAGIC, 0xfd, 0xfc), arg);
114 	printf("ioctl(-1, _IOC(_IOC_WRITE, %#x, 0xfd, 0xfc), %#lx)"
115 	       " = -1 EBADF (%m)\n", PTP_CLK_MAGIC, arg);
116 }
117 
118 int
main(void)119 main(void)
120 {
121 	test_no_device();
122 
123 	puts("+++ exited with 0 +++");
124 	return 0;
125 }
126 
127 #else
128 
129 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_PTP_SYS_OFFSET")
130 
131 #endif /* HAVE_STRUCT_PTP_SYS_OFFSET */
132