Mercurial > hg > xemacs-beta
comparison lib-src/insert-data-in-exec.c @ 5727:86d33ddc7fd6
Avoid EOVERFLOW from stat() calls due to overflowing inode numbers.
The btrfs filesystem now uses 64-bit inode numbers even on 32-bit systems.
This can lead to spurious stat() failures, where EOVERFLOW is returned because
the inode number does not fit into the 32-bit stat structure, even when the
caller is not interested in the inode number. This patch builds with
_FILE_OFFSET_BITS == 64 when possible, and deals with integers that may be
too large to fit into a Lisp fixnum. For more information, see xemacs-patches
message <CAHCOHQk_mPM6WgFChBsGafqhuazep6VED7swFoqfFXOV1r8org@mail.gmail.com>.
author | Jerry James <james@xemacs.org> |
---|---|
date | Wed, 06 Mar 2013 08:32:17 -0700 |
parents | dfc9fe46c294 |
children |
comparison
equal
deleted
inserted
replaced
5726:179f4a9201b5 | 5727:86d33ddc7fd6 |
---|---|
30 http://lkml.org/lkml/2003/7/11/141 for more information. */ | 30 http://lkml.org/lkml/2003/7/11/141 for more information. */ |
31 | 31 |
32 #include <stdio.h> | 32 #include <stdio.h> |
33 #include <stdlib.h> | 33 #include <stdlib.h> |
34 #include <string.h> | 34 #include <string.h> |
35 #include <config.h> | |
35 | 36 |
36 static const unsigned char key[] = { | 37 static const unsigned char key[] = { |
37 255, | 38 255, |
38 6, | 39 6, |
39 1, | 40 1, |
53 | 54 |
54 int main(int argc, char **argv) | 55 int main(int argc, char **argv) |
55 { | 56 { |
56 FILE *te, *xe, *dump; | 57 FILE *te, *xe, *dump; |
57 unsigned char *xed, *p; | 58 unsigned char *xed, *p; |
58 long size, size_dump, size1, i; | 59 size_t size, size_dump, size1, i; |
59 long max_size, offset; | 60 size_t max_size, offset; |
61 OFF_T off; | |
60 | 62 |
61 char msg[65536]; | 63 char msg[65536]; |
62 | 64 |
63 if(argc != 6 && (argc != 3 || strcmp(argv[1], "-s"))) { | 65 if(argc != 6 && (argc != 3 || strcmp(argv[1], "-s"))) { |
64 fprintf(stderr, "Usage:\n%s temacs xemacs.dmp xemacs size offset\n%s -s xemacs.dmp\n", argv[0], argv[0]); | 66 fprintf(stderr, "Usage:\n%s temacs xemacs.dmp xemacs size offset\n%s -s xemacs.dmp\n", argv[0], argv[0]); |
71 if(!dump) { | 73 if(!dump) { |
72 perror(msg); | 74 perror(msg); |
73 exit(1); | 75 exit(1); |
74 } | 76 } |
75 | 77 |
76 if(fseek(dump, 0, SEEK_END)) { | 78 if(FSEEK(dump, 0, SEEK_END)) { |
77 perror("fseek end dump"); | 79 perror("fseek end dump"); |
78 exit(1); | 80 exit(1); |
79 } | 81 } |
80 | 82 |
81 size = ftell(dump); | 83 off = FTELL(dump); |
82 if(size == -1) { | 84 if(off == -1) { |
83 perror("ftell dump"); | 85 perror("ftell dump"); |
84 exit(1); | 86 exit(1); |
85 } | 87 } |
86 | 88 size = (size_t)off; |
87 printf("%ld\n", size); | 89 |
90 printf("%zu\n", size); | |
88 exit(0); | 91 exit(0); |
89 } | 92 } |
90 | 93 |
91 | 94 |
92 max_size = strtol(argv[4], 0, 10); | 95 max_size = strtoul(argv[4], 0, 10); |
93 offset = strtol(argv[5], 0, 10); | 96 offset = strtoul(argv[5], 0, 10); |
94 | 97 |
95 sprintf(msg, "Opening %s failed", argv[1]); | 98 sprintf(msg, "Opening %s failed", argv[1]); |
96 te = fopen(argv[1], "rb"); | 99 te = fopen(argv[1], "rb"); |
97 if(!te) { | 100 if(!te) { |
98 perror(msg); | 101 perror(msg); |
99 exit(1); | 102 exit(1); |
100 } | 103 } |
101 | 104 |
102 if(fseek(te, 0, SEEK_END)) { | 105 if(FSEEK(te, 0, SEEK_END)) { |
103 perror("fseek end"); | 106 perror("fseek end"); |
104 exit(1); | 107 exit(1); |
105 } | 108 } |
106 | 109 |
107 size = ftell(te); | 110 off = FTELL(te); |
108 if(size == -1) { | 111 if(off == -1) { |
109 perror("ftell"); | 112 perror("ftell"); |
110 exit(1); | 113 exit(1); |
111 } | 114 } |
112 | 115 size = (size_t)off; |
113 if(fseek(te, 0, SEEK_SET)) { | 116 |
117 if(FSEEK(te, 0, SEEK_SET)) { | |
114 perror("fseek beginning"); | 118 perror("fseek beginning"); |
115 exit(1); | 119 exit(1); |
116 } | 120 } |
117 | 121 |
118 xed = malloc(size); | 122 xed = malloc(size); |
125 if(size1 != size) { | 129 if(size1 != size) { |
126 if(ferror(te)) { | 130 if(ferror(te)) { |
127 perror("fread temacs"); | 131 perror("fread temacs"); |
128 exit(1); | 132 exit(1); |
129 } | 133 } |
130 fprintf(stderr, "Fread returned %ld, expected %ld ?\n", size1, size); | 134 fprintf(stderr, "Fread returned %zu, expected %zu ?\n", size1, size); |
131 exit(1); | 135 exit(1); |
132 } | 136 } |
133 | 137 |
134 if(fclose(te)) { | 138 if(fclose(te)) { |
135 perror("fclose temacs"); | 139 perror("fclose temacs"); |
136 exit(1); | 140 exit(1); |
137 } | 141 } |
138 | 142 |
139 p = xed; | 143 p = xed; |
140 for(i=0; i<size-(long)sizeof(key); i++) { | 144 for(i=0; i<size-sizeof(key); i++) { |
141 if(!memcmp(p, key, sizeof(key))) | 145 if(!memcmp(p, key, sizeof(key))) |
142 goto found; | 146 goto found; |
143 p++; | 147 p++; |
144 } | 148 } |
145 | 149 |
146 fprintf(stderr, "dumped_data key not found in executable.\n"); | 150 fprintf(stderr, "dumped_data key not found in executable.\n"); |
147 exit(1); | 151 exit(1); |
148 | 152 |
149 found: | 153 found: |
150 fprintf(stderr, "dumped_data found at offset 0x%lx, patching.\n", i); | 154 fprintf(stderr, "dumped_data found at offset 0x%zx, patching.\n", i); |
151 | 155 |
152 sprintf(msg, "Opening %s failed", argv[2]); | 156 sprintf(msg, "Opening %s failed", argv[2]); |
153 dump = fopen(argv[2], "rb"); | 157 dump = fopen(argv[2], "rb"); |
154 if(!dump) { | 158 if(!dump) { |
155 perror(msg); | 159 perror(msg); |
156 exit(1); | 160 exit(1); |
157 } | 161 } |
158 | 162 |
159 if(fseek(dump, 0, SEEK_END)) { | 163 if(FSEEK(dump, 0, SEEK_END)) { |
160 perror("fseek end dump"); | 164 perror("fseek end dump"); |
161 exit(1); | 165 exit(1); |
162 } | 166 } |
163 | 167 |
164 size_dump = ftell(dump); | 168 off = FTELL(dump); |
165 if(size_dump == -1) { | 169 if(off == -1) { |
166 perror("ftell dump"); | 170 perror("ftell dump"); |
167 exit(1); | 171 exit(1); |
168 } | 172 } |
173 size_dump = (size_t)off; | |
169 | 174 |
170 if(size_dump > max_size) { | 175 if(size_dump > max_size) { |
171 fprintf(stderr, "Dump file too big for available space (max=%ld, dump=%ld)\n", max_size, size_dump); | 176 fprintf(stderr, "Dump file too big for available space (max=%zu, dump=%zu)\n", max_size, size_dump); |
172 exit(2); | 177 exit(2); |
173 } | 178 } |
174 | 179 |
175 if(fseek(dump, 0, SEEK_SET)) { | 180 if(FSEEK(dump, 0, SEEK_SET)) { |
176 perror("fseek beginning dump"); | 181 perror("fseek beginning dump"); |
177 exit(1); | 182 exit(1); |
178 } | 183 } |
179 | 184 |
180 size1 = fread(xed+i+offset, 1, size_dump, dump); | 185 size1 = fread(xed+i+offset, 1, size_dump, dump); |
181 if(size1 != size_dump) { | 186 if(size1 != size_dump) { |
182 if(ferror(dump)) { | 187 if(ferror(dump)) { |
183 perror("fread dump"); | 188 perror("fread dump"); |
184 exit(1); | 189 exit(1); |
185 } | 190 } |
186 fprintf(stderr, "Fread dump returned %ld, expected %ld ?\n", size1, size_dump); | 191 fprintf(stderr, "Fread dump returned %zu, expected %zu ?\n", size1, size_dump); |
187 exit(1); | 192 exit(1); |
188 } | 193 } |
189 | 194 |
190 if(fclose(dump)) { | 195 if(fclose(dump)) { |
191 perror("fclose dump"); | 196 perror("fclose dump"); |
197 xed[i ] = size_dump; | 202 xed[i ] = size_dump; |
198 xed[i+1] = size_dump >> 8; | 203 xed[i+1] = size_dump >> 8; |
199 xed[i+2] = size_dump >> 16; | 204 xed[i+2] = size_dump >> 16; |
200 xed[i+3] = size_dump >> 24; | 205 xed[i+3] = size_dump >> 24; |
201 | 206 |
202 fprintf(stderr, "dumped_data found at offset 0x%lx, patching.\n", i); | 207 fprintf(stderr, "dumped_data found at offset 0x%zx, patching.\n", i); |
203 | 208 |
204 sprintf(msg, "Opening %s failed", argv[3]); | 209 sprintf(msg, "Opening %s failed", argv[3]); |
205 xe = fopen(argv[3], "wb"); | 210 xe = fopen(argv[3], "wb"); |
206 if(!xe) { | 211 if(!xe) { |
207 perror(msg); | 212 perror(msg); |
212 if(size1 != size) { | 217 if(size1 != size) { |
213 if(ferror(xe)) { | 218 if(ferror(xe)) { |
214 perror("fwrite xemacs"); | 219 perror("fwrite xemacs"); |
215 exit(1); | 220 exit(1); |
216 } | 221 } |
217 fprintf(stderr, "Fwrite xemacs returned %ld, expected %ld ?\n", size1, size); | 222 fprintf(stderr, "Fwrite xemacs returned %zu, expected %zu ?\n", size1, size); |
218 exit(1); | 223 exit(1); |
219 } | 224 } |
220 | 225 |
221 if(fclose(xe)) { | 226 if(fclose(xe)) { |
222 perror("fclose xemacs"); | 227 perror("fclose xemacs"); |