-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsys.c
More file actions
320 lines (262 loc) · 8.08 KB
/
sys.c
File metadata and controls
320 lines (262 loc) · 8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright 2010, 2011 Andrey Zholos
This file is part of kuc, a vector programming language.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sys.h"
#include "alloc.h"
#include "error.h"
#include "value.h"
#include "verb.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/stat.h>
//
// File I/O
//
static ssize_t read_fd(int fd, void* buf, size_t size) {
ssize_t i;
while ((i = read(fd, buf, size)) == -1)
if (errno != EINTR)
return -1;
return i;
}
static int close_fd(int fd) {
while (close(fd) == -1)
if (errno != EINTR)
return -1;
return 0;
}
static const char* filename(Value x) {
if (x->vector || x->type != type_name || x->_name[0] != ':')
error(error_type);
return x->_name + 1;
}
#define MIN(x, y) ((x) < (y) ? (x) : (y))
Value read_file(Value x) {
const char* fn = filename(x);
int fd;
while ((fd = open(fn, O_RDONLY)) == -1)
if (errno != EINTR)
goto error_file;
// if we can find out the size, do so
struct stat st;
if (fstat(fd, &st) == -1)
if (errno == EOVERFLOW)
goto error_length_close;
else
goto error_file_close;
if (st.st_size > index_max)
goto error_length_close;
MValue r = create_items(type_char, st.st_size);
for (index n = 0;;) {
if (n == r->count) {
r->count = add_length(r->count, 1 << 16);
r = resize_alloc(r, offsetof(struct value, items[0]) + r->count);
}
ssize_t i = read_fd(fd, r->chars + n, MIN(INT_MAX, r->count - n));
if (i == -1)
goto error_file_close;
else if (i == 0) {
r->count = n;
r = resize_alloc(r, offsetof(struct value, items[0]) + r->count);
break;
} else
n += i;
}
if (close_fd(fd) == -1)
goto error_file;
return r;
error_length_close:
close_fd(fd);
error(error_length);
error_file_close:
close_fd(fd);
error_file:
error(error_file);
}
Value write_file(Value x, Value y) {
if (!y->vector || y->type != type_char)
error(error_type);
const char* fn = filename(x);
int fd;
while ((fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
if (errno != EINTR)
goto error_file;
for (index n = 0; n != y->count;) {
ssize_t i = write(fd, y->chars + n, MIN(INT_MAX, y->count - n));
if (i == -1 || i == 0) {
if (i == -1 && errno == EINTR)
continue;
goto error_file_close;
} else
n += i;
}
if (close_fd(fd) == -1)
goto error_file;
return x;
error_file_close:
close_fd(fd);
error_file:
error(error_file);
}
Value mmap_file(Value x) {
const char* fn = filename(x);
int fd;
while ((fd = open(fn, O_RDONLY)) == -1)
if (errno != EINTR)
goto error_file;
struct stat st;
if (fstat(fd, &st) == -1)
if (errno == EOVERFLOW)
goto error_length_close;
else
goto error_file_close;
// we want to place one page (multiple pages if they're really small) in
// front of the file in memory for the Value header
size_t front = pagesize;
front = (offsetof(struct value, items[0]) + (front - 1)) / front * front;
if (st.st_size > SIZE_MAX - front || st.st_size > index_max)
goto error_length_close;
size_t total = front + st.st_size;
#ifndef MAP_NOCORE
#define MAP_NOCORE 0
#endif
// first, map and large enough segment of memory
char* whole = mmap(NULL, total, PROT_NONE,
MAP_NOCORE | MAP_ANON | MAP_PRIVATE, -1, 0);
if (whole == MAP_FAILED)
goto error_file_close;
// then, map the file over it
void* file = mmap(whole + front, total - front, PROT_READ,
MAP_FIXED | MAP_NOCORE | MAP_PRIVATE, fd, 0);
if (file == MAP_FAILED)
goto error_file_unmap;
if (file != whole + front)
assert_failed("mmap() didn't honor MAP_FIXED flag");
// and put the header in front
char* header = mmap(whole, front, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
if (header == MAP_FAILED)
goto error_file_unmap;
if (header != whole)
assert_failed("mmap() didn't honor MAP_FIXED flag");
if (close_fd(fd) == -1)
goto error_file_unmap;
// since items is aligned and mapping is aligned, r should also be aligned
size_t offset = front - offsetof(struct value, items[0]);
MValue r = (MValue)(header + offset);
r->type = type_char;
r->vector = 1;
r->count = st.st_size;
track_mmap_block(r, total - offset, offset);
return r;
error_length_close:
close_fd(fd);
error(error_length);
error_file_unmap:
if (munmap(whole, total) == -1)
assert_failed("munmap() failed");
error_file_close:
close_fd(fd);
error_file:
error(error_file);
}
//
// Random numbers
//
static void __attribute__ ((constructor)) init_random() {
#if HAVE_SRANDOMDEV
srandomdev();
#else
int fd = open("/dev/urandom", O_RDONLY);
if (fd == -1)
assert_failed("open(/dev/urandom) failed");
unsigned long seed;
if (read_fd(fd, &seed, sizeof seed) != sizeof seed)
assert_failed("read(/dev/urandom) failed");
if (close_fd(fd) == -1)
assert_failed("close(/dev/urandom) failed");
srandom(seed);
#endif
}
static int draw_int(int bound) {
assert(bound > 0);
if (bound > RAND_MAX)
assert_failed("random() doesn't have sufficient range");
int draw = RAND_MAX / bound * bound;
long r;
do {
r = random();
if (r > RAND_MAX)
assert_failed("random() > RAND_MAX");
} while (r > draw);
return r % bound;
}
Value draw(Value x) {
if (x->vector) {
return x->count > 0 ? item(x, INDEX_SUFFIX(draw)(x->count))
: item_null(x, 0);
} else if (x->type == type_int) {
if (x->_int <= 0)
error(error_range);
return create_int(draw_int(x->_int));
} else
error(error_todo);
}
//
// Timing
//
static Value time_delta(const struct timeval* t1, const struct timeval* t2) {
if (t1->tv_sec > t2->tv_sec ||
t1->tv_sec == t2->tv_sec && t1->tv_usec > t2->tv_usec)
error(error_sys);
return create_float((t2->tv_sec - t1->tv_sec) +
(t2->tv_usec * 1e-6 - t1->tv_usec * 1e-6));
}
Value time_value(Value x) {
static const struct value_mixed key =
{ type_variant, 1, 3,
{ (Value)&name_user, (Value)&name_system, (Value)&name_value } };
struct rusage rusage1, rusage2;
if (getrusage(RUSAGE_SELF, &rusage1) == -1)
error(error_sys);
Value r = value(x);
if (getrusage(RUSAGE_SELF, &rusage2) == -1)
error(error_sys);
return create_map((Value)&key, triplet(
time_delta(&rusage1.ru_utime, &rusage2.ru_utime),
time_delta(&rusage1.ru_stime, &rusage2.ru_stime), r));
}
//
// Terminal
//
index screen_size(index* height) {
struct winsize ws;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
// failures include there being no terminal
ws.ws_col = 80;
ws.ws_row = 25;
}
if (height)
*height = ws.ws_row;
return ws.ws_col;
}