source: mainline/uspace/lib/posix/source/unistd.c@ c19a5a59

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c19a5a59 was f77c1c9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Return VFS handles separately from error codes.

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