| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
// Disk related functions. Original code was refactored and moved |
| 8 |
// from psutil/_psutil_osx.c in 2023. This is the GIT blame before the move: |
| 9 |
// https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_osx.c |
| 10 |
|
| 11 |
#include <Python.h> |
| 12 |
#include <sys/param.h> |
| 13 |
#include <sys/ucred.h> |
| 14 |
#include <sys/mount.h> |
| 15 |
#include <CoreFoundation/CoreFoundation.h> |
| 16 |
#include <IOKit/IOKitLib.h> |
| 17 |
#include <IOKit/storage/IOBlockStorageDriver.h> |
| 18 |
#include <IOKit/storage/IOMedia.h> |
| 19 |
#include <IOKit/IOBSD.h> |
| 20 |
|
| 21 |
#include "../../arch/all/init.h" |
| 22 |
|
| 23 |
|
| 24 |
/* |
| 25 |
* Return a list of tuples including device, mount point and fs type |
| 26 |
* for all partitions mounted on the system. |
| 27 |
*/ |
| 28 |
PyObject * |
| 29 |
psutil_disk_partitions(PyObject *self, PyObject *args) { |
| 30 |
int num; |
| 31 |
int i; |
| 32 |
int len; |
| 33 |
uint64_t flags; |
| 34 |
char opts[400]; |
| 35 |
struct statfs *fs = NULL; |
| 36 |
PyObject *py_dev = NULL; |
| 37 |
PyObject *py_mountp = NULL; |
| 38 |
PyObject *py_tuple = NULL; |
| 39 |
PyObject *py_retlist = PyList_New(0); |
| 40 |
|
| 41 |
if (py_retlist == NULL) |
| 42 |
return NULL; |
| 43 |
|
| 44 |
// get the number of mount points |
| 45 |
Py_BEGIN_ALLOW_THREADS |
| 46 |
num = getfsstat(NULL, 0, MNT_NOWAIT); |
| 47 |
Py_END_ALLOW_THREADS |
| 48 |
if (num == -1) { |
| 49 |
PyErr_SetFromErrno(PyExc_OSError); |
| 50 |
goto error; |
| 51 |
} |
| 52 |
|
| 53 |
len = sizeof(*fs) * num; |
| 54 |
fs = malloc(len); |
| 55 |
if (fs == NULL) { |
| 56 |
PyErr_NoMemory(); |
| 57 |
goto error; |
| 58 |
} |
| 59 |
|
| 60 |
Py_BEGIN_ALLOW_THREADS |
| 61 |
num = getfsstat(fs, len, MNT_NOWAIT); |
| 62 |
Py_END_ALLOW_THREADS |
| 63 |
if (num == -1) { |
| 64 |
PyErr_SetFromErrno(PyExc_OSError); |
| 65 |
goto error; |
| 66 |
} |
| 67 |
|
| 68 |
for (i = 0; i < num; i++) { |
| 69 |
opts[0] = 0; |
| 70 |
flags = fs[i].f_flags; |
| 71 |
|
| 72 |
// see sys/mount.h |
| 73 |
if (flags & MNT_RDONLY) |
| 74 |
strlcat(opts, "ro", sizeof(opts)); |
| 75 |
else |
| 76 |
strlcat(opts, "rw", sizeof(opts)); |
| 77 |
if (flags & MNT_SYNCHRONOUS) |
| 78 |
strlcat(opts, ",sync", sizeof(opts)); |
| 79 |
if (flags & MNT_NOEXEC) |
| 80 |
strlcat(opts, ",noexec", sizeof(opts)); |
| 81 |
if (flags & MNT_NOSUID) |
| 82 |
strlcat(opts, ",nosuid", sizeof(opts)); |
| 83 |
if (flags & MNT_UNION) |
| 84 |
strlcat(opts, ",union", sizeof(opts)); |
| 85 |
if (flags & MNT_ASYNC) |
| 86 |
strlcat(opts, ",async", sizeof(opts)); |
| 87 |
if (flags & MNT_EXPORTED) |
| 88 |
strlcat(opts, ",exported", sizeof(opts)); |
| 89 |
if (flags & MNT_LOCAL) |
| 90 |
strlcat(opts, ",local", sizeof(opts)); |
| 91 |
if (flags & MNT_QUOTA) |
| 92 |
strlcat(opts, ",quota", sizeof(opts)); |
| 93 |
if (flags & MNT_ROOTFS) |
| 94 |
strlcat(opts, ",rootfs", sizeof(opts)); |
| 95 |
if (flags & MNT_DOVOLFS) |
| 96 |
strlcat(opts, ",dovolfs", sizeof(opts)); |
| 97 |
if (flags & MNT_DONTBROWSE) |
| 98 |
strlcat(opts, ",dontbrowse", sizeof(opts)); |
| 99 |
if (flags & MNT_IGNORE_OWNERSHIP) |
| 100 |
strlcat(opts, ",ignore-ownership", sizeof(opts)); |
| 101 |
if (flags & MNT_AUTOMOUNTED) |
| 102 |
strlcat(opts, ",automounted", sizeof(opts)); |
| 103 |
if (flags & MNT_JOURNALED) |
| 104 |
strlcat(opts, ",journaled", sizeof(opts)); |
| 105 |
if (flags & MNT_NOUSERXATTR) |
| 106 |
strlcat(opts, ",nouserxattr", sizeof(opts)); |
| 107 |
if (flags & MNT_DEFWRITE) |
| 108 |
strlcat(opts, ",defwrite", sizeof(opts)); |
| 109 |
if (flags & MNT_UPDATE) |
| 110 |
strlcat(opts, ",update", sizeof(opts)); |
| 111 |
if (flags & MNT_RELOAD) |
| 112 |
strlcat(opts, ",reload", sizeof(opts)); |
| 113 |
if (flags & MNT_FORCE) |
| 114 |
strlcat(opts, ",force", sizeof(opts)); |
| 115 |
if (flags & MNT_CMDFLAGS) |
| 116 |
strlcat(opts, ",cmdflags", sizeof(opts)); |
| 117 |
// requires macOS >= 10.5 |
| 118 |
#ifdef MNT_QUARANTINE |
| 119 |
if (flags & MNT_QUARANTINE) |
| 120 |
strlcat(opts, ",quarantine", sizeof(opts)); |
| 121 |
#endif |
| 122 |
#ifdef MNT_MULTILABEL |
| 123 |
if (flags & MNT_MULTILABEL) |
| 124 |
strlcat(opts, ",multilabel", sizeof(opts)); |
| 125 |
#endif |
| 126 |
#ifdef MNT_NOATIME |
| 127 |
if (flags & MNT_NOATIME) |
| 128 |
strlcat(opts, ",noatime", sizeof(opts)); |
| 129 |
#endif |
| 130 |
py_dev = PyUnicode_DecodeFSDefault(fs[i].f_mntfromname); |
| 131 |
if (! py_dev) |
| 132 |
goto error; |
| 133 |
py_mountp = PyUnicode_DecodeFSDefault(fs[i].f_mntonname); |
| 134 |
if (! py_mountp) |
| 135 |
goto error; |
| 136 |
py_tuple = Py_BuildValue( |
| 137 |
"(OOss)", |
| 138 |
py_dev, // device |
| 139 |
py_mountp, // mount point |
| 140 |
fs[i].f_fstypename, // fs type |
| 141 |
opts); // options |
| 142 |
if (!py_tuple) |
| 143 |
goto error; |
| 144 |
if (PyList_Append(py_retlist, py_tuple)) |
| 145 |
goto error; |
| 146 |
Py_CLEAR(py_dev); |
| 147 |
Py_CLEAR(py_mountp); |
| 148 |
Py_CLEAR(py_tuple); |
| 149 |
} |
| 150 |
|
| 151 |
free(fs); |
| 152 |
return py_retlist; |
| 153 |
|
| 154 |
error: |
| 155 |
Py_XDECREF(py_dev); |
| 156 |
Py_XDECREF(py_mountp); |
| 157 |
Py_XDECREF(py_tuple); |
| 158 |
Py_DECREF(py_retlist); |
| 159 |
if (fs != NULL) |
| 160 |
free(fs); |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
PyObject * |
| 166 |
psutil_disk_usage_used(PyObject *self, PyObject *args) { |
| 167 |
PyObject *py_default_value; |
| 168 |
PyObject *py_mount_point_bytes = NULL; |
| 169 |
char* mount_point; |
| 170 |
|
| 171 |
if (!PyArg_ParseTuple(args, "O&O", PyUnicode_FSConverter, &py_mount_point_bytes, &py_default_value)) { |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
mount_point = PyBytes_AsString(py_mount_point_bytes); |
| 175 |
if (NULL == mount_point) { |
| 176 |
Py_XDECREF(py_mount_point_bytes); |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
#ifdef ATTR_VOL_SPACEUSED |
| 181 |
/* Call getattrlist(ATTR_VOL_SPACEUSED) to get used space info. */ |
| 182 |
int ret; |
| 183 |
struct { |
| 184 |
uint32_t size; |
| 185 |
uint64_t spaceused; |
| 186 |
} __attribute__((aligned(4), packed)) attrbuf = {0}; |
| 187 |
struct attrlist attrs = {0}; |
| 188 |
|
| 189 |
attrs.bitmapcount = ATTR_BIT_MAP_COUNT; |
| 190 |
attrs.volattr = ATTR_VOL_INFO | ATTR_VOL_SPACEUSED; |
| 191 |
Py_BEGIN_ALLOW_THREADS |
| 192 |
ret = getattrlist(mount_point, &attrs, &attrbuf, sizeof(attrbuf), 0); |
| 193 |
Py_END_ALLOW_THREADS |
| 194 |
if (ret == 0) { |
| 195 |
Py_XDECREF(py_mount_point_bytes); |
| 196 |
return PyLong_FromUnsignedLongLong(attrbuf.spaceused); |
| 197 |
} |
| 198 |
psutil_debug("getattrlist(ATTR_VOL_SPACEUSED) failed, fall-back to default value"); |
| 199 |
#endif |
| 200 |
Py_XDECREF(py_mount_point_bytes); |
| 201 |
Py_INCREF(py_default_value); |
| 202 |
return py_default_value; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
/* |
| 207 |
* Return a Python dict of tuples for disk I/O information |
| 208 |
*/ |
| 209 |
PyObject * |
| 210 |
psutil_disk_io_counters(PyObject *self, PyObject *args) { |
| 211 |
CFDictionaryRef parent_dict; |
| 212 |
CFDictionaryRef props_dict; |
| 213 |
CFDictionaryRef stats_dict; |
| 214 |
io_registry_entry_t parent; |
| 215 |
io_registry_entry_t disk; |
| 216 |
io_iterator_t disk_list = 0; |
| 217 |
PyObject *py_disk_info = NULL; |
| 218 |
PyObject *py_retdict = PyDict_New(); |
| 219 |
|
| 220 |
if (py_retdict == NULL) |
| 221 |
return NULL; |
| 222 |
|
| 223 |
if (IOServiceGetMatchingServices( |
| 224 |
kIOMasterPortDefault, |
| 225 |
IOServiceMatching(kIOMediaClass), |
| 226 |
&disk_list) != kIOReturnSuccess) |
| 227 |
{ |
| 228 |
PyErr_SetString(PyExc_RuntimeError, "unable to get the list of disks"); |
| 229 |
goto error; |
| 230 |
} |
| 231 |
|
| 232 |
while ((disk = IOIteratorNext(disk_list)) != 0) { |
| 233 |
py_disk_info = NULL; |
| 234 |
parent_dict = NULL; |
| 235 |
props_dict = NULL; |
| 236 |
stats_dict = NULL; |
| 237 |
parent = 0; |
| 238 |
|
| 239 |
if (IORegistryEntryGetParentEntry(disk, kIOServicePlane, &parent) != kIOReturnSuccess) { |
| 240 |
PyErr_SetString(PyExc_RuntimeError, "unable to get the disk's parent"); |
| 241 |
IOObjectRelease(disk); |
| 242 |
goto error; |
| 243 |
} |
| 244 |
|
| 245 |
if (!IOObjectConformsTo(parent, "IOBlockStorageDriver")) { |
| 246 |
IOObjectRelease(parent); |
| 247 |
IOObjectRelease(disk); |
| 248 |
continue; |
| 249 |
} |
| 250 |
|
| 251 |
if (IORegistryEntryCreateCFProperties(disk, |
| 252 |
(CFMutableDictionaryRef *)&parent_dict, |
| 253 |
kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) |
| 254 |
{ |
| 255 |
PyErr_SetString( |
| 256 |
PyExc_RuntimeError, "unable to get the parent's properties" |
| 257 |
); |
| 258 |
IOObjectRelease(parent); |
| 259 |
IOObjectRelease(disk); |
| 260 |
goto error; |
| 261 |
} |
| 262 |
|
| 263 |
if (IORegistryEntryCreateCFProperties(parent, |
| 264 |
(CFMutableDictionaryRef *)&props_dict, |
| 265 |
kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) |
| 266 |
{ |
| 267 |
PyErr_SetString( |
| 268 |
PyExc_RuntimeError, "unable to get the disk properties" |
| 269 |
); |
| 270 |
if (parent_dict) CFRelease(parent_dict); |
| 271 |
IOObjectRelease(parent); |
| 272 |
IOObjectRelease(disk); |
| 273 |
goto error; |
| 274 |
} |
| 275 |
|
| 276 |
CFStringRef disk_name_ref = (CFStringRef)CFDictionaryGetValue( |
| 277 |
parent_dict, CFSTR(kIOBSDNameKey) |
| 278 |
); |
| 279 |
if (disk_name_ref == NULL) { |
| 280 |
PyErr_SetString(PyExc_RuntimeError, "unable to get disk name"); |
| 281 |
if (parent_dict) |
| 282 |
CFRelease(parent_dict); |
| 283 |
if (props_dict) |
| 284 |
CFRelease(props_dict); |
| 285 |
IOObjectRelease(parent); |
| 286 |
IOObjectRelease(disk); |
| 287 |
goto error; |
| 288 |
} |
| 289 |
|
| 290 |
const int kMaxDiskNameSize = 64; |
| 291 |
char disk_name[kMaxDiskNameSize]; |
| 292 |
if (!CFStringGetCString(disk_name_ref, disk_name, kMaxDiskNameSize, |
| 293 |
CFStringGetSystemEncoding())) { |
| 294 |
PyErr_SetString( |
| 295 |
PyExc_RuntimeError, "unable to convert disk name to C string" |
| 296 |
); |
| 297 |
if (parent_dict) |
| 298 |
CFRelease(parent_dict); |
| 299 |
if (props_dict) |
| 300 |
CFRelease(props_dict); |
| 301 |
IOObjectRelease(parent); |
| 302 |
IOObjectRelease(disk); |
| 303 |
goto error; |
| 304 |
} |
| 305 |
|
| 306 |
stats_dict = (CFDictionaryRef)CFDictionaryGetValue( |
| 307 |
props_dict, CFSTR(kIOBlockStorageDriverStatisticsKey) |
| 308 |
); |
| 309 |
if (stats_dict == NULL) { |
| 310 |
PyErr_SetString(PyExc_RuntimeError, "unable to get disk stats"); |
| 311 |
if (parent_dict) |
| 312 |
CFRelease(parent_dict); |
| 313 |
if (props_dict) |
| 314 |
CFRelease(props_dict); |
| 315 |
IOObjectRelease(parent); |
| 316 |
IOObjectRelease(disk); |
| 317 |
goto error; |
| 318 |
} |
| 319 |
|
| 320 |
CFNumberRef number; |
| 321 |
int64_t reads = 0, writes = 0, read_bytes = 0, write_bytes = 0; |
| 322 |
int64_t read_time = 0, write_time = 0; |
| 323 |
|
| 324 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 325 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsReadsKey)))) |
| 326 |
CFNumberGetValue(number, kCFNumberSInt64Type, &reads); |
| 327 |
|
| 328 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 329 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsWritesKey)))) |
| 330 |
CFNumberGetValue(number, kCFNumberSInt64Type, &writes); |
| 331 |
|
| 332 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 333 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey)))) |
| 334 |
CFNumberGetValue(number, kCFNumberSInt64Type, &read_bytes); |
| 335 |
|
| 336 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 337 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey)))) |
| 338 |
CFNumberGetValue(number, kCFNumberSInt64Type, &write_bytes); |
| 339 |
|
| 340 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 341 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsTotalReadTimeKey)))) |
| 342 |
CFNumberGetValue(number, kCFNumberSInt64Type, &read_time); |
| 343 |
|
| 344 |
if ((number = (CFNumberRef)CFDictionaryGetValue( |
| 345 |
stats_dict, CFSTR(kIOBlockStorageDriverStatisticsTotalWriteTimeKey)))) |
| 346 |
CFNumberGetValue(number, kCFNumberSInt64Type, &write_time); |
| 347 |
|
| 348 |
py_disk_info = Py_BuildValue( |
| 349 |
"(KKKKKK)", |
| 350 |
(unsigned long long)reads, |
| 351 |
(unsigned long long)writes, |
| 352 |
(unsigned long long)read_bytes, |
| 353 |
(unsigned long long)write_bytes, |
| 354 |
(unsigned long long)(read_time / 1000 / 1000), |
| 355 |
(unsigned long long)(write_time / 1000 / 1000) |
| 356 |
); |
| 357 |
|
| 358 |
if (!py_disk_info) { |
| 359 |
if (parent_dict) |
| 360 |
CFRelease(parent_dict); |
| 361 |
if (props_dict) |
| 362 |
CFRelease(props_dict); |
| 363 |
IOObjectRelease(parent); |
| 364 |
IOObjectRelease(disk); |
| 365 |
goto error; |
| 366 |
} |
| 367 |
|
| 368 |
if (PyDict_SetItemString(py_retdict, disk_name, py_disk_info)) { |
| 369 |
Py_CLEAR(py_disk_info); |
| 370 |
if (parent_dict) |
| 371 |
CFRelease(parent_dict); |
| 372 |
if (props_dict) |
| 373 |
CFRelease(props_dict); |
| 374 |
IOObjectRelease(parent); |
| 375 |
IOObjectRelease(disk); |
| 376 |
goto error; |
| 377 |
} |
| 378 |
|
| 379 |
Py_CLEAR(py_disk_info); |
| 380 |
|
| 381 |
if (parent_dict) |
| 382 |
CFRelease(parent_dict); |
| 383 |
IOObjectRelease(parent); |
| 384 |
if (props_dict) |
| 385 |
CFRelease(props_dict); |
| 386 |
IOObjectRelease(disk); |
| 387 |
} |
| 388 |
|
| 389 |
IOObjectRelease(disk_list); |
| 390 |
return py_retdict; |
| 391 |
|
| 392 |
error: |
| 393 |
Py_XDECREF(py_disk_info); |
| 394 |
Py_DECREF(py_retdict); |
| 395 |
if (disk_list != 0) |
| 396 |
IOObjectRelease(disk_list); |
| 397 |
return NULL; |
| 398 |
} |