1 /*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@strace.io>
3 * Copyright (c) 2014-2023 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8
9 #include "defs.h"
10 #include "xstring.h"
11
12 #include "xlat/ioprio_who.h"
13 #include "xlat/ioprio_class.h"
14
15 #define IOPRIO_CLASS_SHIFT (13)
16 #define IOPRIO_PRIO_MASK ((1ul << IOPRIO_CLASS_SHIFT) - 1)
17
18 #define IOPRIO_PRIO_CLASS(mask) ((mask) >> IOPRIO_CLASS_SHIFT)
19 #define IOPRIO_PRIO_DATA(mask) ((mask) & IOPRIO_PRIO_MASK)
20
21 static const char *
22 sprint_ioprio(unsigned int ioprio)
23 {
24 static char outstr[256];
25 char class_buf[64];
26 unsigned int class, data;
27
28 class = IOPRIO_PRIO_CLASS(ioprio);
29 data = IOPRIO_PRIO_DATA(ioprio);
30 sprintxval(class_buf, sizeof(class_buf), ioprio_class, class,
31 "IOPRIO_CLASS_???");
32 xsprintf(outstr, "IOPRIO_PRIO_VALUE(%s, %d)", class_buf, data);
33
34 return outstr;
35 }
36
37 void
38 print_ioprio(unsigned int ioprio)
39 {
40 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
41 PRINT_VAL_X(ioprio);
42
43 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
44 return;
45
46 const char *str = sprint_ioprio(ioprio);
47
48 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
49 ? tprints_comment : tprints_string)(str);
50 }
51
52 static void
53 ioprio_print_who(struct tcb *tcp, int which, int who)
54 {
55 switch (which)
56 {
57 case IOPRIO_WHO_PROCESS:
58 printpid(tcp, who, PT_TGID);
59 break;
60 case IOPRIO_WHO_PGRP:
61 printpid(tcp, who, PT_PGID);
62 break;
63 default:
64 PRINT_VAL_D(who);
65 break;
66 }
67 }
68
69 SYS_FUNC(ioprio_get)
70 {
71 if (entering(tcp)) {
72 /* which */
73 printxval(ioprio_who, tcp->u_arg[0], "IOPRIO_WHO_???");
74 tprint_arg_next();
75
76 /* who */
77 ioprio_print_who(tcp, tcp->u_arg[0], tcp->u_arg[1]);
78 return 0;
79 } else {
80 if (syserror(tcp))
81 return 0;
82 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
83 tcp->auxstr = NULL;
84 else
85 tcp->auxstr = sprint_ioprio(tcp->u_rval);
86 return RVAL_STR;
87 }
88 }
89
90 SYS_FUNC(ioprio_set)
91 {
92 /* which */
93 printxval(ioprio_who, tcp->u_arg[0], "IOPRIO_WHO_???");
94 tprint_arg_next();
95
96 /* who */
97 ioprio_print_who(tcp, tcp->u_arg[0], tcp->u_arg[1]);
98 tprint_arg_next();
99
100 /* ioprio */
101 if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
102 PRINT_VAL_D((int) tcp->u_arg[2]);
103
104 if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
105 return RVAL_DECODED;
106
107 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_VERBOSE
108 ? tprints_comment : tprints_string)(sprint_ioprio(tcp->u_arg[2]));
109
110 return RVAL_DECODED;
111 }