Back to index

glibc  2.9
tst-mqueue3.c
Go to the documentation of this file.
00001 /* Test SIGEV_THREAD handling for POSIX message queues.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <errno.h>
00022 #include <mqueue.h>
00023 #include <signal.h>
00024 #include <stddef.h>
00025 #include <stdint.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <sys/mman.h>
00030 #include <sys/wait.h>
00031 #include <unistd.h>
00032 
00033 #if _POSIX_THREADS
00034 # include <pthread.h>
00035 
00036 static pid_t pid;
00037 static mqd_t m;
00038 static const char message[] = "hello";
00039 
00040 # define MAXMSG 10
00041 # define MSGSIZE 10
00042 # define UNIQUE 42
00043 
00044 
00045 static void
00046 fct (union sigval s)
00047 {
00048   /* Put the mq in non-blocking mode.  */
00049   struct mq_attr attr;
00050   if (mq_getattr (m, &attr) != 0)
00051     {
00052       printf ("%s: mq_getattr failed: %m\n", __FUNCTION__);
00053       exit (1);
00054     }
00055   attr.mq_flags |= O_NONBLOCK;
00056   if (mq_setattr (m, &attr, NULL) != 0)
00057     {
00058       printf ("%s: mq_setattr failed: %m\n", __FUNCTION__);
00059       exit (1);
00060     }
00061 
00062   /* Check the values.  */
00063   if (attr.mq_maxmsg != MAXMSG)
00064     {
00065       printf ("%s: mq_maxmsg wrong: is %ld, expecte %d\n",
00066              __FUNCTION__, attr.mq_maxmsg, MAXMSG);
00067       exit (1);
00068     }
00069   if (attr.mq_msgsize != MAXMSG)
00070     {
00071       printf ("%s: mq_msgsize wrong: is %ld, expecte %d\n",
00072              __FUNCTION__, attr.mq_msgsize, MSGSIZE);
00073       exit (1);
00074     }
00075 
00076   /* Read the message.  */
00077   char buf[attr.mq_msgsize];
00078   ssize_t n = TEMP_FAILURE_RETRY (mq_receive (m, buf, attr.mq_msgsize, NULL));
00079   if (n != sizeof (message))
00080     {
00081       printf ("%s: length of message wrong: is %zd, expected %zu\n",
00082              __FUNCTION__, n, sizeof (message));
00083       exit (1);
00084     }
00085   if (memcmp (buf, message, sizeof (message)) != 0)
00086     {
00087       printf ("%s: message wrong: is \"%s\", expected \"%s\"\n",
00088              __FUNCTION__, buf, message);
00089       exit (1);
00090     }
00091 
00092   exit (UNIQUE);
00093 }
00094 
00095 
00096 int
00097 do_test (void)
00098 {
00099   char tmpfname[] = "/tmp/tst-mqueue3-barrier.XXXXXX";
00100   int fd = mkstemp (tmpfname);
00101   if (fd == -1)
00102     {
00103       printf ("cannot open temporary file: %m\n");
00104       return 1;
00105     }
00106 
00107   /* Make sure it is always removed.  */
00108   unlink (tmpfname);
00109 
00110   /* Create one page of data.  */
00111   size_t ps = sysconf (_SC_PAGESIZE);
00112   char data[ps];
00113   memset (data, '\0', ps);
00114 
00115   /* Write the data to the file.  */
00116   if (write (fd, data, ps) != (ssize_t) ps)
00117     {
00118       puts ("short write");
00119       return 1;
00120     }
00121 
00122   void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
00123   if (mem == MAP_FAILED)
00124     {
00125       printf ("mmap failed: %m\n");
00126       return 1;
00127     }
00128 
00129   pthread_barrier_t *b;
00130   b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
00131                              & ~(__alignof (pthread_barrier_t) - 1));
00132 
00133   pthread_barrierattr_t a;
00134   if (pthread_barrierattr_init (&a) != 0)
00135     {
00136       puts ("barrierattr_init failed");
00137       return 1;
00138     }
00139 
00140   if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
00141     {
00142       puts ("barrierattr_setpshared failed, could not test");
00143       return 0;
00144     }
00145 
00146   if (pthread_barrier_init (b, &a, 2) != 0)
00147     {
00148       puts ("barrier_init failed");
00149       return 1;
00150     }
00151 
00152   if (pthread_barrierattr_destroy (&a) != 0)
00153     {
00154       puts ("barrierattr_destroy failed");
00155       return 1;
00156     }
00157 
00158   /* Name for the message queue.  */
00159   char mqname[sizeof ("/tst-mqueue3-") + 3 * sizeof (pid_t)];
00160   snprintf (mqname, sizeof (mqname) - 1, "/tst-mqueue3-%ld",
00161            (long int) getpid ());
00162 
00163   /* Create the message queue.  */
00164   struct mq_attr attr = { .mq_maxmsg = MAXMSG, .mq_msgsize = MSGSIZE };
00165   m = mq_open (mqname, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
00166   if (m == -1)
00167     {
00168       if (errno == ENOSYS)
00169        {
00170          puts ("not implemented");
00171          return 0;
00172        }
00173 
00174       puts ("mq_open failed");
00175       return 1;
00176     }
00177 
00178   /* Unlink the message queue right away.  */
00179   if (mq_unlink (mqname) != 0)
00180     {
00181       puts ("mq_unlink failed");
00182       return 1;
00183     }
00184 
00185   pid = fork ();
00186   if (pid == -1)
00187     {
00188       puts ("fork failed");
00189       return 1;
00190     }
00191   if (pid == 0)
00192     {
00193       /* Request notification via thread.  */
00194       struct sigevent ev;
00195       ev.sigev_notify = SIGEV_THREAD;
00196       ev.sigev_notify_function = fct;
00197       ev.sigev_value.sival_ptr = NULL;
00198       ev.sigev_notify_attributes = NULL;
00199 
00200       /* Tell the kernel.  */
00201       if (mq_notify (m,&ev) != 0)
00202        {
00203          puts ("mq_notify failed");
00204          exit (1);
00205        }
00206 
00207       /* Tell the parent we are ready.  */
00208       (void) pthread_barrier_wait (b);
00209 
00210       /* Make sure the process goes away eventually.  */
00211       alarm (10);
00212 
00213       /* Do nothing forever.  */
00214       while (1)
00215        pause ();
00216     }
00217 
00218   /* Wait for the child process to register to notification method.  */
00219   (void) pthread_barrier_wait (b);
00220 
00221   /* Send the message.  */
00222   if (mq_send (m, message, sizeof (message), 1) != 0)
00223     {
00224       kill (pid, SIGKILL);
00225       puts ("mq_send failed");
00226       return 1;
00227     }
00228 
00229   int r;
00230   if (TEMP_FAILURE_RETRY (waitpid (pid, &r, 0)) != pid)
00231     {
00232       kill (pid, SIGKILL);
00233       puts ("waitpid failed");
00234       return 1;
00235     }
00236 
00237   return WIFEXITED (r) && WEXITSTATUS (r) == UNIQUE ? 0 : 1;
00238 }
00239 # define TEST_FUNCTION do_test ()
00240 #else
00241 # define TEST_FUNCTION 0
00242 #endif
00243 
00244 #include "../test-skeleton.c"