Back to index

glibc  2.9
tst-mqueue9.c
Go to the documentation of this file.
00001 /* Copyright (C) 2004 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <mqueue.h>
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include "tst-mqueue.h"
00027 
00028 #define TEST_FUNCTION do_test ()
00029 static int
00030 do_test (void)
00031 {
00032   if (geteuid () != 0)
00033     {
00034       puts ("this test requires root");
00035       return 0;
00036     }
00037 
00038   char name[sizeof "/tst-mqueue9-" + sizeof (pid_t) * 3];
00039   snprintf (name, sizeof (name), "/tst-mqueue9-%u", getpid ());
00040 
00041   struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 };
00042   mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr);
00043 
00044   if (q == (mqd_t) -1)
00045     {
00046       printf ("mq_open failed with: %m\n");
00047       return 0;
00048     }
00049   else
00050     add_temp_mq (name);
00051 
00052   if (seteuid (1) != 0)
00053     {
00054       printf ("failed to seteuid (1): %m\n");
00055       mq_unlink (name);
00056       return 0;
00057     }
00058 
00059   int result = 0;
00060   if (mq_unlink (name) == 0)
00061     {
00062       puts ("mq_unlink unexpectedly succeeded");
00063       result = 1;
00064     }
00065   else if (errno != EACCES)
00066     {
00067       printf ("mq_unlink did not fail with EACCES: %m\n");
00068       result = 1;;
00069     }
00070 
00071   if (seteuid (0) != 0)
00072     {
00073       printf ("failed to seteuid (0): %m\n");
00074       result = 1;
00075     }
00076 
00077   if (mq_unlink (name) != 0)
00078     {
00079       printf ("mq_unlink failed with: %m\n");
00080       result = 1;
00081     }
00082 
00083   if (mq_close (q) != 0)
00084     {
00085       printf ("mq_close failed with: %m\n");
00086       result = 1;
00087     }
00088 
00089   return result;
00090 }
00091 
00092 #include "../test-skeleton.c"