]> granicus.if.org Git - strace/blobdiff - tests/mq_sendrecv.c
strace: terminate itself if interrupted by a signal
[strace] / tests / mq_sendrecv.c
index ce67335bd319f13f1ae19243c0bccc4f45bf15f6..b673e5f39b62b3d43989709562a02dcb2a7ab89a 100644 (file)
@@ -3,29 +3,10 @@
  * mq_unlink syscalls.
  *
  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
+ * Copyright (c) 2016-2018 The strace developers.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the author may not be used to endorse or promote products
- *    derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "tests.h"
@@ -40,7 +21,6 @@
 # include <fcntl.h>
 # include <inttypes.h>
 # include <signal.h>
-# include <stdbool.h>
 # include <stdio.h>
 # include <stdlib.h>
 # include <string.h>
 
 # include "sigevent.h"
 
-# ifndef MQ_NAME
-#  define MQ_NAME "mq_sendrecv.sample"
-# endif
-
 # ifndef DUMPIO_READ
 #  define DUMPIO_READ 0
 # endif
@@ -61,6 +37,7 @@
 #  define DUMPIO_WRITE 0
 # endif
 
+static char *mq_name;
 
 enum {
        NUM_ATTRS = 8,
@@ -124,8 +101,8 @@ cleanup(void)
 {
        long rc;
 
-       rc = syscall(__NR_mq_unlink, MQ_NAME);
-       printf("mq_unlink(\"" MQ_NAME "\") = %s\n", sprintrc(rc));
+       rc = syscall(__NR_mq_unlink, mq_name);
+       printf("mq_unlink(\"%s\") = %s\n", mq_name, sprintrc(rc));
 
        puts("+++ exited with 0 +++");
 }
@@ -147,9 +124,9 @@ do_send(int fd, char *msg, unsigned int msg_size, struct timespec *tmout,
                if (cropped)
                        printf("...");
                errno = saved_errno;
-               printf(", %u, 42, {tv_sec=%jd, tv_nsec=%jd}) = %s\n", msg_size,
-                      (intmax_t) tmout->tv_sec, (intmax_t) tmout->tv_nsec,
-                      sprintrc(rc));
+               printf(", %u, 42, {tv_sec=%lld, tv_nsec=%llu}) = %s\n", msg_size,
+                      (long long) tmout->tv_sec,
+                      zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc));
                errno = saved_errno;
 
                if (rc == -1) {
@@ -185,9 +162,9 @@ do_recv(int fd, char *msg, unsigned int msg_size, struct timespec *tmout,
                        printf("%p", msg);
                }
                errno = saved_errno;
-               printf(", %u, [42], {tv_sec=%jd, tv_nsec=%jd}) = %s\n", MSG_SIZE,
-                      (intmax_t) tmout->tv_sec,
-                      (intmax_t) tmout->tv_nsec, sprintrc(rc));
+               printf(", %u, [42], {tv_sec=%lld, tv_nsec=%llu}) = %s\n", MSG_SIZE,
+                      (long long) tmout->tv_sec,
+                      zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc));
                errno = saved_errno;
 
                if (rc == -1) {
@@ -230,7 +207,7 @@ main(void)
        static const struct timespec future_tmout_data = {
                .tv_sec = (time_t) 0x7ea1fade7e57faceLL,
                .tv_nsec = 999999999,
-       };;
+       };
        struct_sigevent bogus_sev_data = {
                .sigev_notify = 0xdefaced,
                .sigev_signo = 0xfacefeed,
@@ -278,8 +255,8 @@ main(void)
        /* Valid attributes structure */
        rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
                     bogus_attrs);
-       printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, {mq_flags=%#llx"
-              "mq_maxmsg=%lld, mq_msgsize=%lld, mq_curmsgs=%lld}) = %s\n",
+       printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, {mq_flags=%#llx"
+              "mq_maxmsg=%lld, mq_msgsize=%lld, mq_curmsgs=%lld}) = %s\n",
               msg, (unsigned short) bogus_mode,
               (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
               (long long) bogus_attrs[1],
@@ -304,12 +281,12 @@ main(void)
        /* Partially invalid message (memory only partially available) */
        rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
                     MSG_SIZE, bogus_prio, bogus_tmout);
-       printf("mq_timedsend(%d, %p, %llu, %u, {tv_sec=%jd, tv_nsec=%jd}) = "
-              "%s\n",
+       printf("mq_timedsend(%d, %p, %llu, %u, {tv_sec=%lld, tv_nsec=%llu})"
+              " = %s\n",
               (int) bogus_fd, msg + MSG_SIZE - MSG_CUT,
               (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
-              (intmax_t) bogus_tmout->tv_sec, (intmax_t) bogus_tmout->tv_nsec,
-              sprintrc(rc));
+              (long long) bogus_tmout->tv_sec,
+              zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));
 
        /* Fully valid message, uncut */
        rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
@@ -317,10 +294,10 @@ main(void)
        errstr = sprintrc(rc);
        printf("mq_timedsend(%d, ", (int) bogus_fd);
        printstr(MSG_START + MSG_SIZE - MSG_CUT, MSG_CUT);
-       printf(", %llu, %u, {tv_sec=%jd, tv_nsec=%jd}) = %s\n",
+       printf(", %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
               (unsigned long long) MSG_CUT, (unsigned) bogus_prio,
-              (intmax_t) bogus_tmout->tv_sec, (intmax_t) bogus_tmout->tv_nsec,
-              errstr);
+              (long long) bogus_tmout->tv_sec,
+              zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);
 
        /* Partially invalid message, cut at maxstrlen */
        rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_CUT, MSG_SIZE,
@@ -328,10 +305,10 @@ main(void)
        errstr = sprintrc(rc);
        printf("mq_timedsend(%d, ", (int) bogus_fd);
        printstr(MSG_START + MSG_CUT, MSG_MAX_UNCUT);
-       printf("..., %llu, %u, {tv_sec=%jd, tv_nsec=%jd}) = %s\n",
+       printf("..., %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
               (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
-              (intmax_t) bogus_tmout->tv_sec, (intmax_t) bogus_tmout->tv_nsec,
-              errstr);
+              (long long) bogus_tmout->tv_sec,
+              zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);
 
 
        /* mq_timedreceive */
@@ -351,11 +328,11 @@ main(void)
        /* Invalid fd, valid msg pointer */
        rc = syscall(__NR_mq_timedreceive, bogus_fd, msg, bogus_size,
                     bogus_prio_ptr, bogus_tmout);
-       printf("mq_timedreceive(%d, %p, %llu, %p, {tv_sec=%jd, tv_nsec=%jd}) = "
-              "%s\n",
+       printf("mq_timedreceive(%d, %p, %llu, %p, {tv_sec=%lld, tv_nsec=%llu}) "
+              "%s\n",
               (int) bogus_fd, msg, (unsigned long long) bogus_size,
-              bogus_prio_ptr, (intmax_t) bogus_tmout->tv_sec,
-              (intmax_t) bogus_tmout->tv_nsec, sprintrc(rc));
+              bogus_prio_ptr, (long long) bogus_tmout->tv_sec,
+              zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));
 
 
        /* mq_notify */
@@ -381,8 +358,8 @@ main(void)
        /* SIGEV_NONE */
        bogus_sev->sigev_notify = SIGEV_NONE;
        rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
-       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
-              "sigev_signo=%u, sigev_notify=SIGEV_NONE}) = %s\n",
+       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
+              "sigev_signo=%u, sigev_notify=SIGEV_NONE}) = %s\n",
               (int) bogus_fd, bogus_sev->sigev_value.sival_int,
               bogus_sev->sigev_value.sival_ptr,
               bogus_sev->sigev_signo, sprintrc(rc));
@@ -391,8 +368,8 @@ main(void)
        bogus_sev->sigev_notify = SIGEV_SIGNAL;
        bogus_sev->sigev_signo = SIGALRM;
        rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
-       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
-              "sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}) = %s\n",
+       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
+              "sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}) = %s\n",
               (int) bogus_fd, bogus_sev->sigev_value.sival_int,
               bogus_sev->sigev_value.sival_ptr, sprintrc(rc));
 
@@ -403,10 +380,10 @@ main(void)
        bogus_sev->sigev_un.sigev_thread.attribute =
                (unsigned long) 0xcafef00dfacefeedULL;
        rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
-       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
-              "sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD, "
-              "sigev_notify_function=%#lx, sigev_notify_attributes=%#lx}) = "
-              "%s\n",
+       printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
+              ", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD"
+              ", sigev_notify_function=%#lx, sigev_notify_attributes=%#lx})"
+              " = %s\n",
               (int) bogus_fd, bogus_sev->sigev_value.sival_int,
               bogus_sev->sigev_value.sival_ptr,
               bogus_sev->sigev_un.sigev_thread.function,
@@ -430,13 +407,16 @@ main(void)
 
        /* Sending and receiving test */
 
+       if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
+               perror_msg_and_fail("asprintf");
+
 # if DUMPIO_READ || DUMPIO_WRITE
        close(0);
 # endif
        bogus_attrs[1] = 2;
        bogus_attrs[2] = MSG_SIZE;
-       fd = rc = syscall(__NR_mq_open, MQ_NAME,
-                         O_CREAT|O_RDWR|O_NONBLOCK, S_IRWXU, bogus_attrs);
+       fd = rc = syscall(__NR_mq_open, mq_name,
+                         O_CREAT|O_RDWR|O_NONBLOCK, S_IRWXU, bogus_attrs);
        errstr = sprintrc(rc);
        if (rc < 0)
                perror_msg_and_skip("mq_open");
@@ -448,11 +428,11 @@ main(void)
 # endif
        fill_memory_ex(bogus_attrs, sizeof(*bogus_attrs) * NUM_ATTRS,
                       0xbb, 0x70);
-       printf("mq_open(\"" MQ_NAME "\", O_RDWR|O_CREAT|O_NONBLOCK, "
-              "0700, {mq_flags=%#llx, mq_maxmsg=2, mq_msgsize=%u, "
-              "mq_curmsgs=%lld}) = %s\n",
-              (unsigned long long) (kernel_ulong_t) bogus_attrs[0], MSG_SIZE,
-              (long long) bogus_attrs[3], errstr);
+       printf("mq_open(\"%s\", O_RDWR|O_CREAT|O_NONBLOCK, 0700"
+              ", {mq_flags=%#llx, mq_maxmsg=2, mq_msgsize=%u"
+              "mq_curmsgs=%lld}) = %s\n",
+              mq_name, (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
+              MSG_SIZE, (long long) bogus_attrs[3], errstr);
 
        rc = syscall(__NR_mq_getsetattr, fd, NULL, bogus_attrs);
        if (rc < 0)