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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f77c1c9 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
RevLine 
[a16210b5]1/*
2 * Copyright (c) 2011 Jiri Zarevucky
[4f4b4e7]3 * Copyright (c) 2011 Petr Koupy
[a16210b5]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
[4f4b4e7]30/** @addtogroup libposix
31 * @{
32 */
[4cf8ca6]33/** @file Miscellaneous standard definitions.
[4f4b4e7]34 */
35
[491e1ee]36#define LIBPOSIX_INTERNAL
[fdf97f6]37#define __POSIX_DEF__(x) posix_##x
[4f4b4e7]38
[9b1503e]39#include "internal/common.h"
[a3da2b2]40#include "posix/unistd.h"
[ec18957a]41
[a3da2b2]42#include "posix/errno.h"
43#include "posix/string.h"
44#include "posix/fcntl.h"
[ec18957a]45
46#include "libc/task.h"
[1165a419]47#include "libc/thread.h"
[ec18957a]48#include "libc/stats.h"
[fb872c1]49#include "libc/malloc.h"
[58898d1d]50#include "libc/vfs/vfs.h"
51
[582a0b8]52#include <libarch/config.h>
53
[58898d1d]54aoff64_t posix_pos[MAX_OPEN_FILES];
[a16210b5]55
[b4d6252]56/* Array of environment variable strings (NAME=VALUE). */
[7530a00]57char **posix_environ = NULL;
[a92f13d]58char *posix_optarg;
[b4d6252]59
[1165a419]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
[244d6fd]75/**
76 * Get current user name.
[fb872c1]77 *
78 * @return User name (static) string or NULL if not found.
[244d6fd]79 */
80char *posix_getlogin(void)
81{
[fb872c1]82 /* There is currently no support for user accounts in HelenOS. */
[244d6fd]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.
[fb872c1]91 * @return Zero on success, error code otherwise.
[244d6fd]92 */
93int posix_getlogin_r(char *name, size_t namesize)
94{
[fb872c1]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 }
[244d6fd]103}
104
[4f4b4e7]105/**
[fb872c1]106 * Test whether open file descriptor is associated with a terminal.
[8d7e82c1]107 *
[fb872c1]108 * @param fd Open file descriptor to test.
109 * @return Boolean result of the test.
[4f4b4e7]110 */
111int posix_isatty(int fd)
112{
[4c8f5e7]113 // TODO
[fb872c1]114 /* Always returns false, because there is no easy way to find
[d542aad]115 * out under HelenOS. */
[94f8b81c]116 return 0;
[a16210b5]117}
118
[221afc9e]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{
[d96d9bc]128 int rc = rcerrno(vfs_cwd_get, buf, size);
129 if (rc != EOK)
[221afc9e]130 return NULL;
[d96d9bc]131 return buf;
[221afc9e]132}
133
[75406dc]134/**
135 * Change the current working directory.
136 *
137 * @param path New working directory.
138 */
139int posix_chdir(const char *path)
140{
[d96d9bc]141 int rc = rcerrno(vfs_cwd_set, path);
142 if (rc != EOK)
143 return -1;
144 return 0;
[75406dc]145}
146
[72ec8cc]147/**
[fb872c1]148 * Determine the page size of the current run of the process.
149 *
150 * @return Page size of the process.
[72ec8cc]151 */
152int posix_getpagesize(void)
153{
[582a0b8]154 return PAGE_SIZE;
[72ec8cc]155}
156
[823a929]157/**
[fb872c1]158 * Get the process ID of the calling process.
159 *
160 * @return Process ID.
[823a929]161 */
162posix_pid_t posix_getpid(void)
163{
164 return task_get_id();
165}
166
[59f799b]167/**
[fb872c1]168 * Get the real user ID of the calling process.
[59f799b]169 *
[fb872c1]170 * @return User ID.
[59f799b]171 */
172posix_uid_t posix_getuid(void)
173{
[fb872c1]174 /* There is currently no support for user accounts in HelenOS. */
175 return 0;
[59f799b]176}
177
178/**
[fb872c1]179 * Get the real group ID of the calling process.
[59f799b]180 *
[fb872c1]181 * @return Group ID.
[59f799b]182 */
183posix_gid_t posix_getgid(void)
184{
[fb872c1]185 /* There is currently no support for user accounts in HelenOS. */
186 return 0;
[59f799b]187}
188
[75406dc]189/**
190 * Close a file.
191 *
[2a53f71]192 * @param fildes File descriptor of the opened file.
[75406dc]193 * @return 0 on success, -1 on error.
194 */
195int posix_close(int fildes)
196{
[58898d1d]197 posix_pos[fildes] = 0;
[9c4cf0d]198 int rc = rcerrno(vfs_put, fildes);
199 if (rc != EOK)
200 return -1;
201 else
202 return 0;
[75406dc]203}
204
[221afc9e]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{
[8e3498b]215 size_t nread;
216 int rc;
217
218 rc = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte, &nread);
219 if (rc != EOK)
[ce04ea44]220 return -1;
[8e3498b]221 return (ssize_t) nread;
[75406dc]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{
[8e3498b]234 size_t nwr;
235 int rc;
236
237 rc = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte, &nwr);
238 if (rc != EOK)
[ce04ea44]239 return -1;
[8e3498b]240 return nwr;
[75406dc]241}
242
[e3480d5]243/**
244 * Reposition read/write file offset
245 *
246 * @param fildes File descriptor of the opened file.
[bf963b9]247 * @param offset New offset in the file.
248 * @param whence The position from which the offset argument is specified.
[e3480d5]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{
[58898d1d]254 struct stat st;
[23a0368]255 int rc;
[58898d1d]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:
[23a0368]265 rc = rcerrno(vfs_stat, fildes, &st);
266 if (rc != EOK)
[58898d1d]267 return -1;
268 posix_pos[fildes] = st.size + offset;
269 break;
270 }
[59f388a]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 }
[58898d1d]276 return posix_pos[fildes];
[e3480d5]277}
278
[75406dc]279/**
280 * Requests outstanding data to be written to the underlying storage device.
281 *
[2a53f71]282 * @param fildes File descriptor of the opened file.
283 * @return Zero on success, -1 otherwise.
[75406dc]284 */
285int posix_fsync(int fildes)
286{
[a56cef9]287 if (rcerrno(vfs_sync, fildes) != EOK)
288 return -1;
289 else
290 return 0;
[75406dc]291}
292
[2a53f71]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 */
[75406dc]300int posix_ftruncate(int fildes, posix_off_t length)
301{
[67e881c]302 if (rcerrno(vfs_resize, fildes, (aoff64_t) length) != EOK)
303 return -1;
304 else
305 return 0;
[221afc9e]306}
307
[58115ae]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{
[79ea5af]316 if (rcerrno(vfs_unlink_path, path) != EOK)
317 return -1;
318 else
319 return 0;
[58115ae]320}
321
[221afc9e]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{
[79ea5af]330 if (rcerrno(vfs_unlink_path, path) != EOK)
331 return -1;
332 else
333 return 0;
[75406dc]334}
335
[2a53f71]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 */
[75406dc]342int posix_dup(int fildes)
343{
344 return posix_fcntl(fildes, F_DUPFD, 0);
345}
346
[2a53f71]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 */
[75406dc]355int posix_dup2(int fildes, int fildes2)
356{
[f77c1c9]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;
[221afc9e]364}
365
[b08ef1fd]366/**
[fb872c1]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.
[b08ef1fd]372 */
373int posix_access(const char *path, int amode)
374{
[75406dc]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 */
[b19e892]381 int fd = posix_open(path, O_RDONLY);
382 if (fd < 0)
[955c2b0]383 return -1;
[9c4cf0d]384 posix_close(fd);
[955c2b0]385 return 0;
[fb872c1]386 } else {
387 /* Invalid amode argument. */
388 errno = EINVAL;
389 return -1;
390 }
[b08ef1fd]391}
392
[59f799b]393/**
[fb872c1]394 * Get configurable system variables.
[59f799b]395 *
[fb872c1]396 * @param name Variable name.
397 * @return Variable value.
[59f799b]398 */
399long posix_sysconf(int name)
400{
[fb872c1]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 }
[94f8b81c]407 if (cpu_stats) {
408 free(cpu_stats);
409 cpu_stats = 0;
410 }
[fb872c1]411
412 long phys_pages = 0;
413 long avphys_pages = 0;
414 stats_physmem_t *mem_stats = stats_get_physmem();
415 if (mem_stats) {
[582a0b8]416 phys_pages = (long) (mem_stats->total / posix_getpagesize());
417 avphys_pages = (long) (mem_stats->free / posix_getpagesize());
[94f8b81c]418 free(mem_stats);
419 mem_stats = 0;
[fb872c1]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:
[582a0b8]428 return posix_getpagesize();
[fb872c1]429 case _SC_CLK_TCK:
430 return clk_tck;
431 default:
432 errno = EINVAL;
433 return -1;
434 }
[59f799b]435}
436
[823a929]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();
[820104d]447 return -1;
[823a929]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();
[820104d]458 return -1;
[823a929]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();
[820104d]471 return -1;
[823a929]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();
[820104d]484 return -1;
[823a929]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();
[820104d]496 return -1;
[823a929]497}
498
[5759642f]499unsigned int posix_alarm(unsigned int seconds)
500{
501 not_implemented();
502 return 0;
503}
504
[4f4b4e7]505/** @}
506 */
Note: See TracBrowser for help on using the repository browser.