]> git.proxmox.com Git - rustc.git/blob - src/libuv/src/unix/aix.c
Imported Upstream version 0.7
[rustc.git] / src / libuv / src / unix / aix.c
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18 * IN THE SOFTWARE.
19 */
20
21 #include "uv.h"
22 #include "internal.h"
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34 #include <net/if.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <sys/time.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41 #include <utmp.h>
42
43 #include <sys/protosw.h>
44 #include <libperfstat.h>
45 #include <sys/proc.h>
46 #include <sys/procfs.h>
47
48 uint64_t uv__hrtime(void) {
49 uint64_t G = 1000000000;
50 timebasestruct_t t;
51 read_wall_time(&t, TIMEBASE_SZ);
52 time_base_to_time(&t, TIMEBASE_SZ);
53 return (uint64_t) t.tb_high * G + t.tb_low;
54 }
55
56
57 /*
58 * We could use a static buffer for the path manipulations that we need outside
59 * of the function, but this function could be called by multiple consumers and
60 * we don't want to potentially create a race condition in the use of snprintf.
61 */
62 int uv_exepath(char* buffer, size_t* size) {
63 ssize_t res;
64 char pp[64], cwdl[PATH_MAX];
65 size_t cwdl_len;
66 struct psinfo ps;
67 int fd;
68
69 if (buffer == NULL)
70 return (-1);
71
72 if (size == NULL)
73 return (-1);
74
75 (void) snprintf(pp, sizeof(pp), "/proc/%lu/cwd", (unsigned long) getpid());
76
77 res = readlink(pp, cwdl, sizeof(cwdl) - 1);
78 if (res < 0)
79 return res;
80
81 cwdl[res] = '\0';
82 cwdl_len = res;
83
84 (void) snprintf(pp, sizeof(pp), "/proc/%lu/psinfo", (unsigned long) getpid());
85 fd = open(pp, O_RDONLY);
86 if (fd < 0)
87 return fd;
88
89 res = read(fd, &ps, sizeof(ps));
90 close(fd);
91 if (res < 0)
92 return res;
93
94 (void) snprintf(buffer, *size, "%s%s", cwdl, ps.pr_fname);
95 *size = strlen(buffer);
96 return 0;
97 }
98
99
100 uint64_t uv_get_free_memory(void) {
101 perfstat_memory_total_t mem_total;
102 int result = perfstat_memory_total(NULL, &mem_total, sizeof(mem_total), 1);
103 if (result == -1) {
104 return 0;
105 }
106 return mem_total.real_free * 4096;
107 }
108
109
110 uint64_t uv_get_total_memory(void) {
111 perfstat_memory_total_t mem_total;
112 int result = perfstat_memory_total(NULL, &mem_total, sizeof(mem_total), 1);
113 if (result == -1) {
114 return 0;
115 }
116 return mem_total.real_total * 4096;
117 }
118
119
120 void uv_loadavg(double avg[3]) {
121 perfstat_cpu_total_t ps_total;
122 int result = perfstat_cpu_total(NULL, &ps_total, sizeof(ps_total), 1);
123 if (result == -1) {
124 avg[0] = 0.; avg[1] = 0.; avg[2] = 0.;
125 return;
126 }
127 avg[0] = ps_total.loadavg[0] / (double)(1 << SBITS);
128 avg[1] = ps_total.loadavg[1] / (double)(1 << SBITS);
129 avg[2] = ps_total.loadavg[2] / (double)(1 << SBITS);
130 }
131
132
133 int uv_fs_event_init(uv_loop_t* loop,
134 uv_fs_event_t* handle,
135 const char* filename,
136 uv_fs_event_cb cb,
137 int flags) {
138 loop->counters.fs_event_init++;
139 uv__set_sys_error(loop, ENOSYS);
140 return -1;
141 }
142
143
144 void uv__fs_event_close(uv_fs_event_t* handle) {
145 UNREACHABLE();
146 }
147
148
149 char** uv_setup_args(int argc, char** argv) {
150 return argv;
151 }
152
153
154 uv_err_t uv_set_process_title(const char* title) {
155 return uv_ok_;
156 }
157
158
159 uv_err_t uv_get_process_title(char* buffer, size_t size) {
160 if (size > 0) {
161 buffer[0] = '\0';
162 }
163 return uv_ok_;
164 }
165
166
167 uv_err_t uv_resident_set_memory(size_t* rss) {
168 char pp[64];
169 psinfo_t psinfo;
170 uv_err_t err;
171 int fd;
172
173 (void) snprintf(pp, sizeof(pp), "/proc/%lu/psinfo", (unsigned long) getpid());
174
175 fd = open(pp, O_RDONLY);
176 if (fd == -1)
177 return uv__new_sys_error(errno);
178
179 err = uv_ok_;
180
181 if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
182 *rss = (size_t)psinfo.pr_rssize * 1024;
183 else
184 err = uv__new_sys_error(EINVAL);
185
186 close(fd);
187
188 return err;
189 }
190
191
192 uv_err_t uv_uptime(double* uptime) {
193 struct utmp *utmp_buf;
194 size_t entries = 0;
195 time_t boot_time;
196
197 utmpname(UTMP_FILE);
198
199 setutent();
200
201 while ((utmp_buf = getutent()) != NULL) {
202 if (utmp_buf->ut_user[0] && utmp_buf->ut_type == USER_PROCESS)
203 ++entries;
204 if (utmp_buf->ut_type == BOOT_TIME)
205 boot_time = utmp_buf->ut_time;
206 }
207
208 endutent();
209
210 if (boot_time == 0)
211 return uv__new_artificial_error(UV_ENOSYS);
212
213 *uptime = time(NULL) - boot_time;
214 return uv_ok_;
215 }
216
217
218 uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
219 uv_cpu_info_t* cpu_info;
220 perfstat_cpu_total_t ps_total;
221 perfstat_cpu_t* ps_cpus;
222 perfstat_id_t cpu_id;
223 int result, ncpus, idx = 0;
224
225 result = perfstat_cpu_total(NULL, &ps_total, sizeof(ps_total), 1);
226 if (result == -1) {
227 return uv__new_artificial_error(UV_ENOSYS);
228 }
229
230 ncpus = result = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
231 if (result == -1) {
232 return uv__new_artificial_error(UV_ENOSYS);
233 }
234
235 ps_cpus = (perfstat_cpu_t*) malloc(ncpus * sizeof(perfstat_cpu_t));
236 if (!ps_cpus) {
237 return uv__new_artificial_error(UV_ENOMEM);
238 }
239
240 strcpy(cpu_id.name, FIRST_CPU);
241 result = perfstat_cpu(&cpu_id, ps_cpus, sizeof(perfstat_cpu_t), ncpus);
242 if (result == -1) {
243 free(ps_cpus);
244 return uv__new_artificial_error(UV_ENOSYS);
245 }
246
247 *cpu_infos = (uv_cpu_info_t*) malloc(ncpus * sizeof(uv_cpu_info_t));
248 if (!*cpu_infos) {
249 free(ps_cpus);
250 return uv__new_artificial_error(UV_ENOMEM);
251 }
252
253 *count = ncpus;
254
255 cpu_info = *cpu_infos;
256 while (idx < ncpus) {
257 cpu_info->speed = (int)(ps_total.processorHZ / 1000000);
258 cpu_info->model = strdup(ps_total.description);
259 cpu_info->cpu_times.user = ps_cpus[idx].user;
260 cpu_info->cpu_times.sys = ps_cpus[idx].sys;
261 cpu_info->cpu_times.idle = ps_cpus[idx].idle;
262 cpu_info->cpu_times.irq = ps_cpus[idx].wait;
263 cpu_info->cpu_times.nice = 0;
264 cpu_info++;
265 idx++;
266 }
267
268 free(ps_cpus);
269 return uv_ok_;
270 }
271
272
273 void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
274 int i;
275
276 for (i = 0; i < count; ++i) {
277 free(cpu_infos[i].model);
278 }
279
280 free(cpu_infos);
281 }
282
283
284 uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
285 int* count) {
286 uv_interface_address_t* address;
287 int sockfd, size = 1;
288 struct ifconf ifc;
289 struct ifreq *ifr, *p, flg;
290
291 *count = 0;
292
293 if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
294 return uv__new_artificial_error(UV_ENOSYS);
295 }
296
297 if (ioctl(sockfd, SIOCGSIZIFCONF, &size) == -1) {
298 close(sockfd);
299 return uv__new_artificial_error(UV_ENOSYS);
300 }
301
302 ifc.ifc_req = (struct ifreq*)malloc(size);
303 ifc.ifc_len = size;
304 if (ioctl(sockfd, SIOCGIFCONF, &ifc) == -1) {
305 close(sockfd);
306 return uv__new_artificial_error(UV_ENOSYS);
307 }
308
309 #define ADDR_SIZE(p) MAX((p).sa_len, sizeof(p))
310
311 /* Count all up and running ipv4/ipv6 addresses */
312 ifr = ifc.ifc_req;
313 while ((char*)ifr < (char*)ifc.ifc_req + ifc.ifc_len) {
314 p = ifr;
315 ifr = (struct ifreq*)
316 ((char*)ifr + sizeof(ifr->ifr_name) + ADDR_SIZE(ifr->ifr_addr));
317
318 if (!(p->ifr_addr.sa_family == AF_INET6 ||
319 p->ifr_addr.sa_family == AF_INET))
320 continue;
321
322 memcpy(flg.ifr_name, p->ifr_name, sizeof(flg.ifr_name));
323 if (ioctl(sockfd, SIOCGIFFLAGS, &flg) == -1) {
324 close(sockfd);
325 return uv__new_artificial_error(UV_ENOSYS);
326 }
327
328 if (!(flg.ifr_flags & IFF_UP && flg.ifr_flags & IFF_RUNNING))
329 continue;
330
331 (*count)++;
332 }
333
334 /* Alloc the return interface structs */
335 *addresses = (uv_interface_address_t*)
336 malloc(*count * sizeof(uv_interface_address_t));
337 if (!(*addresses)) {
338 close(sockfd);
339 return uv__new_artificial_error(UV_ENOMEM);
340 }
341 address = *addresses;
342
343 ifr = ifc.ifc_req;
344 while ((char*)ifr < (char*)ifc.ifc_req + ifc.ifc_len) {
345 p = ifr;
346 ifr = (struct ifreq*)
347 ((char*)ifr + sizeof(ifr->ifr_name) + ADDR_SIZE(ifr->ifr_addr));
348
349 if (!(p->ifr_addr.sa_family == AF_INET6 ||
350 p->ifr_addr.sa_family == AF_INET))
351 continue;
352
353 memcpy(flg.ifr_name, p->ifr_name, sizeof(flg.ifr_name));
354 if (ioctl(sockfd, SIOCGIFFLAGS, &flg) == -1) {
355 close(sockfd);
356 return uv__new_artificial_error(UV_ENOSYS);
357 }
358
359 if (!(flg.ifr_flags & IFF_UP && flg.ifr_flags & IFF_RUNNING))
360 continue;
361
362 /* All conditions above must match count loop */
363
364 address->name = strdup(p->ifr_name);
365
366 if (p->ifr_addr.sa_family == AF_INET6) {
367 address->address.address6 = *((struct sockaddr_in6 *)&p->ifr_addr);
368 } else {
369 address->address.address4 = *((struct sockaddr_in *)&p->ifr_addr);
370 }
371
372 address->is_internal = flg.ifr_flags & IFF_LOOPBACK ? 1 : 0;
373
374 address++;
375 }
376
377 #undef ADDR_SIZE
378
379 close(sockfd);
380 return uv_ok_;
381 }
382
383
384 void uv_free_interface_addresses(uv_interface_address_t* addresses,
385 int count) {
386 int i;
387
388 for (i = 0; i < count; ++i) {
389 free(addresses[i].name);
390 }
391
392 free(addresses);
393 }