(root)/
strace-6.5/
tests-m32/
ipc.c
       1  /*
       2   * Check decoding of ipc syscall.
       3   *
       4   * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@strace.io>
       5   * Copyright (c) 2016-2021 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  #if defined __NR_ipc && defined HAVE_LINUX_IPC_H
      15  
      16  # include <errno.h>
      17  # include <stdio.h>
      18  # include <unistd.h>
      19  # include <linux/ipc.h>
      20  
      21  # ifndef SEMCTL
      22  #  define SEMCTL 3
      23  # endif
      24  # ifndef MSGRCV
      25  #  define MSGRCV 12
      26  # endif
      27  
      28  static int
      29  ipc_call(const unsigned short version, const unsigned short call,
      30  	 long a1, long a2, long a3, long a4, long a5)
      31  {
      32  	const unsigned long val =
      33  		(unsigned long) 0xfacefeed00000000ULL |
      34  		(unsigned int) version << 16 |
      35  		call;
      36  
      37  	return syscall(__NR_ipc, val, a1, a2, a3, a4, a5);
      38  }
      39  
      40  static int
      41  ipc_call0(const unsigned short version, const unsigned short call)
      42  {
      43  	int rc = ipc_call(version, call, 0, 0, 0, 0, 0);
      44  	int saved_errno = errno;
      45  	printf("ipc(");
      46  	if (version)
      47  		printf("%hu<<16|", version);
      48  	errno = saved_errno;
      49  	printf("%hu, 0, 0, 0, 0%s) = %d %s (%m)\n", call,
      50  # ifdef __s390__
      51  	       "",
      52  # else
      53  	       ", 0",
      54  # endif
      55  	       rc, errno2name());
      56  	return rc;
      57  }
      58  
      59  int
      60  main(void)
      61  {
      62  	void *const efault = tail_alloc(1) + 1;
      63  
      64  	int rc = ipc_call(0, SEMCTL, 0, 0, 0, (long) efault, 0);
      65  	if (rc != -1 || EFAULT != errno)
      66  		perror_msg_and_skip("ipc");
      67  	printf("semctl(0, 0, IPC_RMID, %p) = -1 EFAULT (%m)\n", efault);
      68  
      69  	for (unsigned short call = 0; call <= 40; call += 10) {
      70  		ipc_call0(0, call);
      71  		ipc_call0(42, call);
      72  	}
      73  
      74  	rc = ipc_call(42, SEMCTL, 0, 0, 0, (long) efault, 0);
      75  	int test_version = EFAULT == errno;
      76  	if (test_version)
      77  		printf("semctl(0, 0, IPC_RMID, %p) = %d %s (%m)\n",
      78  		       efault, rc, errno2name());
      79  	else
      80  		printf("ipc(42<<16|SEMCTL, 0, 0, 0, %p) = %d %s (%m)\n",
      81  		       efault, rc, errno2name());
      82  
      83  	if (test_version) {
      84  		const int msqid = -2;
      85  		const long msgsz = -3;
      86  		const long msgtyp = 0;
      87  
      88  		rc = ipc_call(1, MSGRCV,
      89  			      msqid, msgsz, IPC_NOWAIT, (long) efault, msgtyp);
      90  		printf("msgrcv(%d, %p, %lu, %ld, IPC_NOWAIT) = %d %s (%m)\n",
      91  		       msqid, efault, msgsz, msgtyp, rc, errno2name());
      92  	}
      93  
      94  	puts("+++ exited with 0 +++");
      95  	return 0;
      96  }
      97  
      98  #else
      99  
     100  SKIP_MAIN_UNDEFINED("__NR_ipc && HAVE_LINUX_IPC_H")
     101  
     102  #endif