source: mainline/uspace/lib/posix/source/unistd.c@ ee5913c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ee5913c was fdf97f6, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Libposix functions are without posix_ prefix

Prior this commit, libposix headers declared all functions as posix_*
and used macros to rename e.g. strncpy to posix_strncpy in all (ported)
sources.

After this change, libposix headers look as normal POSIX compliant headers
(well, almost) and no renaming is done in the source codei (of the ported
applications). Instead, the renaming is done at object files level to
bypass weird problems that are bound to happen if you use macros.

The scheme is following. libposix headers use special macro to declare
the names. When included from outside, the functions have their normal
(standard) names. When included from the libposix sources, posix_ prefix
is added. Thus, when libposix is compiled and linked, it contains the
posix_* naming while compiling of ported software uses the normal
non-prefixed versions. This way the posix_* can use HelenOS libc without
any problem. Before linking, the posix_* prefix is removed from all
symbols and special prefix helenos_libc_ is added to all functions
that exists in our (HelenOS) libc and its name clashes with the POSIX
one.

The following happens, for example, to the open() function that exists in
both libposix and in libc.

  • Headers and sources of libc are left intact.
  • Copy of libc.a is made and to all clashing functions is added the helenos_libc prefix. This library is called libc4posix.a.
  • POSIX_DEF(open)(const char *) is used in libposix headers. This macro expands to plain open when included from the "outside world". But it expands to posix_open when included from libposix sources.
  • Libposix is compiled and linked, containing posix_open() that internally calls open() [the original one from libc].
  • Libposix is transformed - all open() are replaced with prefix variant: helenos_libc_open() and all posix_open() are replaced with open(). The transformed library is stored as libposixaslibc.a

Binutils and PCC are then linked with libc4posix and libposixaslibc
libraries instead of libc and libposix as was done previously.

WARNING: it looks that binutils, PCC and MSIM still works but not all
architectures were tested.

  • Property mode set to 100644
File size: 9.3 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/stats.h"
48#include "libc/malloc.h"
49
50/* Array of environment variable strings (NAME=VALUE). */
51char **posix_environ = NULL;
52char *posix_optarg;
53
54/**
55 * Get current user name.
56 *
57 * @return User name (static) string or NULL if not found.
58 */
59char *posix_getlogin(void)
60{
61 /* There is currently no support for user accounts in HelenOS. */
62 return (char *) "user";
63}
64
65/**
66 * Get current user name.
67 *
68 * @param name Pointer to a user supplied buffer.
69 * @param namesize Length of the buffer.
70 * @return Zero on success, error code otherwise.
71 */
72int posix_getlogin_r(char *name, size_t namesize)
73{
74 /* There is currently no support for user accounts in HelenOS. */
75 if (namesize >= 5) {
76 posix_strcpy(name, (char *) "user");
77 return 0;
78 } else {
79 errno = ERANGE;
80 return -1;
81 }
82}
83
84/**
85 * Test whether open file descriptor is associated with a terminal.
86 *
87 * @param fd Open file descriptor to test.
88 * @return Boolean result of the test.
89 */
90int posix_isatty(int fd)
91{
92 // TODO
93 /* Always returns false, because there is no easy way to find
94 * out under HelenOS. */
95 return 0;
96}
97
98/**
99 * Get the pathname of the current working directory.
100 *
101 * @param buf Buffer into which the pathname shall be put.
102 * @param size Size of the buffer.
103 * @return Buffer pointer on success, NULL on failure.
104 */
105char *posix_getcwd(char *buf, size_t size)
106{
107 /* Native getcwd() does not set any errno despite the fact that general
108 * usage pattern of this function depends on it (caller is repeatedly
109 * guessing the required size of the buffer by checking for ERANGE on
110 * failure). */
111 if (size == 0) {
112 errno = EINVAL;
113 return NULL;
114 }
115
116 /* Save the original value to comply with the "no modification on
117 * success" semantics.
118 */
119 int orig_errno = errno;
120 errno = EOK;
121
122 char *ret = getcwd(buf, size);
123 if (ret == NULL) {
124 /* Check errno to avoid shadowing other possible errors. */
125 if (errno == EOK) {
126 errno = ERANGE;
127 }
128 } else {
129 /* Success, restore previous errno value. */
130 errno = orig_errno;
131 }
132
133 return ret;
134}
135
136/**
137 * Change the current working directory.
138 *
139 * @param path New working directory.
140 */
141int posix_chdir(const char *path)
142{
143 return errnify(chdir, path);
144}
145
146/**
147 * Determine the page size of the current run of the process.
148 *
149 * @return Page size of the process.
150 */
151int posix_getpagesize(void)
152{
153 return getpagesize();
154}
155
156/**
157 * Get the process ID of the calling process.
158 *
159 * @return Process ID.
160 */
161posix_pid_t posix_getpid(void)
162{
163 return task_get_id();
164}
165
166/**
167 * Get the real user ID of the calling process.
168 *
169 * @return User ID.
170 */
171posix_uid_t posix_getuid(void)
172{
173 /* There is currently no support for user accounts in HelenOS. */
174 return 0;
175}
176
177/**
178 * Get the real group ID of the calling process.
179 *
180 * @return Group ID.
181 */
182posix_gid_t posix_getgid(void)
183{
184 /* There is currently no support for user accounts in HelenOS. */
185 return 0;
186}
187
188/**
189 * Close a file.
190 *
191 * @param fildes File descriptor of the opened file.
192 * @return 0 on success, -1 on error.
193 */
194int posix_close(int fildes)
195{
196 return errnify(close, fildes);
197}
198
199/**
200 * Read from a file.
201 *
202 * @param fildes File descriptor of the opened file.
203 * @param buf Buffer to which the read bytes shall be stored.
204 * @param nbyte Upper limit on the number of read bytes.
205 * @return Number of read bytes on success, -1 otherwise.
206 */
207ssize_t posix_read(int fildes, void *buf, size_t nbyte)
208{
209 return errnify(read, fildes, buf, nbyte);
210}
211
212/**
213 * Write to a file.
214 *
215 * @param fildes File descriptor of the opened file.
216 * @param buf Buffer to write.
217 * @param nbyte Size of the buffer.
218 * @return Number of written bytes on success, -1 otherwise.
219 */
220ssize_t posix_write(int fildes, const void *buf, size_t nbyte)
221{
222 return errnify(write, fildes, buf, nbyte);
223}
224
225/**
226 * Requests outstanding data to be written to the underlying storage device.
227 *
228 * @param fildes File descriptor of the opened file.
229 * @return Zero on success, -1 otherwise.
230 */
231int posix_fsync(int fildes)
232{
233 return errnify(fsync, fildes);
234}
235
236/**
237 * Truncate a file to a specified length.
238 *
239 * @param fildes File descriptor of the opened file.
240 * @param length New length of the file.
241 * @return Zero on success, -1 otherwise.
242 */
243int posix_ftruncate(int fildes, posix_off_t length)
244{
245 return errnify(ftruncate, fildes, (aoff64_t) length);
246}
247
248/**
249 * Remove a directory.
250 *
251 * @param path Directory pathname.
252 * @return Zero on success, -1 otherwise.
253 */
254int posix_rmdir(const char *path)
255{
256 return errnify(rmdir, path);
257}
258
259/**
260 * Remove a link to a file.
261 *
262 * @param path File pathname.
263 * @return Zero on success, -1 otherwise.
264 */
265int posix_unlink(const char *path)
266{
267 return errnify(unlink, path);
268}
269
270/**
271 * Duplicate an open file descriptor.
272 *
273 * @param fildes File descriptor to be duplicated.
274 * @return On success, new file descriptor for the same file, otherwise -1.
275 */
276int posix_dup(int fildes)
277{
278 return posix_fcntl(fildes, F_DUPFD, 0);
279}
280
281/**
282 * Duplicate an open file descriptor.
283 *
284 * @param fildes File descriptor to be duplicated.
285 * @param fildes2 File descriptor to be paired with the same file description
286 * as is paired fildes.
287 * @return fildes2 on success, -1 otherwise.
288 */
289int posix_dup2(int fildes, int fildes2)
290{
291 return errnify(dup2, fildes, fildes2);
292}
293
294/**
295 * Determine accessibility of a file.
296 *
297 * @param path File to check accessibility for.
298 * @param amode Either check for existence or intended access mode.
299 * @return Zero on success, -1 otherwise.
300 */
301int posix_access(const char *path, int amode)
302{
303 if (amode == F_OK || (amode & (X_OK | W_OK | R_OK))) {
304 /* HelenOS doesn't support permissions, permission checks
305 * are equal to existence check.
306 *
307 * Check file existence by attempting to open it.
308 */
309 int fd = open(path, O_RDONLY);
310 if (fd < 0) {
311 errno = -fd;
312 return -1;
313 }
314 close(fd);
315 return 0;
316 } else {
317 /* Invalid amode argument. */
318 errno = EINVAL;
319 return -1;
320 }
321}
322
323/**
324 * Get configurable system variables.
325 *
326 * @param name Variable name.
327 * @return Variable value.
328 */
329long posix_sysconf(int name)
330{
331 long clk_tck = 0;
332 size_t cpu_count = 0;
333 stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
334 if (cpu_stats && cpu_count > 0) {
335 clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
336 }
337 if (cpu_stats) {
338 free(cpu_stats);
339 cpu_stats = 0;
340 }
341
342 long phys_pages = 0;
343 long avphys_pages = 0;
344 stats_physmem_t *mem_stats = stats_get_physmem();
345 if (mem_stats) {
346 phys_pages = (long) (mem_stats->total / getpagesize());
347 avphys_pages = (long) (mem_stats->free / getpagesize());
348 free(mem_stats);
349 mem_stats = 0;
350 }
351
352 switch (name) {
353 case _SC_PHYS_PAGES:
354 return phys_pages;
355 case _SC_AVPHYS_PAGES:
356 return avphys_pages;
357 case _SC_PAGESIZE:
358 return getpagesize();
359 case _SC_CLK_TCK:
360 return clk_tck;
361 default:
362 errno = EINVAL;
363 return -1;
364 }
365}
366
367/**
368 *
369 * @param path
370 * @param name
371 * @return
372 */
373long posix_pathconf(const char *path, int name)
374{
375 // TODO: low priority, just a compile-time dependency of binutils
376 not_implemented();
377}
378
379/**
380 *
381 * @return
382 */
383posix_pid_t posix_fork(void)
384{
385 // TODO: low priority, just a compile-time dependency of binutils
386 not_implemented();
387}
388
389/**
390 *
391 * @param path
392 * @param argv
393 * @return
394 */
395int posix_execv(const char *path, char *const argv[])
396{
397 // TODO: low priority, just a compile-time dependency of binutils
398 not_implemented();
399}
400
401/**
402 *
403 * @param file
404 * @param argv
405 * @return
406 */
407int posix_execvp(const char *file, char *const argv[])
408{
409 // TODO: low priority, just a compile-time dependency of binutils
410 not_implemented();
411}
412
413/**
414 *
415 * @param fildes
416 * @return
417 */
418int posix_pipe(int fildes[2])
419{
420 // TODO: low priority, just a compile-time dependency of binutils
421 not_implemented();
422}
423
424/** @}
425 */
Note: See TracBrowser for help on using the repository browser.