1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* Simple D-Bus types: Unix FD type.
*
* Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
* Copyright (C) 2010 Signove <http://www.signove.com>
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "dbus_bindings-internal.h"
#include <Python.h>
#include <structmember.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#include "types-internal.h"
PyDoc_STRVAR(UnixFd_tp_doc,
"dbus.UnixFd(value: int or file object[, variant_level: int])\n"
"\n"
"A Unix Fd.\n"
"\n"
"``value`` must be the integer value of a file descriptor, or an object that\n"
"implements the fileno() method. Otherwise, `ValueError` will be\n"
"raised.\n"
"\n"
"UnixFd keeps a dup() (duplicate) of the supplied file descriptor. The\n"
"caller remains responsible for closing the original fd.\n"
"\n"
":py:attr:`variant_level` must be non-negative; the default is 0.\n"
"\n"
".. py:attribute:: variant_level\n"
"\n"
" Indicates how many nested Variant containers this object\n"
" is contained in: if a message's wire format has a variant containing a\n"
" variant containing an Unix Fd, this is represented in Python by an\n"
" Unix Fd with variant_level==2.\n"
);
typedef struct {
PyObject_HEAD
int fd;
long variant_level;
} UnixFdObject;
/* Return values:
* -2 - the long value is not plausible as a file descriptor
* -1 - Python failed producing a long (or in Python 2 an int)
* 0 - success (value might not *actually* be a fd, but it *could* be)
* 1 - arg is not a long (or in Python 2 an int)
*
* Or to summarize:
* status < 0 - an error occurred, and a Python exception is set.
* status == 0 - all is okay, output argument *fd is set.
* status > 0 - try something else
*/
static int
make_fd(PyObject *arg, int *fd)
{
long fd_arg;
if (PyLong_Check(arg))
{
/* on Python 2 this accepts either int or long */
fd_arg = PyLong_AsLong(arg);
if (fd_arg == -1 && PyErr_Occurred()) {
return -1;
}
}
else {
return 1;
}
/* Check for int overflow. */
if (fd_arg < 0 || fd_arg > INT_MAX) {
PyErr_Format(PyExc_ValueError, "int is outside fd range");
return -2;
}
*fd = (int)fd_arg;
return 0;
}
static PyObject *
UnixFd_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
UnixFdObject *self = NULL;
PyObject *arg;
int status, fd, fd_original = -1;
static char *argnames[] = {"fd", "variant_level", NULL};
long variant_level = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|l", argnames, &arg, &variant_level)) {
return NULL;
}
if (variant_level < 0) {
PyErr_Format(PyExc_ValueError, "variant_level cannot be less than 0");
return NULL;
}
status = make_fd(arg, &fd_original);
if (status < 0)
return NULL;
if (status > 0) {
if (PyObject_HasAttrString(arg, "fileno")) {
PyObject *fd_number = PyObject_CallMethod(arg, "fileno", NULL);
if (!fd_number)
return NULL;
status = make_fd(fd_number, &fd_original);
Py_CLEAR(fd_number);
if (status < 0)
return NULL;
if (status > 0) {
PyErr_Format(PyExc_ValueError, "Argument's fileno() method "
"returned a non-int value");
return NULL;
}
/* fd_original is all good. */
}
else {
PyErr_Format(PyExc_ValueError, "Argument is not int and does not "
"implement fileno() method");
return NULL;
}
}
assert(fd_original >= 0);
fd = dup(fd_original);
if (fd < 0) {
PyErr_Format(PyExc_ValueError, "Invalid file descriptor");
return NULL;
}
self = (UnixFdObject *) cls->tp_alloc(cls, 0);
if (!self) {
close(fd);
return NULL;
}
self->fd = fd;
self->variant_level = variant_level;
return (PyObject *)self;
}
static void
UnixFd_dealloc(UnixFdObject *self)
{
if (self->fd >= 0) {
close(self->fd);
self->fd = -1;
}
}
PyDoc_STRVAR(UnixFd_take__doc__,
"take() -> int\n"
"\n"
"This method returns the file descriptor owned by UnixFd object.\n"
"Note that, once this method is called, closing the file descriptor is\n"
"the caller's responsibility.\n"
"\n"
"This method may be called at most once; UnixFd 'forgets' the file\n"
"descriptor after it is taken.\n"
"\n"
":Raises ValueError: if this method has already been called\n"
);
static PyObject *
UnixFd_take(UnixFdObject *self)
{
PyObject *fdnumber;
if (self->fd < 0) {
PyErr_SetString(PyExc_ValueError, "File descriptor already taken");
return NULL;
}
fdnumber = Py_BuildValue("i", self->fd);
self->fd = -1;
return fdnumber;
}
int
dbus_py_unix_fd_get_fd(PyObject *self)
{
return ((UnixFdObject *) self)->fd;
}
static PyMethodDef UnixFd_methods[] = {
{"take", (PyCFunction) (void (*)(void)) UnixFd_take, METH_NOARGS, UnixFd_take__doc__ },
{NULL}
};
static struct PyMemberDef UnixFd_tp_members[] = {
{"variant_level", T_LONG, offsetof(UnixFdObject, variant_level),
READONLY,
"Indicates how many nested Variant containers this object\n"
"is contained in: if a message's wire format has a variant containing a\n"
"variant containing a file descriptor, this is represented in Python by\n"
"a UnixFd with variant_level==2.\n"
},
{NULL},
};
PyTypeObject DBusPyUnixFd_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"dbus.UnixFd",
sizeof(UnixFdObject),
0,
(destructor) UnixFd_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
UnixFd_tp_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
UnixFd_methods, /* tp_methods */
UnixFd_tp_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
UnixFd_tp_new, /* tp_new */
};
dbus_bool_t
dbus_py_init_unixfd_type(void)
{
if (PyType_Ready(&DBusPyUnixFd_Type) < 0) return 0;
return 1;
}
dbus_bool_t
dbus_py_insert_unixfd_type(PyObject *this_module)
{
Py_INCREF(&DBusPyUnixFd_Type);
if (PyModule_AddObject(this_module, "UnixFd",
(PyObject *)&DBusPyUnixFd_Type) < 0) return 0;
return 1;
}
/* vim:set ft=c cino< sw=4 sts=4 et: */
|