Back to index

glibc  2.9
tst-mqueue1.c
Go to the documentation of this file.
00001 /* Test message queue passing.
00002    Copyright (C) 2004, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@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 <fcntl.h>
00023 #include <mqueue.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <sys/wait.h>
00028 #include <time.h>
00029 #include <unistd.h>
00030 #include "tst-mqueue.h"
00031 
00032 static int
00033 intcmp (const void *a, const void *b)
00034 {
00035   if (*(unsigned char *)a < *(unsigned char *)b)
00036     return 1;
00037   if (*(unsigned char *)a > *(unsigned char *)b)
00038     return -1;
00039   return 0;
00040 }
00041 
00042 static int
00043 check_attrs (struct mq_attr *attr, int nonblock, long cnt)
00044 {
00045   int result = 0;
00046 
00047   if (attr->mq_maxmsg != 10 || attr->mq_msgsize != 1)
00048     {
00049       printf ("attributes don't match those passed to mq_open\n"
00050              "mq_maxmsg %ld, mq_msgsize %ld\n",
00051              attr->mq_maxmsg, attr->mq_msgsize);
00052       result = 1;
00053     }
00054 
00055   if ((attr->mq_flags & O_NONBLOCK) != nonblock)
00056     {
00057       printf ("mq_flags %lx != %x\n", (attr->mq_flags & O_NONBLOCK), nonblock);
00058       result = 1;
00059     }
00060 
00061   if (attr->mq_curmsgs != cnt)
00062     {
00063       printf ("mq_curmsgs %ld != %ld\n", attr->mq_curmsgs, cnt);
00064       result = 1;
00065     }
00066 
00067   return result;
00068 }
00069 
00070 static int
00071 do_one_test (mqd_t q, const char *name, int nonblock)
00072 {
00073   int result = 0;
00074 
00075   unsigned char v []
00076     = { 0x32, 0x62, 0x22, 0x31, 0x11, 0x73, 0x61, 0x21, 0x72, 0x71, 0x81 };
00077 
00078   struct mq_attr attr;
00079   memset (&attr, 0xaa, sizeof (attr));
00080   if (mq_getattr (q, &attr) != 0)
00081     {
00082       printf ("mq_getattr failed: %m\n");
00083       result = 1;
00084     }
00085   else
00086     result |= check_attrs (&attr, nonblock, 0);
00087 
00088   if (mq_receive (q, (char *) &v[0], 1, NULL) != -1)
00089     {
00090       puts ("mq_receive on O_WRONLY mqd_t unexpectedly succeeded");
00091       result = 1;
00092     }
00093   else if (errno != EBADF)
00094     {
00095       printf ("mq_receive on O_WRONLY mqd_t did not fail with EBADF: %m\n");
00096       result = 1;
00097     }
00098 
00099   struct timespec ts;
00100   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
00101     --ts.tv_sec;
00102   else
00103     {
00104       ts.tv_sec = time (NULL) - 1;
00105       ts.tv_nsec = 0;
00106     }
00107 
00108   int ret;
00109   for (int i = 0; i < 10; ++i)
00110     {
00111       if (i & 1)
00112        ret = mq_send (q, (char *) &v[i], 1, v[i] >> 4);
00113       else
00114        ret = mq_timedsend (q, (char *) &v[i], 1, v[i] >> 4, &ts);
00115 
00116       if (ret)
00117        {
00118          printf ("mq_%ssend failed: %m\n", (i & 1) ? "" : "timed");
00119          result = 1;
00120        }
00121     }
00122 
00123   ret = mq_timedsend (q, (char *) &v[10], 1, 8, &ts);
00124   if (ret != -1)
00125     {
00126       puts ("mq_timedsend on full queue did not fail");
00127       result = 1;
00128     }
00129   else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
00130     {
00131       printf ("mq_timedsend on full queue did not fail with %s: %m\n",
00132              nonblock ? "EAGAIN" : "ETIMEDOUT");
00133       result = 1;
00134     }
00135 
00136   if (nonblock)
00137     {
00138       ret = mq_send (q, (char *) &v[10], 1, 8);
00139       if (ret != -1)
00140        {
00141          puts ("mq_send on full non-blocking queue did not fail");
00142          result = 1;
00143        }
00144       else if (errno != EAGAIN)
00145        {
00146          printf ("mq_send on full non-blocking queue did not fail"
00147                 "with EAGAIN: %m\n");
00148          result = 1;
00149        }
00150     }
00151 
00152   memset (&attr, 0xaa, sizeof (attr));
00153   if (mq_getattr (q, &attr) != 0)
00154     {
00155       printf ("mq_getattr failed: %m\n");
00156       result = 1;
00157     }
00158   else
00159     result |= check_attrs (&attr, nonblock, 10);
00160 
00161   pid_t pid = fork ();
00162   if (pid == -1)
00163     {
00164       printf ("fork failed: %m\n");
00165       result = 1;
00166     }
00167   else if (pid == 0)
00168     {
00169       result = 0;
00170 
00171       if (mq_close (q) != 0)
00172        {
00173          printf ("mq_close in child failed: %m\n");
00174          result = 1;
00175        }
00176 
00177       q = mq_open (name, O_RDONLY | nonblock);
00178       if (q == (mqd_t) -1)
00179         {
00180          printf ("mq_open in child failed: %m\n");
00181          exit (1);
00182         }
00183 
00184       memset (&attr, 0xaa, sizeof (attr));
00185       if (mq_getattr (q, &attr) != 0)
00186        {
00187          printf ("mq_getattr failed: %m\n");
00188          result = 1;
00189        }
00190       else
00191        result |= check_attrs (&attr, nonblock, 10);
00192 
00193       unsigned char vr[11] = { };
00194       unsigned int prio;
00195       ssize_t rets;
00196 
00197       if (mq_send (q, (char *) &v[0], 1, 1) != -1)
00198        {
00199          puts ("mq_send on O_RDONLY mqd_t unexpectedly succeeded");
00200          result = 1;
00201        }
00202       else if (errno != EBADF)
00203        {
00204          printf ("mq_send on O_WRONLY mqd_t did not fail with EBADF: %m\n");
00205          result = 1;
00206        }
00207 
00208       for (int i = 0; i < 10; ++i)
00209        {
00210          if (i & 1)
00211            rets = mq_receive (q, (char *) &vr[i], 1, &prio);
00212          else
00213            rets = mq_timedreceive (q, (char *) &vr[i], 1, &prio, &ts);
00214 
00215          if (rets != 1)
00216            {
00217              if (rets == -1)
00218               printf ("mq_%sreceive failed: %m\n", (i & 1) ? "" : "timed");
00219              else
00220               printf ("mq_%sreceive returned %zd != 1\n",
00221                      (i & 1) ? "" : "timed", rets);
00222              result = 1;
00223            }
00224          else if (prio != (unsigned int) vr[i] >> 4)
00225            {
00226              printf ("unexpected priority %x for value %02x\n", prio,
00227                     vr[i]);
00228              result = 1;
00229            }
00230        }
00231 
00232       qsort (v, 10, 1, intcmp);
00233       if (memcmp (v, vr, 10) != 0)
00234        {
00235          puts ("messages not received in expected order");
00236          result = 1;
00237        }
00238 
00239       rets = mq_timedreceive (q, (char *) &vr[10], 1, &prio, &ts);
00240       if (rets != -1)
00241        {
00242          puts ("mq_timedreceive on empty queue did not fail");
00243          result = 1;
00244        }
00245       else if (errno != (nonblock ? EAGAIN : ETIMEDOUT))
00246        {
00247          printf ("mq_timedreceive on empty queue did not fail with %s: %m\n",
00248                 nonblock ? "EAGAIN" : "ETIMEDOUT");
00249          result = 1;
00250        }
00251 
00252       if (nonblock)
00253        {
00254          ret = mq_receive (q, (char *) &vr[10], 1, &prio);
00255          if (ret != -1)
00256            {
00257              puts ("mq_receive on empty non-blocking queue did not fail");
00258              result = 1;
00259            }
00260          else if (errno != EAGAIN)
00261            {
00262              printf ("mq_receive on empty non-blocking queue did not fail"
00263                     "with EAGAIN: %m\n");
00264              result = 1;
00265            }
00266        }
00267 
00268       memset (&attr, 0xaa, sizeof (attr));
00269       if (mq_getattr (q, &attr) != 0)
00270        {
00271          printf ("mq_getattr failed: %m\n");
00272          result = 1;
00273        }
00274       else
00275        result |= check_attrs (&attr, nonblock, 0);
00276 
00277       if (mq_close (q) != 0)
00278        {
00279          printf ("mq_close in child failed: %m\n");
00280          result = 1;
00281        }
00282 
00283       exit (result);
00284     }
00285 
00286   int status;
00287   if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
00288     {
00289       printf ("waitpid failed: %m\n");
00290       kill (pid, SIGKILL);
00291       result = 1;
00292     }
00293   else if (!WIFEXITED (status) || WEXITSTATUS (status))
00294     {
00295       printf ("child failed: %d\n", status);
00296       result = 1;
00297     }
00298 
00299   memset (&attr, 0xaa, sizeof (attr));
00300   if (mq_getattr (q, &attr) != 0)
00301     {
00302       printf ("mq_getattr failed: %m\n");
00303       result = 1;
00304     }
00305   else
00306     result |= check_attrs (&attr, nonblock, 0);
00307 
00308   return result;
00309 }
00310 
00311 #define TEST_FUNCTION do_test ()
00312 static int
00313 do_test (void)
00314 {
00315   int result = 0;
00316 
00317   char name[sizeof "/tst-mqueue1-" + sizeof (pid_t) * 3];
00318   snprintf (name, sizeof (name), "/tst-mqueue1-%u", getpid ());
00319 
00320   struct mq_attr attr = { .mq_maxmsg = 10, .mq_msgsize = 1 };
00321   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_WRONLY, 0600, &attr);
00322 
00323   if (q == (mqd_t) -1)
00324     {
00325       printf ("mq_open failed with: %m\n");
00326       return result;
00327     }
00328   else
00329     add_temp_mq (name);
00330 
00331   result |= do_one_test (q, name, 0);
00332 
00333   mqd_t q2 = mq_open (name, O_WRONLY | O_NONBLOCK);
00334   if (q2 == (mqd_t) -1)
00335     {
00336       printf ("mq_open failed with: %m\n");
00337       q2 = q;
00338       result = 1;
00339     }
00340   else
00341     {
00342       if (mq_close (q) != 0)
00343        {
00344          printf ("mq_close in parent failed: %m\n");
00345          result = 1;
00346        }
00347 
00348       q = q2;
00349       result |= do_one_test (q, name, O_NONBLOCK);
00350 
00351       if (mq_getattr (q, &attr) != 0)
00352        {
00353          printf ("mq_getattr failed: %m\n");
00354          result = 1;
00355        }
00356       else
00357        {
00358          attr.mq_flags ^= O_NONBLOCK;
00359 
00360          struct mq_attr attr2;
00361          memset (&attr2, 0x55, sizeof (attr2));
00362          if (mq_setattr (q, &attr, &attr2) != 0)
00363            {
00364              printf ("mq_setattr failed: %m\n");
00365              result = 1;
00366            }
00367          else if (attr.mq_flags != (attr2.mq_flags ^ O_NONBLOCK)
00368                  || attr.mq_maxmsg != attr2.mq_maxmsg
00369                  || attr.mq_msgsize != attr2.mq_msgsize
00370                  || attr.mq_curmsgs != 0
00371                  || attr2.mq_curmsgs != 0)
00372            {
00373              puts ("mq_setattr returned unexpected values in *omqstat");
00374              result = 1;
00375            }
00376          else
00377            {
00378              result |= do_one_test (q, name, 0);
00379 
00380              if (mq_setattr (q, &attr2, NULL) != 0)
00381               {
00382                 printf ("mq_setattr failed: %m\n");
00383                 result = 1;
00384               }
00385              else
00386               result |= do_one_test (q, name, O_NONBLOCK);
00387            }
00388        }
00389     }
00390 
00391   if (mq_unlink (name) != 0)
00392     {
00393       printf ("mq_unlink failed: %m\n");
00394       result = 1;
00395     }
00396 
00397   if (mq_close (q) != 0)
00398     {
00399       printf ("mq_close in parent failed: %m\n");
00400       result = 1;
00401     }
00402 
00403   if (mq_close (q) != -1)
00404     {
00405       puts ("second mq_close did not fail");
00406       result = 1;
00407     }
00408   else if (errno != EBADF)
00409     {
00410       printf ("second mq_close did not fail with EBADF: %m\n");
00411       result = 1;
00412     }
00413 
00414   return result;
00415 }
00416 
00417 #include "../test-skeleton.c"