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
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define LIBPOSIX_INTERNAL
|
---|
37 |
|
---|
38 | #include "internal/common.h"
|
---|
39 | #include "unistd.h"
|
---|
40 | #include <task.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include "string.h"
|
---|
43 | #include "fcntl.h"
|
---|
44 | #include <stats.h>
|
---|
45 | #include "libc/malloc.h"
|
---|
46 |
|
---|
47 | /* Array of environment variable strings (NAME=VALUE). */
|
---|
48 | char **posix_environ = NULL;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Get current user name.
|
---|
52 | *
|
---|
53 | * @return User name (static) string or NULL if not found.
|
---|
54 | */
|
---|
55 | char *posix_getlogin(void)
|
---|
56 | {
|
---|
57 | /* There is currently no support for user accounts in HelenOS. */
|
---|
58 | return (char *) "user";
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Get current user name.
|
---|
63 | *
|
---|
64 | * @param name Pointer to a user supplied buffer.
|
---|
65 | * @param namesize Length of the buffer.
|
---|
66 | * @return Zero on success, error code otherwise.
|
---|
67 | */
|
---|
68 | int posix_getlogin_r(char *name, size_t namesize)
|
---|
69 | {
|
---|
70 | /* There is currently no support for user accounts in HelenOS. */
|
---|
71 | if (namesize >= 5) {
|
---|
72 | posix_strcpy(name, (char *) "user");
|
---|
73 | return 0;
|
---|
74 | } else {
|
---|
75 | errno = ERANGE;
|
---|
76 | return -1;
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Test whether open file descriptor is associated with a terminal.
|
---|
82 | *
|
---|
83 | * @param fd Open file descriptor to test.
|
---|
84 | * @return Boolean result of the test.
|
---|
85 | */
|
---|
86 | int posix_isatty(int fd)
|
---|
87 | {
|
---|
88 | /* Always returns false, because there is no easy way to find
|
---|
89 | * out under HelenOS. */
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Determine the page size of the current run of the process.
|
---|
95 | *
|
---|
96 | * @return Page size of the process.
|
---|
97 | */
|
---|
98 | int posix_getpagesize(void)
|
---|
99 | {
|
---|
100 | return getpagesize();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Get the process ID of the calling process.
|
---|
105 | *
|
---|
106 | * @return Process ID.
|
---|
107 | */
|
---|
108 | posix_pid_t posix_getpid(void)
|
---|
109 | {
|
---|
110 | return task_get_id();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Get the real user ID of the calling process.
|
---|
115 | *
|
---|
116 | * @return User ID.
|
---|
117 | */
|
---|
118 | posix_uid_t posix_getuid(void)
|
---|
119 | {
|
---|
120 | /* There is currently no support for user accounts in HelenOS. */
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Get the real group ID of the calling process.
|
---|
126 | *
|
---|
127 | * @return Group ID.
|
---|
128 | */
|
---|
129 | posix_gid_t posix_getgid(void)
|
---|
130 | {
|
---|
131 | /* There is currently no support for user accounts in HelenOS. */
|
---|
132 | return 0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Determine accessibility of a file.
|
---|
137 | *
|
---|
138 | * @param path File to check accessibility for.
|
---|
139 | * @param amode Either check for existence or intended access mode.
|
---|
140 | * @return Zero on success, -1 otherwise.
|
---|
141 | */
|
---|
142 | int posix_access(const char *path, int amode)
|
---|
143 | {
|
---|
144 | if (amode == F_OK) {
|
---|
145 | /* Check file existence by attempt to open it. */
|
---|
146 | int fd = open(path, O_RDONLY);
|
---|
147 | // TODO: propagate a POSIX compatible errno
|
---|
148 | int rc = fd < 0 ? -1 : 0;
|
---|
149 | close(fd);
|
---|
150 | return rc;
|
---|
151 | } else if (amode & (X_OK | W_OK | R_OK)) {
|
---|
152 | /* HelenOS doesn't support permissions, return success. */
|
---|
153 | return 0;
|
---|
154 | } else {
|
---|
155 | /* Invalid amode argument. */
|
---|
156 | errno = EINVAL;
|
---|
157 | return -1;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Get configurable system variables.
|
---|
163 | *
|
---|
164 | * @param name Variable name.
|
---|
165 | * @return Variable value.
|
---|
166 | */
|
---|
167 | long posix_sysconf(int name)
|
---|
168 | {
|
---|
169 | long clk_tck = 0;
|
---|
170 | size_t cpu_count = 0;
|
---|
171 | stats_cpu_t *cpu_stats = stats_get_cpus(&cpu_count);
|
---|
172 | if (cpu_stats && cpu_count > 0) {
|
---|
173 | clk_tck = ((long) cpu_stats[0].frequency_mhz) * 1000000L;
|
---|
174 | }
|
---|
175 | free(cpu_stats);
|
---|
176 | cpu_stats = 0;
|
---|
177 |
|
---|
178 | long phys_pages = 0;
|
---|
179 | long avphys_pages = 0;
|
---|
180 | stats_physmem_t *mem_stats = stats_get_physmem();
|
---|
181 | if (mem_stats) {
|
---|
182 | phys_pages = (long) (mem_stats->total / getpagesize());
|
---|
183 | avphys_pages = (long) (mem_stats->free / getpagesize());
|
---|
184 | }
|
---|
185 | free(mem_stats);
|
---|
186 | mem_stats = 0;
|
---|
187 |
|
---|
188 | switch (name) {
|
---|
189 | case _SC_PHYS_PAGES:
|
---|
190 | return phys_pages;
|
---|
191 | case _SC_AVPHYS_PAGES:
|
---|
192 | return avphys_pages;
|
---|
193 | case _SC_PAGESIZE:
|
---|
194 | return getpagesize();
|
---|
195 | case _SC_CLK_TCK:
|
---|
196 | return clk_tck;
|
---|
197 | default:
|
---|
198 | errno = EINVAL;
|
---|
199 | return -1;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | *
|
---|
205 | * @param path
|
---|
206 | * @param name
|
---|
207 | * @return
|
---|
208 | */
|
---|
209 | long posix_pathconf(const char *path, int name)
|
---|
210 | {
|
---|
211 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
212 | not_implemented();
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | *
|
---|
217 | * @return
|
---|
218 | */
|
---|
219 | posix_pid_t posix_fork(void)
|
---|
220 | {
|
---|
221 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
222 | not_implemented();
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | *
|
---|
227 | * @param path
|
---|
228 | * @param argv
|
---|
229 | * @return
|
---|
230 | */
|
---|
231 | int posix_execv(const char *path, char *const argv[])
|
---|
232 | {
|
---|
233 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
234 | not_implemented();
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | *
|
---|
239 | * @param file
|
---|
240 | * @param argv
|
---|
241 | * @return
|
---|
242 | */
|
---|
243 | int posix_execvp(const char *file, char *const argv[])
|
---|
244 | {
|
---|
245 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
246 | not_implemented();
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | *
|
---|
251 | * @param fildes
|
---|
252 | * @return
|
---|
253 | */
|
---|
254 | int posix_pipe(int fildes[2])
|
---|
255 | {
|
---|
256 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
257 | not_implemented();
|
---|
258 | }
|
---|
259 |
|
---|
260 | /** @}
|
---|
261 | */
|
---|