2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@login.dkuug.dk> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/kern/imgact_gzip.c,v 1.40.2.1 2001/11/03 01:41:08 ps Exp $
11 * This module handles execution of a.out files which have been run through
12 * "gzip". This saves diskspace, but wastes cpu-cycles and VM.
15 * text-segments should be made R/O after being filled
16 * is the vm-stuff safe ?
17 * should handle the entire header of gzip'ed stuff.
18 * inflate isn't quite reentrant yet...
19 * error-handling is a mess...
21 * tidy up unnecesary includes
24 #include <sys/param.h>
26 #include <sys/imgact.h>
27 #include <sys/imgact_aout.h>
28 #include <sys/kernel.h>
31 #include <sys/resourcevar.h>
32 #include <sys/sysent.h>
33 #include <sys/systm.h>
34 #include <sys/vnode.h>
35 #include <sys/inflate.h>
38 #include <vm/vm_param.h>
41 #include <vm/vm_map.h>
42 #include <vm/vm_kern.h>
43 #include <vm/vm_extern.h>
46 struct image_params *ip;
56 u_long virtual_offset, file_offset, file_end, bss_size;
59 static int exec_gzip_imgact __P((struct image_params *imgp));
60 static int NextByte __P((void *vp));
61 static int do_aout_hdr __P((struct imgact_gzip *));
62 static int Flush __P((void *vp, u_char *, u_long siz));
65 exec_gzip_imgact(imgp)
66 struct image_params *imgp;
68 int error, error2 = 0;
69 const u_char *p = (const u_char *) imgp->image_header;
70 struct imgact_gzip igz;
72 struct vmspace *vmspace;
74 /* If these four are not OK, it isn't a gzip file */
76 return -1; /* 0 Simply magic */
78 return -1; /* 1 Simply magic */
80 return -1; /* 2 Compression method */
82 return -1; /* 9 OS compressed on */
85 * If this one contains anything but a comment or a filename marker,
86 * we don't want to chew on it
89 return ENOEXEC; /* 3 Flags */
91 /* These are of no use to us */
95 bzero(&igz, sizeof igz);
96 bzero(&infl, sizeof infl);
97 infl.gz_private = (void *) &igz;
98 infl.gz_input = NextByte;
99 infl.gz_output = Flush;
104 if (p[3] & 0x08) { /* skip a filename */
106 if (igz.idx >= PAGE_SIZE)
109 if (p[3] & 0x10) { /* skip a comment */
111 if (igz.idx >= PAGE_SIZE)
114 igz.len = imgp->attr->va_size;
116 error = inflate(&infl);
119 * The unzipped file may not even have been long enough to contain
120 * a header giving Flush() a chance to return error. Check for this.
122 if ( !igz.gotheader )
126 vmspace = imgp->proc->p_vmspace;
127 error = vm_map_protect(&vmspace->vm_map,
128 (vm_offset_t) vmspace->vm_taddr,
129 (vm_offset_t) (vmspace->vm_taddr +
130 (vmspace->vm_tsize << PAGE_SHIFT)) ,
131 VM_PROT_READ|VM_PROT_EXECUTE,0);
136 vm_map_remove(kernel_map, (vm_offset_t) igz.inbuf,
137 (vm_offset_t) igz.inbuf + PAGE_SIZE);
139 if (igz.error || error || error2) {
140 printf("Output=%lu ", igz.output);
141 printf("Inflate_error=%d igz.error=%d error2=%d where=%d\n",
142 error, igz.error, error2, igz.where);
154 do_aout_hdr(struct imgact_gzip * gz)
157 struct vmspace *vmspace;
161 * Set file/virtual offset based on a.out variant. We do two cases:
162 * host byte order and network byte order (for NetBSD compatibility)
164 switch ((int) (gz->a_out.a_magic & 0xffff)) {
166 gz->virtual_offset = 0;
167 if (gz->a_out.a_text) {
168 gz->file_offset = PAGE_SIZE;
170 /* Bill's "screwball mode" */
175 gz->virtual_offset = PAGE_SIZE;
179 /* NetBSD compatibility */
180 switch ((int) (ntohl(gz->a_out.a_magic) & 0xffff)) {
183 gz->virtual_offset = PAGE_SIZE;
187 gz->where = __LINE__;
192 gz->bss_size = roundup(gz->a_out.a_bss, PAGE_SIZE);
195 * Check various fields in header for validity/bounds.
197 if ( /* entry point must lay with text region */
198 gz->a_out.a_entry < gz->virtual_offset ||
199 gz->a_out.a_entry >= gz->virtual_offset + gz->a_out.a_text ||
201 /* text and data size must each be page rounded */
202 gz->a_out.a_text & PAGE_MASK || gz->a_out.a_data & PAGE_MASK) {
203 gz->where = __LINE__;
207 * text/data/bss must not exceed limits
209 if ( /* text can't exceed maximum text size */
210 gz->a_out.a_text > maxtsiz ||
212 /* data + bss can't exceed rlimit */
213 gz->a_out.a_data + gz->bss_size >
214 gz->ip->proc->p_rlimit[RLIMIT_DATA].rlim_cur) {
215 gz->where = __LINE__;
218 /* Find out how far we should go */
219 gz->file_end = gz->file_offset + gz->a_out.a_text + gz->a_out.a_data;
221 /* copy in arguments and/or environment from old process */
222 error = exec_extract_strings(gz->ip);
224 gz->where = __LINE__;
228 * Destroy old process VM and create a new one (with a new stack)
230 exec_new_vmspace(gz->ip);
232 vmspace = gz->ip->proc->p_vmspace;
234 vmaddr = gz->virtual_offset;
236 error = vm_mmap(&vmspace->vm_map,
238 gz->a_out.a_text + gz->a_out.a_data,
239 VM_PROT_ALL, VM_PROT_ALL, MAP_ANON | MAP_FIXED,
244 gz->where = __LINE__;
248 if (gz->bss_size != 0) {
250 * Allocate demand-zeroed area for uninitialized data.
251 * "bss" = 'block started by symbol' - named after the
252 * IBM 7090 instruction of the same name.
254 vmaddr = gz->virtual_offset + gz->a_out.a_text +
256 error = vm_map_find(&vmspace->vm_map,
261 FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
263 gz->where = __LINE__;
267 /* Fill in process VM information */
268 vmspace->vm_tsize = gz->a_out.a_text >> PAGE_SHIFT;
269 vmspace->vm_dsize = (gz->a_out.a_data + gz->bss_size) >> PAGE_SHIFT;
270 vmspace->vm_taddr = (caddr_t) (uintptr_t) gz->virtual_offset;
271 vmspace->vm_daddr = (caddr_t) (uintptr_t)
272 (gz->virtual_offset + gz->a_out.a_text);
274 /* Fill in image_params */
275 gz->ip->interpreted = 0;
276 gz->ip->entry_addr = gz->a_out.a_entry;
278 gz->ip->proc->p_sysent = &aout_sysvec;
287 struct imgact_gzip *igz = (struct imgact_gzip *) vp;
289 if (igz->idx >= igz->len) {
290 igz->where = __LINE__;
293 if (igz->inbuf && igz->idx < (igz->offset + PAGE_SIZE)) {
294 return igz->inbuf[(igz->idx++) - igz->offset];
297 error = vm_map_remove(kernel_map, (vm_offset_t) igz->inbuf,
298 (vm_offset_t) igz->inbuf + PAGE_SIZE);
300 igz->where = __LINE__;
305 igz->offset = igz->idx & ~PAGE_MASK;
307 error = vm_mmap(kernel_map, /* map */
308 (vm_offset_t *) & igz->inbuf, /* address */
309 PAGE_SIZE, /* size */
310 VM_PROT_READ, /* protection */
311 VM_PROT_READ, /* max protection */
313 (caddr_t) igz->ip->vp, /* vnode */
314 igz->offset); /* offset */
316 igz->where = __LINE__;
320 return igz->inbuf[(igz->idx++) - igz->offset];
324 Flush(void *vp, u_char * ptr, u_long siz)
326 struct imgact_gzip *gz = (struct imgact_gzip *) vp;
330 /* First, find a a.out-header */
331 if (gz->output < sizeof gz->a_out) {
332 q = (u_char *) & gz->a_out;
333 i = min(siz, sizeof gz->a_out - gz->output);
334 bcopy(p, q + gz->output, i);
338 if (gz->output == sizeof gz->a_out) {
343 gz->where = __LINE__;
347 gz->where = __LINE__;
351 if (gz->file_offset == 0) {
352 q = (u_char *) (uintptr_t) gz->virtual_offset;
353 copyout(&gz->a_out, q, sizeof gz->a_out);
357 /* Skip over zero-padded first PAGE if needed */
358 if (gz->output < gz->file_offset &&
359 gz->output + siz > gz->file_offset) {
360 i = min(siz, gz->file_offset - gz->output);
365 if (gz->output >= gz->file_offset && gz->output < gz->file_end) {
366 i = min(siz, gz->file_end - gz->output);
367 q = (u_char *) (uintptr_t)
368 (gz->virtual_offset + gz->output - gz->file_offset);
380 * Tell kern_execve.c about it, with a little help from the linker.
382 static struct execsw gzip_execsw = {exec_gzip_imgact, "gzip"};
383 EXEC_SET(execgzip, gzip_execsw);