forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-pty.c
393 lines (355 loc) · 9.96 KB
/
stress-pty.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#if defined(HAVE_TERMIO_H)
#include <termio.h>
#endif
#define MIN_PTYS (8)
#define MAX_PTYS (65536)
#define DEFAULT_PTYS (65536)
static const stress_help_t help[] = {
{ NULL, "pty N", "start N workers that exercise pseudoterminals" },
{ NULL, "pty-ops N", "stop pty workers after N pty bogo operations" },
{ NULL, "pty-max N", "attempt to open a maximum of N ptys" },
{ NULL, NULL, NULL }
};
#if defined(HAVE_TERMIOS_H) && \
defined(HAVE_TERMIO_H) && \
defined(HAVE_PTSNAME)
typedef struct {
char *followername;
int leader;
int follower;
} stress_pty_info_t;
#endif
/*
* stress_set_pty_max()
* set ptr maximum
*/
static int stress_set_pty_max(const char *opt)
{
uint64_t pty_max;
pty_max = stress_get_uint64(opt);
stress_check_range("pty-max", pty_max,
MIN_PTYS, MAX_PTYS);
return stress_set_setting("pty-max", TYPE_ID_UINT64, &pty_max);
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_pty_max, stress_set_pty_max },
{ 0, NULL }
};
#if defined(HAVE_TERMIOS_H) && \
defined(HAVE_TERMIO_H) && \
defined(HAVE_PTSNAME)
/*
* stress_pty
* stress pty handling
*/
static int stress_pty(const stress_args_t *args)
{
uint64_t pty_max = DEFAULT_PTYS;
stress_pty_info_t *ptys;
const pid_t pid = getpid();
(void)stress_get_setting("pty-max", &pty_max);
ptys = calloc(pty_max, sizeof(*ptys));
if (!ptys) {
pr_inf("%s: allocation of pty array failed: %d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
size_t i, n;
for (n = 0; n < pty_max; n++) {
ptys[n].follower = -1;
ptys[n].leader = open("/dev/ptmx", O_RDWR);
if (ptys[n].leader < 0) {
if ((errno != ENOMEM) &&
(errno != ENOSPC) &&
(errno != EIO) &&
(errno != EMFILE)) {
pr_fail("%s: open /dev/ptmx failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto clean;
}
} else {
ptys[n].followername = ptsname(ptys[n].leader);
if (!ptys[n].followername) {
pr_fail("%s: ptsname failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto clean;
}
if (grantpt(ptys[n].leader) < 0) {
pr_fail("%s: grantpt failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto clean;
}
if (unlockpt(ptys[n].leader) < 0) {
pr_fail("%s: unlockpt failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto clean;
}
ptys[n].follower = open(ptys[n].followername, O_RDWR);
if (ptys[n].follower < 0) {
if (errno != EMFILE) {
pr_fail("%s: open %s failed, errno=%d (%s)\n",
args->name, ptys[n].followername, errno, strerror(errno));
goto clean;
}
}
}
if (!keep_stressing_flag())
goto clean;
}
/*
* ... and exercise ioctls ...
*/
for (i = 0; i < n; i++) {
if ((ptys[i].leader < 0) || (ptys[i].follower < 0))
continue;
(void)stress_read_fdinfo(pid, ptys[i].leader);
(void)stress_read_fdinfo(pid, ptys[i].follower);
#if defined(DHAVE_TCGETATTR)
{
struct termios ios;
if (tcgetattr(ptys[i].leader, &ios) < 0) {
pr_fail("%s: tcgetattr on leader pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
#endif
#if defined(HAVE_TCDRAIN)
{
if (tcdrain(ptys[i].follower) < 0) {
pr_fail("%s: tcdrain on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
#endif
#if defined(HAVE_TCFLUSH)
{
#if defined(TCIFLUSH)
if (tcflush(ptys[i].follower, TCIFLUSH) < 0) {
pr_fail("%s: tcflush TCIFLUSH on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCOFLUSH)
if (tcflush(ptys[i].follower, TCOFLUSH) < 0) {
pr_fail("%s: tcflush TCOFLUSH on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCIOFLUSH)
if (tcflush(ptys[i].follower, TCIOFLUSH) < 0) {
pr_fail("%s: tcflush TCOOFLUSH on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
}
#endif
#if defined(HAVE_TCFLOW)
#if defined(TCOOFF) && \
defined(TCOON)
{
if (tcflow(ptys[i].follower, TCOOFF) < 0) {
pr_fail("%s: tcflow TCOOFF on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
if (tcflow(ptys[i].follower, TCOON) < 0) {
pr_fail("%s: tcflow TCOON on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
#endif
#if defined(TCIOFF) && \
defined(TCION)
{
if (tcflow(ptys[i].follower, TCIOFF) < 0) {
pr_fail("%s: tcflow TCIOFF on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
if (tcflow(ptys[i].follower, TCION) < 0) {
pr_fail("%s: tcflow TCION on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
#endif
#endif
#if defined(TCGETS)
{
struct termios ios;
if ((ioctl(ptys[i].follower, TCGETS, &ios) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCGETS on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETS)
{
struct termios ios;
if ((ioctl(ptys[i].follower, TCSETS, &ios) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETS on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETSW)
{
struct termios ios;
if ((ioctl(ptys[i].follower, TCSETSW, &ios) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETSW on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETSF)
{
struct termios ios;
if ((ioctl(ptys[i].follower, TCSETSF, &ios) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETSF on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCGETA)
{
struct termio io;
if ((ioctl(ptys[i].follower, TCGETA, &io) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCGETA on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETA)
{
struct termio io;
if ((ioctl(ptys[i].follower, TCSETA, &io) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETA on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETAW)
{
struct termio io;
if ((ioctl(ptys[i].follower, TCSETAW, &io) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETAW on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TCSETAF)
{
struct termio io;
if ((ioctl(ptys[i].follower, TCSETAF, &io) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TCSETAF on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TIOCGLCKTRMIOS)
{
struct termios ios;
if ((ioctl(ptys[i].follower, TIOCGLCKTRMIOS, &ios) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TIOCGLCKTRMIOS on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TIOCGWINSZ)
{
struct winsize ws;
if ((ioctl(ptys[i].follower, TIOCGWINSZ, &ws) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TIOCGWINSZ on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TIOCSWINSZ)
{
struct winsize ws;
if ((ioctl(ptys[i].follower, TIOCSWINSZ, &ws) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TIOCSWINSZ on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(FIONREAD)
{
int arg;
if ((ioctl(ptys[i].follower, FIONREAD, &arg) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl FIONREAD on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TIOCINQ)
{
int arg;
if ((ioctl(ptys[i].follower, TIOCINQ, &arg) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TIOCINQ on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
#if defined(TIOCOUTQ)
{
int arg;
if ((ioctl(ptys[i].follower, TIOCOUTQ, &arg) < 0) &&
(errno != EINTR))
pr_fail("%s: ioctl TIOCOUTQ on follower pty failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
#endif
if (!keep_stressing_flag())
goto clean;
}
clean:
/*
* and close
*/
for (i = 0; i < n; i++) {
if (ptys[i].follower != -1)
(void)close(ptys[i].follower);
if (ptys[i].leader != -1)
(void)close(ptys[i].leader);
}
inc_counter(args);
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
free(ptys);
return EXIT_SUCCESS;
}
stressor_info_t stress_pty_info = {
.stressor = stress_pty,
.class = CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
stressor_info_t stress_pty_info = {
.stressor = stress_not_implemented,
.class = CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.help = help
};
#endif