source: mainline/uspace/lib/posix/source/unistd.c@ 8e3498b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e3498b was 8e3498b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

vfs_read/write() should return error code separately from number of bytes transferred.

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