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