source: mainline/uspace/lib/posix/src/unistd.c@ db51219f

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

Privatize <thread.h>.

  • Property mode set to 100644
File size: 10.5 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
[9b1503e]36#include "internal/common.h"
[a3da2b2]37#include "posix/unistd.h"
[ec18957a]38
[0d0b319]39#include <errno.h>
40
[4e6a610]41#include "posix/dirent.h"
[a3da2b2]42#include "posix/string.h"
[4e6a610]43#include "posix/sys/types.h"
[a3da2b2]44#include "posix/fcntl.h"
[ec18957a]45
46#include "libc/task.h"
47#include "libc/stats.h"
[fb872c1]48#include "libc/malloc.h"
[58898d1d]49#include "libc/vfs/vfs.h"
50
[582a0b8]51#include <libarch/config.h>
52
[0d0b319]53// FIXME: replace with a hash table
[777832e]54aoff64_t posix_pos[VFS_MAX_OPEN_FILES];
[a16210b5]55
[b4d6252]56/* Array of environment variable strings (NAME=VALUE). */
[04552d8e]57char **environ = NULL;
[b4d6252]58
[1165a419]59/**
60 * Sleep for the specified number of seconds.
61 *
62 * Note that POSIX allows this call to be interrupted and then the return
63 * value represents remaining seconds for the sleep. HelenOS does not offer
64 * such functionality and thus always the whole sleep is taken.
65 *
66 * @param seconds Number of seconds to sleep.
67 * @return Always 0 on HelenOS.
68 */
[7f9df7b9]69unsigned int sleep(unsigned int seconds)
[1165a419]70{
[38d8849]71 fibril_sleep(seconds);
72 return 0;
[1165a419]73}
74
[244d6fd]75/**
76 * Get current user name.
[fb872c1]77 *
78 * @return User name (static) string or NULL if not found.
[244d6fd]79 */
[7f9df7b9]80char *getlogin(void)
[244d6fd]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 */
[7f9df7b9]93int getlogin_r(char *name, size_t namesize)
[244d6fd]94{
[fb872c1]95 /* There is currently no support for user accounts in HelenOS. */
96 if (namesize >= 5) {
[7f9df7b9]97 strcpy(name, (char *) "user");
[fb872c1]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 */
[7f9df7b9]111int isatty(int fd)
[4f4b4e7]112{
[4c8f5e7]113 // TODO
[7c3fb9b]114 /*
115 * Always returns false, because there is no easy way to find
116 * out under HelenOS.
117 */
[94f8b81c]118 return 0;
[a16210b5]119}
120
[221afc9e]121/**
122 * Get the pathname of the current working directory.
123 *
124 * @param buf Buffer into which the pathname shall be put.
125 * @param size Size of the buffer.
126 * @return Buffer pointer on success, NULL on failure.
127 */
[7f9df7b9]128char *getcwd(char *buf, size_t size)
[221afc9e]129{
[1b20da0]130 if (failed(vfs_cwd_get(buf, size)))
[221afc9e]131 return NULL;
[d96d9bc]132 return buf;
[221afc9e]133}
134
[75406dc]135/**
136 * Change the current working directory.
137 *
138 * @param path New working directory.
139 */
[7f9df7b9]140int chdir(const char *path)
[75406dc]141{
[0d0b319]142 if (failed(vfs_cwd_set(path)))
[d96d9bc]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 */
[7f9df7b9]152int getpagesize(void)
[72ec8cc]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 */
[7f9df7b9]162pid_t getpid(void)
[823a929]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 */
[7f9df7b9]172uid_t getuid(void)
[59f799b]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.
[1b20da0]180 *
[fb872c1]181 * @return Group ID.
[59f799b]182 */
[7f9df7b9]183gid_t getgid(void)
[59f799b]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 */
[7f9df7b9]195int close(int fildes)
[75406dc]196{
[58898d1d]197 posix_pos[fildes] = 0;
[0d0b319]198 if (failed(vfs_put(fildes)))
[9c4cf0d]199 return -1;
200 else
201 return 0;
[75406dc]202}
203
[221afc9e]204/**
205 * Read from a file.
206 *
207 * @param fildes File descriptor of the opened file.
208 * @param buf Buffer to which the read bytes shall be stored.
209 * @param nbyte Upper limit on the number of read bytes.
210 * @return Number of read bytes on success, -1 otherwise.
211 */
[7f9df7b9]212ssize_t read(int fildes, void *buf, size_t nbyte)
[221afc9e]213{
[8e3498b]214 size_t nread;
[0d0b319]215 if (failed(vfs_read(fildes, &posix_pos[fildes], buf, nbyte, &nread)))
[ce04ea44]216 return -1;
[8e3498b]217 return (ssize_t) nread;
[75406dc]218}
219
220/**
221 * Write to a file.
222 *
223 * @param fildes File descriptor of the opened file.
224 * @param buf Buffer to write.
225 * @param nbyte Size of the buffer.
226 * @return Number of written bytes on success, -1 otherwise.
227 */
[7f9df7b9]228ssize_t write(int fildes, const void *buf, size_t nbyte)
[75406dc]229{
[8e3498b]230 size_t nwr;
[0d0b319]231 if (failed(vfs_write(fildes, &posix_pos[fildes], buf, nbyte, &nwr)))
[ce04ea44]232 return -1;
[8e3498b]233 return nwr;
[75406dc]234}
235
[e3480d5]236/**
237 * Reposition read/write file offset
238 *
239 * @param fildes File descriptor of the opened file.
[bf963b9]240 * @param offset New offset in the file.
241 * @param whence The position from which the offset argument is specified.
[e3480d5]242 * @return Upon successful completion, returns the resulting offset
243 * as measured in bytes from the beginning of the file, -1 otherwise.
244 */
[7f9df7b9]245off_t lseek(int fildes, off_t offset, int whence)
[e3480d5]246{
[39330200]247 vfs_stat_t st;
[58898d1d]248
249 switch (whence) {
250 case SEEK_SET:
251 posix_pos[fildes] = offset;
252 break;
253 case SEEK_CUR:
254 posix_pos[fildes] += offset;
255 break;
256 case SEEK_END:
[0d0b319]257 if (failed(vfs_stat(fildes, &st)))
[58898d1d]258 return -1;
259 posix_pos[fildes] = st.size + offset;
260 break;
261 }
[59f388a]262 if (posix_pos[fildes] > INT64_MAX) {
263 /* The native width is too large for the POSIX interface. */
264 errno = ERANGE;
265 return -1;
266 }
[58898d1d]267 return posix_pos[fildes];
[e3480d5]268}
269
[75406dc]270/**
271 * Requests outstanding data to be written to the underlying storage device.
272 *
[2a53f71]273 * @param fildes File descriptor of the opened file.
274 * @return Zero on success, -1 otherwise.
[75406dc]275 */
[7f9df7b9]276int fsync(int fildes)
[75406dc]277{
[0d0b319]278 if (failed(vfs_sync(fildes)))
[a56cef9]279 return -1;
280 else
281 return 0;
[75406dc]282}
283
[2a53f71]284/**
285 * Truncate a file to a specified length.
286 *
287 * @param fildes File descriptor of the opened file.
288 * @param length New length of the file.
289 * @return Zero on success, -1 otherwise.
290 */
[7f9df7b9]291int ftruncate(int fildes, off_t length)
[75406dc]292{
[0d0b319]293 if (failed(vfs_resize(fildes, (aoff64_t) length)))
[67e881c]294 return -1;
295 else
296 return 0;
[221afc9e]297}
298
[58115ae]299/**
300 * Remove a directory.
301 *
302 * @param path Directory pathname.
303 * @return Zero on success, -1 otherwise.
304 */
[7f9df7b9]305int rmdir(const char *path)
[58115ae]306{
[0d0b319]307 if (failed(vfs_unlink_path(path)))
[79ea5af]308 return -1;
309 else
310 return 0;
[58115ae]311}
312
[221afc9e]313/**
314 * Remove a link to a file.
[1b20da0]315 *
[221afc9e]316 * @param path File pathname.
317 * @return Zero on success, -1 otherwise.
318 */
[7f9df7b9]319int unlink(const char *path)
[221afc9e]320{
[0d0b319]321 if (failed(vfs_unlink_path(path)))
[79ea5af]322 return -1;
323 else
324 return 0;
[75406dc]325}
326
[2a53f71]327/**
328 * Duplicate an open file descriptor.
329 *
330 * @param fildes File descriptor to be duplicated.
331 * @return On success, new file descriptor for the same file, otherwise -1.
332 */
[7f9df7b9]333int dup(int fildes)
[75406dc]334{
[7f9df7b9]335 return fcntl(fildes, F_DUPFD, 0);
[75406dc]336}
337
[2a53f71]338/**
339 * Duplicate an open file descriptor.
[1b20da0]340 *
[2a53f71]341 * @param fildes File descriptor to be duplicated.
342 * @param fildes2 File descriptor to be paired with the same file description
343 * as is paired fildes.
344 * @return fildes2 on success, -1 otherwise.
345 */
[7f9df7b9]346int dup2(int fildes, int fildes2)
[75406dc]347{
[f77c1c9]348 int file;
[0d0b319]349 if (failed(vfs_clone(fildes, fildes2, false, &file))) {
[f77c1c9]350 return -1;
351 }
352 return file;
[221afc9e]353}
354
[b08ef1fd]355/**
[fb872c1]356 * Determine accessibility of a file.
357 *
358 * @param path File to check accessibility for.
359 * @param amode Either check for existence or intended access mode.
360 * @return Zero on success, -1 otherwise.
[b08ef1fd]361 */
[7f9df7b9]362int access(const char *path, int amode)
[b08ef1fd]363{
[75406dc]364 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
[7c3fb9b]365 /*
366 * HelenOS doesn't support permissions, permission checks
[75406dc]367 * are equal to existence check.
368 *
369 * Check file existence by attempting to open it.
370 */
[7f9df7b9]371 int fd = open(path, O_RDONLY);
[4e6a610]372 if (fd >= 0) {
373 close(fd);
374 return 0;
375 }
376 DIR *dir = opendir(path);
377 if (dir != NULL) {
378 closedir(dir);
379 return 0;
380 }
381 return -1;
[fb872c1]382 } else {
383 /* Invalid amode argument. */
384 errno = EINVAL;
385 return -1;
386 }
[b08ef1fd]387}
388
[59f799b]389/**
[fb872c1]390 * Get configurable system variables.
[1b20da0]391 *
[fb872c1]392 * @param name Variable name.
393 * @return Variable value.
[59f799b]394 */
[7f9df7b9]395long sysconf(int name)
[59f799b]396{
[fb872c1]397 long clk_tck = 0;
398 size_t cpu_count = 0;
399 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
400 if (cpu_stats && cpu_count > 0) {
401 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
402 }
[94f8b81c]403 if (cpu_stats) {
404 free(cpu_stats);
405 cpu_stats = 0;
406 }
[fb872c1]407
408 long phys_pages = 0;
409 long avphys_pages = 0;
410 stats_physmem_t *mem_stats = stats_get_physmem();
411 if (mem_stats) {
[7f9df7b9]412 phys_pages = (long) (mem_stats->total / getpagesize());
413 avphys_pages = (long) (mem_stats->free / getpagesize());
[94f8b81c]414 free(mem_stats);
415 mem_stats = 0;
[fb872c1]416 }
417
418 switch (name) {
419 case _SC_PHYS_PAGES:
420 return phys_pages;
421 case _SC_AVPHYS_PAGES:
422 return avphys_pages;
423 case _SC_PAGESIZE:
[7f9df7b9]424 return getpagesize();
[fb872c1]425 case _SC_CLK_TCK:
426 return clk_tck;
427 default:
428 errno = EINVAL;
429 return -1;
430 }
[59f799b]431}
432
[823a929]433/**
[1b20da0]434 *
[823a929]435 * @param path
436 * @param name
437 * @return
438 */
[7f9df7b9]439long pathconf(const char *path, int name)
[823a929]440{
441 // TODO: low priority, just a compile-time dependency of binutils
442 not_implemented();
[820104d]443 return -1;
[823a929]444}
445
446/**
[1b20da0]447 *
[823a929]448 * @return
449 */
[7f9df7b9]450pid_t fork(void)
[823a929]451{
452 // TODO: low priority, just a compile-time dependency of binutils
453 not_implemented();
[820104d]454 return -1;
[823a929]455}
456
457/**
[1b20da0]458 *
[823a929]459 * @param path
460 * @param argv
461 * @return
462 */
[7f9df7b9]463int execv(const char *path, char *const argv[])
[823a929]464{
465 // TODO: low priority, just a compile-time dependency of binutils
466 not_implemented();
[820104d]467 return -1;
[823a929]468}
469
470/**
[1b20da0]471 *
[823a929]472 * @param file
473 * @param argv
474 * @return
475 */
[7f9df7b9]476int execvp(const char *file, char *const argv[])
[823a929]477{
478 // TODO: low priority, just a compile-time dependency of binutils
479 not_implemented();
[820104d]480 return -1;
[823a929]481}
482
483/**
[1b20da0]484 *
[823a929]485 * @param fildes
486 * @return
487 */
[7f9df7b9]488int pipe(int fildes[2])
[823a929]489{
490 // TODO: low priority, just a compile-time dependency of binutils
491 not_implemented();
[820104d]492 return -1;
[823a929]493}
494
[7f9df7b9]495unsigned int alarm(unsigned int seconds)
[5759642f]496{
497 not_implemented();
498 return 0;
499}
500
[4f4b4e7]501/** @}
502 */
Note: See TracBrowser for help on using the repository browser.