aboutsummaryrefslogtreecommitdiff
path: root/lib/python/alarms.c
blob: 2c42d520366a05b66f601526e51e08ed13cbcfed (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
164
165
166
167
168
169
170
171
#include <Python.h>

#include <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <uuid/uuid.h>

#include "alarmd_proto.h"
#include "pyalarmd_proto.h"

static PyMethodDef ExportedMethods[] = {
	{"connect"         , pyalarmd_connect   , METH_VARARGS, "Open a connection to the named socket at the given path, returning the fd associated with it."},
	{"disconnect"      , pyalarmd_disconnect, METH_VARARGS, "Close a socket fd previously returned by connect(path)."},
	{"register_alarm"  , pyalarmd_register  , METH_VARARGS, "Send a request over the given socket to register an alarm with the given name. Returns the UUID of the new alarm as a string"},
	{"deregister_alarm", pyalarmd_deregister, METH_VARARGS, "Send a request over the given socket to deregister an alarm with the given UUID."},
	{"raise_alarm"     , pyalarmd_raise     , METH_VARARGS, "Send a request over the given socket to raise the alarm with the given UUID."},
	{"clear_alarm"     , pyalarmd_clear     , METH_VARARGS, "Send a request over the given socket to clear/lower the alarm with the given UUID."},
	{NULL              , NULL               , 0           , NULL   }
};


static struct PyModuleDef alarm_python = {
	PyModuleDef_HEAD_INIT,
	"alarms", /* module name */
	NULL,
	-1,       /* storage size -1 since state kept in global variables. */
	ExportedMethods
};

static PyObject *alarmd_error;

PyObject* PyInit_alarms(void) {
	PyObject *module = NULL;

	module = PyModule_Create(&alarm_python);
	alarmd_error = PyErr_NewException("alarms.error", NULL, NULL);
	PyModule_AddObject(module, "error", alarmd_error);

	return module;
}

PyObject* pyalarmd_connect(PyObject *self, PyObject *args) {
	const char *path = NULL;
	int sock = 0;
	struct sockaddr_un server;

	if (!PyArg_ParseTuple(args, "s", &path)) {
		return NULL;
	}

	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}

	server.sun_family = AF_UNIX;
	strcpy(server.sun_path, path);
	if (connect(sock, (struct sockaddr *)&server, strlen(server.sun_path) + sizeof(server.sun_family)) < 0) {
		close(sock);
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}
	return PyLong_FromLong(sock);
}

PyObject* pyalarmd_disconnect(PyObject *self, PyObject *args) {
	int sock = 0;

	if (!PyArg_ParseTuple(args, "i", &sock)) {
		return NULL;
	}

	close(sock);

	Py_RETURN_NONE;
}

PyObject* pyalarmd_register(PyObject *self, PyObject *args)
{
	int sock = 0;
	char *desc;
	char uuid_str[37];
	uuid_t uuid;

	if (!PyArg_ParseTuple(args, "is", &sock, &desc)) {
		return NULL;
	}

	if (alarmd_register(sock, desc, &uuid)) {
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}

	uuid_unparse(uuid, uuid_str);

	return Py_BuildValue("s", uuid_str);
}

PyObject* pyalarmd_deregister(PyObject *self, PyObject *args)
{
	int sock = 0;
	char *uuid_str = NULL;
	uuid_t uuid;

	if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) {
		return NULL;
	}

	if (uuid_parse(uuid_str, uuid) < 0) {
		PyErr_SetString(alarmd_error, "Invalid UUID");
		return NULL;
	}

	if (alarmd_deregister(sock, uuid)) {
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}

	Py_RETURN_NONE;
}

PyObject* pyalarmd_raise(PyObject *self, PyObject *args)
{
	int sock = 0;
	char *uuid_str = NULL;
	uuid_t uuid;

	if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) {
		return NULL;
	}

	if (uuid_parse(uuid_str, uuid) < 0) {
		PyErr_SetString(alarmd_error, "Invalid UUID");
		return NULL;
	}

	if (alarmd_raise(sock, uuid)) {
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}

	Py_RETURN_NONE;
}

PyObject* pyalarmd_clear(PyObject *self, PyObject *args)
{
	int sock = 0;
	char *uuid_str = NULL;
	uuid_t uuid;

	if (!PyArg_ParseTuple(args, "is", &sock, &uuid_str)) {
		return NULL;
	}

	if (uuid_parse(uuid_str, uuid) < 0) {
		PyErr_SetString(alarmd_error, "Invalid UUID");
		return NULL;
	}

	if (alarmd_clear(sock, uuid)) {
		PyErr_SetFromErrno(alarmd_error);
		return NULL;
	}

	Py_RETURN_NONE;
}