summaryrefslogtreecommitdiff
path: root/src/python-systemd/id128.c
blob: 6dadf7b2fd14d3770a73e9ccbf6a239afa702eb3 (plain)
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
/*-*- 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/>.
***/

#include <Python.h>

#include <systemd/sd-messages.h>

#include "pyutil.h"
#include "log.h"
#include "util.h"
#include "macro.h"

PyDoc_STRVAR(module__doc__,
             "Python interface to the libsystemd-id128 library.\n\n"
             "Provides SD_MESSAGE_* constants and functions to query and generate\n"
             "128-bit unique identifiers."
);

PyDoc_STRVAR(randomize__doc__,
             "randomize() -> UUID\n\n"
             "Return a new random 128-bit unique identifier.\n"
             "Wraps sd_id128_randomize(3)."
);

PyDoc_STRVAR(get_machine__doc__,
             "get_machine() -> UUID\n\n"
             "Return a 128-bit unique identifier for this machine.\n"
             "Wraps sd_id128_get_machine(3)."
);

PyDoc_STRVAR(get_boot__doc__,
             "get_boot() -> UUID\n\n"
             "Return a 128-bit unique identifier for this boot.\n"
             "Wraps sd_id128_get_boot(3)."
);

static PyObject* make_uuid(sd_id128_t id) {
        _cleanup_Py_DECREF_ PyObject
                *uuid = NULL, *UUID = NULL, *bytes = NULL,
                *args = NULL, *kwargs = NULL;

        uuid = PyImport_ImportModule("uuid");
        if (!uuid)
                return NULL;

        UUID = PyObject_GetAttrString(uuid, "UUID");
        bytes = PyBytes_FromStringAndSize((const char*) &id.bytes, sizeof(id.bytes));
        args = Py_BuildValue("()");
        kwargs = PyDict_New();
        if (!UUID || !bytes || !args || !kwargs)
                return NULL;

        if (PyDict_SetItemString(kwargs, "bytes", bytes) < 0)
                return NULL;

        return PyObject_Call(UUID, args, kwargs);
}

#define helper(name)                                                    \
        static PyObject *name(PyObject *self, PyObject *args) {         \
                sd_id128_t id;                                          \
                int r;                                                  \
                                                                        \
                assert(args == NULL);                                   \
                                                                        \
                r = sd_id128_##name(&id);                               \
                if (r < 0) {                                            \
                        errno = -r;                                     \
                        return PyErr_SetFromErrno(PyExc_IOError);       \
                }                                                       \
                                                                        \
                return make_uuid(id);                                   \
        }

helper(randomize)
helper(get_machine)
helper(get_boot)

static PyMethodDef methods[] = {
        { "randomize", randomize, METH_NOARGS, randomize__doc__},
        { "get_machine", get_machine, METH_NOARGS, get_machine__doc__},
        { "get_boot", get_boot, METH_NOARGS, get_boot__doc__},
        { NULL, NULL, 0, NULL }        /* Sentinel */
};

static int add_id(PyObject *module, const char* name, sd_id128_t id) {
        PyObject *obj;

        obj = make_uuid(id);
        if (!obj)
                return -1;

        return PyModule_AddObject(module, name, obj);
}

#if PY_MAJOR_VERSION < 3

DISABLE_WARNING_MISSING_PROTOTYPES;
PyMODINIT_FUNC initid128(void) {
        PyObject *m;

        m = Py_InitModule3("id128", methods, module__doc__);
        if (m == NULL)
                return;

        /* a series of lines like 'add_id() ;' follow */
#define JOINER ;
#include "id128-constants.h"
#undef JOINER
        PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
}
REENABLE_WARNING;

#else

static struct PyModuleDef module = {
        PyModuleDef_HEAD_INIT,
        "id128", /* name of module */
        module__doc__, /* module documentation, may be NULL */
        -1, /* size of per-interpreter state of the module */
        methods
};

DISABLE_WARNING_MISSING_PROTOTYPES;
PyMODINIT_FUNC PyInit_id128(void) {
        PyObject *m;

        m = PyModule_Create(&module);
        if (m == NULL)
                return NULL;

        if ( /* a series of lines like 'add_id() ||' follow */
#define JOINER ||
#include "id128-constants.h"
#undef JOINER
            PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
                Py_DECREF(m);
                return NULL;
        }

        return m;
}
REENABLE_WARNING;

#endif