32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
45 #include <sys/protosw.h>
46 #include <libperfstat.h>
49 #include <sys/procfs.h>
53 #include <sys/pollset.h>
55 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
56 #include <sys/ahafs_evProds.h>
59 #include <sys/mntctl.h>
60 #include <sys/vmount.h>
63 #include <sys/vnode.h>
65 #define RDWR_BUF_SIZE 4096
66 #define EQ(a,b) (strcmp(a,b) == 0)
85 loop->backend_fd = pollset_create(-1);
87 if (
loop->backend_fd == -1)
95 if (
loop->fs_fd != -1) {
100 if (
loop->backend_fd != -1) {
102 loop->backend_fd = -1;
121 if (pollset_ctl(
loop->backend_fd, &pc, 1))
125 if (pollset_ctl(
loop->backend_fd, &pc, 1))
133 struct pollfd events[1024];
149 if (
loop->nfds == 0) {
162 assert(w->
fd < (
int)
loop->nwatchers);
170 if (pollset_ctl(
loop->backend_fd, &pc, 1)) {
171 if (errno != EINVAL) {
172 assert(0 &&
"Failed to add file descriptor (pc.fd) to pollset");
177 rc = pollset_query(
loop->backend_fd, &pqry);
180 assert(0 &&
"Failed to query pollset for file descriptor");
183 assert(0 &&
"Pollset does not contain file descriptor");
191 if (w->
events != 0 || add_failed) {
199 if (pollset_ctl(
loop->backend_fd, &pc, 1)) {
200 assert(0 &&
"Failed to delete file descriptor (pc.fd) from pollset");
204 if (pollset_ctl(
loop->backend_fd, &pc, 1)) {
205 assert(0 &&
"Failed to add file descriptor (pc.fd) to pollset");
218 nfds = pollset_poll(
loop->backend_fd,
235 if (errno != EINTR) {
252 assert(
loop->watchers != NULL);
253 loop->watchers[
loop->nwatchers] = (
void*) events;
256 for (
i = 0;
i < nfds;
i++) {
266 assert((
unsigned) pc.fd <
loop->nwatchers);
268 w =
loop->watchers[pc.fd];
276 pollset_ctl(
loop->backend_fd, &pc, 1);
283 if (w == &
loop->signal_io_watcher)
286 w->
cb(
loop, w, pe->revents);
291 if (have_signals != 0)
292 loop->signal_io_watcher.cb(
loop, &
loop->signal_io_watcher, POLLIN);
294 loop->watchers[
loop->nwatchers] = NULL;
295 loop->watchers[
loop->nwatchers + 1] = NULL;
297 if (have_signals != 0)
328 perfstat_memory_total_t mem_total;
329 int result = perfstat_memory_total(NULL, &mem_total,
sizeof(mem_total), 1);
333 return mem_total.real_free * 4096;
338 perfstat_memory_total_t mem_total;
339 int result = perfstat_memory_total(NULL, &mem_total,
sizeof(mem_total), 1);
343 return mem_total.real_total * 4096;
353 perfstat_cpu_total_t ps_total;
354 int result = perfstat_cpu_total(NULL, &ps_total,
sizeof(ps_total), 1);
356 avg[0] = 0.; avg[1] = 0.; avg[2] = 0.;
359 avg[0] = ps_total.loadavg[0] / (double)(1 << SBITS);
360 avg[1] = ps_total.loadavg[1] / (double)(1 << SBITS);
361 avg[2] = ps_total.loadavg[2] / (double)(1 << SBITS);
365 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
366 static char* uv__rawname(
const char* cp,
char (*
dst)[FILENAME_MAX+1]) {
369 dp = rindex(cp,
'/');
373 snprintf(*
dst,
sizeof(*
dst),
"%.*s/r%s", (
int) (dp - cp), cp, dp + 1);
385 static int uv__path_is_a_directory(
char*
filename) {
391 if (statbuf.st_type == VDIR)
402 static int uv__is_ahafs_mounted(
void){
403 char rawbuf[FILENAME_MAX+1];
406 int size_multiplier = 10;
407 size_t siz =
sizeof(
struct vmount)*size_multiplier;
409 const char *dev =
"/aha";
417 rv = mntctl(MCTL_QUERY, siz, (
char*)
p);
427 rv = mntctl(MCTL_QUERY, siz, (
char*)
p);
433 for(vmt =
p,
i = 0;
i < rv;
i++) {
434 obj = vmt2dataptr(vmt, VMT_OBJECT);
435 stub = vmt2dataptr(vmt, VMT_STUB);
441 vmt = (
struct vmount *) ((
char *) vmt + vmt->vmt_length);
452 static int uv__makedir_p(
const char *dir) {
463 for (
p =
tmp + 1; *
p;
p++) {
466 err = mkdir(
tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
467 if (
err != 0 && errno != EEXIST)
472 return mkdir(
tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
480 static int uv__make_subdirs_p(
const char *
filename) {
491 if (uv__path_is_a_directory((
char*)
filename) == 0) {
492 sprintf(
cmd,
"/aha/fs/modDir.monFactory");
494 sprintf(
cmd,
"/aha/fs/modFile.monFactory");
498 rc = uv__makedir_p(
cmd);
500 if (rc == -1 && errno != EEXIST){
513 static int uv__setup_ahafs(
const char*
filename,
int *fd) {
516 char mon_file[PATH_MAX];
517 int file_is_directory = 0;
520 file_is_directory = uv__path_is_a_directory((
char*)
filename);
522 if (file_is_directory == 0)
523 sprintf(mon_file,
"/aha/fs/modDir.monFactory");
525 sprintf(mon_file,
"/aha/fs/modFile.monFactory");
527 if ((strlen(mon_file) + strlen(
filename) + 5) > PATH_MAX)
528 return UV_ENAMETOOLONG;
532 if (rc == -1 && errno != EEXIST)
536 strcat(mon_file,
".mon");
541 *fd =
open(mon_file, O_CREAT|O_RDWR);
557 if (file_is_directory == 0)
558 sprintf(mon_file_write_string,
"CHANGED=YES;WAIT_TYPE=WAIT_IN_SELECT;INFO_LVL=2");
560 sprintf(mon_file_write_string,
"CHANGED=YES;WAIT_TYPE=WAIT_IN_SELECT;INFO_LVL=1");
562 rc =
write(*fd, mon_file_write_string, strlen(mon_file_write_string)+1);
563 if (rc < 0 && errno != EBUSY)
574 static int uv__skip_lines(
char **
p,
int n) {
578 *
p = strchr(*
p,
'\n');
607 for(
i = 0;
i < PATH_MAX;
i++) {
613 if (strncmp(
buf,
"BUF_WRAP", strlen(
"BUF_WRAP")) == 0) {
614 assert(0 &&
"Buffer wrap detected, Some event occurrences lost!");
625 if (uv__skip_lines(&
p, 9) != 9)
628 if (sscanf(
p,
"RC_FROM_EVPROD=%d\nEND_EVENT_DATA", &evp_rc) == 1) {
629 if (uv__path_is_a_directory(
handle->path) == 0) {
630 if (evp_rc == AHAFS_MODDIR_UNMOUNT || evp_rc == AHAFS_MODDIR_REMOVE_SELF) {
633 handle->dir_filename = NULL;
639 if (uv__skip_lines(&
p, 1) != 1)
643 if (sscanf(
p,
"BEGIN_EVPROD_INFO\n%sEND_EVPROD_INFO",
filename) == 1) {
649 if (evp_rc == AHAFS_MODFILE_RENAME)
668 char fname[PATH_MAX];
679 assert((
bytes >= 0) &&
"uv__ahafs_event - Error reading monitor file");
690 rc = uv__parse_data(result_data, &events,
handle);
699 if (uv__path_is_a_directory(
handle->path) == 0) {
702 p = strrchr(
handle->path,
'/');
718 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
730 unsigned int flags) {
731 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
732 int fd, rc, str_offset = 0;
734 char absolute_path[PATH_MAX];
735 char readlink_cwd[PATH_MAX];
751 snprintf(
cwd,
sizeof(
cwd),
"/proc/%lu/cwd", (
unsigned long) getpid());
752 rc = readlink(
cwd, readlink_cwd,
sizeof(readlink_cwd) - 1);
756 readlink_cwd[rc] =
'\0';
761 snprintf(absolute_path,
sizeof(absolute_path),
"%s%s", readlink_cwd,
765 if (uv__is_ahafs_mounted() < 0)
769 rc = uv__setup_ahafs((
const char *)absolute_path, &fd);
778 handle->dir_filename = NULL;
786 memset(&zt, 0,
sizeof(zt));
789 rc = select(fd + 1, &pollfd, NULL, NULL, &zt);
790 }
while (rc == -1 && errno == EINTR);
799 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
806 if (uv__path_is_a_directory(
handle->path) == 0) {
808 handle->dir_filename = NULL;
814 handle->event_watcher.fd = -1;
824 #ifdef HAVE_SYS_AHAFS_EVPRODS_H
850 for (
i = 0;
i < argc;
i++)
851 size += strlen(argv[
i]) + 1;
854 size += (argc + 1) *
sizeof(
char*);
857 if (new_argv == NULL)
862 s = (
char*) &new_argv[argc + 1];
863 for (
i = 0;
i < argc;
i++) {
864 size = strlen(argv[
i]) + 1;
882 if (new_title == NULL)
941 snprintf(
pp,
sizeof(
pp),
"/proc/%lu/psinfo", (
unsigned long) getpid());
949 if (
read(fd, &psinfo,
sizeof(psinfo)) ==
sizeof(psinfo)) {
950 *rss = (size_t)psinfo.pr_rssize * 1024;
960 struct utmp *utmp_buf;
969 while ((utmp_buf = getutent()) != NULL) {
970 if (utmp_buf->ut_user[0] && utmp_buf->ut_type == USER_PROCESS)
972 if (utmp_buf->ut_type == BOOT_TIME)
973 boot_time = utmp_buf->ut_time;
981 *uptime = time(NULL) - boot_time;
988 perfstat_cpu_total_t ps_total;
989 perfstat_cpu_t* ps_cpus;
990 perfstat_id_t cpu_id;
993 result = perfstat_cpu_total(NULL, &ps_total,
sizeof(ps_total), 1);
998 ncpus =
result = perfstat_cpu(NULL, NULL,
sizeof(perfstat_cpu_t), 0);
1003 ps_cpus = (perfstat_cpu_t*)
uv__malloc(ncpus *
sizeof(perfstat_cpu_t));
1009 uv__strscpy(cpu_id.name, FIRST_CPU,
sizeof(cpu_id.name));
1010 result = perfstat_cpu(&cpu_id, ps_cpus,
sizeof(perfstat_cpu_t), ncpus);
1024 cpu_info = *cpu_infos;
1025 while (
idx < ncpus) {
1026 cpu_info->
speed = (
int)(ps_total.processorHZ / 1000000);
1044 int sockfd, sock6fd, inet6,
i,
r,
size = 1;
1046 struct ifreq *ifr, *
p, flg;
1047 struct in6_ifreq if6;
1048 struct sockaddr_dl* sa_addr;
1056 if (0 > (sockfd =
socket(AF_INET, SOCK_DGRAM, IPPROTO_IP))) {
1066 if (ioctl(sockfd, SIOCGSIZIFCONF, &
size) == -1) {
1072 if (ifc.ifc_req == NULL) {
1077 if (ioctl(sockfd, SIOCGIFCONF, &ifc) == -1) {
1082 #define ADDR_SIZE(p) MAX((p).sa_len, sizeof(p))
1086 while ((
char*)ifr < (
char*)ifc.ifc_req + ifc.ifc_len) {
1088 ifr = (
struct ifreq*)
1089 ((
char*)ifr +
sizeof(ifr->ifr_name) +
ADDR_SIZE(ifr->ifr_addr));
1091 if (!(
p->ifr_addr.sa_family ==
AF_INET6 ||
1092 p->ifr_addr.sa_family == AF_INET))
1095 memcpy(flg.ifr_name,
p->ifr_name,
sizeof(flg.ifr_name));
1096 if (ioctl(sockfd, SIOCGIFFLAGS, &flg) == -1) {
1101 if (!(flg.ifr_flags & IFF_UP && flg.ifr_flags & IFF_RUNNING))
1112 if (!(*addresses)) {
1116 address = *addresses;
1119 while ((
char*)ifr < (
char*)ifc.ifc_req + ifc.ifc_len) {
1121 ifr = (
struct ifreq*)
1122 ((
char*)ifr +
sizeof(ifr->ifr_name) +
ADDR_SIZE(ifr->ifr_addr));
1124 if (!(
p->ifr_addr.sa_family ==
AF_INET6 ||
1125 p->ifr_addr.sa_family == AF_INET))
1128 inet6 = (
p->ifr_addr.sa_family ==
AF_INET6);
1130 memcpy(flg.ifr_name,
p->ifr_name,
sizeof(flg.ifr_name));
1131 if (ioctl(sockfd, SIOCGIFFLAGS, &flg) == -1)
1134 if (!(flg.ifr_flags & IFF_UP && flg.ifr_flags & IFF_RUNNING))
1147 memset(&if6, 0,
sizeof(if6));
1148 r =
uv__strscpy(if6.ifr_name,
p->ifr_name,
sizeof(if6.ifr_name));
1152 memcpy(&if6.ifr_Addr, &
p->ifr_addr,
sizeof(if6.ifr_Addr));
1153 if (ioctl(sock6fd, SIOCGIFNETMASK6, &if6) == -1)
1159 if (ioctl(sockfd, SIOCGIFNETMASK,
p) == -1)
1166 address->
is_internal = flg.ifr_flags & IFF_LOOPBACK ? 1 : 0;
1173 while ((
char*)ifr < (
char*)ifc.ifc_req + ifc.ifc_len) {
1175 ifr = (
struct ifreq*)
1176 ((
char*)ifr +
sizeof(ifr->ifr_name) +
ADDR_SIZE(ifr->ifr_addr));
1178 if (
p->ifr_addr.sa_family != AF_LINK)
1181 address = *addresses;
1183 if (strcmp(address->
name,
p->ifr_name) == 0) {
1184 sa_addr = (
struct sockaddr_dl*) &
p->ifr_addr;
1223 struct pollfd* events;
1228 assert(
loop->watchers != NULL);
1231 events = (
struct pollfd*)
loop->watchers[
loop->nwatchers];
1236 for (
i = 0;
i < nfds;
i++)
1237 if ((
int) events[
i].fd == fd)
1244 if(
loop->backend_fd >= 0)
1245 pollset_ctl(
loop->backend_fd, &pc, 1);