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

Last change on this file since eec201d was ef84413, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

libposix: Correctly disambiguate other uses of off_t

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