(root)/
strace-6.5/
tests-mx32/
ipc_msgbuf.c
       1  /*
       2   * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
       3   * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
       4   * Copyright (c) 2015-2021 The strace developers.
       5   * All rights reserved.
       6   *
       7   * SPDX-License-Identifier: GPL-2.0-or-later
       8   */
       9  
      10  #include "tests.h"
      11  #include <stdio.h>
      12  #include <stdlib.h>
      13  #include <unistd.h>
      14  #include "scno.h"
      15  #include <sys/ipc.h>
      16  #include <sys/msg.h>
      17  #include <sys/stat.h>
      18  
      19  #define text_string "STRACE_STRING"
      20  #define msgsz sizeof(text_string)
      21  
      22  static int msqid = -1;
      23  
      24  #if XLAT_RAW
      25  # define str_ipc_creat "0x200"
      26  # define str_ipc_private "0"
      27  # define str_ipc_rmid "0"
      28  # define str_ipc_64 "0x100"
      29  #elif XLAT_VERBOSE
      30  # define str_ipc_creat "0x200 /\\* IPC_CREAT \\*/"
      31  # define str_ipc_private "0 /\\* IPC_PRIVATE \\*/"
      32  # define str_ipc_rmid "0 /\\* IPC_RMID \\*/"
      33  # define str_ipc_64 "0x100 /\\* IPC_64 \\*/"
      34  #else
      35  # define str_ipc_creat "IPC_CREAT"
      36  # define str_ipc_private "IPC_PRIVATE"
      37  # define str_ipc_rmid "IPC_RMID"
      38  # define str_ipc_64 "IPC_64"
      39  #endif
      40  
      41  static int
      42  cleanup(void)
      43  {
      44  	if (msqid != -1) {
      45  		int rc = msgctl(msqid, IPC_RMID, 0);
      46  		printf("msgctl\\(%d, (%s\\|)?%s, NULL\\) = 0\n",
      47  		       msqid, str_ipc_64, str_ipc_rmid);
      48  		msqid = -1;
      49  		if (rc == -1)
      50  			return 77;
      51  		puts("\\+\\+\\+ exited with 0 \\+\\+\\+");
      52  	}
      53  	return 0;
      54  }
      55  
      56  static int
      57  sys_msgrcv(int msqid, void *msgp, size_t sz, kernel_long_t msgtyp,
      58  	   int msgflg)
      59  {
      60  #if defined __x86_64__ && defined __ILP32__
      61  	return syscall(__NR_msgrcv, msqid, msgp, sz, msgtyp, msgflg);
      62  #else
      63  	return msgrcv(msqid, msgp, sz, msgtyp, msgflg);
      64  #endif
      65  }
      66  
      67  int
      68  main(void)
      69  {
      70  	/* mtype has to be positive */
      71  	const kernel_long_t mtype = (kernel_long_t) 0x7facefed5adc0dedULL;
      72  	struct {
      73  		kernel_long_t mtype;
      74  		char mtext[msgsz];
      75  	} msg = {
      76  		.mtype = mtype,
      77  		.mtext = text_string
      78  	};
      79  	msqid = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
      80  	if (msqid == -1)
      81  		perror_msg_and_skip("msgget");
      82  	printf("msgget\\(%s, %s\\|0700\\) = %d\n",
      83  	       str_ipc_private, str_ipc_creat, msqid);
      84  
      85  	typedef void (*atexit_func)(void);
      86  	atexit((atexit_func) cleanup);
      87  
      88  	printf("msgsnd\\(%d, \\{mtype=%lld, mtext=\"" text_string
      89  	       "\\\\0\"\\}, 14, 0\\) = 0\n",
      90  	       msqid, (long long) mtype);
      91  	if (msgsnd(msqid, &msg, msgsz, 0) == -1)
      92  		perror_msg_and_skip("msgsnd");
      93  
      94  	if (sys_msgrcv(msqid, &msg, msgsz, -mtype, 0) != msgsz)
      95  		perror_msg_and_skip("msgrcv");
      96  	printf("msgrcv\\(%d, \\{mtype=%lld, mtext=\"" text_string
      97  	       "\\\\0\"\\}, 14, %lld, 0\\) = 14\n",
      98  	       msqid, (long long) mtype, -(long long) mtype);
      99  
     100  	return cleanup();
     101  }