Debian Codegraph @

psutil/arch/sunos/disk.c

raw
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 <Python.h>
8
9 #include <sys/mntent.h>
10 #include <sys/mnttab.h>
11 #include <kstat.h>
12
13 #include "../../arch/all/init.h"
14
15
16 PyObject *
17 psutil_disk_io_counters(PyObject *self, PyObject *args) {
18 kstat_ctl_t *kc;
19 kstat_t *ksp;
20 kstat_io_t kio;
21 PyObject *py_retdict = PyDict_New();
22 PyObject *py_disk_info = NULL;
23
24 if (py_retdict == NULL)
25 return NULL;
26 kc = kstat_open();
27 if (kc == NULL) {
28 PyErr_SetFromErrno(PyExc_OSError);;
29 goto error;
30 }
31 ksp = kc->kc_chain;
32 while (ksp != NULL) {
33 if (ksp->ks_type == KSTAT_TYPE_IO) {
34 if (strcmp(ksp->ks_class, "disk") == 0) {
35 if (kstat_read(kc, ksp, &kio) == -1) {
36 kstat_close(kc);
37 return PyErr_SetFromErrno(PyExc_OSError);;
38 }
39 py_disk_info = Py_BuildValue(
40 "(IIKKLL)",
41 kio.reads,
42 kio.writes,
43 kio.nread,
44 kio.nwritten,
45 kio.rtime / 1000 / 1000, // from nano to milli secs
46 kio.wtime / 1000 / 1000 // from nano to milli secs
47 );
48 if (!py_disk_info)
49 goto error;
50 if (PyDict_SetItemString(py_retdict, ksp->ks_name,
51 py_disk_info))
52 goto error;
53 Py_CLEAR(py_disk_info);
54 }
55 }
56 ksp = ksp->ks_next;
57 }
58 kstat_close(kc);
59
60 return py_retdict;
61
62 error:
63 Py_XDECREF(py_disk_info);
64 Py_DECREF(py_retdict);
65 if (kc != NULL)
66 kstat_close(kc);
67 return NULL;
68 }
69
70
71 PyObject *
72 psutil_disk_partitions(PyObject *self, PyObject *args) {
73 FILE *file;
74 struct mnttab mt;
75 PyObject *py_dev = NULL;
76 PyObject *py_mountp = NULL;
77 PyObject *py_tuple = NULL;
78 PyObject *py_retlist = PyList_New(0);
79
80 if (py_retlist == NULL)
81 return NULL;
82
83 file = fopen(MNTTAB, "rb");
84 if (file == NULL) {
85 PyErr_SetFromErrno(PyExc_OSError);
86 goto error;
87 }
88
89 while (getmntent(file, &mt) == 0) {
90 py_dev = PyUnicode_DecodeFSDefault(mt.mnt_special);
91 if (! py_dev)
92 goto error;
93 py_mountp = PyUnicode_DecodeFSDefault(mt.mnt_mountp);
94 if (! py_mountp)
95 goto error;
96 py_tuple = Py_BuildValue(
97 "(OOss)",
98 py_dev, // device
99 py_mountp, // mount point
100 mt.mnt_fstype, // fs type
101 mt.mnt_mntopts); // options
102 if (py_tuple == NULL)
103 goto error;
104 if (PyList_Append(py_retlist, py_tuple))
105 goto error;
106 Py_CLEAR(py_dev);
107 Py_CLEAR(py_mountp);
108 Py_CLEAR(py_tuple);
109 }
110 fclose(file);
111 return py_retlist;
112
113 error:
114 Py_XDECREF(py_dev);
115 Py_XDECREF(py_mountp);
116 Py_XDECREF(py_tuple);
117 Py_DECREF(py_retlist);
118 if (file != NULL)
119 fclose(file);
120 return NULL;
121 }