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
|
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#define CONTROL_SOCKET_NAME "supervisor-notify-socket"
#define NOTIFY_SOCKET_NAME "NOTIFY_SOCKET"
#define MODULE_NAME "notify"
#define RECEIVE_BUFFER_SIZE 2048
#if __linux__
static PyObject *NotifySocketError;
static PyObject *init_control_socket(PyObject *self, PyObject *args)
{
/* create socket */
int controlfd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0);
if (controlfd == -1) goto fail_errno;
/* construct the address; sd_notify() requires that the path is absolute */
struct sockaddr_un server_addr = {0};
server_addr.sun_family = AF_UNIX;
const size_t cwd_max = sizeof(server_addr) - offsetof(struct sockaddr_un, sun_path)
/* but we also need space for making the path longer: */
- 1/*slash*/ - strlen(CONTROL_SOCKET_NAME);
if (!getcwd(server_addr.sun_path, cwd_max))
goto fail_errno;
char *p = server_addr.sun_path + strlen(server_addr.sun_path);
*p = '/';
strcpy(p + 1, CONTROL_SOCKET_NAME);
/* overwrite the (pseudo-)file if it exists */
(void)unlink(CONTROL_SOCKET_NAME);
int res = bind(controlfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (res < 0) goto fail_errno;
/* make sure that we get credentials with messages */
int data = (int)true;
res = setsockopt(controlfd, SOL_SOCKET, SO_PASSCRED, &data, sizeof(data));
if (res < 0) goto fail_errno;
/* store the name of the socket in env to fake systemd */
char *old_value = getenv(NOTIFY_SOCKET_NAME);
if (old_value != NULL) {
printf("[notify_socket] warning, running under systemd and overwriting $%s\n",
NOTIFY_SOCKET_NAME);
// fixme
}
res = setenv(NOTIFY_SOCKET_NAME, server_addr.sun_path, 1);
if (res < 0) goto fail_errno;
return PyLong_FromLong((long)controlfd);
fail_errno:
PyErr_SetFromErrno(NotifySocketError);
return NULL;
}
static PyObject *handle_control_socket_connection_event(PyObject *self,
PyObject *args)
{
long controlfd;
if (!PyArg_ParseTuple(args, "i", &controlfd))
return NULL;
/* read command assuming it fits and it was sent all at once */
// prepare space to read filedescriptors
struct msghdr msg;
msg.msg_name = NULL;
msg.msg_namelen = 0;
// prepare a place to read the actual message
char place_for_data[RECEIVE_BUFFER_SIZE];
bzero(&place_for_data, sizeof(place_for_data));
struct iovec iov = { .iov_base = &place_for_data,
.iov_len = sizeof(place_for_data) };
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
char cmsg[CMSG_SPACE(sizeof(struct ucred))];
msg.msg_control = cmsg;
msg.msg_controllen = sizeof(cmsg);
/* Receive real plus ancillary data */
int len = recvmsg(controlfd, &msg, 0);
if (len == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN) {
Py_RETURN_NONE;
} else {
PyErr_SetFromErrno(NotifySocketError);
return NULL;
}
}
/* read the sender pid */
struct cmsghdr *cmsgp = CMSG_FIRSTHDR(&msg);
pid_t pid = -1;
while (cmsgp != NULL) {
if (cmsgp->cmsg_type == SCM_CREDENTIALS) {
if (
cmsgp->cmsg_len != CMSG_LEN(sizeof(struct ucred)) ||
cmsgp->cmsg_level != SOL_SOCKET
) {
printf("[notify_socket] invalid cmsg data, ignoring\n");
Py_RETURN_NONE;
}
struct ucred cred;
memcpy(&cred, CMSG_DATA(cmsgp), sizeof(cred));
pid = cred.pid;
}
cmsgp = CMSG_NXTHDR(&msg, cmsgp);
}
if (pid == -1) {
printf("[notify_socket] ignoring received data without credentials: %s\n",
place_for_data);
Py_RETURN_NONE;
}
/* return received data as a tuple (pid, data bytes) */
return Py_BuildValue("iy", pid, place_for_data);
}
static PyMethodDef NotifyMethods[] = {
{ "init_socket", init_control_socket, METH_VARARGS,
"Init notify socket. Returns it's file descriptor." },
{ "read_message", handle_control_socket_connection_event, METH_VARARGS,
"Reads datagram from notify socket. Returns tuple of PID and received bytes." },
{ NULL, NULL, 0, NULL } /* Sentinel */
};
static struct PyModuleDef notifymodule = {
PyModuleDef_HEAD_INIT, MODULE_NAME, /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
NotifyMethods
};
PyMODINIT_FUNC PyInit_notify(void)
{
PyObject *m;
m = PyModule_Create(¬ifymodule);
if (m == NULL)
return NULL;
NotifySocketError =
PyErr_NewException(MODULE_NAME ".error", NULL, NULL);
Py_XINCREF(NotifySocketError);
if (PyModule_AddObject(m, "error", NotifySocketError) < 0) {
Py_XDECREF(NotifySocketError);
Py_CLEAR(NotifySocketError);
Py_DECREF(m);
return NULL;
}
return m;
}
#endif
|