merge with 0.30.213
[util-vserver.git] / lib_internal / testsuite / sigbus.c
1 // $Id: sigbus.c 2484 2007-02-04 17:17:02Z ensc $    --*- c -*--
2
3 // Copyright (C) 2005 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 //  
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; version 2 of the License.
8 //  
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //  
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29
30 #define ENSC_WRAPPERS_UNISTD    1
31 #define ENSC_WRAPPERS_SOCKET    1
32 #include <wrappers.h>
33
34 int wrapper_exit_code = 1;
35
36 #define TEST_BLOCKSIZE          (sysconf(_SC_PAGESIZE)*2 + 0x10000)
37
38 static bool                     is_gremlin = false;
39 static int                      sync_p[2];
40
41 static void
42 testit()
43 {
44   if (!is_gremlin) return;
45
46   char  c;
47
48   Esend(sync_p[1], ".", 1, 0);
49   Erecv(sync_p[1], &c,  1, 0);
50 }
51
52 #define TESTSUITE_COPY_CODE     testit()
53
54 #include "../unify.h"
55 #include "../unify-copy.c"
56 #include "../unify-settime.c"
57
58 static bool
59 checkTrunc(char const *src,
60            char const *dst,
61            struct stat const *st,
62            size_t pos)
63 {
64   pid_t         pid = Efork();
65   
66   if (pid==0) {
67     char                c;
68     
69     Erecv(sync_p[0], &c, 1, 0);
70     Etruncate(src, pos);
71     Esend(sync_p[0], &c, 1, 0);
72     exit(0);
73   }
74
75   unlink(dst);
76   return !copyReg(src, st, dst);
77 }
78
79 int main()
80 {
81   char          f_name0[] = "/tmp/sigbus.XXXXXX";
82   char          f_name1[] = "/tmp/sigbus.XXXXXX";
83   int           fd_src    = mkstemp(f_name0);
84   int           fd_dst    = mkstemp(f_name1);
85   char          buf[TEST_BLOCKSIZE];
86   struct stat   st;
87   bool          res;
88
89   memset(buf, 0, TEST_BLOCKSIZE);
90   write(fd_src, buf, TEST_BLOCKSIZE);
91   close(fd_src);
92   close(fd_dst);
93
94   unlink(f_name1);
95   stat(f_name0, &st);
96   if (!copyReg(f_name0, &st, f_name1))
97     return EXIT_FAILURE;
98
99
100   is_gremlin = true;
101
102   Esocketpair(AF_LOCAL, SOCK_STREAM, 0, sync_p);
103   signal(SIGCHLD, SIG_IGN);
104
105   res = (checkTrunc(f_name0, f_name1, &st, TEST_BLOCKSIZE/2) &&
106          checkTrunc(f_name0, f_name1, &st, 0x2345));
107
108   unlink(f_name0);
109   unlink(f_name1);
110   return res ? EXIT_SUCCESS : EXIT_FAILURE;
111 }