Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fcntl.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
  3. * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
  4. * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
  5. * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 1999-2018 The strace developers.
  7. * All rights reserved.
  8. *
  9. * SPDX-License-Identifier: LGPL-2.1-or-later
  10. */
  11. #include "defs.h"
  12. #include "flock.h"
  13. #include "xlat/f_owner_types.h"
  14. #include "xlat/f_seals.h"
  15. #include "xlat/fcntlcmds.h"
  16. #include "xlat/fdflags.h"
  17. #include "xlat/lockfcmds.h"
  18. #include "xlat/notifyflags.h"
  19. static void
  20. print_struct_flock64(const struct_kernel_flock64 *fl, const int getlk)
  21. {
  22. tprints("{l_type=");
  23. printxval(lockfcmds, (unsigned short) fl->l_type, "F_???");
  24. tprints(", l_whence=");
  25. printxval(whence_codes, (unsigned short) fl->l_whence, "SEEK_???");
  26. tprintf(", l_start=%" PRId64 ", l_len=%" PRId64,
  27. (int64_t) fl->l_start, (int64_t) fl->l_len);
  28. if (getlk)
  29. tprintf(", l_pid=%lu", (unsigned long) fl->l_pid);
  30. tprints("}");
  31. }
  32. static void
  33. printflock64(struct tcb *const tcp, const kernel_ulong_t addr, const int getlk)
  34. {
  35. struct_kernel_flock64 fl;
  36. if (fetch_struct_flock64(tcp, addr, &fl))
  37. print_struct_flock64(&fl, getlk);
  38. }
  39. static void
  40. printflock(struct tcb *const tcp, const kernel_ulong_t addr, const int getlk)
  41. {
  42. struct_kernel_flock64 fl;
  43. if (fetch_struct_flock(tcp, addr, &fl))
  44. print_struct_flock64(&fl, getlk);
  45. }
  46. static void
  47. print_f_owner_ex(struct tcb *const tcp, const kernel_ulong_t addr)
  48. {
  49. struct { int type, pid; } owner;
  50. if (umove_or_printaddr(tcp, addr, &owner))
  51. return;
  52. tprints("{type=");
  53. printxval(f_owner_types, owner.type, "F_OWNER_???");
  54. tprintf(", pid=%d}", owner.pid);
  55. }
  56. static int
  57. print_fcntl(struct tcb *tcp)
  58. {
  59. const unsigned int cmd = tcp->u_arg[1];
  60. switch (cmd) {
  61. case F_SETFD:
  62. tprints(", ");
  63. printflags(fdflags, tcp->u_arg[2], "FD_???");
  64. break;
  65. case F_SETOWN:
  66. case F_SETPIPE_SZ:
  67. tprintf(", %" PRI_kld, tcp->u_arg[2]);
  68. break;
  69. case F_DUPFD:
  70. case F_DUPFD_CLOEXEC:
  71. tprintf(", %" PRI_kld, tcp->u_arg[2]);
  72. return RVAL_DECODED | RVAL_FD;
  73. case F_SETFL:
  74. tprints(", ");
  75. tprint_open_modes(tcp->u_arg[2]);
  76. break;
  77. case F_SETLK:
  78. case F_SETLKW:
  79. tprints(", ");
  80. printflock(tcp, tcp->u_arg[2], 0);
  81. break;
  82. case F_OFD_SETLK:
  83. case F_OFD_SETLKW:
  84. tprints(", ");
  85. printflock64(tcp, tcp->u_arg[2], 0);
  86. break;
  87. case F_SETOWN_EX:
  88. tprints(", ");
  89. print_f_owner_ex(tcp, tcp->u_arg[2]);
  90. break;
  91. case F_NOTIFY:
  92. tprints(", ");
  93. printflags64(notifyflags, tcp->u_arg[2], "DN_???");
  94. break;
  95. case F_SETLEASE:
  96. tprints(", ");
  97. printxval64(lockfcmds, tcp->u_arg[2], "F_???");
  98. break;
  99. case F_ADD_SEALS:
  100. tprints(", ");
  101. printflags64(f_seals, tcp->u_arg[2], "F_SEAL_???");
  102. break;
  103. case F_SETSIG:
  104. tprints(", ");
  105. printsignal(tcp->u_arg[2]);
  106. break;
  107. case F_GETOWN:
  108. case F_GETPIPE_SZ:
  109. break;
  110. case F_GETFD:
  111. if (entering(tcp) || syserror(tcp) || tcp->u_rval == 0)
  112. return 0;
  113. tcp->auxstr = sprintflags("flags ", fdflags,
  114. (kernel_ulong_t) tcp->u_rval);
  115. return RVAL_HEX | RVAL_STR;
  116. case F_GETFL:
  117. if (entering(tcp) || syserror(tcp))
  118. return 0;
  119. tcp->auxstr = sprint_open_modes(tcp->u_rval);
  120. return RVAL_HEX | RVAL_STR;
  121. case F_GETLK:
  122. if (entering(tcp))
  123. return 0;
  124. tprints(", ");
  125. printflock(tcp, tcp->u_arg[2], 1);
  126. break;
  127. case F_OFD_GETLK:
  128. if (entering(tcp))
  129. return 0;
  130. tprints(", ");
  131. printflock64(tcp, tcp->u_arg[2], 1);
  132. break;
  133. case F_GETOWN_EX:
  134. if (entering(tcp))
  135. return 0;
  136. tprints(", ");
  137. print_f_owner_ex(tcp, tcp->u_arg[2]);
  138. break;
  139. case F_GETLEASE:
  140. if (entering(tcp) || syserror(tcp))
  141. return 0;
  142. tcp->auxstr = xlookup(lockfcmds, (kernel_ulong_t) tcp->u_rval);
  143. return RVAL_HEX | RVAL_STR;
  144. case F_GET_SEALS:
  145. if (entering(tcp) || syserror(tcp) || tcp->u_rval == 0)
  146. return 0;
  147. tcp->auxstr = sprintflags("seals ", f_seals,
  148. (kernel_ulong_t) tcp->u_rval);
  149. return RVAL_HEX | RVAL_STR;
  150. case F_GETSIG:
  151. if (entering(tcp) || syserror(tcp) || tcp->u_rval == 0)
  152. return 0;
  153. tcp->auxstr = signame(tcp->u_rval);
  154. return RVAL_STR;
  155. default:
  156. tprintf(", %#" PRI_klx, tcp->u_arg[2]);
  157. break;
  158. }
  159. return RVAL_DECODED;
  160. }
  161. SYS_FUNC(fcntl)
  162. {
  163. if (entering(tcp)) {
  164. printfd(tcp, tcp->u_arg[0]);
  165. tprints(", ");
  166. printxval(fcntlcmds, tcp->u_arg[1], "F_???");
  167. }
  168. return print_fcntl(tcp);
  169. }
  170. SYS_FUNC(fcntl64)
  171. {
  172. const unsigned int cmd = tcp->u_arg[1];
  173. if (entering(tcp)) {
  174. printfd(tcp, tcp->u_arg[0]);
  175. tprints(", ");
  176. printxval(fcntlcmds, cmd, "F_???");
  177. }
  178. switch (cmd) {
  179. case F_SETLK64:
  180. case F_SETLKW64:
  181. tprints(", ");
  182. printflock64(tcp, tcp->u_arg[2], 0);
  183. return RVAL_DECODED;
  184. case F_GETLK64:
  185. if (exiting(tcp)) {
  186. tprints(", ");
  187. printflock64(tcp, tcp->u_arg[2], 1);
  188. }
  189. return 0;
  190. }
  191. return print_fcntl(tcp);
  192. }