(root)/
strace-6.5/
tests/
quotactl.h
       1  /*
       2   * Common definitions for Linux and XFS quota tests.
       3   *
       4   * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
       5   * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
       6   * Copyright (c) 2016-2021 The strace developers.
       7   * All rights reserved.
       8   *
       9   * SPDX-License-Identifier: GPL-2.0-or-later
      10   */
      11  
      12  #ifndef STRACE_TESTS_QUOTACTL_H
      13  # define STRACE_TESTS_QUOTACTL_H
      14  
      15  # include <inttypes.h>
      16  # include <stdarg.h>
      17  # include <stdio.h>
      18  # include "print_fields.h"
      19  
      20  # include <linux/quota.h>
      21  
      22  # ifndef QCMD_CMD
      23  #  define QCMD_CMD(_val) ((unsigned) (_val) >> SUBCMDSHIFT)
      24  # endif /* !QCMD_CMD */
      25  
      26  # ifndef QCMD_TYPE
      27  #  define QCMD_TYPE(_val) ((unsigned) (_val) & SUBCMDMASK)
      28  # endif /* !QCMD_TYPE */
      29  
      30  typedef void (*print_cb)(long rc, void *addr, void *arg);
      31  
      32  enum check_quotactl_flag_bits {
      33  	CQF_ID_SKIP_BIT,
      34  	CQF_ID_STR_BIT,
      35  	CQF_ADDR_SKIP_BIT,
      36  	CQF_ADDR_STR_BIT,
      37  	CQF_ADDR_CB_BIT,
      38  };
      39  
      40  enum check_quotactl_flags {
      41  	CQF_NONE,
      42  	CQF_ID_SKIP   = 1 << CQF_ID_SKIP_BIT,
      43  	CQF_ID_STR    = 1 << CQF_ID_STR_BIT,
      44  	CQF_ADDR_SKIP = 1 << CQF_ADDR_SKIP_BIT,
      45  	CQF_ADDR_STR  = 1 << CQF_ADDR_STR_BIT,
      46  	CQF_ADDR_CB   = 1 << CQF_ADDR_CB_BIT,
      47  };
      48  
      49  static const char *errstr;
      50  
      51  /**
      52   * Generic quotactl syscall checker function.  Call convention:
      53   *
      54   *     check_quota(flags, cmd, cmd_str, special, special_str
      55   *		   [, id [, id_str]]
      56   *		   [, addr [, { addr_cb, addr_cb_arg | addr_str }]])
      57   *
      58   * check_quota performs a syscall invocation and prints the expected output
      59   * for it.
      60   *
      61   * It might be useful to employ ARG_STR macro for passing cmd/cmd_str,
      62   * special_special_str, id/id_str, and addr/addr_str argument pairs.
      63   *
      64   * @param flags Check flags:
      65   *               - CQF_ID_SKIP: the "id" syscall argument is ignored
      66   *                 in the syscall invocation.  No id and id_str arguments
      67   *                 should be provided if this flag is set.
      68   *                 This flag has priority over the CQF_ID_STR flag.
      69   *               - CQF_ID_STR: the "id" syscall argument has a special string
      70   *                 representation.  id_str argument should be provided if this
      71   *                 flag is set; no id_str argument should be provided and id
      72   *                 argument is printed as unsigned integer (with an exception
      73   *                 for -1, which is printed as signed) if this flag is not set.
      74   *               - CQF_ADDR_SKIP: the "addr" syscall argument is ignored
      75   *                 in the syscall invocation.  None of the addr, addr_cb,
      76   *                 addr_cb_arg, and/or addr_str arguments should be provided
      77   *                 if this flag is set.  This flag has priority
      78   *                 over the CQF_ADDR_STR and CQF_ADDR_CB flags.
      79   *               - CQF_ADDR_CB: the "addr" syscall argument printing is handled
      80   *                 via a callback function.  addr_cb (that points to a callback
      81   *                 function of type print_cb) and addr_cb_arg (an opaque pointer
      82   *                 that is passed to addr_cb in the third argument) should
      83   *                 be provided if this flag is set.
      84   *                 This flag has priority over the CQF_ADDR_STR flag.
      85   *               - CQF_ADDR_STR: addr syscall argument has a special string
      86   *                 representation.  addr_str argument should be provided if this
      87   *                 flag is set.  If both CQF_ADDR_CB and CQF_ADDR_STR flags
      88   *                 are not set, addr syscall argument is printed using "%p"
      89   *                 printf format.
      90   * @param cmd Value of the "cmd" syscall argument that should be passed
      91   *            in the syscall invocation.
      92   * @param cmd_str String representation of the "cmd" syscall argument.
      93   * @param special Value of the "special" syscall argument that should be passed
      94   *                in the syscall invocation.
      95   * @param special_str String representation of the "special" syscall argument.
      96   * @param ... Additional arguments depend on the flags being set:
      97   *             - id: Value of the "id" syscall argument.  Provided
      98   *               if CQF_ID_SKIP is not set, otherwise -1 is passed
      99   *               in the syscall invocation and the argument is not printed
     100   *               in the expected output.
     101   *             - id_str: String representation of the "id" syscall argument.
     102   *               Provided if CQF_ID_SKIP is not set and CQF_ID_STR is set.
     103   *             - addr: Value of the "addr" syscall argument.  Provided
     104   *               if CQF_ADDR_SKIP is not set, otherwise NULL is passed
     105   *               in the syscall invocation and the argument is not printed
     106   *               in the expected output.
     107   *             - addr_cb: Callback function that is called for the "addr"
     108   *               syscall argument printing. Should be of print_cb type.
     109   *               Provided if CQF_ADDR_SKIP is not set and CQF_ADDR_CB is set.
     110   *             - addr_cb_arg: Opaque pointer that is passed to addr_cb,
     111   *               Provided if CQF_ADDR_SKIP is not set and CQF_ADDR_CB is set.
     112   *             - addr_str: String representation of the "addr" syscall argument.
     113   *               Provided if CQF_ADDR_SKIP is not set, CQF_ADDR_CB is not set,
     114   *               and CQF_ADDR_STR is set.
     115   */
     116  static inline void
     117  check_quota(uint32_t flags, int cmd, const char *cmd_str,
     118  	const char *special, const char *special_str, ...)
     119  {
     120  	long rc;
     121  	const char *addr_str = NULL;
     122  	const char *id_str = NULL;
     123  	void *addr = NULL;
     124  	print_cb addr_cb = NULL;
     125  	void *addr_cb_arg = NULL;
     126  	uint32_t id = -1;
     127  
     128  	va_list ap;
     129  
     130  	va_start(ap, special_str);
     131  
     132  	if (!(flags & CQF_ID_SKIP)) {
     133  		id = va_arg(ap, uint32_t);
     134  
     135  		if (flags & CQF_ID_STR)
     136  			id_str = va_arg(ap, const char *);
     137  	}
     138  
     139  	if (!(flags & CQF_ADDR_SKIP)) {
     140  		addr = va_arg(ap, void *);
     141  
     142  		if (flags & CQF_ADDR_CB) {
     143  			addr_cb = va_arg(ap, print_cb);
     144  			addr_cb_arg = va_arg(ap, void *);
     145  		} else if (flags & CQF_ADDR_STR) {
     146  			addr_str = va_arg(ap, const char *);
     147  		}
     148  	}
     149  
     150  	va_end(ap);
     151  
     152  	rc = syscall(__NR_quotactl, cmd, special, id, addr);
     153  
     154  	errstr = sprintrc(rc);
     155  
     156  # ifdef INJECT_RETVAL
     157  	if (rc != INJECT_RETVAL)
     158  		error_msg_and_fail("Got a return value of %ld != %d",
     159  				   rc, INJECT_RETVAL);
     160  
     161  	static char inj_errstr[4096];
     162  
     163  	snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr);
     164  	errstr = inj_errstr;
     165  # endif
     166  
     167  	printf("quotactl(%s, %s", cmd_str, special_str);
     168  
     169  	if (!(flags & CQF_ID_SKIP)) {
     170  		if (flags & CQF_ID_STR) {
     171  			printf(", %s", id_str);
     172  		} else {
     173  			if (id == (uint32_t)-1)
     174  				printf(", -1");
     175  			else
     176  				printf(", %u", id);
     177  		}
     178  	}
     179  
     180  	if (!(flags & CQF_ADDR_SKIP)) {
     181  		if (flags & CQF_ADDR_CB) {
     182  			printf(", ");
     183  			addr_cb(rc, addr, addr_cb_arg);
     184  		} else if (flags & CQF_ADDR_STR) {
     185  			printf(", %s", addr_str);
     186  		} else {
     187  			printf(", %p", addr);
     188  		}
     189  	}
     190  
     191  	printf(") = %s\n", errstr);
     192  }
     193  
     194  
     195  static const int bogus_cmd = 0xbadc0ded;
     196  static const int bogus_id = 0xca7faced;
     197  
     198  /* It is invalid anyway due to the slash in the end */
     199  static const char *bogus_dev = "/dev/bogus/";
     200  static const char *bogus_dev_str = "\"/dev/bogus/\"";
     201  
     202  static const char unterminated_data[] = { '\1', '\2', '\3' };
     203  
     204  #endif /* !STRACE_TESTS_QUOTACTL_H */