static char * osdep_get_cwd_fallback(int fd) { static char wd[PATH_MAX]; struct kinfo_file *info = NULL; pid_t pgrp; int nrecords, i; if ((pgrp = tcgetpgrp(fd)) == -1) return (NULL); if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL) return (NULL); for (i = 0; i < nrecords; i++) { if (info[i].kf_fd == KF_FD_TYPE_CWD) { strlcpy(wd, info[i].kf_path, sizeof wd); free(info); return (wd); } } free(info); return (NULL); }
PyObject * psutil_proc_cwd(PyObject *self, PyObject *args) { long pid; struct kinfo_file *freep = NULL; struct kinfo_file *kif; struct kinfo_proc kipp; const char *encoding_errs; PyObject *py_path = NULL; int i, cnt; if (! PyArg_ParseTuple(args, "l", &pid)) goto error; if (psutil_kinfo_proc(pid, &kipp) == -1) goto error; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { psutil_raise_ad_or_nsp(pid); goto error; } for (i = 0; i < cnt; i++) { kif = &freep[i]; if (kif->kf_fd == KF_FD_TYPE_CWD) { #if PY_MAJOR_VERSION >= 3 py_path = PyUnicode_DecodeFSDefault(kif->kf_path); #else py_path = Py_BuildValue("s", kif->kf_path); #endif if (!py_path) goto error; break; } } /* * For lower pids it seems we can't retrieve any information * (lsof can't do that it either). Since this happens even * as root we return an empty string instead of AccessDenied. */ if (py_path == NULL) py_path = Py_BuildValue("s", ""); free(freep); return py_path; error: Py_XDECREF(py_path); if (freep != NULL) free(freep); return NULL; }
/* * Use kinfo_getfile() to fetch the list of file descriptors and figure out * the highest open file descriptor. */ static int highest_fd(void) { struct kinfo_file *kif; int cnt, i, highest; kif = kinfo_getfile(getpid(), &cnt); if (kif == NULL) fail_err("kinfo_getfile"); highest = INT_MIN; for (i = 0; i < cnt; i++) if (kif[i].kf_fd > highest) highest = kif[i].kf_fd; free(kif); return (highest); }
PyObject * psutil_proc_num_fds(PyObject *self, PyObject *args) { long pid; int cnt; struct kinfo_file *freep; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { psutil_raise_ad_or_nsp(pid); return NULL; } free(freep); return Py_BuildValue("i", cnt); }
/* * Return process current working directory. */ static PyObject* get_process_cwd(PyObject* self, PyObject* args) { long pid; PyObject *path = NULL; struct kinfo_file *freep, *kif; struct kinfo_proc kipp; int i, cnt; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; if (get_kinfo_proc(pid, &kipp) == -1) return NULL; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { PyErr_SetFromErrno(0); return NULL; } for (i = 0; i < cnt; i++) { kif = &freep[i]; if (kif->kf_fd == KF_FD_TYPE_CWD) { path = Py_BuildValue("s", kif->kf_path); break; } } /* * For lower pids it seems we can't retrieve any information * (lsof can't do that it either). Since this happens even * as root we return an empty string instead of AccessDenied. */ if (path == NULL) { path = Py_BuildValue("s", ""); } free(freep); return path; }
/* * Return files opened by process as a list of (path, fd) tuples */ static PyObject* get_process_open_files(PyObject* self, PyObject* args) { long pid; PyObject *retList = PyList_New(0); PyObject *tuple = NULL; struct kinfo_file *freep, *kif; struct kinfo_proc kipp; int i, cnt; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; if (get_kinfo_proc(pid, &kipp) == -1) return NULL; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { PyErr_SetFromErrno(0); return NULL; } for (i = 0; i < cnt; i++) { kif = &freep[i]; if ((kif->kf_type == KF_TYPE_VNODE) && (kif->kf_vnode_type == KF_VTYPE_VREG)) { tuple = Py_BuildValue("(si)", kif->kf_path, kif->kf_fd); PyList_Append(retList, tuple); Py_DECREF(tuple); } } free(freep); return retList; }
glibtop_open_files_entry * glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pid_t pid) { #if __FreeBSD_version > 800018 || (__FreeBSD_version < 800000 && __FreeBSD_version >= 700104) struct kinfo_file *freep, *kif; #ifndef HAVE_KINFO_GETFILE int name[4]; size_t len; #else int cnt; #endif ssize_t i; #else char *output; #endif GArray *entries; memset(buf, 0, sizeof (glibtop_proc_open_files)); #if __FreeBSD_version > 800018 || (__FreeBSD_version < 800000 && __FreeBSD_version >= 700104) #ifndef HAVE_KINFO_GETFILE name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_FILEDESC; name[3] = pid; if (sysctl(name, 4, NULL, &len, NULL, 0) < 0) return NULL; freep = kif = g_malloc(len); if (sysctl(name, 4, kif, &len, NULL, 0) < 0) { g_free(freep); return NULL; } #else freep = kinfo_getfile(pid, &cnt); #endif entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry)); #ifndef HAVE_KINFO_GETFILE for (i = 0; i < len / sizeof(*kif); i++, kif++) { glibtop_open_files_entry entry = {0}; if (kif->kf_structsize != sizeof(*kif)) continue; #else for (i = 0; i < cnt; i++) { glibtop_open_files_entry entry = {0}; kif = &freep[i]; #endif if (kif->kf_fd < 0) continue; if (kif->kf_type == KF_TYPE_SOCKET) { if (kif->kf_sock_domain == AF_LOCAL) { struct sockaddr_un *sun; entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET; sun = (struct sockaddr_un *)&kif->kf_sa_local; if (sun->sun_path[0]) { char *addrstr; addrstr = addr_to_string(&kif->kf_sa_local); g_strlcpy(entry.info.localsock.name, addrstr, sizeof(entry.info.localsock.name)); g_free(addrstr); } else { char *addrstr; addrstr = addr_to_string(&kif->kf_sa_peer); g_strlcpy(entry.info.localsock.name, addrstr, sizeof(entry.info.localsock.name)); g_free(addrstr); } } else if (kif->kf_sock_domain == AF_INET || kif->kf_sock_domain == AF_INET6) { char *addrstr; if (kif->kf_sock_domain == AF_INET) entry.type = GLIBTOP_FILE_TYPE_INETSOCKET; else entry.type = GLIBTOP_FILE_TYPE_INET6SOCKET; addrstr = addr_to_string(&kif->kf_sa_peer); g_strlcpy(entry.info.sock.dest_host, addrstr, sizeof(entry.info.sock.dest_host)); g_free(addrstr); entry.info.sock.dest_port = addr_to_port(&kif->kf_sa_peer); } } else if (kif->kf_type == KF_TYPE_PIPE) { entry.type = GLIBTOP_FILE_TYPE_PIPE; } else if (kif->kf_type == KF_TYPE_VNODE) { entry.type = GLIBTOP_FILE_TYPE_FILE; g_strlcpy(entry.info.file.name, kif->kf_path, sizeof(entry.info.file.name)); } else continue; entry.fd = kif->kf_fd; g_array_append_val(entries, entry); } g_free(freep); #else output = execute_lsof(pid); if (output == NULL) return NULL; entries = parse_output(output); g_free(output); #endif buf->flags = _glibtop_sysdeps_proc_open_files; buf->number = entries->len; buf->size = sizeof(glibtop_open_files_entry); buf->total = buf->number * buf->size; return (glibtop_open_files_entry*)g_array_free(entries, FALSE); }
static struct xtcpcb * psutil_search_tcplist(char *buf, struct kinfo_file *kif) { struct xtcpcb *tp; struct xinpcb *inp; #else static struct tcpcb * psutil_search_tcplist(char *buf, struct kinfo_file *kif) { struct tcpcb *tp; struct inpcb *inp; #endif struct xinpgen *xig, *oxig; struct xsocket *so; oxig = xig = (struct xinpgen *)buf; for (xig = (struct xinpgen *)((char *)xig + xig->xig_len); xig->xig_len > sizeof(struct xinpgen); xig = (struct xinpgen *)((char *)xig + xig->xig_len)) { #if __FreeBSD_version >= 1200026 tp = (struct xtcpcb *)xig; inp = &tp->xt_inp; so = &inp->xi_socket; #else tp = &((struct xtcpcb *)xig)->xt_tp; inp = &((struct xtcpcb *)xig)->xt_inp; so = &((struct xtcpcb *)xig)->xt_socket; #endif if (so->so_type != kif->kf_sock_type || so->xso_family != kif->kf_sock_domain || so->xso_protocol != kif->kf_sock_protocol) continue; if (kif->kf_sock_domain == AF_INET) { if (!psutil_sockaddr_matches( AF_INET, inp->inp_lport, &inp->inp_laddr, &kif->kf_sa_local)) continue; if (!psutil_sockaddr_matches( AF_INET, inp->inp_fport, &inp->inp_faddr, &kif->kf_sa_peer)) continue; } else { if (!psutil_sockaddr_matches( AF_INET6, inp->inp_lport, &inp->in6p_laddr, &kif->kf_sa_local)) continue; if (!psutil_sockaddr_matches( AF_INET6, inp->inp_fport, &inp->in6p_faddr, &kif->kf_sa_peer)) continue; } return (tp); } return NULL; } PyObject * psutil_proc_connections(PyObject *self, PyObject *args) { // Return connections opened by process. long pid; int i; int cnt; struct kinfo_file *freep = NULL; struct kinfo_file *kif; char *tcplist = NULL; #if __FreeBSD_version >= 1200026 struct xtcpcb *tcp; #else struct tcpcb *tcp; #endif PyObject *py_retlist = PyList_New(0); PyObject *py_tuple = NULL; PyObject *py_laddr = NULL; PyObject *py_raddr = NULL; PyObject *py_af_filter = NULL; PyObject *py_type_filter = NULL; PyObject *py_family = NULL; PyObject *py_type = NULL; if (py_retlist == NULL) return NULL; if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) goto error; if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; } errno = 0; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { psutil_raise_for_pid(pid, "kinfo_getfile() failed"); goto error; } tcplist = psutil_fetch_tcplist(); if (tcplist == NULL) { PyErr_SetFromErrno(PyExc_OSError); goto error; } for (i = 0; i < cnt; i++) { int lport, rport, state; char lip[200], rip[200]; char path[PATH_MAX]; int inseq; py_tuple = NULL; py_laddr = NULL; py_raddr = NULL; kif = &freep[i]; if (kif->kf_type == KF_TYPE_SOCKET) { // apply filters py_family = PyLong_FromLong((long)kif->kf_sock_domain); inseq = PySequence_Contains(py_af_filter, py_family); Py_DECREF(py_family); if (inseq == 0) continue; py_type = PyLong_FromLong((long)kif->kf_sock_type); inseq = PySequence_Contains(py_type_filter, py_type); Py_DECREF(py_type); if (inseq == 0) continue; // IPv4 / IPv6 socket if ((kif->kf_sock_domain == AF_INET) || (kif->kf_sock_domain == AF_INET6)) { // fill status state = PSUTIL_CONN_NONE; if (kif->kf_sock_type == SOCK_STREAM) { tcp = psutil_search_tcplist(tcplist, kif); if (tcp != NULL) state = (int)tcp->t_state; } // build addr and port inet_ntop( kif->kf_sock_domain, psutil_sockaddr_addr(kif->kf_sock_domain, &kif->kf_sa_local), lip, sizeof(lip)); inet_ntop( kif->kf_sock_domain, psutil_sockaddr_addr(kif->kf_sock_domain, &kif->kf_sa_peer), rip, sizeof(rip)); lport = htons(psutil_sockaddr_port(kif->kf_sock_domain, &kif->kf_sa_local)); rport = htons(psutil_sockaddr_port(kif->kf_sock_domain, &kif->kf_sa_peer)); // construct python tuple/list py_laddr = Py_BuildValue("(si)", lip, lport); if (!py_laddr) goto error; if (rport != 0) py_raddr = Py_BuildValue("(si)", rip, rport); else py_raddr = Py_BuildValue("()"); if (!py_raddr) goto error; py_tuple = Py_BuildValue( "(iiiNNi)", kif->kf_fd, kif->kf_sock_domain, kif->kf_sock_type, py_laddr, py_raddr, state ); if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); } // UNIX socket. // Note: remote path cannot be determined. else if (kif->kf_sock_domain == AF_UNIX) { struct sockaddr_un *sun; sun = (struct sockaddr_un *)&kif->kf_sa_local; snprintf( path, sizeof(path), "%.*s", (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))), sun->sun_path); py_laddr = PyUnicode_DecodeFSDefault(path); if (! py_laddr) goto error; py_tuple = Py_BuildValue( "(iiiOsi)", kif->kf_fd, kif->kf_sock_domain, kif->kf_sock_type, py_laddr, "", // raddr can't be determined PSUTIL_CONN_NONE ); if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); Py_DECREF(py_laddr); } } } free(freep); free(tcplist); return py_retlist; error: Py_XDECREF(py_tuple); Py_XDECREF(py_laddr); Py_XDECREF(py_raddr); Py_DECREF(py_retlist); if (freep != NULL) free(freep); if (tcplist != NULL) free(tcplist); return NULL; }
PyObject * psutil_proc_connections(PyObject *self, PyObject *args) { long pid; int i, cnt; struct kinfo_file *freep = NULL; struct kinfo_file *kif; char *tcplist = NULL; PyObject *py_retlist = PyList_New(0); PyObject *py_tuple = NULL; PyObject *py_laddr = NULL; PyObject *py_raddr = NULL; PyObject *py_af_filter = NULL; PyObject *py_type_filter = NULL; PyObject *py_family = NULL; PyObject *_type = NULL; if (py_retlist == NULL) return NULL; if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) goto error; if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; } freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { psutil_raise_ad_or_nsp(pid); goto error; } for (i = 0; i < cnt; i++) { int state; int lport; int rport; char addrbuf[NI_MAXHOST + 2]; int inseq; struct in6_addr laddr6; py_tuple = NULL; py_laddr = NULL; py_raddr = NULL; kif = &freep[i]; if (kif->f_type == DTYPE_SOCKET) { // apply filters py_family = PyLong_FromLong((long)kif->so_family); inseq = PySequence_Contains(py_af_filter, py_family); Py_DECREF(py_family); if (inseq == 0) continue; _type = PyLong_FromLong((long)kif->so_type); inseq = PySequence_Contains(py_type_filter, _type); Py_DECREF(_type); if (inseq == 0) continue; // IPv4 / IPv6 socket if ((kif->so_family == AF_INET) || (kif->so_family == AF_INET6)) { // fill status if (kif->so_type == SOCK_STREAM) state = kif->t_state; else state = PSUTIL_CONN_NONE; // ports lport = ntohs(kif->inp_lport); rport = ntohs(kif->inp_fport); // local address, IPv4 if (kif->so_family == AF_INET) { py_laddr = Py_BuildValue( "(si)", psutil_convert_ipv4(kif->so_family, kif->inp_laddru), lport); if (!py_laddr) goto error; } else { // local address, IPv6 memcpy(&laddr6, kif->inp_laddru, sizeof(laddr6)); snprintf(addrbuf, sizeof(addrbuf), "%s", psutil_inet6_addrstr(&laddr6)); py_laddr = Py_BuildValue("(si)", addrbuf, lport); if (!py_laddr) goto error; } if (rport != 0) { // remote address, IPv4 if (kif->so_family == AF_INET) { py_raddr = Py_BuildValue( "(si)", psutil_convert_ipv4( kif->so_family, kif->inp_faddru), rport); } else { // remote address, IPv6 memcpy(&laddr6, kif->inp_faddru, sizeof(laddr6)); snprintf(addrbuf, sizeof(addrbuf), "%s", psutil_inet6_addrstr(&laddr6)); py_raddr = Py_BuildValue("(si)", addrbuf, rport); if (!py_raddr) goto error; } } else { py_raddr = Py_BuildValue("()"); } if (!py_raddr) goto error; py_tuple = Py_BuildValue( "(iiiNNi)", kif->fd_fd, kif->so_family, kif->so_type, py_laddr, py_raddr, state); if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); } // UNIX socket else if (kif->so_family == AF_UNIX) { py_tuple = Py_BuildValue( "(iiisOi)", kif->fd_fd, kif->so_family, kif->so_type, kif->unp_path, Py_None, PSUTIL_CONN_NONE); if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); Py_INCREF(Py_None); } } } free(freep); free(tcplist); return py_retlist; error: Py_XDECREF(py_tuple); Py_XDECREF(py_laddr); Py_XDECREF(py_raddr); Py_DECREF(py_retlist); if (freep != NULL) free(freep); if (tcplist != NULL) free(tcplist); return NULL; }
bool fbsd_nat_target::info_proc (const char *args, enum info_proc_what what) { #ifdef HAVE_KINFO_GETFILE gdb::unique_xmalloc_ptr<struct kinfo_file> fdtbl; int nfd = 0; #endif struct kinfo_proc kp; char *tmp; pid_t pid; bool do_cmdline = false; bool do_cwd = false; bool do_exe = false; #ifdef HAVE_KINFO_GETVMMAP bool do_mappings = false; #endif bool do_status = false; switch (what) { case IP_MINIMAL: do_cmdline = true; do_cwd = true; do_exe = true; break; #ifdef HAVE_KINFO_GETVMMAP case IP_MAPPINGS: do_mappings = true; break; #endif case IP_STATUS: case IP_STAT: do_status = true; break; case IP_CMDLINE: do_cmdline = true; break; case IP_EXE: do_exe = true; break; case IP_CWD: do_cwd = true; break; case IP_ALL: do_cmdline = true; do_cwd = true; do_exe = true; #ifdef HAVE_KINFO_GETVMMAP do_mappings = true; #endif do_status = true; break; default: error (_("Not supported on this target.")); } gdb_argv built_argv (args); if (built_argv.count () == 0) { pid = inferior_ptid.pid (); if (pid == 0) error (_("No current process: you must name one.")); } else if (built_argv.count () == 1 && isdigit (built_argv[0][0])) pid = strtol (built_argv[0], NULL, 10); else error (_("Invalid arguments.")); printf_filtered (_("process %d\n"), pid); #ifdef HAVE_KINFO_GETFILE if (do_cwd || do_exe) fdtbl.reset (kinfo_getfile (pid, &nfd)); #endif if (do_cmdline) { gdb::unique_xmalloc_ptr<char> cmdline = fbsd_fetch_cmdline (pid); if (cmdline != nullptr) printf_filtered ("cmdline = '%s'\n", cmdline.get ()); else warning (_("unable to fetch command line")); } if (do_cwd) { const char *cwd = NULL; #ifdef HAVE_KINFO_GETFILE struct kinfo_file *kf = fdtbl.get (); for (int i = 0; i < nfd; i++, kf++) { if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_CWD) { cwd = kf->kf_path; break; } } #endif if (cwd != NULL) printf_filtered ("cwd = '%s'\n", cwd); else warning (_("unable to fetch current working directory")); } if (do_exe) { const char *exe = NULL; #ifdef HAVE_KINFO_GETFILE struct kinfo_file *kf = fdtbl.get (); for (int i = 0; i < nfd; i++, kf++) { if (kf->kf_type == KF_TYPE_VNODE && kf->kf_fd == KF_FD_TYPE_TEXT) { exe = kf->kf_path; break; } } #endif if (exe == NULL) exe = pid_to_exec_file (pid); if (exe != NULL) printf_filtered ("exe = '%s'\n", exe); else warning (_("unable to fetch executable path name")); } #ifdef HAVE_KINFO_GETVMMAP if (do_mappings) { int nvment; gdb::unique_xmalloc_ptr<struct kinfo_vmentry> vmentl (kinfo_getvmmap (pid, &nvment)); if (vmentl != nullptr) { printf_filtered (_("Mapped address spaces:\n\n")); #ifdef __LP64__ printf_filtered (" %18s %18s %10s %10s %9s %s\n", "Start Addr", " End Addr", " Size", " Offset", "Flags ", "File"); #else printf_filtered ("\t%10s %10s %10s %10s %9s %s\n", "Start Addr", " End Addr", " Size", " Offset", "Flags ", "File"); #endif struct kinfo_vmentry *kve = vmentl.get (); for (int i = 0; i < nvment; i++, kve++) { ULONGEST start, end; start = kve->kve_start; end = kve->kve_end; #ifdef __LP64__ printf_filtered (" %18s %18s %10s %10s %9s %s\n", hex_string (start), hex_string (end), hex_string (end - start), hex_string (kve->kve_offset), fbsd_vm_map_entry_flags (kve->kve_flags, kve->kve_protection), kve->kve_path); #else printf_filtered ("\t%10s %10s %10s %10s %9s %s\n", hex_string (start), hex_string (end), hex_string (end - start), hex_string (kve->kve_offset), fbsd_vm_map_entry_flags (kve->kve_flags, kve->kve_protection), kve->kve_path); #endif } } else warning (_("unable to fetch virtual memory map")); } #endif if (do_status) { if (!fbsd_fetch_kinfo_proc (pid, &kp)) warning (_("Failed to fetch process information")); else { const char *state; int pgtok; printf_filtered ("Name: %s\n", kp.ki_comm); switch (kp.ki_stat) { case SIDL: state = "I (idle)"; break; case SRUN: state = "R (running)"; break; case SSTOP: state = "T (stopped)"; break; case SZOMB: state = "Z (zombie)"; break; case SSLEEP: state = "S (sleeping)"; break; case SWAIT: state = "W (interrupt wait)"; break; case SLOCK: state = "L (blocked on lock)"; break; default: state = "? (unknown)"; break; } printf_filtered ("State: %s\n", state); printf_filtered ("Parent process: %d\n", kp.ki_ppid); printf_filtered ("Process group: %d\n", kp.ki_pgid); printf_filtered ("Session id: %d\n", kp.ki_sid); printf_filtered ("TTY: %ju\n", (uintmax_t) kp.ki_tdev); printf_filtered ("TTY owner process group: %d\n", kp.ki_tpgid); printf_filtered ("User IDs (real, effective, saved): %d %d %d\n", kp.ki_ruid, kp.ki_uid, kp.ki_svuid); printf_filtered ("Group IDs (real, effective, saved): %d %d %d\n", kp.ki_rgid, kp.ki_groups[0], kp.ki_svgid); printf_filtered ("Groups: "); for (int i = 0; i < kp.ki_ngroups; i++) printf_filtered ("%d ", kp.ki_groups[i]); printf_filtered ("\n"); printf_filtered ("Minor faults (no memory page): %ld\n", kp.ki_rusage.ru_minflt); printf_filtered ("Minor faults, children: %ld\n", kp.ki_rusage_ch.ru_minflt); printf_filtered ("Major faults (memory page faults): %ld\n", kp.ki_rusage.ru_majflt); printf_filtered ("Major faults, children: %ld\n", kp.ki_rusage_ch.ru_majflt); printf_filtered ("utime: %jd.%06ld\n", (intmax_t) kp.ki_rusage.ru_utime.tv_sec, kp.ki_rusage.ru_utime.tv_usec); printf_filtered ("stime: %jd.%06ld\n", (intmax_t) kp.ki_rusage.ru_stime.tv_sec, kp.ki_rusage.ru_stime.tv_usec); printf_filtered ("utime, children: %jd.%06ld\n", (intmax_t) kp.ki_rusage_ch.ru_utime.tv_sec, kp.ki_rusage_ch.ru_utime.tv_usec); printf_filtered ("stime, children: %jd.%06ld\n", (intmax_t) kp.ki_rusage_ch.ru_stime.tv_sec, kp.ki_rusage_ch.ru_stime.tv_usec); printf_filtered ("'nice' value: %d\n", kp.ki_nice); printf_filtered ("Start time: %jd.%06ld\n", kp.ki_start.tv_sec, kp.ki_start.tv_usec); pgtok = getpagesize () / 1024; printf_filtered ("Virtual memory size: %ju kB\n", (uintmax_t) kp.ki_size / 1024); printf_filtered ("Data size: %ju kB\n", (uintmax_t) kp.ki_dsize * pgtok); printf_filtered ("Stack size: %ju kB\n", (uintmax_t) kp.ki_ssize * pgtok); printf_filtered ("Text size: %ju kB\n", (uintmax_t) kp.ki_tsize * pgtok); printf_filtered ("Resident set size: %ju kB\n", (uintmax_t) kp.ki_rssize * pgtok); printf_filtered ("Maximum RSS: %ju kB\n", (uintmax_t) kp.ki_rusage.ru_maxrss); printf_filtered ("Pending Signals: "); for (int i = 0; i < _SIG_WORDS; i++) printf_filtered ("%08x ", kp.ki_siglist.__bits[i]); printf_filtered ("\n"); printf_filtered ("Ignored Signals: "); for (int i = 0; i < _SIG_WORDS; i++) printf_filtered ("%08x ", kp.ki_sigignore.__bits[i]); printf_filtered ("\n"); printf_filtered ("Caught Signals: "); for (int i = 0; i < _SIG_WORDS; i++) printf_filtered ("%08x ", kp.ki_sigcatch.__bits[i]); printf_filtered ("\n"); } } return true; }
static PyObject * psutil_proc_open_files(PyObject *self, PyObject *args) { long pid; int i, cnt; struct kinfo_file *freep = NULL; struct kinfo_file *kif; kinfo_proc kipp; PyObject *py_retlist = PyList_New(0); PyObject *py_tuple = NULL; if (py_retlist == NULL) return NULL; if (! PyArg_ParseTuple(args, "l", &pid)) goto error; if (psutil_kinfo_proc(pid, &kipp) == -1) goto error; freep = kinfo_getfile(pid, &cnt); if (freep == NULL) { psutil_raise_ad_or_nsp(pid); goto error; } for (i = 0; i < cnt; i++) { kif = &freep[i]; #ifdef __FreeBSD__ if ((kif->kf_type == KF_TYPE_VNODE) && (kif->kf_vnode_type == KF_VTYPE_VREG)) { py_tuple = Py_BuildValue("(si)", kif->kf_path, kif->kf_fd); #elif defined(__OpenBSD__) if ((kif->f_type == DTYPE_VNODE) && (kif->v_type == VREG)) { py_tuple = Py_BuildValue("(si)", "", kif->fd_fd); #elif defined(__NetBSD__) if ((kif->ki_ftype == DTYPE_VNODE) && (kif->ki_vtype == VREG)) { py_tuple = Py_BuildValue("(si)", "", kif->ki_fd); #endif if (py_tuple == NULL) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); } } free(freep); return py_retlist; error: Py_XDECREF(py_tuple); Py_DECREF(py_retlist); if (freep != NULL) free(freep); return NULL; } #endif /* * Return a list of tuples including device, mount point and fs type * for all partitions mounted on the system. */ static PyObject * psutil_disk_partitions(PyObject *self, PyObject *args) { int num; int i; long len; uint64_t flags; char opts[200]; #if defined(__NetBSD__) struct statvfs *fs = NULL; #else struct statfs *fs = NULL; #endif PyObject *py_retlist = PyList_New(0); PyObject *py_tuple = NULL; if (py_retlist == NULL) return NULL; // get the number of mount points Py_BEGIN_ALLOW_THREADS #if defined(__NetBSD__) num = getvfsstat(NULL, 0, MNT_NOWAIT); #else num = getfsstat(NULL, 0, MNT_NOWAIT); #endif Py_END_ALLOW_THREADS if (num == -1) { PyErr_SetFromErrno(PyExc_OSError); goto error; } len = sizeof(*fs) * num; fs = malloc(len); if (fs == NULL) { PyErr_NoMemory(); goto error; } Py_BEGIN_ALLOW_THREADS #if defined(__NetBSD__) num = getvfsstat(fs, len, MNT_NOWAIT); #else num = getfsstat(fs, len, MNT_NOWAIT); #endif Py_END_ALLOW_THREADS if (num == -1) { PyErr_SetFromErrno(PyExc_OSError); goto error; } for (i = 0; i < num; i++) { py_tuple = NULL; opts[0] = 0; #if defined(__NetBSD__) flags = fs[i].f_flag; #else flags = fs[i].f_flags; #endif // see sys/mount.h if (flags & MNT_RDONLY) strlcat(opts, "ro", sizeof(opts)); else strlcat(opts, "rw", sizeof(opts)); if (flags & MNT_SYNCHRONOUS) strlcat(opts, ",sync", sizeof(opts)); if (flags & MNT_NOEXEC) strlcat(opts, ",noexec", sizeof(opts)); if (flags & MNT_NOSUID) strlcat(opts, ",nosuid", sizeof(opts)); if (flags & MNT_ASYNC) strlcat(opts, ",async", sizeof(opts)); if (flags & MNT_NOATIME) strlcat(opts, ",noatime", sizeof(opts)); if (flags & MNT_SOFTDEP) strlcat(opts, ",softdep", sizeof(opts)); #ifdef __FreeBSD__ if (flags & MNT_UNION) strlcat(opts, ",union", sizeof(opts)); if (flags & MNT_SUIDDIR) strlcat(opts, ",suiddir", sizeof(opts)); if (flags & MNT_SOFTDEP) strlcat(opts, ",softdep", sizeof(opts)); if (flags & MNT_NOSYMFOLLOW) strlcat(opts, ",nosymfollow", sizeof(opts)); if (flags & MNT_GJOURNAL) strlcat(opts, ",gjournal", sizeof(opts)); if (flags & MNT_MULTILABEL) strlcat(opts, ",multilabel", sizeof(opts)); if (flags & MNT_ACLS) strlcat(opts, ",acls", sizeof(opts)); if (flags & MNT_NOCLUSTERR) strlcat(opts, ",noclusterr", sizeof(opts)); if (flags & MNT_NOCLUSTERW) strlcat(opts, ",noclusterw", sizeof(opts)); if (flags & MNT_NFS4ACLS) strlcat(opts, ",nfs4acls", sizeof(opts)); #elif __NetBSD__ if (flags & MNT_NODEV) strlcat(opts, ",nodev", sizeof(opts)); if (flags & MNT_UNION) strlcat(opts, ",union", sizeof(opts)); if (flags & MNT_NOCOREDUMP) strlcat(opts, ",nocoredump", sizeof(opts)); if (flags & MNT_RELATIME) strlcat(opts, ",relatime", sizeof(opts)); if (flags & MNT_IGNORE) strlcat(opts, ",ignore", sizeof(opts)); #if defined(MNT_DISCARD) if (flags & MNT_DISCARD) strlcat(opts, ",discard", sizeof(opts)); #endif if (flags & MNT_EXTATTR) strlcat(opts, ",extattr", sizeof(opts)); if (flags & MNT_LOG) strlcat(opts, ",log", sizeof(opts)); if (flags & MNT_SYMPERM) strlcat(opts, ",symperm", sizeof(opts)); if (flags & MNT_NODEVMTIME) strlcat(opts, ",nodevmtime", sizeof(opts)); #endif py_tuple = Py_BuildValue("(ssss)", fs[i].f_mntfromname, // device fs[i].f_mntonname, // mount point fs[i].f_fstypename, // fs type opts); // options if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); } free(fs); return py_retlist; error: Py_XDECREF(py_tuple); Py_DECREF(py_retlist); if (fs != NULL) free(fs); return NULL; } /* * Return a Python list of named tuples with overall network I/O information */ static PyObject * psutil_net_io_counters(PyObject *self, PyObject *args) { char *buf = NULL, *lim, *next; struct if_msghdr *ifm; int mib[6]; size_t len; PyObject *py_retdict = PyDict_New(); PyObject *py_ifc_info = NULL; if (py_retdict == NULL) return NULL; mib[0] = CTL_NET; // networking subsystem mib[1] = PF_ROUTE; // type of information mib[2] = 0; // protocol (IPPROTO_xxx) mib[3] = 0; // address family mib[4] = NET_RT_IFLIST; // operation mib[5] = 0; if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { PyErr_SetFromErrno(PyExc_OSError); goto error; } buf = malloc(len); if (buf == NULL) { PyErr_NoMemory(); goto error; } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { PyErr_SetFromErrno(PyExc_OSError); goto error; } lim = buf + len; for (next = buf; next < lim; ) { py_ifc_info = NULL; ifm = (struct if_msghdr *)next; next += ifm->ifm_msglen; if (ifm->ifm_type == RTM_IFINFO) { struct if_msghdr *if2m = (struct if_msghdr *)ifm; struct sockaddr_dl *sdl = (struct sockaddr_dl *)(if2m + 1); char ifc_name[32]; strncpy(ifc_name, sdl->sdl_data, sdl->sdl_nlen); ifc_name[sdl->sdl_nlen] = 0; // XXX: ignore usbus interfaces: // http://lists.freebsd.org/pipermail/freebsd-current/ // 2011-October/028752.html // 'ifconfig -a' doesn't show them, nor do we. if (strncmp(ifc_name, "usbus", 5) == 0) continue; py_ifc_info = Py_BuildValue("(kkkkkkki)", if2m->ifm_data.ifi_obytes, if2m->ifm_data.ifi_ibytes, if2m->ifm_data.ifi_opackets, if2m->ifm_data.ifi_ipackets, if2m->ifm_data.ifi_ierrors, if2m->ifm_data.ifi_oerrors, if2m->ifm_data.ifi_iqdrops, 0); // dropout not supported if (!py_ifc_info) goto error; if (PyDict_SetItemString(py_retdict, ifc_name, py_ifc_info)) goto error; Py_DECREF(py_ifc_info); } else { continue; } } free(buf); return py_retdict; error: Py_XDECREF(py_ifc_info); Py_DECREF(py_retdict); if (buf != NULL) free(buf); return NULL; }