77
88#define _GNU_SOURCE
99
10+ #include <assert.h>
1011#include <err.h>
1112#include <errno.h>
1213#include <stdlib.h>
1819#include <sys/socket.h>
1920#include <sys/un.h>
2021
22+ #if HAVE_LIBSYSTEMD
23+ #include <systemd/sd-event.h>
24+ #endif
2125#include <linux/netlink.h>
2226
2327#include "mctp-ops.h"
@@ -38,10 +42,12 @@ static int mctp_op_socket(int type)
3842 struct iovec iov ;
3943 int rc , var , sd ;
4044
41- if (type == AF_MCTP )
45+ if (type == CONTROL_OP_SOCKET_MCTP )
4246 req .type = CONTROL_OP_SOCKET_MCTP ;
43- else if (type == AF_NETLINK )
47+ else if (type == CONTROL_OP_SOCKET_NL )
4448 req .type = CONTROL_OP_SOCKET_NL ;
49+ else if (type == CONTROL_OP_TIMER )
50+ req .type = CONTROL_OP_TIMER ;
4551 else
4652 errx (EXIT_FAILURE , "invalid socket type?" );
4753
@@ -72,12 +78,12 @@ static int mctp_op_socket(int type)
7278
7379static int mctp_op_mctp_socket (void )
7480{
75- return mctp_op_socket (AF_MCTP );
81+ return mctp_op_socket (CONTROL_OP_SOCKET_MCTP );
7682}
7783
7884static int mctp_op_netlink_socket (void )
7985{
80- return mctp_op_socket (AF_NETLINK );
86+ return mctp_op_socket (CONTROL_OP_SOCKET_NL );
8187}
8288
8389static int mctp_op_bind (int sd , struct sockaddr * addr , socklen_t addrlen )
@@ -221,6 +227,115 @@ static void mctp_bug_warn(const char *fmt, va_list args)
221227 abort ();
222228}
223229
230+ #if HAVE_LIBSYSTEMD
231+ struct wrapped_time_userdata {
232+ sd_event_time_handler_t callback ;
233+ void * userdata ;
234+ };
235+
236+ int wrapped_time_callback (sd_event_source * source , int fd , uint revents ,
237+ void * userdata )
238+ {
239+ struct wrapped_time_userdata * wrapud = userdata ;
240+ uint64_t usec ;
241+ ssize_t rc ;
242+
243+ rc = read (fd , & usec , sizeof (usec ));
244+ if (rc != 8 )
245+ errx (EXIT_FAILURE , "ops protocol error" );
246+
247+ rc = wrapud -> callback (source , usec , wrapud -> userdata );
248+ warnx ("%ld" , rc );
249+
250+ return 0 ;
251+ }
252+
253+ void wrapped_time_destroy (void * wrapud )
254+ {
255+ free (wrapud );
256+ }
257+
258+ static int mctp_op_sd_event_add_time_relative (
259+ sd_event * e , sd_event_source * * ret , clockid_t clock , uint64_t usec ,
260+ uint64_t accuracy , sd_event_time_handler_t callback , void * userdata )
261+ {
262+ struct wrapped_time_userdata * wrapud = NULL ;
263+ sd_event_source * source = NULL ;
264+ int sd = -1 ;
265+ int rc = 0 ;
266+
267+ sd = mctp_op_socket (CONTROL_OP_TIMER );
268+ if (sd < 0 )
269+ return - errno ;
270+
271+ rc = write (sd , & usec , sizeof (usec ));
272+ if (rc != 8 )
273+ errx (EXIT_FAILURE , "ops protocol error" );
274+
275+ wrapud = malloc (sizeof (* wrapud ));
276+ if (!wrapud ) {
277+ rc = - ENOMEM ;
278+ goto fail ;
279+ }
280+
281+ wrapud -> callback = callback ;
282+ wrapud -> userdata = userdata ;
283+
284+ rc = sd_event_add_io (e , & source , sd , EPOLLIN , wrapped_time_callback ,
285+ wrapud );
286+ if (rc < 0 )
287+ goto fail ;
288+
289+ rc = sd_event_source_set_destroy_callback (source , wrapped_time_destroy );
290+ if (rc < 0 )
291+ goto fail ;
292+
293+ wrapud = NULL ;
294+
295+ rc = sd_event_source_set_io_fd_own (source , 1 );
296+ if (rc < 0 )
297+ goto fail ;
298+
299+ sd = -1 ;
300+
301+ rc = sd_event_source_set_enabled (source , SD_EVENT_ONESHOT );
302+ if (rc < 0 )
303+ goto fail ;
304+
305+ if (!ret ) {
306+ rc = sd_event_source_set_floating (source , 1 );
307+ if (rc < 0 )
308+ goto fail ;
309+
310+ sd_event_source_unref (source );
311+ } else {
312+ * ret = source ;
313+ }
314+
315+ return 0 ;
316+
317+ fail :
318+ if (sd > 0 )
319+ close (sd );
320+ free (wrapud );
321+ sd_event_source_disable_unref (* ret );
322+ return rc ;
323+ }
324+
325+ static int mctp_op_sd_event_source_set_time_relative (sd_event_source * s ,
326+ uint64_t usec )
327+ {
328+ int sd = sd_event_source_get_io_fd (s );
329+ ssize_t rc ;
330+
331+ rc = write (sd , & usec , sizeof (usec ));
332+ if (rc != 8 )
333+ errx (EXIT_FAILURE , "ops protocol error" );
334+
335+ return 0 ;
336+ }
337+ #endif
338+
224339const struct mctp_ops mctp_ops = {
225340 .mctp = {
226341 .socket = mctp_op_mctp_socket ,
@@ -238,6 +353,12 @@ const struct mctp_ops mctp_ops = {
238353 .recvfrom = mctp_op_recvfrom ,
239354 .close = mctp_op_close ,
240355 },
356+ #if HAVE_LIBSYSTEMD
357+ .sd_event = {
358+ .add_time_relative = mctp_op_sd_event_add_time_relative ,
359+ .source_set_time_relative = mctp_op_sd_event_source_set_time_relative ,
360+ },
361+ #endif
241362 .bug_warn = mctp_bug_warn ,
242363};
243364
0 commit comments