Back to index

glibc  2.9
tst-mqueue4.c
Go to the documentation of this file.
00001 /* Test message queue passing.
00002    Copyright (C) 2004 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 <limits.h>
00025 #include <signal.h>
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <sys/time.h>
00030 #include <sys/wait.h>
00031 #include <time.h>
00032 #include <unistd.h>
00033 #include "tst-mqueue.h"
00034 
00035 #define TIMEOUT 4
00036 #define TEST_FUNCTION do_test ()
00037 static int
00038 do_test (void)
00039 {
00040   int result = 0;
00041 
00042   char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX];
00043   char *p;
00044   p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ());
00045   struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 };
00046   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
00047 
00048   if (q == (mqd_t) -1)
00049     {
00050       printf ("mq_open failed with: %m\n");
00051       return result;
00052     }
00053   else
00054     add_temp_mq (name);
00055 
00056   *p = '.';
00057   memset (p + 1, 'x', NAME_MAX + 1 - (p - name));
00058   name[NAME_MAX + 1] = '\0';
00059 
00060   mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
00061   if (q2 == (mqd_t) -1)
00062     {
00063       printf ("mq_open with NAME_MAX long name compoment failed with: %m\n");
00064       result = 1;
00065     }
00066 
00067   if (mq_unlink (name) != 0)
00068     {
00069       printf ("mq_unlink failed: %m\n");
00070       result = 1;
00071     }
00072 
00073   if (mq_close (q2) != 0)
00074     {
00075       printf ("mq_close failed: %m\n");
00076       result = 1;
00077     }
00078 
00079   name[NAME_MAX + 1] = 'x';
00080   name[NAME_MAX + 2] = '\0';
00081   q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
00082   if (q2 != (mqd_t) -1)
00083     {
00084       puts ("mq_open with too long name component unexpectedly succeeded");
00085       mq_unlink (name);
00086       mq_close (q2);
00087       result = 1;
00088     }
00089   else if (errno != ENAMETOOLONG)
00090     {
00091       printf ("mq_open with too long name component did not fail with "
00092              "ENAMETOOLONG: %m\n");
00093       result = 1;
00094     }
00095 
00096   if (mq_unlink (name) == 0)
00097     {
00098       puts ("mq_unlink with too long name component unexpectedly succeeded");
00099       result = 1;
00100     }
00101   else if (errno != ENAMETOOLONG)
00102     {
00103       printf ("mq_unlink with too long name component did not fail with "
00104              "ENAMETOOLONG: %m\n");
00105       result = 1;
00106     }
00107 
00108   *p = '\0';
00109   attr.mq_maxmsg = 1;
00110   attr.mq_msgsize = 3;
00111   q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr);
00112   if (q2 == (mqd_t) -1)
00113     {
00114       printf ("mq_open without O_EXCL failed with %m\n");
00115       result = 1;
00116     }
00117 
00118   char buf[3];
00119   strcpy (buf, "jk");
00120   if (mq_send (q, buf, 2, 4) != 0)
00121     {
00122       printf ("mq_send failed: %m\n");
00123       result = 1;
00124     }
00125 
00126   if (mq_send (q, buf + 1, 1, 5) != 0)
00127     {
00128       printf ("mq_send failed: %m\n");
00129       result = 1;
00130     }
00131 
00132   if (mq_getattr (q2, &attr) != 0)
00133     {
00134       printf ("mq_getattr failed: %m\n");
00135       result = 1;
00136     }
00137 
00138   if ((attr.mq_flags & O_NONBLOCK)
00139       || attr.mq_maxmsg != 2
00140       || attr.mq_msgsize != 2
00141       || attr.mq_curmsgs != 2)
00142     {
00143       printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
00144              ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
00145              attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
00146       result = 1;
00147     }
00148 
00149   struct timespec ts;
00150   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
00151     ++ts.tv_sec;
00152   else
00153     {
00154       ts.tv_sec = time (NULL) + 1;
00155       ts.tv_nsec = 0;
00156     }
00157 
00158   if (mq_timedsend (q2, buf, 1, 1, &ts) == 0)
00159     {
00160       puts ("mq_timedsend unexpectedly succeeded");
00161       result = 1;
00162     }
00163   else if (errno != ETIMEDOUT)
00164     {
00165       printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n");
00166       result = 1;
00167     }
00168 
00169   if (mq_close (q2) != 0)
00170     {
00171       printf ("mq_close failed: %m\n");
00172       result = 1;
00173     }
00174 
00175   q2 = mq_open (name, O_RDONLY, 0600);
00176   if (q2 == (mqd_t) -1)
00177     {
00178       printf ("mq_open without O_CREAT failed with %m\n");
00179       result = 1;
00180     }
00181 
00182   mqd_t q3 = mq_open (name, O_RDONLY, 0600);
00183   if (q3 == (mqd_t) -1)
00184     {
00185       printf ("mq_open without O_CREAT failed with %m\n");
00186       result = 1;
00187     }
00188 
00189   memset (buf, ' ', sizeof (buf));
00190 
00191   unsigned int prio;
00192   ssize_t rets = mq_receive (q2, buf, 2, &prio);
00193   if (rets != 1)
00194     {
00195       if (rets == -1)
00196        printf ("mq_receive failed with: %m\n");
00197       else
00198        printf ("mq_receive returned %zd != 1\n", rets);
00199       result = 1;
00200     }
00201   else if (prio != 5 || memcmp (buf, "k  ", 3) != 0)
00202     {
00203       printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k  \")\n",
00204              prio, buf[0], buf[1], buf[2]);
00205       result = 1;
00206     }
00207 
00208   if (mq_getattr (q3, &attr) != 0)
00209     {
00210       printf ("mq_getattr failed: %m\n");
00211       result = 1;
00212     }
00213 
00214   if ((attr.mq_flags & O_NONBLOCK)
00215       || attr.mq_maxmsg != 2
00216       || attr.mq_msgsize != 2
00217       || attr.mq_curmsgs != 1)
00218     {
00219       printf ("mq_getattr returned unexpected { .mq_flags = %ld,\n"
00220              ".mq_maxmsg = %ld, .mq_msgsize = %ld, .mq_curmsgs = %ld }\n",
00221              attr.mq_flags, attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);
00222       result = 1;
00223     }
00224 
00225   rets = mq_receive (q3, buf, 2, NULL);
00226   if (rets != 2)
00227     {
00228       if (rets == -1)
00229        printf ("mq_receive failed with: %m\n");
00230       else
00231        printf ("mq_receive returned %zd != 2\n", rets);
00232       result = 1;
00233     }
00234   else if (memcmp (buf, "jk ", 3) != 0)
00235     {
00236       printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n",
00237              buf[0], buf[1], buf[2]);
00238       result = 1;
00239     }
00240 
00241   if (clock_gettime (CLOCK_REALTIME, &ts) == 0)
00242     ++ts.tv_sec;
00243   else
00244     {
00245       ts.tv_sec = time (NULL) + 1;
00246       ts.tv_nsec = 0;
00247     }
00248 
00249   if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1)
00250     {
00251       puts ("mq_timedreceive on empty queue unexpectedly succeeded");
00252       result = 1;
00253     }
00254   else if (errno != ETIMEDOUT)
00255     {
00256       printf ("mq_timedreceive on empty queue did not fail with "
00257              "ETIMEDOUT: %m\n");
00258       result = 1;
00259     }
00260 
00261   if (mq_unlink (name) != 0)
00262     {
00263       printf ("mq_unlink failed: %m\n");
00264       result = 1;
00265     }
00266 
00267   if (mq_close (q) != 0)
00268     {
00269       printf ("mq_close failed: %m\n");
00270       result = 1;
00271     }
00272 
00273   if (mq_close (q2) != 0)
00274     {
00275       printf ("mq_close failed: %m\n");
00276       result = 1;
00277     }
00278 
00279   if (mq_close (q3) != 0)
00280     {
00281       printf ("mq_close failed: %m\n");
00282       result = 1;
00283     }
00284 
00285   return result;
00286 }
00287 
00288 #include "../test-skeleton.c"