Back to index

glibc  2.9
tst-gettext6.c
Go to the documentation of this file.
00001 /* Test that gettext() in multithreaded applications works correctly.
00002    Copyright (C) 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2008.
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 <libintl.h>
00022 #include <locale.h>
00023 #include <pthread.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <sys/wait.h>
00027 #include <unistd.h>
00028 
00029 pthread_barrier_t b;
00030 
00031 static void *
00032 tf (void *arg)
00033 {
00034   pthread_barrier_wait (&b);
00035   return gettext ("Operation not permitted");
00036 }
00037 
00038 int
00039 test (void)
00040 {
00041   pthread_t th[4];
00042   unsetenv ("LANGUAGE");
00043   unsetenv ("OUTPUT_CHARSET");
00044   textdomain ("tstgettext6");
00045   bindtextdomain ("tstgettext6", OBJPFX "domaindir");
00046   setlocale (LC_ALL, "ja_JP.UTF-8");
00047   pthread_barrier_init (&b, NULL, 4);
00048   for (int i = 0; i < 4; i++)
00049     if (pthread_create (&th[i], NULL, tf, NULL))
00050       {
00051        puts ("pthread_create failed");
00052        return 1;
00053       }
00054   for (int i = 0; i < 4; i++)
00055     pthread_join (th[i], NULL);
00056   return 0;
00057 }
00058 
00059 int
00060 main (void)
00061 {
00062   for (int i = 0; i < 300; i++)
00063     {
00064       pid_t p = fork ();
00065       if (p == -1)
00066        {
00067          printf ("fork failed: %m\n");
00068          return 1;
00069        }
00070       if (p == 0)
00071        _exit (test ());
00072       int status;
00073       wait (&status);
00074       if (WIFEXITED (status) && WEXITSTATUS (status) != 0)
00075        {
00076          printf ("child exited with %d\n", WEXITSTATUS (status));
00077          return 1;
00078        }
00079       else if (WIFSIGNALED (status))
00080        {
00081          printf ("child killed by signal %d\n", WTERMSIG (status));
00082          return 1;
00083        }
00084     }
00085   return 0;
00086 }