summaryrefslogtreecommitdiff
path: root/src/python-systemd
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-05-08 19:46:49 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-05-08 21:35:03 -0400
commit539e0a4d583bca7db837275b07a20a933b7f8f83 (patch)
tree2a6b6a275354d289d5301bc3f1b9c6f35733ef94 /src/python-systemd
parent5afbe712db5cc68213a24c45396ffb43fab05e3e (diff)
systemd-python: add wrappers for easy functions in sd-login
sd_get_uids, sd_get_seats, sd_get_sessions, and sd_get_machine_names.
Diffstat (limited to 'src/python-systemd')
-rw-r--r--src/python-systemd/_reader.c14
-rw-r--r--src/python-systemd/docs/index.rst1
-rw-r--r--src/python-systemd/docs/login.rst5
-rw-r--r--src/python-systemd/login.c176
-rw-r--r--src/python-systemd/pyutil.c16
-rw-r--r--src/python-systemd/pyutil.h1
6 files changed, 200 insertions, 13 deletions
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c
index 50ad889201..d20c58d2a8 100644
--- a/src/python-systemd/_reader.c
+++ b/src/python-systemd/_reader.c
@@ -213,19 +213,7 @@ static PyObject* Reader_get_timeout_ms(Reader *self, PyObject *args)
if (r < 0)
return NULL;
- if (t == (uint64_t) -1)
- return PyLong_FromLong(-1);
- else {
- struct timespec ts;
- uint64_t n;
- int msec;
-
- clock_gettime(CLOCK_MONOTONIC, &ts);
- n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
- msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
-
- return PyLong_FromLong(msec);
- }
+ return absolute_timeout(t);
}
diff --git a/src/python-systemd/docs/index.rst b/src/python-systemd/docs/index.rst
index 8a94d0750b..e78d966274 100644
--- a/src/python-systemd/docs/index.rst
+++ b/src/python-systemd/docs/index.rst
@@ -14,6 +14,7 @@ Contents:
journal
id128
daemon
+ login
Indices and tables
==================
diff --git a/src/python-systemd/docs/login.rst b/src/python-systemd/docs/login.rst
new file mode 100644
index 0000000000..2cd9d8cbee
--- /dev/null
+++ b/src/python-systemd/docs/login.rst
@@ -0,0 +1,5 @@
+`systemd.login` module
+=======================
+
+.. automodule:: systemd.login
+ :members:
diff --git a/src/python-systemd/login.c b/src/python-systemd/login.c
new file mode 100644
index 0000000000..1dbe5ac5bf
--- /dev/null
+++ b/src/python-systemd/login.c
@@ -0,0 +1,176 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2013 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
+
+ systemd is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ systemd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#define PY_SSIZE_T_CLEAN
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wredundant-decls"
+#include <Python.h>
+#pragma GCC diagnostic pop
+
+#include "systemd/sd-login.h"
+#include "pyutil.h"
+#include "util.h"
+#include "strv.h"
+
+PyDoc_STRVAR(module__doc__,
+ "Python interface to the libsystemd-login library."
+);
+
+#define helper(name) \
+static PyObject* name(PyObject *self, PyObject *args) { \
+ _cleanup_strv_free_ char **list = NULL; \
+ int r; \
+ PyObject *ans; \
+ \
+ assert(args == NULL); \
+ \
+ r = sd_get_##name(&list); \
+ if (r < 0) { \
+ errno = -r; \
+ return PyErr_SetFromErrno(PyExc_IOError); \
+ } \
+ \
+ ans = PyList_New(r); \
+ if (!ans) \
+ return NULL; \
+ \
+ for (r--; r >= 0; r--) { \
+ PyObject *s = unicode_FromString(list[r]); \
+ if (!s) { \
+ Py_DECREF(ans); \
+ return NULL; \
+ } \
+ \
+ PyList_SetItem(ans, r, s); \
+ } \
+ \
+ return ans; \
+}
+
+helper(seats)
+helper(sessions)
+helper(machine_names)
+#undef helper
+
+static PyObject* uids(PyObject *self, PyObject *args) {
+ _cleanup_free_ uid_t *list = NULL;
+ int r;
+ PyObject *ans;
+
+ assert(args == NULL);
+
+ r = sd_get_uids(&list);
+ if (r < 0) {
+ errno = -r;
+ return PyErr_SetFromErrno(PyExc_IOError);
+ }
+
+ ans = PyList_New(r);
+ if (!ans)
+ return NULL;
+
+ for (r--; r >= 0; r--) {
+ PyObject *s = long_FromLong(list[r]);
+ if (!s) {
+ Py_DECREF(ans);
+ return NULL;
+ }
+
+ PyList_SetItem(ans, r, s);
+ }
+
+ return ans;
+}
+
+PyDoc_STRVAR(seats__doc__,
+ "seats() -> list\n\n"
+ "Returns a list of currently available local seats.\n"
+ "Wraps sd_get_seats(3)."
+);
+
+PyDoc_STRVAR(sessions__doc__,
+ "sessions() -> list\n\n"
+ "Returns a list of current login sessions.\n"
+ "Wraps sd_get_sessions(3)."
+);
+
+PyDoc_STRVAR(machine_names__doc__,
+ "machine_names() -> list\n\n"
+ "Returns a list of currently running virtual machines\n"
+ "and containers on the system.\n"
+ "Wraps sd_get_machine_names(3)."
+);
+
+PyDoc_STRVAR(uids__doc__,
+ "uids() -> list\n\n"
+ "Returns a list of uids of users who currently have login sessions.\n"
+ "Wraps sd_get_uids(3)."
+);
+
+static PyMethodDef methods[] = {
+ { "seats", seats, METH_NOARGS, seats__doc__},
+ { "sessions", sessions, METH_NOARGS, sessions__doc__},
+ { "machine_names", machine_names, METH_NOARGS, machine_names__doc__},
+ { "uids", uids, METH_NOARGS, uids__doc__},
+ {} /* Sentinel */
+};
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+
+#if PY_MAJOR_VERSION < 3
+
+PyMODINIT_FUNC initlogin(void) {
+ PyObject *m;
+
+ m = Py_InitModule3("login", methods, module__doc__);
+ if (m == NULL)
+ return;
+ PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
+}
+#else
+
+static struct PyModuleDef module = {
+ PyModuleDef_HEAD_INIT,
+ "login", /* name of module */
+ module__doc__, /* module documentation, may be NULL */
+ -1, /* size of per-interpreter state of the module */
+ methods
+};
+
+PyMODINIT_FUNC PyInit_login(void) {
+ PyObject *m;
+
+ m = PyModule_Create(&module);
+ if (m == NULL)
+ return NULL;
+
+ if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
+ Py_DECREF(m);
+ return NULL;
+ }
+
+ return m;
+}
+
+#endif
+
+#pragma GCC diagnostic pop
diff --git a/src/python-systemd/pyutil.c b/src/python-systemd/pyutil.c
index 79065a11c0..9510acdddb 100644
--- a/src/python-systemd/pyutil.c
+++ b/src/python-systemd/pyutil.c
@@ -28,3 +28,19 @@ void cleanup_Py_DECREFp(PyObject **p) {
Py_DECREF(*p);
}
+
+PyObject* absolute_timeout(uint64_t t) {
+ if (t == (uint64_t) -1)
+ return PyLong_FromLong(-1);
+ else {
+ struct timespec ts;
+ uint64_t n;
+ int msec;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ n = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+ msec = t > n ? (int) ((t - n + 999) / 1000) : 0;
+
+ return PyLong_FromLong(msec);
+ }
+}
diff --git a/src/python-systemd/pyutil.h b/src/python-systemd/pyutil.h
index 2163fda9ef..5c7ea37cdb 100644
--- a/src/python-systemd/pyutil.h
+++ b/src/python-systemd/pyutil.h
@@ -27,6 +27,7 @@
#endif
void cleanup_Py_DECREFp(PyObject **p);
+PyObject* absolute_timeout(uint64_t t);
#define _cleanup_Py_DECREF_ __attribute__((cleanup(cleanup_Py_DECREFp)))