fea5ddc173cb9c18ed18163ecb9dc307771d6078
[util-vserver.git] / lib_internal / testsuite / sigbus.c
1 // $Id: sigbus.c,v 1.3 2005/03/24 12:42:56 ensc Exp $    --*- 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  0x10000
37 static bool                     is_gremlin = false;
38 static int                      sync_p[2];
39
40 static void
41 testit()
42 {
43   if (!is_gremlin) return;
44
45   char  c;
46
47   Esend(sync_p[1], ".", 1, 0);
48   Erecv(sync_p[1], &c,  1, 0);
49 }
50
51 #define TESTSUITE_COPY_CODE     testit()
52
53 #include "../unify.h"
54 #include "../unify-copy.c"
55 #include "../unify-settime.c"
56
57 static bool
58 checkTrunc(char const *src,
59            char const *dst,
60            struct stat const *st,
61            size_t pos)
62 {
63   pid_t         pid = Efork();
64   
65   if (pid==0) {
66     char                c;
67     
68     Erecv(sync_p[0], &c, 1, 0);
69     Etruncate(src, pos);
70     Esend(sync_p[0], &c, 1, 0);
71     exit(0);
72   }
73
74   unlink(dst);
75   return !copyReg(src, st, dst);
76 }
77
78 int main()
79 {
80   char          f_name0[] = "/tmp/sigbus.XXXXXX";
81   char          f_name1[] = "/tmp/sigbus.XXXXXX";
82   int           fd_src    = mkstemp(f_name0);
83   int           fd_dst    = mkstemp(f_name1);
84   char          buf[TEST_BLOCKSIZE] = { [0] = '\0' };
85   struct stat   st;
86   bool          res;
87   
88   fd_src = 
89   
90   write(fd_src, buf, sizeof(buf));
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 }