2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 /* declaration of features of libsystemd */
27 * Definition of the facilities provided by the daemon.
29 struct afb_daemon_itf {
30 int (*event_broadcast)(void *closure, const char *name, struct json_object *object); /* broadcasts evant 'name' with 'object' */
31 struct sd_event *(*get_event_loop)(void *closure); /* gets the common systemd's event loop */
32 struct sd_bus *(*get_user_bus)(void *closure); /* gets the common systemd's user d-bus */
33 struct sd_bus *(*get_system_bus)(void *closure); /* gets the common systemd's system d-bus */
34 void (*vverbose)(void*closure, int level, const char *file, int line, const char *fmt, va_list args);
35 struct afb_event (*event_make)(void *closure, const char *name); /* creates an event of 'name' */
36 int (*rootdir_get_fd)(void *closure);
37 int (*rootdir_open_locale)(void *closure, const char *filename, int flags, const char *locale);
38 int (*queue_job)(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
42 * Structure for accessing daemon.
43 * See also: afb_daemon_get_event_sender, afb_daemon_get_event_loop, afb_daemon_get_user_bus, afb_daemon_get_system_bus
46 const struct afb_daemon_itf *itf; /* the interfacing functions */
47 void *closure; /* the closure when calling these functions */
51 * Retrieves the common systemd's event loop of AFB
52 * 'daemon' MUST be the daemon given in interface when activating the binding.
54 static inline struct sd_event *afb_daemon_get_event_loop(struct afb_daemon daemon)
56 return daemon.itf->get_event_loop(daemon.closure);
60 * Retrieves the common systemd's user/session d-bus of AFB
61 * 'daemon' MUST be the daemon given in interface when activating the binding.
63 static inline struct sd_bus *afb_daemon_get_user_bus(struct afb_daemon daemon)
65 return daemon.itf->get_user_bus(daemon.closure);
69 * Retrieves the common systemd's system d-bus of AFB
70 * 'daemon' MUST be the daemon given in interface when activating the binding.
72 static inline struct sd_bus *afb_daemon_get_system_bus(struct afb_daemon daemon)
74 return daemon.itf->get_system_bus(daemon.closure);
78 * Broadcasts widely the event of 'name' with the data 'object'.
79 * 'object' can be NULL.
80 * 'daemon' MUST be the daemon given in interface when activating the binding.
82 * For convenience, the function calls 'json_object_put' for 'object'.
83 * Thus, in the case where 'object' should remain available after
84 * the function returns, the function 'json_object_get' shall be used.
86 * Returns the count of clients that received the event.
88 static inline int afb_daemon_broadcast_event(struct afb_daemon daemon, const char *name, struct json_object *object)
90 return daemon.itf->event_broadcast(daemon.closure, name, object);
94 * Creates an event of 'name' and returns it.
95 * 'daemon' MUST be the daemon given in interface when activating the binding.
97 static inline struct afb_event afb_daemon_make_event(struct afb_daemon daemon, const char *name)
99 return daemon.itf->event_make(daemon.closure, name);
103 * Send a message described by 'fmt' and following parameters
104 * to the journal for the verbosity 'level'.
105 * 'file' and 'line' are indicators of position of the code in source files.
106 * 'daemon' MUST be the daemon given in interface when activating the binding.
108 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...) __attribute__((format(printf, 5, 6)));
109 static inline void afb_daemon_verbose(struct afb_daemon daemon, int level, const char *file, int line, const char *fmt, ...)
113 daemon.itf->vverbose(daemon.closure, level, file, line, fmt, args);
118 * Get the root directory file descriptor. This file descriptor can
119 * be used with functions 'openat', 'fstatat', ...
121 static inline int afb_daemon_rootdir_get_fd(struct afb_daemon daemon)
123 return daemon.itf->rootdir_get_fd(daemon.closure);
127 * Opens 'filename' within the root directory with 'flags' (see function openat)
128 * using the 'locale' definition (example: "jp,en-US") that can be NULL.
129 * Returns the file descriptor or -1 in case of error.
131 static inline int afb_daemon_rootdir_open_locale(struct afb_daemon daemon, const char *filename, int flags, const char *locale)
133 return daemon.itf->rootdir_open_locale(daemon.closure, filename, flags, locale);
137 * Queue the job defined by 'callback' and 'argument' for being executed asynchronously
138 * in this thread (later) or in an other thread.
139 * If 'group' is not NUL, the jobs queued with a same value (as the pointer value 'group')
140 * are executed in sequence in the order of there submission.
141 * If 'timeout' is not 0, it represent the maximum execution time for the job in seconds.
142 * At first, the job is called with 0 as signum and the given argument.
143 * The job is executed with the monitoring of its time and some signals like SIGSEGV and
144 * SIGFPE. When a such signal is catched, the job is terminated and reexecuted but with
145 * signum being the signal number (SIGALRM when timeout expired).
147 * Returns 0 in case of success or -1 in case of error.
149 static inline int afb_daemon_queue_job(struct afb_daemon daemon, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
151 return daemon.itf->queue_job(daemon.closure, callback, argument, group, timeout);