1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
3 | * Copyright (c) 2011 Petr Koupy
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libposix
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file Miscellaneous standard definitions.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define _LARGEFILE64_SOURCE
|
---|
37 | #undef _FILE_OFFSET_BITS
|
---|
38 |
|
---|
39 | #include "internal/common.h"
|
---|
40 | #include <unistd.h>
|
---|
41 |
|
---|
42 | #include <errno.h>
|
---|
43 |
|
---|
44 | #include <dirent.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <fcntl.h>
|
---|
48 |
|
---|
49 | #include <fibril.h>
|
---|
50 | #include <task.h>
|
---|
51 | #include <stats.h>
|
---|
52 | #include <malloc.h>
|
---|
53 | #include <vfs/vfs.h>
|
---|
54 |
|
---|
55 | #include <libarch/config.h>
|
---|
56 |
|
---|
57 | // FIXME: replace with a hash table
|
---|
58 | aoff64_t posix_pos[VFS_MAX_OPEN_FILES];
|
---|
59 |
|
---|
60 | /* Array of environment variable strings (NAME=VALUE). */
|
---|
61 | char **environ = NULL;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Sleep for the specified number of seconds.
|
---|
65 | *
|
---|
66 | * Note that POSIX allows this call to be interrupted and then the return
|
---|
67 | * value represents remaining seconds for the sleep. HelenOS does not offer
|
---|
68 | * such functionality and thus always the whole sleep is taken.
|
---|
69 | *
|
---|
70 | * @param seconds Number of seconds to sleep.
|
---|
71 | * @return Always 0 on HelenOS.
|
---|
72 | */
|
---|
73 | unsigned int sleep(unsigned int seconds)
|
---|
74 | {
|
---|
75 | fibril_sleep(seconds);
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Get current user name.
|
---|
81 | *
|
---|
82 | * @return User name (static) string or NULL if not found.
|
---|
83 | */
|
---|
84 | char *getlogin(void)
|
---|
85 | {
|
---|
86 | /* There is currently no support for user accounts in HelenOS. */
|
---|
87 | return (char *) "user";
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Get current user name.
|
---|
92 | *
|
---|
93 | * @param name Pointer to a user supplied buffer.
|
---|
94 | * @param namesize Length of the buffer.
|
---|
95 | * @return Zero on success, error code otherwise.
|
---|
96 | */
|
---|
97 | int getlogin_r(char *name, size_t namesize)
|
---|
98 | {
|
---|
99 | /* There is currently no support for user accounts in HelenOS. */
|
---|
100 | if (namesize >= 5) {
|
---|
101 | strcpy(name, (char *) "user");
|
---|
102 | return 0;
|
---|
103 | } else {
|
---|
104 | errno = ERANGE;
|
---|
105 | return -1;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Test whether open file descriptor is associated with a terminal.
|
---|
111 | *
|
---|
112 | * @param fd Open file descriptor to test.
|
---|
113 | * @return Boolean result of the test.
|
---|
114 | */
|
---|
115 | int isatty(int fd)
|
---|
116 | {
|
---|
117 | // TODO
|
---|
118 | /*
|
---|
119 | * Always returns false, because there is no easy way to find
|
---|
120 | * out under HelenOS.
|
---|
121 | */
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Get the pathname of the current working directory.
|
---|
127 | *
|
---|
128 | * @param buf Buffer into which the pathname shall be put.
|
---|
129 | * @param size Size of the buffer.
|
---|
130 | * @return Buffer pointer on success, NULL on failure.
|
---|
131 | */
|
---|
132 | char *getcwd(char *buf, size_t size)
|
---|
133 | {
|
---|
134 | if (failed(vfs_cwd_get(buf, size)))
|
---|
135 | return NULL;
|
---|
136 | return buf;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Change the current working directory.
|
---|
141 | *
|
---|
142 | * @param path New working directory.
|
---|
143 | */
|
---|
144 | int chdir(const char *path)
|
---|
145 | {
|
---|
146 | if (failed(vfs_cwd_set(path)))
|
---|
147 | return -1;
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Determine the page size of the current run of the process.
|
---|
153 | *
|
---|
154 | * @return Page size of the process.
|
---|
155 | */
|
---|
156 | int getpagesize(void)
|
---|
157 | {
|
---|
158 | return PAGE_SIZE;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Get the process ID of the calling process.
|
---|
163 | *
|
---|
164 | * @return Process ID.
|
---|
165 | */
|
---|
166 | pid_t getpid(void)
|
---|
167 | {
|
---|
168 | return task_get_id();
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Get the real user ID of the calling process.
|
---|
173 | *
|
---|
174 | * @return User ID.
|
---|
175 | */
|
---|
176 | uid_t getuid(void)
|
---|
177 | {
|
---|
178 | /* There is currently no support for user accounts in HelenOS. */
|
---|
179 | return 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Get the real group ID of the calling process.
|
---|
184 | *
|
---|
185 | * @return Group ID.
|
---|
186 | */
|
---|
187 | gid_t getgid(void)
|
---|
188 | {
|
---|
189 | /* There is currently no support for user accounts in HelenOS. */
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Close a file.
|
---|
195 | *
|
---|
196 | * @param fildes File descriptor of the opened file.
|
---|
197 | * @return 0 on success, -1 on error.
|
---|
198 | */
|
---|
199 | int close(int fildes)
|
---|
200 | {
|
---|
201 | posix_pos[fildes] = 0;
|
---|
202 | if (failed(vfs_put(fildes)))
|
---|
203 | return -1;
|
---|
204 | else
|
---|
205 | return 0;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Read from a file.
|
---|
210 | *
|
---|
211 | * @param fildes File descriptor of the opened file.
|
---|
212 | * @param buf Buffer to which the read bytes shall be stored.
|
---|
213 | * @param nbyte Upper limit on the number of read bytes.
|
---|
214 | * @return Number of read bytes on success, -1 otherwise.
|
---|
215 | */
|
---|
216 | ssize_t read(int fildes, void *buf, size_t nbyte)
|
---|
217 | {
|
---|
218 | size_t nread;
|
---|
219 | if (failed(vfs_read(fildes, &posix_pos[fildes], buf, nbyte, &nread)))
|
---|
220 | return -1;
|
---|
221 | return (ssize_t) nread;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Write to a file.
|
---|
226 | *
|
---|
227 | * @param fildes File descriptor of the opened file.
|
---|
228 | * @param buf Buffer to write.
|
---|
229 | * @param nbyte Size of the buffer.
|
---|
230 | * @return Number of written bytes on success, -1 otherwise.
|
---|
231 | */
|
---|
232 | ssize_t write(int fildes, const void *buf, size_t nbyte)
|
---|
233 | {
|
---|
234 | size_t nwr;
|
---|
235 | if (failed(vfs_write(fildes, &posix_pos[fildes], buf, nbyte, &nwr)))
|
---|
236 | return -1;
|
---|
237 | return nwr;
|
---|
238 | }
|
---|
239 |
|
---|
240 | static off64_t _lseek(int fildes, off64_t offset, off64_t max_pos, int whence)
|
---|
241 | {
|
---|
242 | vfs_stat_t st;
|
---|
243 | off64_t new_pos;
|
---|
244 |
|
---|
245 | switch (whence) {
|
---|
246 | case SEEK_SET:
|
---|
247 | new_pos = offset;
|
---|
248 | break;
|
---|
249 | case SEEK_CUR:
|
---|
250 | if (__builtin_add_overflow(posix_pos[fildes], offset, &new_pos)) {
|
---|
251 | errno = EOVERFLOW;
|
---|
252 | return -1;
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | case SEEK_END:
|
---|
256 | if (failed(vfs_stat(fildes, &st)))
|
---|
257 | return -1;
|
---|
258 |
|
---|
259 | if (__builtin_add_overflow(st.size, offset, &new_pos)) {
|
---|
260 | errno = EOVERFLOW;
|
---|
261 | return -1;
|
---|
262 | }
|
---|
263 | break;
|
---|
264 | default:
|
---|
265 | errno = EINVAL;
|
---|
266 | return -1;
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (new_pos < 0) {
|
---|
270 | errno = EINVAL;
|
---|
271 | return -1;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (new_pos > max_pos) {
|
---|
275 | /* The resulting file offset is too large for the interface. */
|
---|
276 | errno = EOVERFLOW;
|
---|
277 | return -1;
|
---|
278 | }
|
---|
279 |
|
---|
280 | posix_pos[fildes] = new_pos;
|
---|
281 | return new_pos;
|
---|
282 | }
|
---|
283 |
|
---|
284 | static off64_t _lseek64(int fildes, off64_t offset, int whence)
|
---|
285 | {
|
---|
286 | return _lseek(fildes, offset, INT64_MAX, whence);
|
---|
287 | }
|
---|
288 |
|
---|
289 | off64_t lseek64(int fildes, off64_t offset, int whence)
|
---|
290 | {
|
---|
291 | return _lseek64(fildes, offset, whence);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Reposition read/write file offset
|
---|
296 | *
|
---|
297 | * @param fildes File descriptor of the opened file.
|
---|
298 | * @param offset New offset in the file.
|
---|
299 | * @param whence The position from which the offset argument is specified.
|
---|
300 | * @return Upon successful completion, returns the resulting offset
|
---|
301 | * as measured in bytes from the beginning of the file, -1 otherwise.
|
---|
302 | */
|
---|
303 | off_t lseek(int fildes, off_t offset, int whence)
|
---|
304 | {
|
---|
305 | #if LONG_MAX == INT_MAX
|
---|
306 | return _lseek(fildes, offset, LONG_MAX, whence);
|
---|
307 | #else
|
---|
308 | return _lseek64(fildes, offset, whence);
|
---|
309 | #endif
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Requests outstanding data to be written to the underlying storage device.
|
---|
314 | *
|
---|
315 | * @param fildes File descriptor of the opened file.
|
---|
316 | * @return Zero on success, -1 otherwise.
|
---|
317 | */
|
---|
318 | int fsync(int fildes)
|
---|
319 | {
|
---|
320 | if (failed(vfs_sync(fildes)))
|
---|
321 | return -1;
|
---|
322 | else
|
---|
323 | return 0;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Truncate a file to a specified length.
|
---|
328 | *
|
---|
329 | * @param fildes File descriptor of the opened file.
|
---|
330 | * @param length New length of the file.
|
---|
331 | * @return Zero on success, -1 otherwise.
|
---|
332 | */
|
---|
333 | int ftruncate(int fildes, off_t length)
|
---|
334 | {
|
---|
335 | return ftruncate64(fildes, length);
|
---|
336 | }
|
---|
337 |
|
---|
338 | int ftruncate64(int fildes, off64_t length)
|
---|
339 | {
|
---|
340 | if (failed(vfs_resize(fildes, (aoff64_t) length)))
|
---|
341 | return -1;
|
---|
342 | else
|
---|
343 | return 0;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Remove a directory.
|
---|
348 | *
|
---|
349 | * @param path Directory pathname.
|
---|
350 | * @return Zero on success, -1 otherwise.
|
---|
351 | */
|
---|
352 | int rmdir(const char *path)
|
---|
353 | {
|
---|
354 | if (failed(vfs_unlink_path(path)))
|
---|
355 | return -1;
|
---|
356 | else
|
---|
357 | return 0;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Remove a link to a file.
|
---|
362 | *
|
---|
363 | * @param path File pathname.
|
---|
364 | * @return Zero on success, -1 otherwise.
|
---|
365 | */
|
---|
366 | int unlink(const char *path)
|
---|
367 | {
|
---|
368 | if (failed(vfs_unlink_path(path)))
|
---|
369 | return -1;
|
---|
370 | else
|
---|
371 | return 0;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Duplicate an open file descriptor.
|
---|
376 | *
|
---|
377 | * @param fildes File descriptor to be duplicated.
|
---|
378 | * @return On success, new file descriptor for the same file, otherwise -1.
|
---|
379 | */
|
---|
380 | int dup(int fildes)
|
---|
381 | {
|
---|
382 | return fcntl(fildes, F_DUPFD, 0);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Duplicate an open file descriptor.
|
---|
387 | *
|
---|
388 | * @param fildes File descriptor to be duplicated.
|
---|
389 | * @param fildes2 File descriptor to be paired with the same file description
|
---|
390 | * as is paired fildes.
|
---|
391 | * @return fildes2 on success, -1 otherwise.
|
---|
392 | */
|
---|
393 | int dup2(int fildes, int fildes2)
|
---|
394 | {
|
---|
395 | int file;
|
---|
396 | if (failed(vfs_clone(fildes, fildes2, false, &file))) {
|
---|
397 | return -1;
|
---|
398 | }
|
---|
399 | return file;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Determine accessibility of a file.
|
---|
404 | *
|
---|
405 | * @param path File to check accessibility for.
|
---|
406 | * @param amode Either check for existence or intended access mode.
|
---|
407 | * @return Zero on success, -1 otherwise.
|
---|
408 | */
|
---|
409 | int access(const char *path, int amode)
|
---|
410 | {
|
---|
411 | if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
|
---|
412 | /*
|
---|
413 | * HelenOS doesn't support permissions, permission checks
|
---|
414 | * are equal to existence check.
|
---|
415 | *
|
---|
416 | * Check file existence by attempting to open it.
|
---|
417 | */
|
---|
418 | int fd = open(path, O_RDONLY);
|
---|
419 | if (fd >= 0) {
|
---|
420 | close(fd);
|
---|
421 | return 0;
|
---|
422 | }
|
---|
423 | DIR *dir = opendir(path);
|
---|
424 | if (dir != NULL) {
|
---|
425 | closedir(dir);
|
---|
426 | return 0;
|
---|
427 | }
|
---|
428 | return -1;
|
---|
429 | } else {
|
---|
430 | /* Invalid amode argument. */
|
---|
431 | errno = EINVAL;
|
---|
432 | return -1;
|
---|
433 | }
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Get configurable system variables.
|
---|
438 | *
|
---|
439 | * @param name Variable name.
|
---|
440 | * @return Variable value.
|
---|
441 | */
|
---|
442 | long sysconf(int name)
|
---|
443 | {
|
---|
444 | long clk_tck = 0;
|
---|
445 | size_t cpu_count = 0;
|
---|
446 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
447 | if (cpu_stats && cpu_count > 0) {
|
---|
448 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
449 | }
|
---|
450 | if (cpu_stats) {
|
---|
451 | free(cpu_stats);
|
---|
452 | cpu_stats = 0;
|
---|
453 | }
|
---|
454 |
|
---|
455 | long phys_pages = 0;
|
---|
456 | long avphys_pages = 0;
|
---|
457 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
458 | if (mem_stats) {
|
---|
459 | phys_pages = (long) (mem_stats->total / getpagesize());
|
---|
460 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
---|
461 | free(mem_stats);
|
---|
462 | mem_stats = 0;
|
---|
463 | }
|
---|
464 |
|
---|
465 | switch (name) {
|
---|
466 | case _SC_PHYS_PAGES:
|
---|
467 | return phys_pages;
|
---|
468 | case _SC_AVPHYS_PAGES:
|
---|
469 | return avphys_pages;
|
---|
470 | case _SC_PAGESIZE:
|
---|
471 | return getpagesize();
|
---|
472 | case _SC_CLK_TCK:
|
---|
473 | return clk_tck;
|
---|
474 | default:
|
---|
475 | errno = EINVAL;
|
---|
476 | return -1;
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | *
|
---|
482 | * @param path
|
---|
483 | * @param name
|
---|
484 | * @return
|
---|
485 | */
|
---|
486 | long pathconf(const char *path, int name)
|
---|
487 | {
|
---|
488 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
489 | not_implemented();
|
---|
490 | return -1;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /**
|
---|
494 | *
|
---|
495 | * @return
|
---|
496 | */
|
---|
497 | pid_t fork(void)
|
---|
498 | {
|
---|
499 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
500 | not_implemented();
|
---|
501 | return -1;
|
---|
502 | }
|
---|
503 |
|
---|
504 | /**
|
---|
505 | *
|
---|
506 | * @param path
|
---|
507 | * @param argv
|
---|
508 | * @return
|
---|
509 | */
|
---|
510 | int execv(const char *path, char *const argv[])
|
---|
511 | {
|
---|
512 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
513 | not_implemented();
|
---|
514 | return -1;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | *
|
---|
519 | * @param file
|
---|
520 | * @param argv
|
---|
521 | * @return
|
---|
522 | */
|
---|
523 | int execvp(const char *file, char *const argv[])
|
---|
524 | {
|
---|
525 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
526 | not_implemented();
|
---|
527 | return -1;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | *
|
---|
532 | * @param fildes
|
---|
533 | * @return
|
---|
534 | */
|
---|
535 | int pipe(int fildes[2])
|
---|
536 | {
|
---|
537 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
538 | not_implemented();
|
---|
539 | return -1;
|
---|
540 | }
|
---|
541 |
|
---|
542 | unsigned int alarm(unsigned int seconds)
|
---|
543 | {
|
---|
544 | not_implemented();
|
---|
545 | return 0;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /** @}
|
---|
549 | */
|
---|