/* * Return information about mounted filesystems. */ int getmntvinfo(struct statfs **mntbufp, struct statvfs **mntvbufp, int flags) { static struct statfs *mntsbuf; static struct statvfs *mntvbuf; static int mntsize; static int bufsize; static int vbufsize; if (mntsize <= 0 && (mntsize = getvfsstat(NULL, NULL, 0, MNT_NOWAIT)) < 0) return (0); if (vbufsize > 0 && (mntsize = getvfsstat(mntsbuf, mntvbuf, vbufsize, flags)) < 0) return (0); while (vbufsize <= mntsize * (int)sizeof(struct statvfs)) { if (mntsbuf) free(mntsbuf); bufsize = (mntsize + 1) * sizeof(struct statfs); vbufsize = (mntsize + 1) * sizeof(struct statvfs); if ((mntsbuf = (struct statfs *)malloc(bufsize)) == NULL) return (0); if ((mntvbuf = (struct statvfs *)malloc(vbufsize)) == NULL) return (0); if ((mntsize = getvfsstat(mntsbuf, mntvbuf, vbufsize, flags)) < 0) return (0); } *mntbufp = mntsbuf; *mntvbufp = mntvbuf; return (mntsize); }
static int _volinfo_bsd(struct volinfo ** dev) { int ret; struct statvfs * buf; int cnt; int cnt2; if((cnt = getvfsstat(NULL, 0, ST_WAIT)) == -1) return _probe_perror("getvfsstat", -1); if((buf = malloc(sizeof(struct statvfs) * cnt)) == NULL) return _probe_perror(NULL, -1); if((cnt2 = getvfsstat(buf, sizeof(struct statvfs) * cnt, ST_WAIT)) == -1) { free(buf); return _probe_perror("getvfsstat", -1); } for(ret = 0; ret < cnt && ret < cnt2; ret++) { if(_volinfo_bsd_append(dev, &buf[ret], ret) == 0) continue; ret = -1; break; } free(buf); return ret; }
static int _mount_print(void) { int cnt; size_t s; struct statvfs * f; int i; if((cnt = getvfsstat(NULL, 0, ST_WAIT)) < 0) return _mount_error("getvfsstat", 1); s = sizeof(*f) * cnt; if((f = malloc(s)) == NULL) return _mount_error("malloc", 1); /* XXX race condition (the result of getvfsstat() may be different) */ if((cnt = getvfsstat(f, s, ST_WAIT)) < 0) { free(f); return _mount_error("getvfsstat", 1); } for(i = 0; i < cnt; i++) { printf("%s%s%s%s%s%s", f[i].f_mntfromname, " on ", f[i].f_mntonname, " type ", f[i].f_fstypename, " ("); _mount_print_flags(f[i].f_flag); printf("%s\n", ")"); } free(f); return 0; }
static int _df_mtab(Prefs * prefs) { #ifdef ST_WAIT int cnt; struct statvfs * f; int i; if((cnt = getvfsstat(NULL, 0, ST_WAIT)) < 0) return _df_error("getvfsstat", 1); if((f = malloc(sizeof(*f) * cnt)) == NULL) return _df_error("malloc", 1); if(getvfsstat(f, sizeof(*f) * cnt, ST_WAIT) != cnt) { free(f); return _df_error("getvfsstat", 1); } for(i = 0; i < cnt; i++) _df_print(prefs, &f[i]); free(f); #else /* FIXME incomplete workaround when getvfsstat() is missing */ struct statvfs f; if(statvfs(".", &f) != 0) return _df_error(".", 1); _df_print(prefs, &f); #endif return 0; }
/* * Return information about mounted filesystems. */ int getmntinfo(struct statvfs **mntbufp, int flags) { static struct statvfs *mntbuf; static int mntsize; static size_t bufsize; _DIAGASSERT(mntbufp != NULL); if (mntsize <= 0 && (mntsize = getvfsstat(NULL, (size_t)0, MNT_NOWAIT)) == -1) return (0); if (bufsize > 0 && (mntsize = getvfsstat(mntbuf, bufsize, flags)) == -1) return (0); while (bufsize <= mntsize * sizeof(struct statvfs)) { if (mntbuf) free(mntbuf); bufsize = (mntsize + 1) * sizeof(struct statvfs); if ((mntbuf = malloc(bufsize)) == NULL) return (0); if ((mntsize = getvfsstat(mntbuf, bufsize, flags)) == -1) return (0); } *mntbufp = mntbuf; return (mntsize); }
char *device_mountpoint_sysdep(char *dev, char *buf, int buflen) { int countfs; ASSERT(dev); if ((countfs = getvfsstat(NULL, 0, ST_NOWAIT)) != -1) { struct statvfs *statvfs = CALLOC(countfs, sizeof(struct statvfs)); if ((countfs = getvfsstat(statvfs, countfs * sizeof(struct statvfs), ST_NOWAIT)) != -1) { for (int i = 0; i < countfs; i++) { struct statvfs *sfs = statvfs + i; if (IS(sfs->f_mntfromname, dev)) { snprintf(buf, buflen, "%s", sfs->f_mntonname); FREE(statvfs); return buf; } } } FREE(statvfs); } LogError("Error getting mountpoint for filesystem '%s' -- %s\n", dev, STRERROR); return NULL; }
/* * Print the list of mounted file systems. */ static void root_mounts(void) { struct statvfs buf[NR_MNTS]; int i, count; if ((count = getvfsstat(buf, sizeof(buf), ST_NOWAIT)) < 0) return; for (i = 0; i < count; i++) { buf_printf("%s on %s type %s (%s)\n", buf[i].f_mntfromname, buf[i].f_mntonname, buf[i].f_fstypename, (buf[i].f_flag & ST_RDONLY) ? "ro" : "rw"); } }
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; }