Debian Codegraph @

psutil/arch/bsd/disk.c

raw
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 #include <Python.h>
8 #include <sys/mount.h>
9 #if PSUTIL_NETBSD
10 // getvfsstat()
11 #include <sys/types.h>
12 #include <sys/statvfs.h>
13 #else
14 // getfsstat()
15 #include <sys/param.h>
16 #include <sys/ucred.h>
17 #include <sys/mount.h>
18 #endif
19
20 #include "../../arch/all/init.h"
21
22 PyObject *
23 psutil_disk_partitions(PyObject *self, PyObject *args) {
24 int num;
25 int i;
26 long len;
27 uint64_t flags;
28 char opts[200];
29 #ifdef PSUTIL_NETBSD
30 struct statvfs *fs = NULL;
31 #else
32 struct statfs *fs = NULL;
33 #endif
34 PyObject *py_retlist = PyList_New(0);
35 PyObject *py_dev = NULL;
36 PyObject *py_mountp = NULL;
37 PyObject *py_tuple = NULL;
38
39 if (py_retlist == NULL)
40 return NULL;
41
42 // get the number of mount points
43 Py_BEGIN_ALLOW_THREADS
44 #ifdef PSUTIL_NETBSD
45 num = getvfsstat(NULL, 0, MNT_NOWAIT);
46 #else
47 num = getfsstat(NULL, 0, MNT_NOWAIT);
48 #endif
49 Py_END_ALLOW_THREADS
50 if (num == -1) {
51 PyErr_SetFromErrno(PyExc_OSError);
52 goto error;
53 }
54
55 len = sizeof(*fs) * num;
56 fs = malloc(len);
57 if (fs == NULL) {
58 PyErr_NoMemory();
59 goto error;
60 }
61
62 Py_BEGIN_ALLOW_THREADS
63 #ifdef PSUTIL_NETBSD
64 num = getvfsstat(fs, len, MNT_NOWAIT);
65 #else
66 num = getfsstat(fs, len, MNT_NOWAIT);
67 #endif
68 Py_END_ALLOW_THREADS
69 if (num == -1) {
70 PyErr_SetFromErrno(PyExc_OSError);
71 goto error;
72 }
73
74 for (i = 0; i < num; i++) {
75 py_tuple = NULL;
76 opts[0] = 0;
77 #ifdef PSUTIL_NETBSD
78 flags = fs[i].f_flag;
79 #else
80 flags = fs[i].f_flags;
81 #endif
82
83 // see sys/mount.h
84 if (flags & MNT_RDONLY)
85 strlcat(opts, "ro", sizeof(opts));
86 else
87 strlcat(opts, "rw", sizeof(opts));
88 if (flags & MNT_SYNCHRONOUS)
89 strlcat(opts, ",sync", sizeof(opts));
90 if (flags & MNT_NOEXEC)
91 strlcat(opts, ",noexec", sizeof(opts));
92 if (flags & MNT_NOSUID)
93 strlcat(opts, ",nosuid", sizeof(opts));
94 if (flags & MNT_ASYNC)
95 strlcat(opts, ",async", sizeof(opts));
96 if (flags & MNT_NOATIME)
97 strlcat(opts, ",noatime", sizeof(opts));
98 if (flags & MNT_SOFTDEP)
99 strlcat(opts, ",softdep", sizeof(opts));
100 #ifdef PSUTIL_FREEBSD
101 if (flags & MNT_UNION)
102 strlcat(opts, ",union", sizeof(opts));
103 if (flags & MNT_SUIDDIR)
104 strlcat(opts, ",suiddir", sizeof(opts));
105 if (flags & MNT_SOFTDEP)
106 strlcat(opts, ",softdep", sizeof(opts));
107 if (flags & MNT_NOSYMFOLLOW)
108 strlcat(opts, ",nosymfollow", sizeof(opts));
109 #ifdef MNT_GJOURNAL
110 if (flags & MNT_GJOURNAL)
111 strlcat(opts, ",gjournal", sizeof(opts));
112 #endif
113 if (flags & MNT_MULTILABEL)
114 strlcat(opts, ",multilabel", sizeof(opts));
115 if (flags & MNT_ACLS)
116 strlcat(opts, ",acls", sizeof(opts));
117 if (flags & MNT_NOCLUSTERR)
118 strlcat(opts, ",noclusterr", sizeof(opts));
119 if (flags & MNT_NOCLUSTERW)
120 strlcat(opts, ",noclusterw", sizeof(opts));
121 #ifdef MNT_NFS4ACLS
122 if (flags & MNT_NFS4ACLS)
123 strlcat(opts, ",nfs4acls", sizeof(opts));
124 #endif
125 #elif PSUTIL_NETBSD
126 if (flags & MNT_NODEV)
127 strlcat(opts, ",nodev", sizeof(opts));
128 if (flags & MNT_UNION)
129 strlcat(opts, ",union", sizeof(opts));
130 if (flags & MNT_NOCOREDUMP)
131 strlcat(opts, ",nocoredump", sizeof(opts));
132 #ifdef MNT_RELATIME
133 if (flags & MNT_RELATIME)
134 strlcat(opts, ",relatime", sizeof(opts));
135 #endif
136 if (flags & MNT_IGNORE)
137 strlcat(opts, ",ignore", sizeof(opts));
138 #ifdef MNT_DISCARD
139 if (flags & MNT_DISCARD)
140 strlcat(opts, ",discard", sizeof(opts));
141 #endif
142 #ifdef MNT_EXTATTR
143 if (flags & MNT_EXTATTR)
144 strlcat(opts, ",extattr", sizeof(opts));
145 #endif
146 if (flags & MNT_LOG)
147 strlcat(opts, ",log", sizeof(opts));
148 if (flags & MNT_SYMPERM)
149 strlcat(opts, ",symperm", sizeof(opts));
150 if (flags & MNT_NODEVMTIME)
151 strlcat(opts, ",nodevmtime", sizeof(opts));
152 #endif
153 py_dev = PyUnicode_DecodeFSDefault(fs[i].f_mntfromname);
154 if (! py_dev)
155 goto error;
156 py_mountp = PyUnicode_DecodeFSDefault(fs[i].f_mntonname);
157 if (! py_mountp)
158 goto error;
159 py_tuple = Py_BuildValue("(OOss)",
160 py_dev, // device
161 py_mountp, // mount point
162 fs[i].f_fstypename, // fs type
163 opts); // options
164 if (!py_tuple)
165 goto error;
166 if (PyList_Append(py_retlist, py_tuple))
167 goto error;
168 Py_CLEAR(py_dev);
169 Py_CLEAR(py_mountp);
170 Py_CLEAR(py_tuple);
171 }
172
173 free(fs);
174 return py_retlist;
175
176 error:
177 Py_XDECREF(py_dev);
178 Py_XDECREF(py_mountp);
179 Py_XDECREF(py_tuple);
180 Py_DECREF(py_retlist);
181 if (fs != NULL)
182 free(fs);
183 return NULL;
184 }