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
Line 
1/*
2 * Copyright (c) 2011 Jiri Zarevucky
3 * Copyright (c) 2011 Petr Koupy
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
30/** @addtogroup libposix
31 * @{
32 */
33/** @file Miscellaneous standard definitions.
34 */
35
36#define LIBPOSIX_INTERNAL
37#define __POSIX_DEF__(x) posix_##x
38
39#include "internal/common.h"
40#include "posix/unistd.h"
41
42#include "posix/errno.h"
43#include "posix/string.h"
44#include "posix/fcntl.h"
45
46#include "libc/task.h"
47#include "libc/thread.h"
48#include "libc/stats.h"
49#include "libc/malloc.h"
50#include "libc/vfs/vfs.h"
51
52#include <libarch/config.h>
53
54aoff64_t posix_pos[MAX_OPEN_FILES];
55
56/* Array of environment variable strings (NAME=VALUE). */
57char **posix_environ = NULL;
58char *posix_optarg;
59
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
75/**
76 * Get current user name.
77 *
78 * @return User name (static) string or NULL if not found.
79 */
80char *posix_getlogin(void)
81{
82 /* There is currently no support for user accounts in HelenOS. */
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.
91 * @return Zero on success, error code otherwise.
92 */
93int posix_getlogin_r(char *name, size_t namesize)
94{
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 }
103}
104
105/**
106 * Test whether open file descriptor is associated with a terminal.
107 *
108 * @param fd Open file descriptor to test.
109 * @return Boolean result of the test.
110 */
111int posix_isatty(int fd)
112{
113 // TODO
114 /* Always returns false, because there is no easy way to find
115 * out under HelenOS. */
116 return 0;
117}
118
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{
128 int rc = rcerrno(vfs_cwd_get, buf, size);
129 if (rc != EOK)
130 return NULL;
131 return buf;
132}
133
134/**
135 * Change the current working directory.
136 *
137 * @param path New working directory.
138 */
139int posix_chdir(const char *path)
140{
141 int rc = rcerrno(vfs_cwd_set, path);
142 if (rc != EOK)
143 return -1;
144 return 0;
145}
146
147/**
148 * Determine the page size of the current run of the process.
149 *
150 * @return Page size of the process.
151 */
152int posix_getpagesize(void)
153{
154 return PAGE_SIZE;
155}
156
157/**
158 * Get the process ID of the calling process.
159 *
160 * @return Process ID.
161 */
162posix_pid_t posix_getpid(void)
163{
164 return task_get_id();
165}
166
167/**
168 * Get the real user ID of the calling process.
169 *
170 * @return User ID.
171 */
172posix_uid_t posix_getuid(void)
173{
174 /* There is currently no support for user accounts in HelenOS. */
175 return 0;
176}
177
178/**
179 * Get the real group ID of the calling process.
180 *
181 * @return Group ID.
182 */
183posix_gid_t posix_getgid(void)
184{
185 /* There is currently no support for user accounts in HelenOS. */
186 return 0;
187}
188
189/**
190 * Close a file.
191 *
192 * @param fildes File descriptor of the opened file.
193 * @return 0 on success, -1 on error.
194 */
195int posix_close(int fildes)
196{
197 posix_pos[fildes] = 0;
198 int rc = rcerrno(vfs_put, fildes);
199 if (rc != EOK)
200 return -1;
201 else
202 return 0;
203}
204
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{
215 ssize_t size = rcerrno(vfs_read, fildes, &posix_pos[fildes], buf, nbyte);
216 if (size < 0)
217 return -1;
218 return size;
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{
231 ssize_t size = rcerrno(vfs_write, fildes, &posix_pos[fildes], buf, nbyte);
232 if (size < 0)
233 return -1;
234 return size;
235}
236
237/**
238 * Reposition read/write file offset
239 *
240 * @param fildes File descriptor of the opened file.
241 * @param offset New offset in the file.
242 * @param whence The position from which the offset argument is specified.
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{
248 struct stat st;
249 int rc;
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:
259 rc = rcerrno(vfs_stat, fildes, &st);
260 if (rc != EOK)
261 return -1;
262 posix_pos[fildes] = st.size + offset;
263 break;
264 }
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 }
270 return posix_pos[fildes];
271}
272
273/**
274 * Requests outstanding data to be written to the underlying storage device.
275 *
276 * @param fildes File descriptor of the opened file.
277 * @return Zero on success, -1 otherwise.
278 */
279int posix_fsync(int fildes)
280{
281 if (rcerrno(vfs_sync, fildes) != EOK)
282 return -1;
283 else
284 return 0;
285}
286
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 */
294int posix_ftruncate(int fildes, posix_off_t length)
295{
296 if (rcerrno(vfs_resize, fildes, (aoff64_t) length) != EOK)
297 return -1;
298 else
299 return 0;
300}
301
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{
310 if (rcerrno(vfs_unlink_path, path) != EOK)
311 return -1;
312 else
313 return 0;
314}
315
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{
324 if (rcerrno(vfs_unlink_path, path) != EOK)
325 return -1;
326 else
327 return 0;
328}
329
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 */
336int posix_dup(int fildes)
337{
338 return posix_fcntl(fildes, F_DUPFD, 0);
339}
340
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 */
349int posix_dup2(int fildes, int fildes2)
350{
351 return negerrno(vfs_clone, fildes, fildes2, false);
352}
353
354/**
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.
360 */
361int posix_access(const char *path, int amode)
362{
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 */
369 int fd = posix_open(path, O_RDONLY);
370 if (fd < 0)
371 return -1;
372 posix_close(fd);
373 return 0;
374 } else {
375 /* Invalid amode argument. */
376 errno = EINVAL;
377 return -1;
378 }
379}
380
381/**
382 * Get configurable system variables.
383 *
384 * @param name Variable name.
385 * @return Variable value.
386 */
387long posix_sysconf(int name)
388{
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 }
395 if (cpu_stats) {
396 free(cpu_stats);
397 cpu_stats = 0;
398 }
399
400 long phys_pages = 0;
401 long avphys_pages = 0;
402 stats_physmem_t *mem_stats = stats_get_physmem();
403 if (mem_stats) {
404 phys_pages = (long) (mem_stats->total / posix_getpagesize());
405 avphys_pages = (long) (mem_stats->free / posix_getpagesize());
406 free(mem_stats);
407 mem_stats = 0;
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:
416 return posix_getpagesize();
417 case _SC_CLK_TCK:
418 return clk_tck;
419 default:
420 errno = EINVAL;
421 return -1;
422 }
423}
424
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();
435 return -1;
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();
446 return -1;
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();
459 return -1;
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();
472 return -1;
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();
484 return -1;
485}
486
487unsigned int posix_alarm(unsigned int seconds)
488{
489 not_implemented();
490 return 0;
491}
492
493/** @}
494 */
Note: See TracBrowser for help on using the repository browser.