source: trunk/minix/commands/simple/chmem.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.5 KB
Line 
1/* chmem - set total memory size for execution Author: Andy Tanenbaum */
2
3#include <minix/config.h>
4#include <sys/types.h>
5#include <a.out.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <stdio.h>
11
12#define MAX_8086 0x10000L /* maximum allocation size for 8086 */
13#define MAX_386 0x7FFFFFFFL /* etc */
14#define MAX_68K 0x7FFFFFFFL
15#define MAX_SPARC 0x20000000L /* No more than 512MB on a SparcStation! */
16
17char *progname;
18
19_PROTOTYPE(int main, (int argc, char **argv));
20_PROTOTYPE(void error, (char *s1, char *s2));
21_PROTOTYPE(void usage, (void));
22
23int main(argc, argv)
24int argc;
25char *argv[];
26{
27/* The 8088 architecture does not make it possible to catch stacks that grow
28 * big. The only way to deal with this problem is to let the stack grow down
29 * towards the data segment and the data segment grow up towards the stack.
30 * Normally, a total of 64K is allocated for the two of them, but if the
31 * programmer knows that a smaller amount is sufficient, he can change it
32 * using chmem.
33 *
34 * chmem =4096 prog sets the total space for stack + data growth to 4096
35 * chmem +200 prog increments the total space for stack + data growth by 200
36 */
37
38 char *p;
39 int fd = -1, separate;
40 size_t s;
41 long lsize, olddynam, newdynam, newtot, overflow;
42 struct exec exec;
43 char cpu;
44 long max;
45 int last_failed = 0, any_failed = 0;
46
47 progname = argv[0];
48 if (argc < 3) usage();
49 p = argv[1];
50 if (*p != '=' && *p != '+' && *p != '-') usage();
51 lsize = atol(p + 1);
52 s = sizeof(struct exec);
53
54 if (lsize < 0) {
55 error(p + 1, "is negative");
56 exit(1);
57 }
58 argc -= 1;
59 argv += 1;
60
61 while (--argc) {
62 if(last_failed) any_failed = 1;
63
64 /* Unless we reach the end of this loop, this one failed. */
65 last_failed = 1;
66 ++argv;
67 if(fd != -1) close(fd);
68 fd = open(*argv, O_RDWR);
69 if (fd < 0) {
70 error("can't open", *argv);
71 continue;
72 }
73 if (read(fd, (char *) &exec, s) != s) {
74 error("can't read header in", *argv);
75 continue;
76 }
77 if (BADMAG(exec)) {
78 error(*argv, "is not executable");
79 continue;
80 }
81 separate = (exec.a_flags & A_SEP ? 1 : 0);
82 cpu = exec.a_cpu;
83
84#if (CHIP == M68000)
85 if (cpu == A_I8086) cpu = A_M68K;
86#endif
87
88 switch (cpu) {
89 case A_I8086: max = MAX_8086; break;
90 case A_I80386: max = MAX_386; break;
91 case A_M68K: max = MAX_68K; break;
92 case A_SPARC: max = MAX_SPARC; break;
93 default:
94 error("bad CPU type in", *argv);
95 continue;
96 }
97
98 if (lsize > max) {
99 error("size is too large for", *argv);
100 continue;
101 }
102 olddynam = exec.a_total - exec.a_data - exec.a_bss;
103 if (separate == 0) olddynam -= exec.a_text;
104
105 if (*p == '=')
106 newdynam = lsize;
107 else if (*p == '+')
108 newdynam = olddynam + lsize;
109 else if (*p == '-')
110 newdynam = olddynam - lsize;
111
112 newtot = exec.a_data + exec.a_bss + newdynam;
113 if (separate == 0) newtot += exec.a_text;
114 overflow = (newtot > max ? newtot - max : 0);
115 newdynam -= overflow;
116 newtot -= overflow;
117 exec.a_total = newtot;
118 lseek(fd, (long) 0, SEEK_SET);
119 if (write(fd, (char *) &exec, s) != s) {
120 error("can't modify", *argv);
121 continue;
122 }
123 printf("%s: Stack+malloc area changed from %ld to %ld bytes.\n",
124 *argv, olddynam, newdynam);
125
126 /* This one didn't fail. */
127 last_failed = 0;
128 }
129 return(any_failed || last_failed ? 1 : 0);
130}
131
132void error(s1, s2)
133char *s1;
134char *s2;
135{
136 fprintf(stderr, "%s: %s ", progname, s1);
137 if (errno != 0)
138 perror(s2);
139 else
140 fprintf(stderr, "%s\n", s2);
141 errno = 0;
142}
143
144void usage()
145{
146 fprintf(stderr, "Usage: %s {=+-} amount file\n", progname);
147 exit(1);
148}
Note: See TracBrowser for help on using the repository browser.