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+ #include <systemd/sd-event.h>
2123#include <linux/netlink.h>
2224
2325#include "mctp-ops.h"
@@ -38,10 +40,12 @@ static int mctp_op_socket(int type)
3840 struct iovec iov ;
3941 int rc , var , sd ;
4042
41- if (type == AF_MCTP )
43+ if (type == CONTROL_OP_SOCKET_MCTP )
4244 req .type = CONTROL_OP_SOCKET_MCTP ;
43- else if (type == AF_NETLINK )
45+ else if (type == CONTROL_OP_SOCKET_NL )
4446 req .type = CONTROL_OP_SOCKET_NL ;
47+ else if (type == CONTROL_OP_TIMER )
48+ req .type = CONTROL_OP_TIMER ;
4549 else
4650 errx (EXIT_FAILURE , "invalid socket type?" );
4751
@@ -72,12 +76,12 @@ static int mctp_op_socket(int type)
7276
7377static int mctp_op_mctp_socket (void )
7478{
75- return mctp_op_socket (AF_MCTP );
79+ return mctp_op_socket (CONTROL_OP_SOCKET_MCTP );
7680}
7781
7882static int mctp_op_netlink_socket (void )
7983{
80- return mctp_op_socket (AF_NETLINK );
84+ return mctp_op_socket (CONTROL_OP_SOCKET_NL );
8185}
8286
8387static int mctp_op_bind (int sd , struct sockaddr * addr , socklen_t addrlen )
@@ -221,6 +225,113 @@ static void mctp_bug_warn(const char *fmt, va_list args)
221225 abort ();
222226}
223227
228+ struct wrapped_time_userdata {
229+ sd_event_time_handler_t callback ;
230+ void * userdata ;
231+ };
232+
233+ int wrapped_time_callback (sd_event_source * source , int fd , uint revents ,
234+ void * userdata )
235+ {
236+ struct wrapped_time_userdata * wrapud = userdata ;
237+ uint64_t usec ;
238+ ssize_t rc ;
239+
240+ rc = read (fd , & usec , sizeof (usec ));
241+ if (rc != 8 )
242+ errx (EXIT_FAILURE , "ops protocol error" );
243+
244+ rc = wrapud -> callback (source , usec , wrapud -> userdata );
245+ warnx ("%ld" , rc );
246+
247+ return 0 ;
248+ }
249+
250+ void wrapped_time_destroy (void * wrapud )
251+ {
252+ free (wrapud );
253+ }
254+
255+ static int mctp_op_sd_event_add_time_relative (
256+ sd_event * e , sd_event_source * * ret , clockid_t clock , uint64_t usec ,
257+ uint64_t accuracy , sd_event_time_handler_t callback , void * userdata )
258+ {
259+ struct wrapped_time_userdata * wrapud = NULL ;
260+ sd_event_source * source = NULL ;
261+ int sd = -1 ;
262+ int rc = 0 ;
263+
264+ sd = mctp_op_socket (CONTROL_OP_TIMER );
265+ if (sd < 0 )
266+ return - errno ;
267+
268+ rc = write (sd , & usec , sizeof (usec ));
269+ if (rc != 8 )
270+ errx (EXIT_FAILURE , "ops protocol error" );
271+
272+ wrapud = malloc (sizeof (* wrapud ));
273+ if (!wrapud ) {
274+ rc = - ENOMEM ;
275+ goto fail ;
276+ }
277+
278+ wrapud -> callback = callback ;
279+ wrapud -> userdata = userdata ;
280+
281+ rc = sd_event_add_io (e , & source , sd , EPOLLIN , wrapped_time_callback ,
282+ wrapud );
283+ if (rc < 0 )
284+ goto fail ;
285+
286+ rc = sd_event_source_set_destroy_callback (source , wrapped_time_destroy );
287+ if (rc < 0 )
288+ goto fail ;
289+
290+ wrapud = NULL ;
291+
292+ rc = sd_event_source_set_io_fd_own (source , 1 );
293+ if (rc < 0 )
294+ goto fail ;
295+
296+ sd = -1 ;
297+
298+ rc = sd_event_source_set_enabled (source , SD_EVENT_ONESHOT );
299+ if (rc < 0 )
300+ goto fail ;
301+
302+ if (!ret ) {
303+ rc = sd_event_source_set_floating (source , 1 );
304+ if (rc < 0 )
305+ goto fail ;
306+
307+ sd_event_source_unref (source );
308+ } else {
309+ * ret = source ;
310+ }
311+
312+ return 0 ;
313+
314+ fail :
315+ if (sd > 0 )
316+ close (sd );
317+ free (wrapud );
318+ sd_event_source_disable_unref (* ret );
319+ return rc ;
320+ }
321+
322+ static int mctp_op_sd_event_source_set_time_relative (sd_event_source * s ,
323+ uint64_t usec )
324+ {
325+ int sd = sd_event_source_get_io_fd (s );
326+ ssize_t rc ;
327+
328+ rc = write (sd , & usec , sizeof (usec ));
329+ if (rc != 8 )
330+ errx (EXIT_FAILURE , "ops protocol error" );
331+
332+ return 0 ;
333+ }
334+
224335const struct mctp_ops mctp_ops = {
225336 .mctp = {
226337 .socket = mctp_op_mctp_socket ,
@@ -238,6 +349,10 @@ const struct mctp_ops mctp_ops = {
238349 .recvfrom = mctp_op_recvfrom ,
239350 .close = mctp_op_close ,
240351 },
352+ .sd_event = {
353+ .add_time_relative = mctp_op_sd_event_add_time_relative ,
354+ .source_set_time_relative = mctp_op_sd_event_source_set_time_relative ,
355+ },
241356 .bug_warn = mctp_bug_warn ,
242357};
243358
0 commit comments