root/include/crm/common/mainloop.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2  * Copyright 2009-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #ifndef PCMK__CRM_COMMON_MAINLOOP__H
  11 #  define PCMK__CRM_COMMON_MAINLOOP__H
  12 
  13 #  include <signal.h> // sighandler_t
  14 #  include <glib.h>
  15 #  include <stdbool.h>
  16 
  17 #ifdef __cplusplus
  18 extern "C" {
  19 #endif
  20 
  21 /**
  22  * \file
  23  * \brief Wrappers for and extensions to glib mainloop
  24  * \ingroup core
  25  */
  26 
  27 enum mainloop_child_flags {
  28     /* don't kill pid group on timeout, only kill the pid */
  29     mainloop_leave_pid_group = 0x01,
  30 };
  31 
  32 typedef struct trigger_s crm_trigger_t;
  33 typedef struct mainloop_io_s mainloop_io_t;
  34 typedef struct mainloop_child_s mainloop_child_t;
  35 typedef struct mainloop_timer_s mainloop_timer_t;
  36 
  37 void mainloop_cleanup(void);
  38 
  39 crm_trigger_t *mainloop_add_trigger(int priority, int (*dispatch) (gpointer user_data),
  40                                     gpointer userdata);
  41 
  42 void mainloop_set_trigger(crm_trigger_t * source);
  43 
  44 void mainloop_trigger_complete(crm_trigger_t * trig);
  45 
  46 gboolean mainloop_destroy_trigger(crm_trigger_t * source);
  47 
  48 #  ifndef HAVE_SIGHANDLER_T
  49 typedef void (*sighandler_t)(int);
  50 #  endif
  51 
  52 sighandler_t crm_signal_handler(int sig, sighandler_t dispatch);
  53 
  54 gboolean mainloop_add_signal(int sig, void (*dispatch) (int sig));
  55 
  56 gboolean mainloop_destroy_signal(int sig);
  57 
  58 bool mainloop_timer_running(mainloop_timer_t *t);
  59 
  60 void mainloop_timer_start(mainloop_timer_t *t);
  61 
  62 void mainloop_timer_stop(mainloop_timer_t *t);
  63 
  64 guint mainloop_timer_set_period(mainloop_timer_t *t, guint period_ms);
  65 
  66 mainloop_timer_t *mainloop_timer_add(const char *name, guint period_ms, bool repeat, GSourceFunc cb, void *userdata);
  67 
  68 void mainloop_timer_del(mainloop_timer_t *t);
  69 
  70 
  71 #  include <crm/common/ipc.h>
  72 #  include <qb/qbipcs.h>
  73 
  74 struct ipc_client_callbacks {
  75     /*!
  76      * \brief Dispatch function for an IPC connection used as mainloop source
  77      *
  78      * \param[in] buffer    Message read from IPC connection
  79      * \param[in] length    Number of bytes in \p buffer
  80      * \param[in] userdata  User data passed when creating mainloop source
  81      *
  82      * \return Negative value to remove source, anything else to keep it
  83      */
  84     int (*dispatch) (const char *buffer, ssize_t length, gpointer userdata);
  85 
  86     /*!
  87      * \brief Destroy function for mainloop IPC connection client data
  88      *
  89      * \param[in] userdata  User data passed when creating mainloop source
  90      */
  91     void (*destroy) (gpointer userdata);
  92 };
  93 
  94 qb_ipcs_service_t *mainloop_add_ipc_server(const char *name, enum qb_ipc_type type,
  95                                            struct qb_ipcs_service_handlers *callbacks);
  96 
  97 /*!
  98  * \brief Start server-side API end-point, hooked into the internal event loop
  99  *
 100  * \param[in] name    name of the IPC end-point ("address" for the client)
 101  * \param[in] type    selects libqb's IPC back-end (or use #QB_IPC_NATIVE)
 102  * \param[in] callbacks  defines libqb's IPC service-level handlers
 103  * \param[in] priority  priority relative to other events handled in the
 104  *                      abstract handling loop, use #QB_LOOP_MED when unsure
 105  *
 106  * \return libqb's opaque handle to the created service abstraction
 107  *
 108  * \note For portability concerns, do not use this function if you keep
 109  *       \p priority as #QB_LOOP_MED, stick with #mainloop_add_ipc_server
 110  *       (with exactly such semantics) instead (once you link with this new
 111  *       symbol employed, you can't downgrade the library freely anymore).
 112  *
 113  * \note The intended effect will only get fully reflected when run-time
 114  *       linked to patched libqb: https://github.com/ClusterLabs/libqb/pull/352
 115  */
 116 qb_ipcs_service_t *mainloop_add_ipc_server_with_prio(const char *name,
 117                                                     enum qb_ipc_type type,
 118                                                     struct qb_ipcs_service_handlers *callbacks,
 119                                                     enum qb_loop_priority prio);
 120 
 121 void mainloop_del_ipc_server(qb_ipcs_service_t * server);
 122 
 123 mainloop_io_t *mainloop_add_ipc_client(const char *name, int priority, size_t max_size,
 124                                        void *userdata, struct ipc_client_callbacks *callbacks);
 125 
 126 void mainloop_del_ipc_client(mainloop_io_t * client);
 127 
 128 crm_ipc_t *mainloop_get_ipc_client(mainloop_io_t * client);
 129 
 130 struct mainloop_fd_callbacks {
 131     /*!
 132      * \brief Dispatch function for mainloop file descriptor with data ready
 133      *
 134      * \param[in] userdata  User data passed when creating mainloop source
 135      *
 136      * \return Negative value to remove source, anything else to keep it
 137      */
 138     int (*dispatch) (gpointer userdata);
 139 
 140     /*!
 141      * \brief Destroy function for mainloop file descriptor client data
 142      *
 143      * \param[in] userdata  User data passed when creating mainloop source
 144      */
 145     void (*destroy) (gpointer userdata);
 146 };
 147 
 148 mainloop_io_t *mainloop_add_fd(const char *name, int priority, int fd, void *userdata,
 149                                struct mainloop_fd_callbacks *callbacks);
 150 
 151 void mainloop_del_fd(mainloop_io_t * client);
 152 
 153 /*
 154  * Create a new tracked process
 155  * To track a process group, use -pid
 156  */
 157 void mainloop_child_add(pid_t pid,
 158                         int timeout,
 159                         const char *desc,
 160                         void *userdata,
 161                         void (*callback) (mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode));
 162 
 163 void mainloop_child_add_with_flags(pid_t pid,
 164                         int timeout,
 165                         const char *desc,
 166                         void *userdata,
 167                         enum mainloop_child_flags,
 168                         void (*callback) (mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode));
 169 
 170 void *mainloop_child_userdata(mainloop_child_t * child);
 171 int mainloop_child_timeout(mainloop_child_t * child);
 172 const char *mainloop_child_name(mainloop_child_t * child);
 173 
 174 pid_t mainloop_child_pid(mainloop_child_t * child);
 175 void mainloop_clear_child_userdata(mainloop_child_t * child);
 176 gboolean mainloop_child_kill(pid_t pid);
 177 
 178 void pcmk_quit_main_loop(GMainLoop *mloop, unsigned int n);
 179 void pcmk_drain_main_loop(GMainLoop *mloop, guint timer_ms,
 180                           bool (*check)(guint));
 181 
 182 #  define G_PRIORITY_MEDIUM (G_PRIORITY_HIGH/2)
 183 
 184 #if !defined(PCMK_ALLOW_DEPRECATED) || (PCMK_ALLOW_DEPRECATED == 1)
 185 #include <crm/common/mainloop_compat.h>
 186 #endif
 187 
 188 #ifdef __cplusplus
 189 }
 190 #endif
 191 
 192 #endif

/* [previous][next][first][last][top][bottom][index][help] */