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

Last change on this file was 44e8541, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Move stdlib.h and some of its function into /common

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