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