| 1 |
/* |
| 2 |
* Copyright (c) 2009, 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 |
#include "../../arch/all/init.h" |
| 8 |
|
| 9 |
#include <Python.h> |
| 10 |
#include <mntent.h> |
| 11 |
|
| 12 |
|
| 13 |
// Return disk mounted partitions as a list of tuples including device, |
| 14 |
// mount point and filesystem type. |
| 15 |
PyObject * |
| 16 |
psutil_disk_partitions(PyObject *self, PyObject *args) { |
| 17 |
FILE *file = NULL; |
| 18 |
struct mntent *entry; |
| 19 |
char *mtab_path; |
| 20 |
PyObject *py_dev = NULL; |
| 21 |
PyObject *py_mountp = NULL; |
| 22 |
PyObject *py_tuple = NULL; |
| 23 |
PyObject *py_retlist = PyList_New(0); |
| 24 |
|
| 25 |
if (py_retlist == NULL) |
| 26 |
return NULL; |
| 27 |
|
| 28 |
if (!PyArg_ParseTuple(args, "s", &mtab_path)) |
| 29 |
return NULL; |
| 30 |
|
| 31 |
Py_BEGIN_ALLOW_THREADS |
| 32 |
file = setmntent(mtab_path, "r"); |
| 33 |
Py_END_ALLOW_THREADS |
| 34 |
if ((file == 0) || (file == NULL)) { |
| 35 |
psutil_debug("setmntent() failed"); |
| 36 |
PyErr_SetFromErrnoWithFilename(PyExc_OSError, mtab_path); |
| 37 |
goto error; |
| 38 |
} |
| 39 |
|
| 40 |
while ((entry = getmntent(file))) { |
| 41 |
if (entry == NULL) { |
| 42 |
PyErr_Format(PyExc_RuntimeError, "getmntent() syscall failed"); |
| 43 |
goto error; |
| 44 |
} |
| 45 |
py_dev = PyUnicode_DecodeFSDefault(entry->mnt_fsname); |
| 46 |
if (! py_dev) |
| 47 |
goto error; |
| 48 |
py_mountp = PyUnicode_DecodeFSDefault(entry->mnt_dir); |
| 49 |
if (! py_mountp) |
| 50 |
goto error; |
| 51 |
py_tuple = Py_BuildValue("(OOss)", |
| 52 |
py_dev, // device |
| 53 |
py_mountp, // mount point |
| 54 |
entry->mnt_type, // fs type |
| 55 |
entry->mnt_opts); // options |
| 56 |
if (! py_tuple) |
| 57 |
goto error; |
| 58 |
if (PyList_Append(py_retlist, py_tuple)) |
| 59 |
goto error; |
| 60 |
Py_CLEAR(py_dev); |
| 61 |
Py_CLEAR(py_mountp); |
| 62 |
Py_CLEAR(py_tuple); |
| 63 |
} |
| 64 |
endmntent(file); |
| 65 |
return py_retlist; |
| 66 |
|
| 67 |
error: |
| 68 |
if (file != NULL) |
| 69 |
endmntent(file); |
| 70 |
Py_XDECREF(py_dev); |
| 71 |
Py_XDECREF(py_mountp); |
| 72 |
Py_XDECREF(py_tuple); |
| 73 |
Py_DECREF(py_retlist); |
| 74 |
return NULL; |
| 75 |
} |