(root)/
strace-6.5/
tests-m32/
ppoll.c
       1  /*
       2   * Check decoding of ppoll syscall.
       3   *
       4   * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@strace.io>
       5   * Copyright (c) 2015-2023 The strace developers.
       6   * All rights reserved.
       7   *
       8   * SPDX-License-Identifier: GPL-2.0-or-later
       9   */
      10  
      11  #include "tests.h"
      12  #include "scno.h"
      13  
      14  #ifdef __NR_ppoll
      15  
      16  # include <errno.h>
      17  # include <poll.h>
      18  # include <signal.h>
      19  # include <stdio.h>
      20  # include <string.h>
      21  # include <unistd.h>
      22  
      23  # ifndef PATH_TRACING_FD
      24  #  define PATH_TRACING_FD 0
      25  # endif
      26  # ifndef TRACING_FDS
      27  #  define TRACING_FDS 0
      28  # endif
      29  # ifndef TRACING_FD1
      30  #  define TRACING_FD1 0
      31  # endif
      32  # ifndef TRACING_FD2
      33  #  define TRACING_FD2 0
      34  # endif
      35  # ifndef TRACE_FD1
      36  #  define TRACE_FD1 (!PATH_TRACING_FD && (!TRACING_FDS || TRACING_FD1))
      37  # endif
      38  # ifndef TRACE_FD2
      39  #  define TRACE_FD2 (!PATH_TRACING_FD && (!TRACING_FDS || TRACING_FD2))
      40  # endif
      41  # ifndef TRACE_OTHER_FDS
      42  #  define TRACE_OTHER_FDS (!PATH_TRACING_FD && !TRACING_FDS)
      43  # endif
      44  
      45  static const char *errstr;
      46  
      47  static long
      48  sys_ppoll(const kernel_ulong_t ufds,
      49  	  const kernel_ulong_t nfds,
      50  	  const kernel_ulong_t tsp,
      51  	  const kernel_ulong_t sigmask,
      52  	  const kernel_ulong_t sigsetsize)
      53  {
      54  	long rc = syscall(__NR_ppoll, ufds, nfds, tsp, sigmask, sigsetsize);
      55  	errstr = sprintrc(rc);
      56  	return rc;
      57  }
      58  
      59  int
      60  main(void)
      61  {
      62  # if PATH_TRACING_FD
      63  	skip_if_unavailable("/proc/self/fd/");
      64  # endif
      65  
      66  	static const kernel_ulong_t bogus_nfds =
      67  		(kernel_ulong_t) 0xdeadbeeffacefeedULL;
      68  	static const kernel_ulong_t bogus_sigsetsize =
      69  		(kernel_ulong_t) 0xdeadbeefbadc0dedULL;
      70  	static const char *const POLLWRNORM_str =
      71  		(POLLWRNORM == POLLOUT) ? "" : "|POLLWRNORM";
      72  	static const char *const USR2_CHLD_str =
      73  		(SIGUSR2 < SIGCHLD) ? "USR2 CHLD" : "CHLD USR2";
      74  	void *const efault = tail_alloc(1024) + 1024;
      75  	TAIL_ALLOC_OBJECT_CONST_PTR(kernel_old_timespec_t, ts);
      76  	const unsigned int sigset_size = get_sigset_size();
      77  	void *const sigmask = tail_alloc(sigset_size);
      78  	struct pollfd *fds;
      79  	sigset_t mask;
      80  	int pipe_fd[4];
      81  	long rc;
      82  
      83  	sys_ppoll(0, bogus_nfds, 0, 0, bogus_sigsetsize);
      84  	if (ENOSYS == errno)
      85  		perror_msg_and_skip("ppoll");
      86  # if !PATH_TRACING_FD && !TRACING_FDS
      87  	printf("ppoll(NULL, %u, NULL, NULL, %llu) = %s\n",
      88  	       (unsigned) bogus_nfds, (unsigned long long) bogus_sigsetsize,
      89  	       errstr);
      90  # endif
      91  
      92  	sys_ppoll((unsigned long) efault, 42, (unsigned long) efault + 8,
      93  		  (unsigned long) efault + 16, sigset_size);
      94  # if !PATH_TRACING_FD && !TRACING_FDS
      95  	printf("ppoll(%p, %u, %p, %p, %u) = %s\n",
      96  	       efault, 42, efault + 8, efault + 16, sigset_size, errstr);
      97  # endif
      98  
      99  	ts->tv_sec = 0xdeadbeefU;
     100  	ts->tv_nsec = 0xfacefeedU;
     101  	sys_ppoll(0, 0, (unsigned long) ts, 0, sigset_size);
     102  # if !PATH_TRACING_FD && !TRACING_FDS
     103  	printf("ppoll(NULL, 0, {tv_sec=%lld, tv_nsec=%llu}, NULL, %u) = %s\n",
     104  	       (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec),
     105  	       sigset_size, errstr);
     106  # endif
     107  
     108  	ts->tv_sec = (typeof(ts->tv_sec)) 0xcafef00ddeadbeefLL;
     109  	ts->tv_nsec = (long) 0xbadc0dedfacefeedL;
     110  	sys_ppoll(0, 0, (unsigned long) ts, 0, sigset_size);
     111  # if !PATH_TRACING_FD && !TRACING_FDS
     112  	printf("ppoll(NULL, 0, {tv_sec=%lld, tv_nsec=%llu}, NULL, %u) = %s\n",
     113  	       (long long) ts->tv_sec, zero_extend_signed_to_ull(ts->tv_nsec),
     114  	       sigset_size, errstr);
     115  # endif
     116  
     117  	if (pipe(pipe_fd) || pipe(pipe_fd + 2))
     118  		perror_msg_and_fail("pipe");
     119  
     120  # if TRACING_FD1
     121  	if (dup2(pipe_fd[3], TRACING_FD1) < 0)
     122  		perror_msg_and_fail("dup2 1");
     123  	close(pipe_fd[3]);
     124  	pipe_fd[3] = TRACING_FD1;
     125  # endif
     126  
     127  # if TRACING_FD2
     128  	if (dup2(pipe_fd[1], TRACING_FD2) < 0)
     129  		perror_msg_and_fail("dup2 2");
     130  	close(pipe_fd[1]);
     131  	pipe_fd[1] = TRACING_FD2;
     132  # endif
     133  
     134  	ts->tv_sec = 42;
     135  	ts->tv_nsec = 999999999;
     136  
     137  	const struct pollfd fds1[] = {
     138  		{ .fd = pipe_fd[0], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
     139  		{ .fd = pipe_fd[1], .events = POLLOUT | POLLWRNORM | POLLWRBAND },
     140  		{ .fd = pipe_fd[2], .events = POLLIN | POLLPRI },
     141  		{ .fd = pipe_fd[3], .events = POLLOUT }
     142  	};
     143  	fds = efault - sizeof(fds1);
     144  	memcpy(fds, fds1, sizeof(fds1));
     145  
     146  	sigemptyset(&mask);
     147  	sigaddset(&mask, SIGUSR2);
     148  	sigaddset(&mask, SIGCHLD);
     149  	memcpy(sigmask, &mask, sigset_size);
     150  
     151  	rc = sys_ppoll((unsigned long) fds,
     152  		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
     153  		       (unsigned long) sigmask, sigset_size);
     154  	if (rc != 2)
     155  		perror_msg_and_fail("ppoll 1");
     156  # if TRACE_FD1 || TRACE_FD2 || TRACE_OTHER_FDS
     157  	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
     158  	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
     159  #  if VERBOSE
     160  	       ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
     161  #  else
     162  	       ", ...]"
     163  #  endif
     164  	       ", %u, {tv_sec=42, tv_nsec=999999999}, [%s], %u) = %ld"
     165  	       " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
     166  	       ", left {tv_sec=%u, tv_nsec=%u})\n",
     167  	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
     168  #  if VERBOSE
     169  	       pipe_fd[2], pipe_fd[3],
     170  #  endif
     171  	       (unsigned int) ARRAY_SIZE(fds1), USR2_CHLD_str,
     172  	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
     173  	       pipe_fd[3], (unsigned int) ts->tv_sec,
     174  	       (unsigned int) ts->tv_nsec);
     175  # endif /* !PATH_TRACING_FDS */
     176  
     177  	ts->tv_sec = 23;
     178  	ts->tv_nsec = 123456789;
     179  
     180  	rc = sys_ppoll((unsigned long) fds,
     181  		       F8ILL_KULONG_MASK | (ARRAY_SIZE(fds1) - 2),
     182  		       (unsigned long) ts,
     183  		       (unsigned long) sigmask, sigset_size);
     184  	if (rc != 1)
     185  		perror_msg_and_fail("ppoll 1.5");
     186  # if TRACE_FD2 || TRACE_OTHER_FDS
     187  	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
     188  	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}]"
     189  	       ", %u, {tv_sec=23, tv_nsec=123456789}, [%s], %u) = %ld"
     190  	       " ([{fd=%d, revents=POLLOUT%s}]"
     191  	       ", left {tv_sec=%u, tv_nsec=%u})\n",
     192  	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
     193  	       (unsigned int) ARRAY_SIZE(fds1) - 2, USR2_CHLD_str,
     194  	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
     195  	       (unsigned int) ts->tv_sec, (unsigned int) ts->tv_nsec);
     196  # endif /* !PATH_TRACING_FDS */
     197  
     198  # if PATH_TRACING_FD
     199  	ts->tv_sec = 123;
     200  	ts->tv_nsec = 987654321;
     201  	fds[3].fd = PATH_TRACING_FD;
     202  
     203  	rc = sys_ppoll((unsigned long) fds,
     204  		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds1), (unsigned long) ts,
     205  		       (unsigned long) sigmask, sigset_size);
     206  	if (rc != 2)
     207  		perror_msg_and_fail("ppoll -P");
     208  	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
     209  	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}"
     210  #  if VERBOSE
     211  	       ", {fd=%d, events=POLLIN|POLLPRI}, {fd=%d, events=POLLOUT}]"
     212  #  else
     213  	       ", ...]"
     214  #  endif
     215  	       ", %u, {tv_sec=123, tv_nsec=987654321}, [%s], %u) = %ld"
     216  	       " ([{fd=%d, revents=POLLOUT%s}, {fd=%d, revents=POLLOUT}]"
     217  	       ", left {tv_sec=%u, tv_nsec=%u})\n",
     218  	       pipe_fd[0], pipe_fd[1], POLLWRNORM_str,
     219  #  if VERBOSE
     220  	       pipe_fd[2], PATH_TRACING_FD,
     221  #  endif
     222  	       (unsigned int) ARRAY_SIZE(fds1), USR2_CHLD_str,
     223  	       (unsigned int) sigset_size, rc, pipe_fd[1], POLLWRNORM_str,
     224  	       PATH_TRACING_FD, (unsigned int) ts->tv_sec,
     225  	       (unsigned int) ts->tv_nsec);
     226  # endif /* PATH_TRACING_FD */
     227  
     228  	ts->tv_sec = 0;
     229  	ts->tv_nsec = 999;
     230  	const struct pollfd fds2[] = {
     231  		{ .fd = pipe_fd[1], .events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND },
     232  		{ .fd = pipe_fd[0], .events = POLLOUT | POLLWRNORM | POLLWRBAND }
     233  	};
     234  	fds = efault - sizeof(fds2);
     235  	memcpy(fds, fds2, sizeof(fds2));
     236  
     237  	memset(&mask, -1, sizeof(mask));
     238  	sigdelset(&mask, SIGHUP);
     239  	sigdelset(&mask, SIGKILL);
     240  	sigdelset(&mask, SIGSTOP);
     241  	memcpy(sigmask, &mask, sigset_size);
     242  
     243  	rc = sys_ppoll((unsigned long) fds,
     244  		       F8ILL_KULONG_MASK | ARRAY_SIZE(fds2), (unsigned long) ts,
     245  		       (unsigned long) sigmask, sigset_size);
     246  	if (rc != 0)
     247  		perror_msg_and_fail("ppoll 2");
     248  # if TRACE_FD2 || TRACE_OTHER_FDS
     249  	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}"
     250  	       ", {fd=%d, events=POLLOUT%s|POLLWRBAND}], %u"
     251  	       ", {tv_sec=0, tv_nsec=999}, ~[HUP KILL STOP], %u)"
     252  	       " = %ld (Timeout)\n",
     253  	       pipe_fd[1], pipe_fd[0], POLLWRNORM_str,
     254  	       (unsigned) ARRAY_SIZE(fds2), sigset_size, rc);
     255  # endif /* !PATH_TRACING_FD */
     256  
     257  	ts->tv_sec = 0;
     258  	ts->tv_nsec = 1;
     259  
     260  	rc = sys_ppoll((unsigned long) fds,
     261  		       F8ILL_KULONG_MASK | 1, (unsigned long) ts,
     262  		       (unsigned long) sigmask, sigset_size);
     263  	if (rc != 0)
     264  		perror_msg_and_fail("ppoll 2");
     265  # if !PATH_TRACING_FD && TRACE_FD2
     266  	printf("ppoll([{fd=%d, events=POLLIN|POLLPRI|POLLRDNORM|POLLRDBAND}], 1"
     267  	       ", {tv_sec=0, tv_nsec=1}, ~[HUP KILL STOP], %u)"
     268  	       " = %ld (Timeout)\n",
     269  	       pipe_fd[1], sigset_size, rc);
     270  # endif /* !PATH_TRACING_FD */
     271  
     272  	if (F8ILL_KULONG_SUPPORTED) {
     273  		sys_ppoll(f8ill_ptr_to_kulong(fds), ARRAY_SIZE(fds2),
     274  			  f8ill_ptr_to_kulong(ts), f8ill_ptr_to_kulong(sigmask),
     275  			  sigset_size);
     276  # if !PATH_TRACING_FD && !TRACING_FDS
     277  		printf("ppoll(%#llx, %u, %#llx, %#llx, %u) = %s\n",
     278  		       (unsigned long long) f8ill_ptr_to_kulong(fds),
     279  		       (unsigned) ARRAY_SIZE(fds2),
     280  		       (unsigned long long) f8ill_ptr_to_kulong(ts),
     281  		       (unsigned long long) f8ill_ptr_to_kulong(sigmask),
     282  		       (unsigned) sigset_size, errstr);
     283  # endif /* !PATH_TRACING_FD */
     284  	}
     285  
     286  	puts("+++ exited with 0 +++");
     287  	return 0;
     288  }
     289  
     290  #else
     291  
     292  SKIP_MAIN_UNDEFINED("__NR_ppoll")
     293  
     294  #endif