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 LIBPOSIX_INTERNAL
|
---|
37 | #define __POSIX_DEF__(x) posix_##x
|
---|
38 |
|
---|
39 | #include "internal/common.h"
|
---|
40 | #include "posix/unistd.h"
|
---|
41 |
|
---|
42 | #include "posix/errno.h"
|
---|
43 | #include "posix/string.h"
|
---|
44 | #include "posix/fcntl.h"
|
---|
45 |
|
---|
46 | #include "libc/task.h"
|
---|
47 | #include "libc/stats.h"
|
---|
48 | #include "libc/malloc.h"
|
---|
49 | #include "libc/vfs/vfs.h"
|
---|
50 |
|
---|
51 | #include <libarch/config.h>
|
---|
52 |
|
---|
53 | aoff64_t posix_pos[MAX_OPEN_FILES];
|
---|
54 |
|
---|
55 | /* Array of environment variable strings (NAME=VALUE). */
|
---|
56 | char **posix_environ = NULL;
|
---|
57 | char *posix_optarg;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Get current user name.
|
---|
61 | *
|
---|
62 | * @return User name (static) string or NULL if not found.
|
---|
63 | */
|
---|
64 | char *posix_getlogin(void)
|
---|
65 | {
|
---|
66 | /* There is currently no support for user accounts in HelenOS. */
|
---|
67 | return (char *) "user";
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Get current user name.
|
---|
72 | *
|
---|
73 | * @param name Pointer to a user supplied buffer.
|
---|
74 | * @param namesize Length of the buffer.
|
---|
75 | * @return Zero on success, error code otherwise.
|
---|
76 | */
|
---|
77 | int posix_getlogin_r(char *name, size_t namesize)
|
---|
78 | {
|
---|
79 | /* There is currently no support for user accounts in HelenOS. */
|
---|
80 | if (namesize >= 5) {
|
---|
81 | posix_strcpy(name, (char *) "user");
|
---|
82 | return 0;
|
---|
83 | } else {
|
---|
84 | errno = ERANGE;
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Test whether open file descriptor is associated with a terminal.
|
---|
91 | *
|
---|
92 | * @param fd Open file descriptor to test.
|
---|
93 | * @return Boolean result of the test.
|
---|
94 | */
|
---|
95 | int posix_isatty(int fd)
|
---|
96 | {
|
---|
97 | // TODO
|
---|
98 | /* Always returns false, because there is no easy way to find
|
---|
99 | * out under HelenOS. */
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Get the pathname of the current working directory.
|
---|
105 | *
|
---|
106 | * @param buf Buffer into which the pathname shall be put.
|
---|
107 | * @param size Size of the buffer.
|
---|
108 | * @return Buffer pointer on success, NULL on failure.
|
---|
109 | */
|
---|
110 | char *posix_getcwd(char *buf, size_t size)
|
---|
111 | {
|
---|
112 | int rc = rcerrno(vfs_cwd_get, buf, size);
|
---|
113 | if (rc != EOK)
|
---|
114 | return NULL;
|
---|
115 | return buf;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Change the current working directory.
|
---|
120 | *
|
---|
121 | * @param path New working directory.
|
---|
122 | */
|
---|
123 | int posix_chdir(const char *path)
|
---|
124 | {
|
---|
125 | int rc = rcerrno(vfs_cwd_set, path);
|
---|
126 | if (rc != EOK)
|
---|
127 | return -1;
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Determine the page size of the current run of the process.
|
---|
133 | *
|
---|
134 | * @return Page size of the process.
|
---|
135 | */
|
---|
136 | int posix_getpagesize(void)
|
---|
137 | {
|
---|
138 | return PAGE_SIZE;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Get the process ID of the calling process.
|
---|
143 | *
|
---|
144 | * @return Process ID.
|
---|
145 | */
|
---|
146 | posix_pid_t posix_getpid(void)
|
---|
147 | {
|
---|
148 | return task_get_id();
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Get the real user ID of the calling process.
|
---|
153 | *
|
---|
154 | * @return User ID.
|
---|
155 | */
|
---|
156 | posix_uid_t posix_getuid(void)
|
---|
157 | {
|
---|
158 | /* There is currently no support for user accounts in HelenOS. */
|
---|
159 | return 0;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Get the real group ID of the calling process.
|
---|
164 | *
|
---|
165 | * @return Group ID.
|
---|
166 | */
|
---|
167 | posix_gid_t posix_getgid(void)
|
---|
168 | {
|
---|
169 | /* There is currently no support for user accounts in HelenOS. */
|
---|
170 | return 0;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Close a file.
|
---|
175 | *
|
---|
176 | * @param fildes File descriptor of the opened file.
|
---|
177 | * @return 0 on success, -1 on error.
|
---|
178 | */
|
---|
179 | int posix_close(int fildes)
|
---|
180 | {
|
---|
181 | posix_pos[fildes] = 0;
|
---|
182 | int rc = rcerrno(vfs_put, fildes);
|
---|
183 | if (rc != EOK)
|
---|
184 | return -1;
|
---|
185 | else
|
---|
186 | return 0;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Read from a file.
|
---|
191 | *
|
---|
192 | * @param fildes File descriptor of the opened file.
|
---|
193 | * @param buf Buffer to which the read bytes shall be stored.
|
---|
194 | * @param nbyte Upper limit on the number of read bytes.
|
---|
195 | * @return Number of read bytes on success, -1 otherwise.
|
---|
196 | */
|
---|
197 | ssize_t posix_read(int fildes, void *buf, size_t nbyte)
|
---|
198 | {
|
---|
199 | ssize_t size = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte);
|
---|
200 | if (size < 0)
|
---|
201 | return -1;
|
---|
202 | return size;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Write to a file.
|
---|
207 | *
|
---|
208 | * @param fildes File descriptor of the opened file.
|
---|
209 | * @param buf Buffer to write.
|
---|
210 | * @param nbyte Size of the buffer.
|
---|
211 | * @return Number of written bytes on success, -1 otherwise.
|
---|
212 | */
|
---|
213 | ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
|
---|
214 | {
|
---|
215 | ssize_t size = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte);
|
---|
216 | if (size < 0)
|
---|
217 | return -1;
|
---|
218 | return size;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Reposition read/write file offset
|
---|
223 | *
|
---|
224 | * @param fildes File descriptor of the opened file.
|
---|
225 | * @param offset New offset in the file.
|
---|
226 | * @param whence The position from which the offset argument is specified.
|
---|
227 | * @return Upon successful completion, returns the resulting offset
|
---|
228 | * as measured in bytes from the beginning of the file, -1 otherwise.
|
---|
229 | */
|
---|
230 | posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
|
---|
231 | {
|
---|
232 | struct stat st;
|
---|
233 | int rc;
|
---|
234 |
|
---|
235 | switch (whence) {
|
---|
236 | case SEEK_SET:
|
---|
237 | posix_pos[fildes] = offset;
|
---|
238 | break;
|
---|
239 | case SEEK_CUR:
|
---|
240 | posix_pos[fildes] += offset;
|
---|
241 | break;
|
---|
242 | case SEEK_END:
|
---|
243 | rc = rcerrno(vfs_stat, fildes, &st);
|
---|
244 | if (rc != EOK)
|
---|
245 | return -1;
|
---|
246 | posix_pos[fildes] = st.size + offset;
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | if (posix_pos[fildes] > INT64_MAX) {
|
---|
250 | /* The native width is too large for the POSIX interface. */
|
---|
251 | errno = ERANGE;
|
---|
252 | return -1;
|
---|
253 | }
|
---|
254 | return posix_pos[fildes];
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Requests outstanding data to be written to the underlying storage device.
|
---|
259 | *
|
---|
260 | * @param fildes File descriptor of the opened file.
|
---|
261 | * @return Zero on success, -1 otherwise.
|
---|
262 | */
|
---|
263 | int posix_fsync(int fildes)
|
---|
264 | {
|
---|
265 | if (rcerrno(vfs_sync, fildes) != EOK)
|
---|
266 | return -1;
|
---|
267 | else
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Truncate a file to a specified length.
|
---|
273 | *
|
---|
274 | * @param fildes File descriptor of the opened file.
|
---|
275 | * @param length New length of the file.
|
---|
276 | * @return Zero on success, -1 otherwise.
|
---|
277 | */
|
---|
278 | int posix_ftruncate(int fildes, posix_off_t length)
|
---|
279 | {
|
---|
280 | if (rcerrno(vfs_resize, fildes, (aoff64_t) length) != EOK)
|
---|
281 | return -1;
|
---|
282 | else
|
---|
283 | return 0;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Remove a directory.
|
---|
288 | *
|
---|
289 | * @param path Directory pathname.
|
---|
290 | * @return Zero on success, -1 otherwise.
|
---|
291 | */
|
---|
292 | int posix_rmdir(const char *path)
|
---|
293 | {
|
---|
294 | if (rcerrno(vfs_unlink_path, path) != EOK)
|
---|
295 | return -1;
|
---|
296 | else
|
---|
297 | return 0;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * Remove a link to a file.
|
---|
302 | *
|
---|
303 | * @param path File pathname.
|
---|
304 | * @return Zero on success, -1 otherwise.
|
---|
305 | */
|
---|
306 | int posix_unlink(const char *path)
|
---|
307 | {
|
---|
308 | if (rcerrno(vfs_unlink_path, path) != EOK)
|
---|
309 | return -1;
|
---|
310 | else
|
---|
311 | return 0;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * Duplicate an open file descriptor.
|
---|
316 | *
|
---|
317 | * @param fildes File descriptor to be duplicated.
|
---|
318 | * @return On success, new file descriptor for the same file, otherwise -1.
|
---|
319 | */
|
---|
320 | int posix_dup(int fildes)
|
---|
321 | {
|
---|
322 | return posix_fcntl(fildes, F_DUPFD, 0);
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Duplicate an open file descriptor.
|
---|
327 | *
|
---|
328 | * @param fildes File descriptor to be duplicated.
|
---|
329 | * @param fildes2 File descriptor to be paired with the same file description
|
---|
330 | * as is paired fildes.
|
---|
331 | * @return fildes2 on success, -1 otherwise.
|
---|
332 | */
|
---|
333 | int posix_dup2(int fildes, int fildes2)
|
---|
334 | {
|
---|
335 | return negerrno(vfs_clone, fildes, fildes2, false);
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Determine accessibility of a file.
|
---|
340 | *
|
---|
341 | * @param path File to check accessibility for.
|
---|
342 | * @param amode Either check for existence or intended access mode.
|
---|
343 | * @return Zero on success, -1 otherwise.
|
---|
344 | */
|
---|
345 | int posix_access(const char *path, int amode)
|
---|
346 | {
|
---|
347 | if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
|
---|
348 | /* HelenOS doesn't support permissions, permission checks
|
---|
349 | * are equal to existence check.
|
---|
350 | *
|
---|
351 | * Check file existence by attempting to open it.
|
---|
352 | */
|
---|
353 | int fd = posix_open(path, O_RDONLY);
|
---|
354 | if (fd < 0)
|
---|
355 | return -1;
|
---|
356 | posix_close(fd);
|
---|
357 | return 0;
|
---|
358 | } else {
|
---|
359 | /* Invalid amode argument. */
|
---|
360 | errno = EINVAL;
|
---|
361 | return -1;
|
---|
362 | }
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Get configurable system variables.
|
---|
367 | *
|
---|
368 | * @param name Variable name.
|
---|
369 | * @return Variable value.
|
---|
370 | */
|
---|
371 | long posix_sysconf(int name)
|
---|
372 | {
|
---|
373 | long clk_tck = 0;
|
---|
374 | size_t cpu_count = 0;
|
---|
375 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
376 | if (cpu_stats && cpu_count > 0) {
|
---|
377 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
378 | }
|
---|
379 | if (cpu_stats) {
|
---|
380 | free(cpu_stats);
|
---|
381 | cpu_stats = 0;
|
---|
382 | }
|
---|
383 |
|
---|
384 | long phys_pages = 0;
|
---|
385 | long avphys_pages = 0;
|
---|
386 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
387 | if (mem_stats) {
|
---|
388 | phys_pages = (long) (mem_stats->total / posix_getpagesize());
|
---|
389 | avphys_pages = (long) (mem_stats->free / posix_getpagesize());
|
---|
390 | free(mem_stats);
|
---|
391 | mem_stats = 0;
|
---|
392 | }
|
---|
393 |
|
---|
394 | switch (name) {
|
---|
395 | case _SC_PHYS_PAGES:
|
---|
396 | return phys_pages;
|
---|
397 | case _SC_AVPHYS_PAGES:
|
---|
398 | return avphys_pages;
|
---|
399 | case _SC_PAGESIZE:
|
---|
400 | return posix_getpagesize();
|
---|
401 | case _SC_CLK_TCK:
|
---|
402 | return clk_tck;
|
---|
403 | default:
|
---|
404 | errno = EINVAL;
|
---|
405 | return -1;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | *
|
---|
411 | * @param path
|
---|
412 | * @param name
|
---|
413 | * @return
|
---|
414 | */
|
---|
415 | long posix_pathconf(const char *path, int name)
|
---|
416 | {
|
---|
417 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
418 | not_implemented();
|
---|
419 | return -1;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /**
|
---|
423 | *
|
---|
424 | * @return
|
---|
425 | */
|
---|
426 | posix_pid_t posix_fork(void)
|
---|
427 | {
|
---|
428 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
429 | not_implemented();
|
---|
430 | return -1;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /**
|
---|
434 | *
|
---|
435 | * @param path
|
---|
436 | * @param argv
|
---|
437 | * @return
|
---|
438 | */
|
---|
439 | int posix_execv(const char *path, char *const argv[])
|
---|
440 | {
|
---|
441 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
442 | not_implemented();
|
---|
443 | return -1;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | *
|
---|
448 | * @param file
|
---|
449 | * @param argv
|
---|
450 | * @return
|
---|
451 | */
|
---|
452 | int posix_execvp(const char *file, char *const argv[])
|
---|
453 | {
|
---|
454 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
455 | not_implemented();
|
---|
456 | return -1;
|
---|
457 | }
|
---|
458 |
|
---|
459 | /**
|
---|
460 | *
|
---|
461 | * @param fildes
|
---|
462 | * @return
|
---|
463 | */
|
---|
464 | int posix_pipe(int fildes[2])
|
---|
465 | {
|
---|
466 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
467 | not_implemented();
|
---|
468 | return -1;
|
---|
469 | }
|
---|
470 |
|
---|
471 | unsigned int posix_alarm(unsigned int seconds)
|
---|
472 | {
|
---|
473 | not_implemented();
|
---|
474 | return 0;
|
---|
475 | }
|
---|
476 |
|
---|
477 | /** @}
|
---|
478 | */
|
---|