source: mainline/uspace/lib/posix/source/unistd.c@ 40feeac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 40feeac was 1165a419, checked in by Vojtech Horky <vojtechhorky@…>, 8 years ago

libposix: add sleep() function (needed by GCC)

  • Property mode set to 100644
File size: 10.7 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{
[ce04ea44]215 ssize_t size = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte);
216 if (size < 0)
217 return -1;
218 return size;
[75406dc]219}
220
221/**
222 * Write to a file.
223 *
224 * @param fildes File descriptor of the opened file.
225 * @param buf Buffer to write.
226 * @param nbyte Size of the buffer.
227 * @return Number of written bytes on success, -1 otherwise.
228 */
229ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
230{
[ce04ea44]231 ssize_t size = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte);
232 if (size < 0)
233 return -1;
234 return size;
[75406dc]235}
236
[e3480d5]237/**
238 * Reposition read/write file offset
239 *
240 * @param fildes File descriptor of the opened file.
[bf963b9]241 * @param offset New offset in the file.
242 * @param whence The position from which the offset argument is specified.
[e3480d5]243 * @return Upon successful completion, returns the resulting offset
244 * as measured in bytes from the beginning of the file, -1 otherwise.
245 */
246posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence)
247{
[58898d1d]248 struct stat st;
[23a0368]249 int rc;
[58898d1d]250
251 switch (whence) {
252 case SEEK_SET:
253 posix_pos[fildes] = offset;
254 break;
255 case SEEK_CUR:
256 posix_pos[fildes] += offset;
257 break;
258 case SEEK_END:
[23a0368]259 rc = rcerrno(vfs_stat, fildes, &st);
260 if (rc != EOK)
[58898d1d]261 return -1;
262 posix_pos[fildes] = st.size + offset;
263 break;
264 }
[59f388a]265 if (posix_pos[fildes] > INT64_MAX) {
266 /* The native width is too large for the POSIX interface. */
267 errno = ERANGE;
268 return -1;
269 }
[58898d1d]270 return posix_pos[fildes];
[e3480d5]271}
272
[75406dc]273/**
274 * Requests outstanding data to be written to the underlying storage device.
275 *
[2a53f71]276 * @param fildes File descriptor of the opened file.
277 * @return Zero on success, -1 otherwise.
[75406dc]278 */
279int posix_fsync(int fildes)
280{
[a56cef9]281 if (rcerrno(vfs_sync, fildes) != EOK)
282 return -1;
283 else
284 return 0;
[75406dc]285}
286
[2a53f71]287/**
288 * Truncate a file to a specified length.
289 *
290 * @param fildes File descriptor of the opened file.
291 * @param length New length of the file.
292 * @return Zero on success, -1 otherwise.
293 */
[75406dc]294int posix_ftruncate(int fildes, posix_off_t length)
295{
[67e881c]296 if (rcerrno(vfs_resize, fildes, (aoff64_t) length) != EOK)
297 return -1;
298 else
299 return 0;
[221afc9e]300}
301
[58115ae]302/**
303 * Remove a directory.
304 *
305 * @param path Directory pathname.
306 * @return Zero on success, -1 otherwise.
307 */
308int posix_rmdir(const char *path)
309{
[79ea5af]310 if (rcerrno(vfs_unlink_path, path) != EOK)
311 return -1;
312 else
313 return 0;
[58115ae]314}
315
[221afc9e]316/**
317 * Remove a link to a file.
318 *
319 * @param path File pathname.
320 * @return Zero on success, -1 otherwise.
321 */
322int posix_unlink(const char *path)
323{
[79ea5af]324 if (rcerrno(vfs_unlink_path, path) != EOK)
325 return -1;
326 else
327 return 0;
[75406dc]328}
329
[2a53f71]330/**
331 * Duplicate an open file descriptor.
332 *
333 * @param fildes File descriptor to be duplicated.
334 * @return On success, new file descriptor for the same file, otherwise -1.
335 */
[75406dc]336int posix_dup(int fildes)
337{
338 return posix_fcntl(fildes, F_DUPFD, 0);
339}
340
[2a53f71]341/**
342 * Duplicate an open file descriptor.
343 *
344 * @param fildes File descriptor to be duplicated.
345 * @param fildes2 File descriptor to be paired with the same file description
346 * as is paired fildes.
347 * @return fildes2 on success, -1 otherwise.
348 */
[75406dc]349int posix_dup2(int fildes, int fildes2)
350{
[fcab7ef]351 return negerrno(vfs_clone, fildes, fildes2, false);
[221afc9e]352}
353
[b08ef1fd]354/**
[fb872c1]355 * Determine accessibility of a file.
356 *
357 * @param path File to check accessibility for.
358 * @param amode Either check for existence or intended access mode.
359 * @return Zero on success, -1 otherwise.
[b08ef1fd]360 */
361int posix_access(const char *path, int amode)
362{
[75406dc]363 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
364 /* HelenOS doesn't support permissions, permission checks
365 * are equal to existence check.
366 *
367 * Check file existence by attempting to open it.
368 */
[b19e892]369 int fd = posix_open(path, O_RDONLY);
370 if (fd < 0)
[955c2b0]371 return -1;
[9c4cf0d]372 posix_close(fd);
[955c2b0]373 return 0;
[fb872c1]374 } else {
375 /* Invalid amode argument. */
376 errno = EINVAL;
377 return -1;
378 }
[b08ef1fd]379}
380
[59f799b]381/**
[fb872c1]382 * Get configurable system variables.
[59f799b]383 *
[fb872c1]384 * @param name Variable name.
385 * @return Variable value.
[59f799b]386 */
387long posix_sysconf(int name)
388{
[fb872c1]389 long clk_tck = 0;
390 size_t cpu_count = 0;
391 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
392 if (cpu_stats && cpu_count > 0) {
393 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
394 }
[94f8b81c]395 if (cpu_stats) {
396 free(cpu_stats);
397 cpu_stats = 0;
398 }
[fb872c1]399
400 long phys_pages = 0;
401 long avphys_pages = 0;
402 stats_physmem_t *mem_stats = stats_get_physmem();
403 if (mem_stats) {
[582a0b8]404 phys_pages = (long) (mem_stats->total / posix_getpagesize());
405 avphys_pages = (long) (mem_stats->free / posix_getpagesize());
[94f8b81c]406 free(mem_stats);
407 mem_stats = 0;
[fb872c1]408 }
409
410 switch (name) {
411 case _SC_PHYS_PAGES:
412 return phys_pages;
413 case _SC_AVPHYS_PAGES:
414 return avphys_pages;
415 case _SC_PAGESIZE:
[582a0b8]416 return posix_getpagesize();
[fb872c1]417 case _SC_CLK_TCK:
418 return clk_tck;
419 default:
420 errno = EINVAL;
421 return -1;
422 }
[59f799b]423}
424
[823a929]425/**
426 *
427 * @param path
428 * @param name
429 * @return
430 */
431long posix_pathconf(const char *path, int name)
432{
433 // TODO: low priority, just a compile-time dependency of binutils
434 not_implemented();
[820104d]435 return -1;
[823a929]436}
437
438/**
439 *
440 * @return
441 */
442posix_pid_t posix_fork(void)
443{
444 // TODO: low priority, just a compile-time dependency of binutils
445 not_implemented();
[820104d]446 return -1;
[823a929]447}
448
449/**
450 *
451 * @param path
452 * @param argv
453 * @return
454 */
455int posix_execv(const char *path, char *const argv[])
456{
457 // TODO: low priority, just a compile-time dependency of binutils
458 not_implemented();
[820104d]459 return -1;
[823a929]460}
461
462/**
463 *
464 * @param file
465 * @param argv
466 * @return
467 */
468int posix_execvp(const char *file, char *const argv[])
469{
470 // TODO: low priority, just a compile-time dependency of binutils
471 not_implemented();
[820104d]472 return -1;
[823a929]473}
474
475/**
476 *
477 * @param fildes
478 * @return
479 */
480int posix_pipe(int fildes[2])
481{
482 // TODO: low priority, just a compile-time dependency of binutils
483 not_implemented();
[820104d]484 return -1;
[823a929]485}
486
[5759642f]487unsigned int posix_alarm(unsigned int seconds)
488{
489 not_implemented();
490 return 0;
491}
492
[4f4b4e7]493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.