3fbb50c9bef8628312eaa969d44efd87a71d345a
[platform/upstream/nodejs.git] / deps / uv / src / unix / sunos.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 #ifndef SUNOS_NO_IFADDRS
32 # include <ifaddrs.h>
33 #endif
34 #include <net/if.h>
35
36 #include <sys/loadavg.h>
37 #include <sys/time.h>
38 #include <unistd.h>
39 #include <kstat.h>
40 #include <fcntl.h>
41
42 #include <sys/port.h>
43 #include <port.h>
44
45 #define PORT_FIRED 0x69
46 #define PORT_UNUSED 0x0
47 #define PORT_LOADED 0x99
48 #define PORT_DELETED -1
49
50 #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
51 #define PROCFS_FILE_OFFSET_BITS_HACK 1
52 #undef _FILE_OFFSET_BITS
53 #else
54 #define PROCFS_FILE_OFFSET_BITS_HACK 0
55 #endif
56
57 #include <procfs.h>
58
59 #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
60 #define _FILE_OFFSET_BITS 64
61 #endif
62
63
64 int uv__platform_loop_init(uv_loop_t* loop, int default_loop) {
65   loop->fs_fd = -1;
66   loop->backend_fd = port_create();
67
68   if (loop->backend_fd == -1)
69     return -1;
70
71   uv__cloexec(loop->backend_fd, 1);
72
73   return 0;
74 }
75
76
77 void uv__platform_loop_delete(uv_loop_t* loop) {
78   if (loop->fs_fd != -1) {
79     close(loop->fs_fd);
80     loop->fs_fd = -1;
81   }
82
83   if (loop->backend_fd != -1) {
84     close(loop->backend_fd);
85     loop->backend_fd = -1;
86   }
87 }
88
89
90 void uv__io_poll(uv_loop_t* loop, int timeout) {
91   struct port_event events[1024];
92   struct port_event* pe;
93   struct timespec spec;
94   ngx_queue_t* q;
95   uv__io_t* w;
96   uint64_t base;
97   uint64_t diff;
98   unsigned int nfds;
99   unsigned int i;
100   int saved_errno;
101   int nevents;
102   int count;
103   int fd;
104
105   if (loop->nfds == 0) {
106     assert(ngx_queue_empty(&loop->watcher_queue));
107     return;
108   }
109
110   while (!ngx_queue_empty(&loop->watcher_queue)) {
111     q = ngx_queue_head(&loop->watcher_queue);
112     ngx_queue_remove(q);
113     ngx_queue_init(q);
114
115     w = ngx_queue_data(q, uv__io_t, watcher_queue);
116     assert(w->pevents != 0);
117
118     if (port_associate(loop->backend_fd, PORT_SOURCE_FD, w->fd, w->pevents, 0))
119       abort();
120
121     w->events = w->pevents;
122   }
123
124   assert(timeout >= -1);
125   base = loop->time;
126   count = 48; /* Benchmarks suggest this gives the best throughput. */
127
128   for (;;) {
129     if (timeout != -1) {
130       spec.tv_sec = timeout / 1000;
131       spec.tv_nsec = (timeout % 1000) * 1000000;
132     }
133
134     /* Work around a kernel bug where nfds is not updated. */
135     events[0].portev_source = 0;
136
137     nfds = 1;
138     saved_errno = 0;
139     if (port_getn(loop->backend_fd,
140                   events,
141                   ARRAY_SIZE(events),
142                   &nfds,
143                   timeout == -1 ? NULL : &spec)) {
144       /* Work around another kernel bug: port_getn() may return events even
145        * on error.
146        */
147       if (errno == EINTR || errno == ETIME)
148         saved_errno = errno;
149       else
150         abort();
151     }
152
153     /* Update loop->time unconditionally. It's tempting to skip the update when
154      * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
155      * operating system didn't reschedule our process while in the syscall.
156      */
157     SAVE_ERRNO(uv__update_time(loop));
158
159     if (events[0].portev_source == 0) {
160       if (timeout == 0)
161         return;
162
163       if (timeout == -1)
164         continue;
165
166       goto update_timeout;
167     }
168
169     if (nfds == 0) {
170       assert(timeout != -1);
171       return;
172     }
173
174     nevents = 0;
175
176     for (i = 0; i < nfds; i++) {
177       pe = events + i;
178       fd = pe->portev_object;
179
180       assert(fd >= 0);
181       assert((unsigned) fd < loop->nwatchers);
182
183       w = loop->watchers[fd];
184
185       /* File descriptor that we've stopped watching, ignore. */
186       if (w == NULL)
187         continue;
188
189       w->cb(loop, w, pe->portev_events);
190       nevents++;
191
192       /* Events Ports operates in oneshot mode, rearm timer on next run. */
193       if (w->pevents != 0 && ngx_queue_empty(&w->watcher_queue))
194         ngx_queue_insert_tail(&loop->watcher_queue, &w->watcher_queue);
195     }
196
197     if (nevents != 0) {
198       if (nfds == ARRAY_SIZE(events) && --count != 0) {
199         /* Poll for more events but don't block this time. */
200         timeout = 0;
201         continue;
202       }
203       return;
204     }
205
206     if (saved_errno == ETIME) {
207       assert(timeout != -1);
208       return;
209     }
210
211     if (timeout == 0)
212       return;
213
214     if (timeout == -1)
215       continue;
216
217 update_timeout:
218     assert(timeout > 0);
219
220     diff = loop->time - base;
221     if (diff >= (uint64_t) timeout)
222       return;
223
224     timeout -= diff;
225   }
226 }
227
228
229 uint64_t uv__hrtime(void) {
230   return gethrtime();
231 }
232
233
234 /*
235  * We could use a static buffer for the path manipulations that we need outside
236  * of the function, but this function could be called by multiple consumers and
237  * we don't want to potentially create a race condition in the use of snprintf.
238  */
239 int uv_exepath(char* buffer, size_t* size) {
240   ssize_t res;
241   char buf[128];
242
243   if (buffer == NULL)
244     return (-1);
245
246   if (size == NULL)
247     return (-1);
248
249   (void) snprintf(buf, sizeof(buf), "/proc/%lu/path/a.out", (unsigned long) getpid());
250   res = readlink(buf, buffer, *size - 1);
251
252   if (res < 0)
253     return (res);
254
255   buffer[res] = '\0';
256   *size = res;
257   return (0);
258 }
259
260
261 uint64_t uv_get_free_memory(void) {
262   return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
263 }
264
265
266 uint64_t uv_get_total_memory(void) {
267   return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
268 }
269
270
271 void uv_loadavg(double avg[3]) {
272   (void) getloadavg(avg, 3);
273 }
274
275
276 #if defined(PORT_SOURCE_FILE)
277
278 static void uv__fs_event_rearm(uv_fs_event_t *handle) {
279   if (handle->fd == -1)
280     return;
281
282   if (port_associate(handle->loop->fs_fd,
283                      PORT_SOURCE_FILE,
284                      (uintptr_t) &handle->fo,
285                      FILE_ATTRIB | FILE_MODIFIED,
286                      handle) == -1) {
287     uv__set_sys_error(handle->loop, errno);
288   }
289   handle->fd = PORT_LOADED;
290 }
291
292
293 static void uv__fs_event_read(uv_loop_t* loop,
294                               uv__io_t* w,
295                               unsigned int revents) {
296   uv_fs_event_t *handle = NULL;
297   timespec_t timeout;
298   port_event_t pe;
299   int events;
300   int r;
301
302   (void) w;
303   (void) revents;
304
305   do {
306     uint_t n = 1;
307
308     /*
309      * Note that our use of port_getn() here (and not port_get()) is deliberate:
310      * there is a bug in event ports (Sun bug 6456558) whereby a zeroed timeout
311      * causes port_get() to return success instead of ETIME when there aren't
312      * actually any events (!); by using port_getn() in lieu of port_get(),
313      * we can at least workaround the bug by checking for zero returned events
314      * and treating it as we would ETIME.
315      */
316     do {
317       memset(&timeout, 0, sizeof timeout);
318       r = port_getn(loop->fs_fd, &pe, 1, &n, &timeout);
319     }
320     while (r == -1 && errno == EINTR);
321
322     if ((r == -1 && errno == ETIME) || n == 0)
323       break;
324
325     handle = (uv_fs_event_t *)pe.portev_user;
326     assert((r == 0) && "unexpected port_get() error");
327
328     events = 0;
329     if (pe.portev_events & (FILE_ATTRIB | FILE_MODIFIED))
330       events |= UV_CHANGE;
331     if (pe.portev_events & ~(FILE_ATTRIB | FILE_MODIFIED))
332       events |= UV_RENAME;
333     assert(events != 0);
334     handle->fd = PORT_FIRED;
335     handle->cb(handle, NULL, events, 0);
336   }
337   while (handle->fd != PORT_DELETED);
338
339   if (handle != NULL && handle->fd != PORT_DELETED)
340     uv__fs_event_rearm(handle);
341 }
342
343
344 int uv_fs_event_init(uv_loop_t* loop,
345                      uv_fs_event_t* handle,
346                      const char* filename,
347                      uv_fs_event_cb cb,
348                      int flags) {
349   int portfd;
350   int first_run = 0;
351
352   if (loop->fs_fd == -1) {
353     if ((portfd = port_create()) == -1) {
354       uv__set_sys_error(loop, errno);
355       return -1;
356     }
357     loop->fs_fd = portfd;
358     first_run = 1;
359   }
360
361   uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
362   uv__handle_start(handle); /* FIXME shouldn't start automatically */
363   handle->filename = strdup(filename);
364   handle->fd = PORT_UNUSED;
365   handle->cb = cb;
366
367   memset(&handle->fo, 0, sizeof handle->fo);
368   handle->fo.fo_name = handle->filename;
369   uv__fs_event_rearm(handle);
370
371   if (first_run) {
372     uv__io_init(&loop->fs_event_watcher, uv__fs_event_read, portfd);
373     uv__io_start(loop, &loop->fs_event_watcher, UV__POLLIN);
374   }
375
376   return 0;
377 }
378
379
380 void uv__fs_event_close(uv_fs_event_t* handle) {
381   if (handle->fd == PORT_FIRED || handle->fd == PORT_LOADED) {
382     port_dissociate(handle->loop->fs_fd, PORT_SOURCE_FILE, (uintptr_t)&handle->fo);
383   }
384   handle->fd = PORT_DELETED;
385   free(handle->filename);
386   handle->filename = NULL;
387   handle->fo.fo_name = NULL;
388   uv__handle_stop(handle);
389 }
390
391 #else /* !defined(PORT_SOURCE_FILE) */
392
393 int uv_fs_event_init(uv_loop_t* loop,
394                      uv_fs_event_t* handle,
395                      const char* filename,
396                      uv_fs_event_cb cb,
397                      int flags) {
398   uv__set_sys_error(loop, ENOSYS);
399   return -1;
400 }
401
402
403 void uv__fs_event_close(uv_fs_event_t* handle) {
404   UNREACHABLE();
405 }
406
407 #endif /* defined(PORT_SOURCE_FILE) */
408
409
410 char** uv_setup_args(int argc, char** argv) {
411   return argv;
412 }
413
414
415 uv_err_t uv_set_process_title(const char* title) {
416   return uv_ok_;
417 }
418
419
420 uv_err_t uv_get_process_title(char* buffer, size_t size) {
421   if (size > 0) {
422     buffer[0] = '\0';
423   }
424   return uv_ok_;
425 }
426
427
428 uv_err_t uv_resident_set_memory(size_t* rss) {
429   psinfo_t psinfo;
430   uv_err_t err;
431   int fd;
432
433   fd = open("/proc/self/psinfo", O_RDONLY);
434   if (fd == -1)
435     return uv__new_sys_error(errno);
436
437   err = uv_ok_;
438
439   if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo))
440     *rss = (size_t)psinfo.pr_rssize * 1024;
441   else
442     err = uv__new_sys_error(EINVAL);
443
444   close(fd);
445
446   return err;
447 }
448
449
450 uv_err_t uv_uptime(double* uptime) {
451   kstat_ctl_t   *kc;
452   kstat_t       *ksp;
453   kstat_named_t *knp;
454
455   long hz = sysconf(_SC_CLK_TCK);
456
457   if ((kc = kstat_open()) == NULL)
458     return uv__new_sys_error(errno);
459
460   ksp = kstat_lookup(kc, (char *)"unix", 0, (char *)"system_misc");
461
462   if (kstat_read(kc, ksp, NULL) == -1) {
463     *uptime = -1;
464   } else {
465     knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clk_intr");
466     *uptime = knp->value.ul / hz;
467   }
468
469   kstat_close(kc);
470
471   return uv_ok_;
472 }
473
474
475 uv_err_t uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
476   int           lookup_instance;
477   kstat_ctl_t   *kc;
478   kstat_t       *ksp;
479   kstat_named_t *knp;
480   uv_cpu_info_t* cpu_info;
481
482   if ((kc = kstat_open()) == NULL) {
483     return uv__new_sys_error(errno);
484   }
485
486   /* Get count of cpus */
487   lookup_instance = 0;
488   while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
489     lookup_instance++;
490   }
491
492   *cpu_infos = (uv_cpu_info_t*)
493     malloc(lookup_instance * sizeof(uv_cpu_info_t));
494   if (!(*cpu_infos)) {
495     return uv__new_artificial_error(UV_ENOMEM);
496   }
497
498   *count = lookup_instance;
499
500   cpu_info = *cpu_infos;
501   lookup_instance = 0;
502   while ((ksp = kstat_lookup(kc, (char *)"cpu_info", lookup_instance, NULL))) {
503     if (kstat_read(kc, ksp, NULL) == -1) {
504       cpu_info->speed = 0;
505       cpu_info->model = NULL;
506     } else {
507       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"clock_MHz");
508       assert(knp->data_type == KSTAT_DATA_INT32 ||
509              knp->data_type == KSTAT_DATA_INT64);
510       cpu_info->speed = (knp->data_type == KSTAT_DATA_INT32) ? knp->value.i32
511                                                              : knp->value.i64;
512
513       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"brand");
514       assert(knp->data_type == KSTAT_DATA_STRING);
515       cpu_info->model = strdup(KSTAT_NAMED_STR_PTR(knp));
516     }
517
518     lookup_instance++;
519     cpu_info++;
520   }
521
522   cpu_info = *cpu_infos;
523   lookup_instance = 0;
524   while ((ksp = kstat_lookup(kc, (char *)"cpu", lookup_instance, (char *)"sys"))){
525
526     if (kstat_read(kc, ksp, NULL) == -1) {
527       cpu_info->cpu_times.user = 0;
528       cpu_info->cpu_times.nice = 0;
529       cpu_info->cpu_times.sys = 0;
530       cpu_info->cpu_times.idle = 0;
531       cpu_info->cpu_times.irq = 0;
532     } else {
533       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_user");
534       assert(knp->data_type == KSTAT_DATA_UINT64);
535       cpu_info->cpu_times.user = knp->value.ui64;
536
537       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_kernel");
538       assert(knp->data_type == KSTAT_DATA_UINT64);
539       cpu_info->cpu_times.sys = knp->value.ui64;
540
541       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"cpu_ticks_idle");
542       assert(knp->data_type == KSTAT_DATA_UINT64);
543       cpu_info->cpu_times.idle = knp->value.ui64;
544
545       knp = (kstat_named_t *) kstat_data_lookup(ksp, (char *)"intr");
546       assert(knp->data_type == KSTAT_DATA_UINT64);
547       cpu_info->cpu_times.irq = knp->value.ui64;
548       cpu_info->cpu_times.nice = 0;
549     }
550
551     lookup_instance++;
552     cpu_info++;
553   }
554
555   kstat_close(kc);
556
557   return uv_ok_;
558 }
559
560
561 void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
562   int i;
563
564   for (i = 0; i < count; i++) {
565     free(cpu_infos[i].model);
566   }
567
568   free(cpu_infos);
569 }
570
571
572 uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
573   int* count) {
574 #ifdef SUNOS_NO_IFADDRS
575   return uv__new_artificial_error(UV_ENOSYS);
576 #else
577   struct ifaddrs *addrs, *ent;
578   char ip[INET6_ADDRSTRLEN];
579   uv_interface_address_t* address;
580
581   if (getifaddrs(&addrs) != 0) {
582     return uv__new_sys_error(errno);
583   }
584
585   *count = 0;
586
587   /* Count the number of interfaces */
588   for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
589     if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING) ||
590         (ent->ifa_addr == NULL) ||
591         (ent->ifa_addr->sa_family == PF_PACKET)) {
592       continue;
593     }
594
595     (*count)++;
596   }
597
598   *addresses = (uv_interface_address_t*)
599     malloc(*count * sizeof(uv_interface_address_t));
600   if (!(*addresses)) {
601     return uv__new_artificial_error(UV_ENOMEM);
602   }
603
604   address = *addresses;
605
606   for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
607     memset(&ip, 0, sizeof(ip));
608
609     if (!(ent->ifa_flags & IFF_UP && ent->ifa_flags & IFF_RUNNING)) {
610       continue;
611     }
612
613     if (ent->ifa_addr == NULL) {
614       continue;
615     }
616
617     address->name = strdup(ent->ifa_name);
618
619     if (ent->ifa_addr->sa_family == AF_INET6) {
620       address->address.address6 = *((struct sockaddr_in6 *)ent->ifa_addr);
621     } else {
622       address->address.address4 = *((struct sockaddr_in *)ent->ifa_addr);
623     }
624
625     address->is_internal = ent->ifa_flags & IFF_PRIVATE || ent->ifa_flags &
626         IFF_LOOPBACK ? 1 : 0;
627
628     address++;
629   }
630
631   freeifaddrs(addrs);
632
633   return uv_ok_;
634 #endif  /* SUNOS_NO_IFADDRS */
635 }
636
637
638 void uv_free_interface_addresses(uv_interface_address_t* addresses,
639   int count) {
640   int i;
641
642   for (i = 0; i < count; i++) {
643     free(addresses[i].name);
644   }
645
646   free(addresses);
647 }